【问题标题】:Webcam from worker thread来自工作线程的网络摄像头
【发布时间】:2017-07-31 05:52:07
【问题描述】:

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

问题是框架不可见。如何让它可见?

【问题讨论】:

  • sigh 每天至少有一个。 imshow 没有 waitKey。阅读documentation:“这个函数后面应该有waitKey函数,它会显示指定毫秒的图像。否则,它不会显示图像。”

标签: python multithreading opencv python-multithreading


【解决方案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()

【讨论】:

  • 你能帮我解决这个问题吗stackoverflow.com/questions/45409937/…
  • 即使预测很慢,我也希望视频帧流畅
  • 代码只回答,没有任何解释。不测试cap.read() 是否成功。误导性的“试图打开相机”仍然远远超过它实际发生的点(第 5 行)。无意义的睡眠。在绝大多数情况下,它永远不会尝试加入线程。
猜你喜欢
  • 1970-01-01
  • 2012-06-19
  • 2023-02-10
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
相关资源
最近更新 更多