简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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