是否可以并行读取网络摄像头帧? [英] Is it possible to read webcam frames in parallel?

查看:86
本文介绍了是否可以并行读取网络摄像头帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Python 脚本,该脚本使用我的网络摄像头具有流式传输的潜力30 FPS,但是由于我的 Raspberry Pi 不够强大,因此我只能读取约20 FPS.运行脚本时,我的CPU的一个核心已最大化到100%,但是其余的核心未受影响,因此,我试图将读取的数据分成尽可能多的线程,以便最大程度地使用我的CPU.潜力,轻松达到30 FPS.

I have a simple Python script that captures a webcam using 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. 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.

那么可以并行读取网络摄像头帧吗?

So is it possible to read webcam frames in parallel?

这是我的尝试:

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

运行此脚本时,我在终端中收到几个VIDIOC_QBUF: Invalid argument(通常4次).睡眠值越大,得到的错误消息越少.因此,例如,如果我time.sleep(0.1),我可能会收到2-3条错误消息,而不是4条消息.

When running this script, I get a couple of VIDIOC_QBUF: Invalid argument in my terminal (usually 4 times). 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.

这是有问题的,因为在脚本的第二部分(未在此处发布)中生成的结果视频文件已损坏.仅当并行读取网络摄像头时,才会发生此错误.当顺序执行所有操作时,视频文件很好,我可以毫无问题地阅读它.

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!

我认为还必须注意,VIDIOC_QBUF: Invalid argument错误消息是在读取前几帧后发生的.例如,我可以启动我的脚本,这几乎会立即触发这些错误,但随后我的脚本可以在无限"的时间内运行良好,而没有任何错误消息.

I think it is also important to note that the VIDIOC_QBUF: Invalid argument error messages happen after reading the first couple frames. 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.

推荐答案

在等待开始读取大量线程之前完全读取第一帧时,VIDIOC_QBUF: Invalid argument消失了!

When waiting for the first frame to be completely read before starting plenty of threads, the VIDIOC_QBUF: Invalid argument goes away!

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

请注意,即使所有CPU内核都与此算法一起使用,我能够达到的最大FPS也为24.

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.

这篇关于是否可以并行读取网络摄像头帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆