python - CV2.imshow() 窗口不会重新打开 - 实时视频捕获
问题描述
我正在尝试创建一个 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()
解决方案
当您关闭 cv2 窗口时,线程继续在后台运行。cv2.imshow 的线程最终会超时。但是,为了加快速度,您可以让线程异常关闭。如
thread.raise_exception()
thread.join()
推荐阅读
- selenium - Selenium + sikuli 错误 - *No ClassDefFoundError: org/sikuli/script/Screen* 在命令提示符中
- python - 使用 Tensorflow 进行训练时如何正确实施验证?
- python - python在导入期间停止
- mongodb - MongoDB插入文档时出错,我该如何解决?
- python - 使用 Python 的 Elasticsearch JSON 批量索引
- react-native - React - 如何测试装饰类
- javascript - 不返回 min 但返回 Max
- java - 在 SharedPreferences 中存储图像
- android - 导入新项目时如何决定选择哪个选项?
- arrays - 如何在 ReactJS 复杂中使用读取这个数组