如何同时分析和流式传输树莓派视频

3

我正在使用raspivid和netcat将一个视频从RaspberryPi Zero流式传输到我的PC:

raspivid -t 0 -n -w 320 -h 240 -hf -fps 30 -o - | nc PC_IP PORT

现在我想逐帧分析这个树莓派上的视频以进行对象检测。树莓派必须对对象检测做出反应,因此我必须在实时流视频的同时在Pi上进行分析。
我的想法是使用tee命令创建一个命名管道,并在Python程序中读取此命名管道以获取每一帧:
mkfifo streampipe    
raspivid -t 0 -n -w 320 -h 240 -hf -fps 30-o - | tee nc PC_IP PORT | streampipe

但是这并没有起作用,它显示sh1: 1: streampipe: not found

我的Python程序如下所示:

import subprocess as sp
import numpy

FFMPEG_BIN = "ffmpeg"
command = [ FFMPEG_BIN,
        '-i', 'streampipe',       # streampipe is the named pipe
        '-pix_fmt', 'bgr24',      
        '-vcodec', 'rawvideo',
        '-an','-sn',              # we want to disable audio processing (there is no audio)
        '-f', 'image2pipe', '-']    
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)

while True:
    # Capture frame-by-frame
    raw_image = pipe.stdout.read(640*480*3)
    # transform the byte read into a numpy array
    image =  numpy.fromstring(raw_image, dtype='uint8')
    image = image.reshape((480,640,3))          # Notice how height is specified first and then width
    if image is not None:

        analyse(image)...

    pipe.stdout.flush()

有人知道如何做到这一点吗?

感谢您的回答。

1个回答

2
< p > tee 命令将 stdin 复制到 stdout,同时也会复制到您提到的任何其他文件中:

ProcessThatWriteSTDOUT | tee SOMEFILE | ProcessThatReadsSTDIN

或者制作两份副本:

ProcessThatWriteSTDOUT | tee FILE1 FILE2 | ProcessThatReadsSTDIN

您的nectcat命令并不是一个文件,而是一个进程。因此,您需要让您的进程看起来像一个文件——这就是所谓的“进程替换”。您可以按照以下步骤进行:

ProcessThatWriteSTDOUT | tee >(SomeProcess) | ProcessThatReadsSTDIN

因此,简而言之,您需要更像这样的东西:
raspivid ... -fps 30-o - | tee >(nc PC_IP PORT) | streampipe

非常感谢您的解释。对我来说,它是这样工作的:raspivid ....... | sudo tee /tmp/streampipe | sudo nc PC_IP PORT,但它具有很高的延迟,并且仅适用于10fps视频流。 - Tom
你是如何在电脑上接收数据的?它可能会等待并建立缓冲区。或者你可能需要发送更低带宽(更压缩)的数据 - 也许是YUV而不是RGB。 - Mark Setchell
在我的电脑上,我使用命令:nc -l PORT | mplayer -vf mirror -fps 30 -demuxer h264es -,那么我该如何为流添加更多的压缩? - Tom
通过自动曝光控制,更亮的照明意味着曝光时间更短、噪音更少,这通常意味着每秒钟可能拍摄更多的曝光,并且更容易实现更高的压缩。 - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接