0
votes

I am trying to achieve results as shown on the video (Method 3 using netcat) https://www.youtube.com/watch?v=sYGdge3T30o It is to stream video from raspberry pi to PC and process it using openCV and python.

I use command

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

to stream the video to my PC and then on the PC I created name pipe 'fifo' and redirected the output

nc -l -p 8000 -v > fifo

then i am trying to read the pipe and display the result in the python script

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

But I got this error:

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)

It seems that the numpy array is empty, so any ideas to fix this?

1
Maybe try adding the full path to ffmpeg.exe including the directory. - Mark Setchell
From your post, it looks like pipe.stdout.flush() is inside the while loop. Is it a copy and paste problem, or is it inside the loop? - Rotem
@Rotem There will be the same error as mentioned above whether it is inside the loop or outside the loop - BlueNut
@Mark Setchell There will be a 'FileNotFoundError' if I add the full path FFMPEG_BIN = "G:\tool\ffmpeg\bin\ffmpeg.exe" - BlueNut
You posted "stream video from raspberry pi to ubuntu PC", but according to your error message it looks like you are using Windows. - Rotem

1 Answers

0
votes

image array is empty. It looks like the video wasn't read since raw_image is also empty. Make sure that the video is opened and being read.