【问题标题】:Why is OpenCV crashing when I use it with threading? [duplicate]为什么当我将 OpenCV 与线程一起使用时会崩溃? [复制]
【发布时间】:2021-12-08 18:19:03
【问题描述】:

我正在做一个项目,我必须使用 opencv 对来自实时视频源的帧运行图像分类器。现在,我还必须从用户那里获得输入,以运行各种其他功能。但是,在我更新 opencv 帧的同一循环上获取输入将不起作用,因为输入函数会暂停整个程序。

例如-

import cv2

cam=cv2.VideoCapture(0)

def background():
        ret, frame= cam.read()
        cv2.imshow("Screen", frame)




while True:
    background()
    text=input("->\t")
    if text=="quit":
        break
        
cam.release()
cv2.destroyAllWindows()

所以我所做的是,我使用库 threading 制作了background 线程化函数,以便它可以独立运行。

这是代码-

import threading
import cv2

cam=cv2.VideoCapture(0)

def background():
        while True:
            ret, frame= cam.read()
            cv2.imshow("Screen", frame)


threading1 = threading.Thread(target=background)
threading1.start()

while True:
    text=input("->\t")
    if text=="quit":
        break
        
cam.release()
cv2.destroyAllWindows()

但在运行时,OpenCv 窗口崩溃。虽然输入功能正在工作:/

这是屏幕截图-

请有人帮我解决这个问题:D

【问题讨论】:

  • waitKey 在哪里?
  • 我认为这是不必要的,但请给我一秒钟
  • 天哪!我真是个白痴!现在可以了。我觉得自己很傻,很抱歉!谢谢! @YunusTemurlenk

标签: python multithreading opencv python-multithreading


【解决方案1】:

只是添加waitkey,在后台功能修复它。

【讨论】:

    猜你喜欢
    • 2017-06-24
    • 2016-04-02
    • 1970-01-01
    • 2017-12-25
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多