CV2.imshow() window 不会重新打开 - 实时视频捕获

CV2.imshow() window won't reopen - Live Video Capture

我正在尝试创建一个 python 程序,该程序可以根据实时摄像头画面进行自动瞳孔检测。我的程序有多个线程来从我的相机获取图像、分析代码并显示相机馈送的编辑版本。

由于我是线程处理的新手,所以我当前的代码只显示相机馈送的底片。当 运行 时,程序按预期工作。但是,如果我在关闭 cv2 window 后再次尝试 运行 代码,程序将无法按预期运行。我的相机打开(如预期的那样)但是,新的 cv2 window 没有打开。我需要重新打开我的 ide (spyder) 才能使程序再次正常运行。

我认为这可能是因为我的线程没有正确终止,但是,鉴于我在该领域缺乏经验,我不确定。如果我运行

threading.current_thread() 

关闭 window 后我得到

<_MainThread(MainThread, started 2468)> 

对于问题所在的任何见解,我将不胜感激。

我的代码:

frame_to_detect = None
filtering = True
filter_frame = None
view = True
stopper = None

class Filter(Thread):
#indenting got mess up here
def __init_(self):
    Thread.__init__(self)

def run(self):
    global frame_to_detect
    global filter_frame


    while view:
        if frame_to_detect is not None:
            filter_frame = 255-frame_to_detect

class displayFrame(Thread):
#indenting got messed up here
def __init__(self):
    Thread.__init__(self)

def run(self):
    global view
    while view:
        if filter_frame is not None:
            cv2.imshow('frame',filter_frame)
            if (cv2.waitKey(1) & 0xFF == ord('q')):
                view = False

Filter_thread = Filter()
Filter_thread.daemon = True
Filter_thread.start()
display = displayFrame()
display.daemon = True
display.start()
video_capture = cv2.VideoCapture(filepath)

while view:
    ret,frame_to_detect = video_capture.read()


filtering = False
video_capture.release()
cv2.destroyAllWindows()

当您关闭 cv2 window 时,线程会在后台继续 运行。 cv2.imshow 的线程最终会超时。但是,为了加快速度,您可以让线程异常关闭。如

thread.raise_exception() 
thread.join()