简体   繁体   English

是否可以并行读取网络摄像头帧?

[英]Is it possible to read webcam frames in parallel?

I have a simple Python script that captures a webcam using OpenCV . 我有一个简单的Python脚本,可以使用OpenCV捕获网络摄像头。 My webcam has the potential to stream 30 FPS, but since my Raspberry Pi isn't powerful enough, I can only read ~ 20 FPS. 我的网络摄像头可能具有30 FPS的传输能力,但是由于Raspberry Pi的功能不够强大,因此我只能读取〜20 FPS。 When running the script, one core of my CPU is maxed to 100%, but the the rest of the cores are untouched, so I am trying to split up the reading into the most threads I can in order to use my CPU to it's maximum potential and easily reach 30 FPS. 运行脚本时,我的CPU的一个核心最大可使用100%,而其余的核心则保持不变,因此我试图将读取的数据分成尽可能多的线程,以便最大程度地使用我的CPU。潜力,轻松达到30 FPS。

So is it possible to read webcam frames in parallel? 那么可以并行读取网络摄像头帧吗?

This is my attempt: 这是我的尝试:

import numpy as np
import cv2
import time
from threading import Thread


CV_CAP_PROP_FPS = 5

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, cap.get(CV_CAP_PROP_FPS), (640, 480))
threads = []

class MyThread(Thread):
    def run(self):
        ret, frame = cap.read()

if __name__ == '__main__':
    try: 
        while(cap.isOpened()):
            thread = MyThread()
            thread.start()
            threads.append(thread)
            time.sleep(0.035)
    except KeyboardInterrupt:
        for thread in threads:
            thread.join()
        cap.release()
        out.release()

When running this script, I get a couple of VIDIOC_QBUF: Invalid argument in my terminal (usually 4 times). 运行此脚本时,我在终端中收到几个VIDIOC_QBUF: Invalid argument (通常为4次)。 The greater the sleep value is, the less error messages I get. 睡眠值越大,我得到的错误消息越少。 So for example, if I time.sleep(0.1) , I might get 2-3 error messages instead of 4. 因此,例如,如果我time.sleep(0.1) ,我可能会收到2-3条错误消息,而不是4条。

This is problematic, because the resulting video file that is generated in the second part of my script (that isn't posted here), is corrupted. 这是有问题的,因为在脚本的第二部分(此处未发布)中生成的结果视频文件已损坏。 This error only occurs when reading the webcam feed in parallel. 仅当并行读取网络摄像头时才会发生此错误。 When executing everything sequentially, the video file is good and I can read it with no problems at all. 当顺序执行所有操作时,视频文件很好,我可以毫无问题地阅读它。

Any insight is greatly appreciated. 非常感谢任何见解。 Cheers! 干杯!

Edit: I think it is also important to note that the VIDIOC_QBUF: Invalid argument error messages happen after reading the first couple frames. 编辑:我认为也很重要,请注意VIDIOC_QBUF: Invalid argument错误消息出现在读取前几帧后。 For example, I could start my script, which would almost instantaneously trigger those errors, but then my script could run fine for an "infinite" amount of time without any error messages. 例如,我可以启动我的脚本,这几乎会立即触发这些错误,但随后我的脚本可以在“无限”的时间内运行良好,而没有任何错误消息。

When waiting for the first frame to be completely read before starting plenty of threads, the VIDIOC_QBUF: Invalid argument goes away! 当等待开始读取大量线程之前完全读取第一帧时, VIDIOC_QBUF: Invalid argument消失了!

...
try: 
    ret, frame = cap.read()
    while(cap.isOpened()):
        thread = MyThread()
        thread.start()
        threads.append(thread)
        time.sleep(0.035)
...

Note that even if all the CPU cores are being used with this algorithm, the maximum FPS I have been able to reach is 24. 请注意,即使所有CPU内核都与此算法一起使用,我能够达到的最大FPS也为24。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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