简体   繁体   English

与线程一起使用时,cv2 imshow在关闭并再次打开后不会再次打开窗口

[英]cv2 imshow doesnt open window again after closing and opening again, when used with threads

In the sample code below, 在下面的示例代码中,

import cv2
from threading import Thread

class Person_Item_Association(object):
    def __init__(self):
        self.stop = False

    def start_camera(self):
        self.stop =False
        camera_thread = Thread(target=self.start_analysis)
        camera_thread.start()

    def stop_camera(self):
        self.stop = True

    def start_analysis(self):
        cap = cv2.VideoCapture(0)

        while not self.stop:
            ret,image = cap.read()
            cv2.imshow("frame",image)
            cv2.waitKey(1)

        cap.release()
        print("resource released")
        cv2.destroyAllWindows()

I do the following sequence, call obj.start_camera(), obj.stop_camera(), cv2.imshow() opens a window , but when i again do obj.start_camera() and obj.stop_camera(), it doesn't open a window. 我按以下顺序进行操作,分别调用obj.start_camera(),obj.stop_camera(),cv2.imshow()打开一个窗口,但是当我再次执行obj.start_camera()和obj.stop_camera()时,它没有打开一个窗口。 What is wrong here ? 这是怎么了

You can use multiprocessing module instead of threading modeule to mitigate the issue. 您可以使用multiprocessing模块代替threading模块来缓解此问题。 But I am still unable to find how to fix this with threading . 但是我仍然找不到如何通过threading解决此问题的方法。

Take a look at a similar question . 看一个类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM