Tuesday, June 25, 2013

26. Streaming Live Video from the RasPiCam to the PC


This is the next step in using the RasPiCam - live video streaming.  I got the instructions from the following link:

Download and install on the PC, the open source (free) programs Netcat and MPlayer.  Netcat is "a computer networking service for reading from and writing to network connections using TCP or UDP" and MPlayer is a media player which can play most movie formats. This download can be conveniently done via the dropbox link by Matthew Manning of Raspberry π IV Beginners:

The files in this zip file can then be unzipped and extracted into a folder whose name should be noted for later access.  I put mine into my C:\Documents and Settings\User\mplayer and netcat\ folder.  As we shall see soon, this location is convenient.

Now we want to download and install these two programs on the Raspberry Pi:

sudo apt-get install mplayer netcat

Now we need to set up a Windows PC to receive a signal.  On the PC (running MS Windows), click 

Start -> All Programs -> Accessories -> Command Prompt

At this point, you can find out what the PC's IP address is by entering ipconfig in the Command Prompt program.  You will need this soon to tell the Pi where to send the video data.

Within the Command Prompt (terminal program) window, go to the directory, where nc and mplayer are (my Command Prompt program goes automatically to C:\Documents and Settings\User\, so I just have to enter cd mplayer and netcat to change directory to the mplayer and netcat directory to get the prompt (>) in the right directory.  

Now enter:

nc -l -p 5001 | mplayer -fps 31 -cache 1024 - 

which commands Netcat to set up a listener (with the -l) on port 5001, and the pipe (|) pipes the data it 'hears' to the MPlayer program, to play at 31 frames per second, with a memory cache size of 1MB. 

On the Pi, to send the video data, enter 

raspivid -t 9999999 -o - | nc 192.168.1.64 5001 

to run the raspivid program for a long-ish time (9999.999 seconds, or approximately 2¾ hours), the pipe (|) pipes the output of this part of the command to the program nc (Netcat) for transmission to 192.168.1.64 which is the IP address of my PC, found above using the ipconfig command, on port 5001.

When this is done, a tab at the bottom of the PC desktop for MPlayer, opens up and clicking on this opens a full-screen real-time video of whatever the RasPiCam is pointing at.

Here is a screenshot of some video I got from my house window:

The quality's not great, but hey - once again - it's magic.  I could keep an eye on my cruiser (if I had one)!!  Country fans - enjoy the music.

Thursday, June 20, 2013

25. Still Fiddling around with RasPiCam Time-Lapse and Video

1000 page views!!
The recent spell of good weather here has given me the opportunity to snap away with the RasPiCam.  I've been using it so much recently that I actually thought the Pi had died - communications stopped and the LEDs on the Pi were off, despite the fact that the power was on and the input capacitor was hot.  I dismantled the whole setup and I was about to parcel up the Pi for return for repair under warranty (assuming there is a 12-month warranty), when a final test (measuring voltages etc) showed that all was well.  

Interestingly, I received my Pi on the 25-Jun-12, just under a year ago.  It's going fine now but the poor thing must have over-heated in the summer heat.  Mind you, it has been powered up for virtually all of that year since I got it!  Hopefully it's going to be OK.

Anyway, taking advantage of the sunshine for decent images, I made some more YouTube productions:


Above is a time lapse sequence of images taken at 10 second intervals.

Above is part of an overnight time lapse sequence at 15 minute intervals. I removed 4 or 5 hours of complete darkness.

This is a 10 minute time lapse sequence of images taken at 1 minute apart. The above images have been processed to remove haziness. I did this using ImageJ's Image -> Adjust-> Brightness/Contrast facility. Some of the above images are arguably over-processed, losing information.

Here's a 60 minute time lapse series of 60 images taken from Rostrevor, showing the changing light over Slieve Gullion and Warrenpoint:



Above is an un-processed 30-second mp4 video, not time-lapse, taken as:
raspivid -o video1.h264 -w 1080 -h 1080 -t 30000

(There's not a great deal happening in that scene).  The video was produced by using the previously installed gpac's MP4Box to convert the .h264 to .mp4 on the Pi itself:
MP4Box -add video1.h264 video1.mp4 

and now transferring the resulting mp4 file to my PC using WinSCP (not by email as before!), also previously described.

I used VideoPad Video Editor (free download) to read the mp4 file (some processing can be done by VideoPad) and export it as a sequence of jpeg's (1094 images) which ImageJ is able to import.  ImageJ can then File -> Save As -> AVI.  The AVI is a suitable format for YouTube input.  When no intermediate processing is to be done (as above) YouTube can import the untreated mp4 file:
Above is a screenshot of VideoPad in action.

You can see how hazy the above video is due to light scattering in the 2 mile distance in humid conditions.  The heat haze also adds to the unsharpness of the images due to convection currents.  I decided to show it unprocessed to show how effective contrast enhancement can be on the other images.

Here is a processed version, which is over-processed.  It's quite difficult to apply very subtle processing without losing information:



The colours are somewhat improved, but some might say - over-saturated in places.  It's not easy!!

Saturday, June 8, 2013

24. More RasPi Cam Pictures

I thought I would try hitching up my Raspberry Pi camera to my scope.  The scope is an Opticron Mighty Midget Waterproof Field Scope which has a 14 to 36 times zoom with a 50 mm objective.  There is a UV filter on the objective and the outside diameter of the eyepiece end is about 40 mm.   

The angle of the Pi with its WiFi adapter attached, is not ideal for picking up the WiFi network so that there are only certain positions in the upstairs room where I can pick up WiFi and at the same time get a view through the windows.  The position shown below happens to be almost directly above the WiFi router downstairs, with only a ceiling and wooden floor (no bricks, concrete or steel) in between.

Here's the setup:


The Mighty Midget was mounted on a tripod, with the RasPi Cam fitted to the lens cover, with the Pi hanging on for dear life below.  

It's very difficult to get reasonable results by just sticking the RasPi Cam on the end with tape, which I discovered with my time lapse sequences.  The camera gradually moves if there is any tension on the tape, especially if the room is very warm, or there's very strong sunshine about so I cannibalised my plastic eyepiece lens cover by boring a 7.5mm x 7.5mm square hole in the centre for pushing the camera part of the camera board into.  Here are some still pictures, some of which I straightened and cropped a little using Photoshop Elements:
This first picture, shown raw, was taken on a hazy sunny morning.  The quality hasn't been enhanced, so the internal window reflections add to the haze.  The buildings in the background are about 4 miles away.

The following images have been taken at the lowest zoom setting, and have been enhanced using Photoshop Elements.  

The boat above is about a half mile away.

These buildings are up to 4 miles away.

Distance here is up to about 4 miles.

The background buildings above are about 2 miles away.


The same buildings above imaged with maximum zoom.  Sharpness falls off with high zoom on this scope.

Distance here is about 3 miles.  The image was taken at minimum zoom.

Not bad for casual shots through the window?  Make sure you click on these to see them full size!

Here is a time lapse series taken with the field scope.  It's a 60-image series taken at 1 second intervals:



For the next one, I managed to get a setup with the Pi Cam properly aligned and with improved WiFi reception.  You can see a Jet Ski pulling a Banana Boat going past.  There was a little jitter on the video despite my binning of sticky tape - there's probably something elasticy - maybe the rubber lens cover that I cannibalised.  

I was able to fix this using Kang Li & Stephen Kang's Image Stabiliser Plugin for ImageJ, and it works a treat!  See http://www.cs.cmu.edu/~kangli/code/Image_Stabilizer.html  

I would certainly recommend ImageJ for image processing - see http://imagej.nih.gov/ij/features.html - it's Java based, open source, regularly updated, and there's a world of Plugins available, and - if you know a little Java, you can write your own Plugins!!.

Have a look at these YouTube videos full screen:

Tuesday, June 4, 2013

23. Making Time Lapse Sequences with the Pi Cam

The first thing I had to do was to find a way of transferring files from the Pi to my PC.  Up until now I have been emailing them to myself from the Pi and picking them up on the PC - a bit primitive, and not very practical for files bigger than 25 MB (the email limit) or for a large number of files.

I came across WinSCP (Secure Copy Protocol for Windows).  This was very easy to install and run, and it works great!  You can easily download it free from winscp.net.  Here is a screenshot of the setup window:



I just ignored the Private key file and Select color options as I don't know what they are for.  When you click Login, you get a screen like this:



It's magic!  On the left pane are my PC files and on the right, the Pi's files.  That's pretty impressive, as the PC is on Windows, and the Pi is on Linux.  To transfer files from one machine to the other, you can drag 'n drop or highlight and move - brilliant!  If you look carefully, you'll see a series of files on the Pi named a_00001_1min.jpg to a_00010_1min.jpg.  These are my very first attempt at time lapse photography and I'll tell you more about these below.

I got the instructions from the very helpful Alex Eames of RasPiTV at http://raspi.tv/tag/raspicam-nine-day-time-lapse-video

So to get a 1-minute time lapse sequence of jpegs, each representing a 6 second interval, I ran the following command:

raspistill -t 60000 -hf -w 1920 -h 1080 -tl 6000 -o a_%05d_1min.jpg
By way of explanation,
-t 60000 is the number of milliseconds in 1 minute - the period the program runs for.
-hf is horizontal flip (to reverse the mirror reflection effect)
-w 1920 -h 1080 width and height for 1080p video output. There's no point in recording at the absolute maximum still image size of 2592 pixels by 1944 pixels, as HD video is happy with 1920 x 1080 pixels.
-tl 6000 is the number of milliseconds between shots (6 seconds)
-o a_%05d_1min.jpg is the option to name the output file.  The %05d part gives you a 5 figure integer padded with leading zeros. So, the first one will be 00001, and the tenth would be 00010. 
Each jpeg produced is about 1 MB in size.  I used WinSCP above to transfer the series of images to the PC and I then used my old friend ImageJ to process the images:

This very useful (free) Java-based program is great for all sorts of image processing, and you can write your own Java macros to do exactly what you want with images.  I read the series of jpegs into ImageJ and asked it to produce a stack, to play at 3 fps.

Then I saved the stack as an animated GIF file.  This format is suitable for playing on browsers, including Safari on iOS devices:

What you are seeing here is a demonstration of my Darkness Detector which uses the Darlington Pair of transistors (See Post 17 on this Blog - HERE) - exciting stuff!!

My next task is to run my Pi headless (ie without a monitor, as I want to move my Pi to a window for recording more exciting time lapse exposures) and to do this I need to be able to control it through SSH - ie see its desktop on my PC (See Post 21 on this Blog - HERE).

Hey Presto!  Running Xming and PuTTY works!!  I can now control my headless Raspberry Pi which is upstairs, with my downstairs PC, using the wireless network.

Here's my next attempt at time lapse - a 12 hour sequence (with the black night-time images removed):

Unfortunately it's a little crooked, but it's quite difficult to guess how straight the camera board is when taped to the inside of a window.  It also has lost a lot of image quality in the conversion to a movie.

Watch out for the early bird flying past at about 5 am, and then another - and the tide coming in - and the ship going past - and then another.

Here's another time lapse sequence of some blue tits on my bird feeder.  (Must fill up with peanuts soon):
I used ImageJ in macro mode to crop to 769x769 pixels, a series of 44 exposures taken at 2 minute intervals,  and I enhanced contrast to remove some of the glass reflections from my window.

Now wouldn't it be nice to have a proximity monitor (PIR detector?) to trigger the recording?

Here's a couple more: