OpenCV 中的 Python 线程问题 [英] Python threading issue in OpenCV

查看:89
本文介绍了OpenCV 中的 Python 线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OpenCV 中使用 threading 模块,我遇到了一个奇怪的问题.使用线程时,我无法再次启动相机以获取视频输入.我会拍一帧然后停下来.虽然在使用 multiprocessing 模块时没有这样的问题.我无法理解是什么导致了这种奇怪的行为.

I was using threading module with OpenCV, I came accross a bizarre issue. When using thread I was unable to start the camera again to take video input. I would take one frame and then stop. While there was no problem like this when using multiprocessing module. I am unable to understand what causes this strange behaviour.

这段代码总结了我遇到的问题,第二次创建线程时程序会卡住.

This code summarizes the problem I have, The program will stuck when the thread is created second time.

import cv2
import time
import threading

def open_cam():
    count = 0
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        print(ret_val)
        cv2.imshow("Image", img)
        count += 1
        if count == 100:
            break
        if (cv2.waitKey(1) & 0xFF) == ord('q'):
            break
    cv2.destroyAllWindows()



def start_thread():
    print("CREATING THREAD")
    cam_thread = threading.Thread(target=open_cam)
    print("STARTING THREAD")
    cam_thread.start()

start_thread()
time.sleep(5)
start_thread()  

然而,这段代码完全按照我的意愿工作,它使用 multiprocessing 模块而不是 threading

However, This code works exactly how I desire, This uses multiprocessing module instead of threading

import cv2
import time
import multiprocessing

def open_cam():
    count = 0   
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        print(ret_val)
        cv2.imshow("Image", img)
        count += 1
        if count == 100:
            break
        if (cv2.waitKey(1) & 0xFF) == ord('q'):
            break
    cv2.destroyAllWindows()



def start_process():
    print("CREATING process")
    cam_process = multiprocessing.Process(target=open_cam)
    print("STARTING process")
    cam_process.start()

start_process()
time.sleep(5)
start_process()

问题的根本原因是什么,我该如何解决?

What is the root cause of the problem and How can I fix it?

推荐答案

这是由全局解释器锁引起的.线程共享程序内存.为了防止由不同线程更改同一变量引起的冲突,Python 将执行锁定到特定线程.这意味着在任何时候都只有一个线程在运行.当 CPU 空闲时,程序会在线程之间切换,从而使受 IO 限制的应用程序运行得更快.相比之下,进程在不同的内核上同时运行,不共享内存.

This is caused by the Global Interpreter Lock. Threads share the program memory. To protect against conflicts caused by separate threads changing the same variable, Python locks execution to a specific thread. Meaning that at any moment only one thread runs. When the CPU is idle the program switches between threads, making IO-bound applications run faster. By contrast, Processes run simultaneously on separate cores and do not share memory.

当您的代码中的第二个线程启动时,两个线程都尝试访问相同的变量.这会导致线程出错,而进程运行正常.

When the second thread starts in your code, both Threads try to access the same variables. This causes the error with threads, while processes run fine.

这里是优秀且冗长的解释.

这篇关于OpenCV 中的 Python 线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆