MJPG-Streamer
When integrating FingerVision into a robotic system, we would use many FingerVision sensors. Due to the bandwidth of USB cameras, it would be difficult to handle many cameras simultaneously with a PC. One solution is using a single board computer (e.g. Raspberry Pi) to convert USB camera data to video stream over HTTP. MJPG-streamer provides such a conversion.
Raspberry Pi and OS †
We tested Raspberry Pi 3B which worked well. We could stream video from two USB cameras at more than 60 FPS in 320x240. For more details, refer to: Raspberry Pi Setup.
- Note
- This information is obsolete:
We recommend to use Ubuntu MATE 16 (ubuntu-mate-16.04-desktop-armhf-raspberry-pi.img.xz) as the operating system of RPi. We found that a newer version (Ubuntu MATE 16.04.2) had a trouble in streaming MJPG data; it seemed that ffmpeg installed in default was the reason of this issue. Use Ubuntu MATE 16.04.
- Note
- We recommend to use Raspbian rather than Ubuntu MATE 16. The version we tested is Version:June 2018 (Release date:2018-06-27, Kernel version:4.14). With this OS, we could stream video from multiple (two) USB cameras without troubles. We didn't experience the "No space left on device" issue when using multiple cameras.
MJPG-Streamer †
There are some forks of MJPG-Streamer.
- Original version: https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer
- Had a trouble in streaming video from USB cameras on Raspberry Pi.
- jacksonliam's version: https://github.com/jacksonliam/mjpg-streamer
- Forked version from the original.
- Working well with USB cameras on Raspberry Pi.
- Our version: https://github.com/akihikoy/mjpg-streamer
- Forked from jacksonliam's version.
- Made the OpenCV plugin compatible with OpenCV 2.4.8 (the repository version of Ubuntu 16.04).
- Added an input_opencv_file plugin for loading video from a file. This is useful to test FingerVision programs with the recorded videos without modifying the program.
Thus, we recommend to use our version.
Installation †
cf. the installation guide of MJPG-streamer.
$ sudo apt-get install git cmake libjpeg8-dev uvcdynctrl v4l-utils $ mkdir ~/prg && cd ~/prg/ $ git clone https://github.com/akihikoy/mjpg-streamer.git mjpg-streamer2 $ cd mjpg-streamer2/mjpg-streamer-experimental $ make
Usage †
An example command line of launching MJPG-streamer is:
$ ./mjpg_streamer -i "./input_uvc.so -f -1 -r 320x240 -d /dev/video0 -n" -o "./output_http.so -w ./www -p 8080"
This will stream the video from the camera device /dev/video0 of to the URL http://localhost:8080/
Options:
- -f -1: FPS is automatically chosen.
- -r 320x240: Camera resolution.
- -d /dev/video0: Device.
- -n: Do not initialize dynctrls of Linux-UVC driver.
- -y: YUV format (default: MJPG format).
- -e NUM: Capture every NUM frame (other frames are dropped).
Access the URL, you will see an web site where you can see the view stream and control the camera parameters. If the computer has an IP or domain, you can access the same web site by http://CAMERA_PC:8080/
The video stream can be directly accessed by http://localhost:8080/?action=stream&dummy=file.mjpg where dummy=file.mjpg is a trick for OpenCV. Without this option, OpenCV cannot recognize the URL as mjpg video stream.
You can stream multiple cameras by changing the port from 8080 to 8081 or some. A bottleneck is the bandwidth of USB.
Refer to Video Capture and Rectification#Utility for capturing from video stream.
$ ./capture.out "http://localhost:8080/?action=stream&dummy=file.mjpg"
Change localhost to the other address to see the video stream over the Ethernet network.
MJPG-Streamer on Raspberry Pi †
The script mjpg_stream.sh is a utility to use MJPG-Streamer with multiple cameras. It also has a function to automatically restart MJPG-Streamer when it stops.
SSH on Raspberry Pi and run:
$ cd $ wget https://raw.githubusercontent.com/akihikoy/fingervision/master/tools/mjpg_stream.sh $ wget https://raw.githubusercontent.com/akihikoy/fingervision/master/tools/stream.sh $ chmod 755 stream.sh
stream.sh is an example to use mjpg_stream.sh whose code is:
#!/bin/bash
uvc_opt="-f -1 -r 320x240 -n"
devs=(0 1)
source mjpg_stream.sh
uvc_opt denotes options of the input_uvc plugin that is common for all cameras. devs is an array of video device IDs (0 means /dev/video0).
By running this script, the videos are streamed to ports 8080, 8081, ... of the Raspberry Pi address.
In mjpg_stream.sh, we assume that MJPG-Streamer is installed in ~/prg/mjpg-streamer/mjpg-streamer-experimental which is defined by the mjpg_streamer_dir variable. Modify it if necessary.
Usage of stream.sh: On a remote PC,
$ ssh CAMERA_PC './stream.sh'
Then it starts to stream videos. You can access the streams from CAMERA_PC:8080, CAMERA_PC:8081, etc.
Press ENTER to quit streaming.
Streaming Video Files †
Our version of MJPG-streamer has a plugin to stream video files. Using input_opencv_file, we can stream a video file over Ethernet.
For example,
$ ./mjpg_streamer -i "./input_opencv_file.so -f 60 -d cam0_0001.m4v" -o "./output_http.so -w ./www -p 8080"
-f 60 is an option to set FPS.
Access http://localhost:8080/stream.html to see the video stream. Note that cam0_0001.m4v should be replaced by a video file path.
The fingervision package provides utility scripts to stream multiple video files simultaneously. It also provides video files (dummy FingerVision data) taken with actual FingerVision sensors. Refer to Dummy FingerVision Data.