Keras无法在线程

时间:2017-04-23 12:43:56

标签: multithreading python-3.x machine-learning tensorflow keras

我正在编写一个基于PyQt5的人脸识别程序。面部识别功能在一个线程中运行,但它不能很好地工作。这是面部识别线程:

class FaceRecognizeThread(QThread):
    signal_recognize_end = pyqtSignal(set)

    def __init__(self):
        print("[INFO] Initiating FaceRecognizeThread...")
        super(FaceRecognizeThread, self).__init__()
        self.faceDealer = FaceDealer()
        print("[INFO] Initiating FaceRecognizeThread done!")

    def setImage(self, qimage):
        self.qimage = qimage

    def run(self):
        self.signal_recognize_end.emit(self.faceDealer.detect_predict(self.qimage))

这是面子经销商:

import numpy as np
import cv2
from PyQt5.QtGui import QImage
import qimage2ndarray
from keras.models import load_model
from keras.utils import np_utils

from sign_system.const import Const    
class FaceDealer:
    def __init__(self):
        print("[INFO] initiating FaceDealer...")
        self.faceDetector = cv2.CascadeClassifier(Const.HAARCASCADE_FRONTALFACE_ALT)
        self.faceRecognizer = load_model(Const.FACE_RECOGNIZER_DATA)

    def detect_predict(self, qimg):
        if qimg.format() != QImage.Format_RGB32:
           qimg = qimg.convertToFormat(QImage.Format_RGB32)
        image = qimage2ndarray.rgb_view(qimg)
        gray_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        rects = self.faceDetector.detectMultiScale(gray_img, 1.1, 2, minSize=(30,30))
        faces = [cv2.resize(gray_img[y: y + h, x: x + w], (30,30)) for (x, y, w, h) in rects]
        faces = np.array(faces)[:, np.newaxis, :, :] / 255.0
        print("[INFO] predicting in FaceDealer...")
        ans = self.faceRecognizer.predict(faces).argmax(axis=1)
        return set(ans)

当它转到ans = self.faceRecognizer.predict(faces).argmax(axis=1)时,整个程序在没有任何信息的情况下破解。如果我将此代码放入try...except...块,我收到此错误:

Tensor Tensor("activation_4/Softmax:0", shape=(?, 18), dtype=float32) is not an element of this graph.

首先,我认为它可能是faces的问题,所以我写了一个新的测试程序,我将faces保存到文件中并重新加载它们,predict函数工作了好!然后我将整个预测部分抓到一个新文件中作为打击:

import cv2
import numpy as np
from PyQt5.QtGui import QImage
import qimage2ndarray
from keras.models import load_model


model = load_model("data/lenet_without_negative_all.hdf5")
faceDetector = cv2.CascadeClassifier("data/haarcascade_frontalface_alt.xml")

player = cv2.VideoCapture("E:\\GraduationDesign\\Data\\5.mp4")

delay = int(1000 / player.get(cv2.CAP_PROP_FPS))
i = 0
not_end, image = player.read()
while not_end:
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    qimg = QImage(image.data,
                  image.shape[1],
                  image.shape[0],
                  QImage.Format_RGB888)
    if i // 2000:
        if qimg.format() != QImage.Format_RGB32:
            qimg = qimg.convertToFormat(QImage.Format_RGB32)
        cimg = qimage2ndarray.rgb_view(qimg)
        gray_img = cv2.cvtColor(cimg, cv2.COLOR_RGB2GRAY)
        rects = faceDetector.detectMultiScale(gray_img, 1.1, 2, minSize=(30, 30))
        faces = [cv2.resize(gray_img[y: y + h, x: x + w], (30, 30)) for (x, y, w, h) in rects]
        faces = np.array(faces)[:, np.newaxis, :, :] / 255.0
        ans = model.predict(faces).argmax(axis=1)
        print(ans)
        i = 0

    not_end, image = player.read()
    i = i + delay
    cv2.waitKey(delay)

效果很好,我不知道为什么,这很奇怪。有人可以帮帮我吗?我错过了一些import吗?非常感谢~~~~

0 个答案:

没有答案