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

时间:2020-02-28 16:44:36

标签: python opencv ffmpeg raspberry-pi

我正在尝试获得视频所示的结果(使用netcat的方法3)https://www.youtube.com/watch?v=sYGdge3T30o 它是将树莓派的视频流传输到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数组是空的,所以有什么办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

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

相关问题