OpenCV Python,从命名管道中读取视频

问题描述 投票:0回答:1

我正在尝试获得视频所示的结果(使用netcat的方法3)https://www.youtube.com/watch?v=sYGdge3T30o它是将视频从树莓派流式传输到ubuntu PC并使用openCV和python处理它。

我使用命令

raspivid -vf -n -w 640 -h 480 -o - -t 0 -b 2000000 | nc 192.168.1.137 8000

将视频流式传输到我的PC,然后在PC上创建名称管道'fifo'并重定向输出

nc -l -p 8000 -v > fifo

然后我试图读取管道并在python脚本中显示结果

import cv2
import subprocess as sp
import numpy

FFMPEG_BIN = "ffmpeg.exe"
command = [ FFMPEG_BIN,
        '-i', 'fifo',             # fifo is the named pipe
        '-pix_fmt', 'bgr24',      # opencv requires bgr24 pixel format.
        '-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.frombuffer(raw_image, dtype='uint8')
    image = image.reshape((480,640,3))          # Notice how height is specified first and then width
    if image is not None:
        cv2.imshow('Video', image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    pipe.stdout.flush()

cv2.destroyAllWindows()

但是我遇到了这个错误:

Traceback (most recent call last):
  File "C:\Users\Nick\Desktop\python video stream\stream.py", line 19, in <module>
    image = image.reshape((480,640,3))          # Notice how height is specified first and then width
ValueError: cannot reshape array of size 0 into shape (480,640,3)

似乎numpy数组为空,因此有解决此问题的想法吗?

python opencv ffmpeg raspberry-pi
1个回答
0
投票

图像数组为空。由于raw_image也为空,因此似乎未读取视频。确保视频已打开并正在阅读。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.