opencv blobFromImage在主线程中运行正常,在子进程中无限期挂起

时间:2017-10-10 15:45:20

标签: python multithreading python-3.x opencv

我正在编写一些代码来获取图像,将其传递给神经网络,然后将预测和边界框从网络返回到主线程。最终目标是利用多个流程来实现这一目标。我已经将'detect'方法定义为“Detector”类的一部分。该方法如下所示:

    def detect(self, image):

    self.debug("cnn_obj_detect:Detector:detect: Initiated")
    prediction = []

    (h, w) = image.shape[:2]

    self.debug("cnn_obj_detect:Detector:detect: Image dimensions calculated successfully {}".format((h, w)))

    # this is where it hangs in a subprocess
    blob = cv2.dnn.blobFromImage(image, 0.007843, (300, 300), 127.5)

    self.debug("cnn_obj_detect:Detector:detect: Blob loaded")
    self.net.setInput(blob)
    detections = self.net.forward()
    self.debug("cnn_obj_detect:Detector:detect: CNN successfully ran")

    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]

        if confidence > self.confidence_min:
            self.debug("cnn_obj_detect:Detector:detect: Detected")
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])

            prediction.append([box, self.CLASSES[idx], confidence * 100])

    return prediction

当我从主线程调用methoed时,不使用多处理,它运行正常,我可以打印出结果。我一直这样做:

if __name__ == "__main__":
    image = cv2.imread("example_02.jpg")
    cv2.imshow("image", image)
    cv2.waitKey(0)
    print(Detector("path/to/prototxt",
         "path/to/model", result_queue).detect(image))

但是当我尝试从进程中调用相同的方法时,它会无限期地挂起cv2.dnn.blobFromImage。如果我在调用detect方法之前不使用imshow,它运行正常,即使是在进程内。这是一个错误还是我错过了一些明显的东西?我在OSX和cv 3.3.0上使用Python 3.6.2。任何帮助是极大的赞赏。提前谢谢。

0 个答案:

没有答案