Skip to content

Instantly share code, notes, and snippets.

@bhaskar-nair2
Created May 25, 2019 06:27
Show Gist options
  • Save bhaskar-nair2/b6506ad2eea72b752b3c8e84c6994480 to your computer and use it in GitHub Desktop.
Save bhaskar-nair2/b6506ad2eea72b752b3c8e84c6994480 to your computer and use it in GitHub Desktop.
Stream video from raspberry pi to PC without latency (<150ms)

To run this correctly

  1. Run the readFifo.sh file on client and ls the same directory to see if a pipe is made
  2. Run the showVid.py on the client (same directory) and see if your readFifo.sh gives output
  3. Run the ncStream.sh script on your raspberry pi, your client will pop out a video stream!!!
#!/bin/bash
raspivid -vf -n -w 640 -h 480 -o - -t 0 -b 2000000 | nc <your-pc's-ip> 5777
# This is the code to be run on the raspberry-pi
# Your pc's IP is the client IP which you can get by ifcongif
# Make sure both of these devices are on the same network ex. Home Wifi
# 5777 is the port
#!/bin/bash
if [ -p fifo264 ]
then
echo "Starting stream on fifo264"
rm fifo264
fi
mkfifo fifo264
nc -l -v -p 5777 > fifo264
# this script is run on the client side
# this makes a named pipe fifio264 which listens to the stream from port 5777
# this is another script which you need to run on the client side
# Make sure you have open-cv installed
import cv2
import subprocess as sp
import numpy
FFMPEG_BIN = "ffmpeg"
command = [FFMPEG_BIN, '-i', 'fifo264', '-pix_fmt', 'bgr24',
'-vcodec', 'rawvideo', '-an', '-sn', '-f', 'image2pipe', '-']
# fifo264 is the pipe name
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
while True:
raw_image = pipe.stdout.read(640*480*3)
image = numpy.fromstring(raw_image, dtype='uint8')
image = image.reshape((480, 640, 3))
if image is not None:
cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pipe.stdout.flush()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment