代码之家  ›  专栏  ›  技术社区  ›  user3600801

来自worker线程的网络摄像头

  •  0
  • user3600801  · 技术社区  · 9 年前

    我使用以下代码在后台线程中运行网络摄像头。我必须做大量的处理,所以我希望这能提高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 回复  |  直到 9 年前
        1
  •  0
  •   Salih Karagoz    9 年前

    这就是你问题的答案

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