By Petr Kout, August 24, 2014
There are multiple ways to stream video from Raspberry Pi (RPi) to another computer via the wired or wifi ethernet. After trying multiple methods, stumbled upon one that leads to minimal latency and works really well over a wifi connection. The solution is the combination of mjpeg-streamer on the RPi and OpenCV client program on the other computer. In this tutorial, I am working with linux Ubuntu as the client computer. Let’s get to it! First, we will install the RPi software, then receiving computer software.
We will not use the standard mjpeg-streamer package because that one doesn’t have a built-in support for the RPi camera. Instead, we’ll use this guy’s great fork of mjpeg-streamer: https://github.com/jacksonliam/mjpg-streamer. It allows streaming video frames directly from your RPi camera, which is very efficient as well as convenient. So, let’s install it:
Log into your RPi and go to /usr/src/ and create a directory mjpeg-streamer there:
cd /usr/src sudo mkdir mjpg-streamer sudo chown `whoami`:users mjpg-streamer cd mjpg-streamer
Now clone the mjpeg-streamer from the github repository there:
git clone https://github.com/jacksonliam/mjpg-streamer.git .
In order to compile the code, we’ll need to install some library dependencies: apt-get install libv4l-dev libjpeg8-dev imagemagick build-essential cmake subversion Next, we’ll need to compile the mjpeg-streamer. Enter:
cd mjpg-streamer-experimental make
Now we should be set to start streaming the video. There are many options you can set. For details, visit the GitHub page linked above and look at the readme page. Here, we will do a simple example of streaming of 640×480 resolution video at 20 frames per second. If you lower the resolution, the latency will get smaller.
export LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so -x 640 -y 480 -fps 20 -ex night"
The export LD_LIBRARY_PATH variable sets the current directory as a path where programs should look for libraries. Our program uses output_http.so and input_raspicam.so libararies found in the current directory, which is why we added that directory to LD_LIBRARY_PATH.
That’s it! Mjpg-streamer will now stream the video to the port 8080 on this RPi. We will access the streamer from the other computer over the network and display it.
Note down the RPi’s IP address as we will need it to access the stream on its port 8080. Before we do so, we’ll need to install Python and OpenCV, if they don’t exist on your system yet.
Install Python and OpenCV:
sudo apt-get install python python-opencv python-numpy
Now, you have two options how to view the stream from the RPi. Either you open a browser and enter your RPi’s IP address followed by 8080 (e.g. http://192.168.0.193:8080). This will load a web page generated by the RPi. You can then click on the Stream tab and see the video stream right there. Alternatively, and those who want to further process the video will prefer this, you can create a file rpi-stream.py and paste the script below into it to get a video stream from the RPi and display it using OpenCV.
Paste this into the file and save it.
import cv2 import urllib import numpy as np stream=urllib.urlopen('http://192.168.0.193:8080/?action=stream') bytes='' while True: bytes+=stream.read(1024) a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a !=- 1 and b != -1: jpg = bytes[a:b+2] bytes= bytes[b+2:] i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR) cv2.imshow('i',i) if cv2.waitKey(1) == 27: exit(0)
Don’t forget to change the IP address in the script with your RPI’s IP address.
Finally with the mjpg-streamer running on the RPi, execute the above Python script on the computer where you want to see the video:
You should now see a window open and start streaming the video with a latency 0.5 seconds or less.
BIG THANK YOU to jacksonliam user on GitHub for doing this work! You’ve done an amazing job!