【问题标题】:How to simultaneously analyse and stream RaspberryPi video如何同时分析和流式传输 RaspberryPi 视频
【发布时间】:2020-01-24 18:52:31
【问题描述】:

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

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

现在我想在树莓派上逐帧分析这个视频来做物体检测。 Raspi 必须对对象检测做出反应,因此我必须在流式传输视频时对 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()

有人知道怎么做吗?

感谢您的回答。

【问题讨论】:

    标签: python camera raspberry-pi video-streaming named-pipes


    【解决方案1】:

    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 的视频流
    • 酷。请考虑通过点击投票数中的 ✅ 来接受它作为答案,以便其他人知道它有效 - 我也得到了一些积分:-)
    • 您如何在 PC 上接收数据?它可能会等待并建立一个缓冲区。或者您可能需要发送较低带宽(压缩程度更高)的数据 - 可能是 YUV 而不是 RGB。
    • 在我使用的 PC 上:nc -l PORT | mplayer -vf mirror -fps 30 -demuxer h264es -,那么如何为流添加更多压缩?
    • 通过自动曝光控制,更亮的照明意味着更短的曝光时间和更少的噪点,这通常意味着每秒可以进行更多的曝光并且更容易实现更高的压缩率。
    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2013-01-25
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多