Streaming OpenCV Output Through HTTP/Network with MJPEG

If you want to stream your OpenCV output image to another device which connected to the same network, one way is to write the OpenCV output as MJPEG file, then stream this file through HTTP.

Please note that:
– This might be not the best way (correct me if I’m wrong)
– OpenCV 2.3 cv::VideoWriter has memory leak bug

Steps:
0. OpenCV Stuff
I assume you already have your OpenCV libraries installed, because several of OpenCV’s dependencies are also MJPEG-Streamer’s, such as libv4l-dev and libjpeg-dev. And make sure your OpenCV application works (it writes the mjpeg file)

1. Install MJPEG-Streamer
Install the dependencies first

sudo apt-get install imagemagick

Then get the latest MJPEG-Streamer from the repository:

git clone https://github.com/codewithpassion/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr/local install

Then set the environment variable LD_LIBRARY_PATH if you haven’t, either in /etc/bash.bashrc or your own ~/.bashrc, add this line:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

2. Into The Main Show
First fire up your OpenCV application, this is my application, simple background subtraction and writes the output into mjpg file.

git clone https://github.com/ariandyy/bgsubtract.git
cd bgsubtract/
make bgsubtract2

Prepare the folder to put the mjpg file into, for example in my home folder:

mkdir /home/ariandy/mjpg

Then run the application:

./bgsubtract2 /home/ariandy/mjpg/out.mjpg -bgs

Leave it running in one terminal, and open another terminal to test the MJPEG-Streamer:

mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www"
 

The command above is the most basic setting to get the streaming to work. By default, the web server is listening to port 8080. If you want to use port 80, you have to run the command as root (or with sudo).

The default www root of MJPEG-Streamer is located at /usr/local/www. There you can find the demo pages, from which you can get the idea of several ways to display the stream: either direct stream (supported by most Grade-A Desktop Browsers (Sorry, IE!)), using Java MJPEG viewer (cambozola), javascript, etc.

3. Viewing The Stream
On the client, simply point the browser to the server’s IP address with corresponding port (8080). e.g. server’s IP address is 192.168.0.1, then to access it from the client, point to http://192.168.0.1:8080. If you want to omit the :8080 part, then you have to run the server on port 80 (HTTP), as following:

sudo mjpeg_streamer -i "input_file.so -f /home/ariandy/mjpg" -o "output_http.so -w /usr/local/www -p 80"
 

I tried this with Google Chrome, Firefox, Android Browser, and Firefox for Android.

Sources:
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8659
http://sourceforge.net/projects/mjpg-streamer

Advertisements
Leave a comment

6 Comments

  1. Marcin

     /  April 9, 2013

    Thank you. I’ve been looking for it few days. ๐Ÿ™‚

    Reply
    • ariandy

       /  April 18, 2013

      My pleasure ๐Ÿ™‚

      But as I said in the post above, OpenCV (at least version 2.3.x which I am using) has memory leak problem. Someone has submitted a patch for that, maybe they fixed it in later version of OpenCV.

      Reply
  2. Zarnuji

     /  May 24, 2013

    i can do those all things.
    but, how to access the stream from another browser?

    Reply
    • ariandy

       /  May 24, 2013

      Hi,

      First of all the server and the client should be in the same network. Now let’s say your server’s IP address is 192.168.0.10. If you run mjpeg_streamer with default options (like what I did above), point the browser on the client to http://192.168.0.10:8080.

      Thanks for commenting, I updated the post with how to access the stream.

      I hope it helps ๐Ÿ™‚

      Reply
  3. I get an error:

    $ svn co https://mjpg-streamer.svn.sourceforge.net/svnroot/mjpg-streamer mjpg-streamer
    p11-kit: invalid config filename, will be ignored in the future: /etc/pkcs11/modules/gnome-keyring-module
    svn: Repository moved permanently to ‘https://svn.code.sf.net/p/mjpg-streamer/code/!svn/vcc/default’; please relocate

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: