简体   繁体   English

工作线程中的网络摄像头

[英]Webcam from worker thread

Im using the following code to run webcam in background thread. 我使用以下代码在后台线程中运行网络摄像头。 I have to do heavy processing so I have done this hoping that it will improve the fps 我必须进行繁重的处理,因此我希望这样做可以提高fps

import cv2
import time
from threading import Thread

cap = cv2.VideoCapture(0)
threads = []

class WorkerThread(Thread):
    def run(self):
        print("start")
        ret, frame = cap.read()
        cv2.imshow('Face', frame)


if __name__ == '__main__':
    try:
        print("Trying to open camera")
        while(cap.isOpened()):
            thread = WorkerThread()
            thread.start()
            threads.append(thread)
            time.sleep(0.35)
    except KeyboardInterrupt:
        for thread in threads:
            thread.join()
        cap.release()

the issue is that the frame is not visible. 问题是框​​架不可见。 how can I make it visible? 如何使其可见?

This the answer of your problem 这是你问题的答案

import cv2
import time
from threading import Thread

cap = cv2.VideoCapture(0)
threads = []

class WorkerThread(Thread):
    def run(self):
        print("started")
        while True:            
            ret, frame = cap.read()
            cv2.imshow('Face', frame)
            k = cv2.waitKey(5) & 0xFF
            if k == ord('q'):
                break


if __name__ == '__main__':
    try:
        print("Trying to open camera")
        if(cap.isOpened()):
            thread = WorkerThread()
            thread.start()
            threads.append(thread)
            time.sleep(0.35)
    except KeyboardInterrupt:
        for thread in threads:
            thread.join()
        cap.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM