簡體   English   中英

工作線程中的網絡攝像頭

[英]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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM