工作线程的网络摄像头

时间:2017-07-31 05:52:07

标签: python multithreading opencv python-multithreading

我使用以下代码在后台线程中运行网络摄像头。我必须做大量的处理,所以我做了这个,希望它能改善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()

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

1 个答案:

答案 0 :(得分:1)

这是您的问题的答案

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