python中多线程的替代方法是什么?

时间:2017-09-26 12:58:04

标签: python multithreading opencv deep-learning

使用卷积神经网络+开放式CV的实时图像分类模型,此处为Link to code

我正在尝试运行以下文件camera_test.py,该文件实现多线程以改进程序的fps。 while循环准备帧,而线程处理它以预测帧内图像的标签

from imagenet_utils import decode_predictions
from imagenet_utils import preprocess_input
from keras.applications.vgg16 import VGG16
import cv2
import numpy as np
import sys
import threading

label = ''
frame = None

class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global label
    # Load the VGG16 network
        print("[INFO] loading network...")
        self.model = VGG16(weights="imagenet")

        while (~(frame is None)):
            (inID, label) = self.predict(frame)

    def predict(self, frame):
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
    #image = image.transpose((2, 0, 1))
        image = image.reshape((1,) + image.shape)
        image = preprocess_input(image)
        preds = self.model.predict(image)
        return decode_predictions(preds)[0]

cap = cv2.VideoCapture(0)
if (cap.isOpened()):
    print("Camera OK")
else:
    cap.open()

keras_thread = MyThread()
keras_thread.start()

while (True):
    ret, original = cap.read()

    frame = cv2.resize(original, (224, 224))

# Display the predictions
# print("ImageNet ID: {}, Label: {}".format(inID, label))
    cv2.putText(original, "Label: {}".format(label), (10, 30), 
    cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    cv2.imshow("Classification", original)

    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break;

cap.release()
frame = None
cv2.destroyAllWindows()
sys.exit()

但是,表现是不可预测的。代码有时完美无缺,另一次抛出一个随机错误。我在运行此代码时常常遇到错误:ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dtype=float32) is not an element of this graph

我尝试过实现multiprocessing.pool而不是线程,但程序会挂起很多并冻结。还有其他选择吗?或者有没有办法修复此代码?

0 个答案:

没有答案