【问题标题】:CV2.imshow() window won't reopen - Live Video CaptureCV2.imshow() 窗口不会重新打开 - 实时视频捕获
【发布时间】:2019-08-30 08:52:30
【问题描述】:

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

由于我是线程新手,我当前的代码只显示了摄像头馈送的负片。运行时,程序按预期工作。但是,如果我在关闭 cv2 窗口后再次尝试运行代码,则程序无法按预期运行。我的相机打开(如预期)但是,一个新的 cv2 窗口没有打开。我需要重新打开我的 ide (spyder) 以使程序再次正常工作。

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

threading.current_thread() 

关闭窗口后,我得到了

<_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()

【问题讨论】:

    标签: python multithreading cv2


    【解决方案1】:

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

    thread.raise_exception() 
    thread.join() 
    

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多