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?
ffmpeg.exeincluding the directory. - Mark Setchellpipe.stdout.flush()is inside thewhileloop. Is it a copy and paste problem, or is it inside the loop? - Rotem