来自工作线程的网络摄像头

Webcam from worker thread

我正在后台线程中使用以下代码 运行 网络摄像头。我必须进行繁重的处理,所以我这样做了,希望它能提高 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()

问题是框架不可见。我怎样才能让它可见?

这就是你的问题的答案

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