Wednesday, April 16, 2014

52. Robotics with PiBot. VII - The Robot's View, Live on PC Monitor

23-Apr-14 Sparks N Smoke is one year old

16,000 page views!!

And here she is:

Yes - that's Poppy - being a little spooked by the PiBot! - Isn't she cute?

The last post showed how video could be recorded from the RasPiCam as the PiBot went about its travels.  The .h264 video file was recorded onto the PiBot's SD card, and later was processed on the Pi, to be 'wrapped' as a .mp4 file.  This more common file type was then transferred to the PC using WinSCP, and could be viewed on Windows Media Player or on YouTube.

This time, we are running a series of JPEG images, taken every 100 ms, and transmitting them 'live' to the PC over the WiFi network.

Here's an example of what was seen on my PC monitor, recorded by Microsoft Expression Encoder 4:

I have left a little bit of margin on the left hand side and at the top so that you can see that this has been captured from my PC screen.

Installing MJPG-Streamer:
The following steps are required to achieve this.  We will be using MJPG-Streamer, not picamera as before, so I followed the instructions of Miguel Grinberg as follows:


To install three libraries which are required by MJPG-Streamer:
$ sudo apt-get install libjpeg8-dev imagemagick libv4l-dev
MJPG-Streamer also requires a header file to be linked:
$ sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
The MJPG-Streamer source code zip file is downloaded from sourceforge:
$ wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip
The source code files are extracted:
$ unzip mjpg-streamer-code-182.zip

MJPG-Streamer is built using some plugins, which are needed to stream video :

$ cd mjpg-streamer-code-182/mjpg-streamer
$ make mjpg_streamer input_file.so output_http.so

The required files are put in the right directories:

$ sudo cp mjpg_streamer /usr/local/bin
$ sudo cp output_http.so input_file.so /usr/local/lib/
$ sudo cp -R www /usr/local/www
Running the PiCam:
The camera module is started:
$ mkdir /tmp/stream
$ raspistill --nopreview -w 640 -h 480 -vf -hf -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
making sure the picture is not upside-down, as the PiCam itself is.  The first command is needed each time this is run, as the directories made are only kept temporarily.  Miguel points out that different options of raspistill could have been used here.

I found that the only way to proceed from here was to start a duplicate session in PuTTY, logging in to the Pi in a second PuTTY window:

Running MJPG-Streamer:

MJPG-Streamer is started using:

LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /tmp/stream -n pic.jpg" -o "output_http.so -w /usr/local/www"
Now, using a web browser, I can watch the stream live at the address http://192.168.1.68:8080/stream_simple.html, where 192.168.1.68 is my Pi's IP address. Please note that this link will only work from within my own personal wireless network, not on the wider internet.
Miguel also recommends cleaning up by removing the source code, after it has been verified that everything is working:
$ cd ../../
$ rm -rf mjpg-streamer-182
To get the PiBot going, I used the same procedure as before, necessitating a third PuTTY session to be run - connected to TightVNC tvnviewer.  Then I ran Geany on the Pi's desktop, and then the python program which is the same as that in the last post, only with all references to picam removed:

Capturing the Video on the PC screen:
Finally, to capture what's being seen on the PC's monitor while the PiBot is roaming around, as I mentioned above, I used Microsoft Expression Encoder 4. 

This free program can be downloaded from HERE  and there is an option to upgrade to a paid version which of course, has more options.  To install the free version, just skip the part where a product key is required.  The free version will only produce .wmv files, but these are useful and can be uploaded to YouTube.  A couple of program files are downloaded - Microsoft Expression Encoder 4 Screen Capture and Microsoft Expression Encoder 4.  To start up, go to Microsoft Expression Encoder 4 Screen Capture, and the following screen window opens up:



Click on the red Record button on the right, and a rectangle surrounded by a thick red line is placed on the screen.  This rectangle can be moved around and re-sized until you're happy with the area of the screen that will be recorded.  Press Record, and a countdown 3, 2, 1, 0 is superimposed and the recording starts.  To stop the recording, press Stop.  The following window opens:
showing a list of recent recordings, the most recent being at the top.  Select the most recent and press Send to Encoder.  Animations show the encoder working, and the following screen comes up:
The video, in the top left section, can be played and even edited at this point.  When the Encode button at the bottom is pressed, the edited version is saved in .wmv format - a Windows Media Audio/Video file.  Navigate to this file, and clicking on it will play it on the Windows Media Player.

I should say that there are lots of options available including selecting the quality, frame rate, adding audio and selecting destinations to save the files in.

I should also say that I did this in this manner because I couldn't get picamera python code to control the camera while transmitting images, and that running three PuTTY sessions at the same time is very clumsy.  It also seems to interfere with the running of the python program as some funny things happen.

If there is anybody out there who knows how to make picamera work with this - please let me know!!

Voilà !!