Friday, May 1, 2015

66. A Graphical User Interface, written in Python, for use with the Raspberry Pi Camera

42,000 page views!!
In the course of investigating the Pi's GPIO ports and also python Graphical User Interfaces (GUI), I thought I would re-visit the Raspberry Pi camera and see how I could exploit the software attributes of this magnificent piece of hardware, and include GPIO control at the same time.

Here is the physical setup:
As you can see, the Raspberry Pi Camera Module is connected through the Pimoroni Pibow CoupĂ© Flotilla case case to the Pi 2.  The Pi's GPIO pins are connected to the Cyntech B+ 40-way Paddle Board and from that, connections from GND and GPIO17 are made to a mini breadboard containing a mini push button switch.
  
Here is a picture of the the GUI:



and here is a whole-screen shot:
The sunflower seedling in the pot was the subject of a one week time lapse sequence of 2-hour delayed images, showing the birth of this seedling from a single seed.  The sequence of images was very impressive, and I would have liked to show them here, but unfortunately I lost all the images!  I'll try it again some time.

In the meantime, here's an animated GIF of a time lapse sequence of an internet clock, taken at 60 second intervals:
(I'm not sure what happened the alignment here - it looked nicely centred in the Preview).

Here's a better one, taken at 30 minute intervals:


You can see from the second hand in both the above examples, that a small delay introduced by the software increases the set interval by approximately one second each exposure.

Here is a shot where one example of the 22 available (including 'none') image effects - 'negative' - has been set:
You can see that on the left of the screen, there is an indication (in the LXTerminal) of values of such things as Brightness, Contrast, Saturation, Image Effect etc.

The "Set favourites" button returns all attributes (except Brightness, Contrast and Saturation) to their initial settings.

The "Demo" button starts a routine where the automatic white balance (awb) is changed for each 3-second preview, right through all 10 of the available effects (including "auto" and "off").  However, these camera.image_effect attributes do not seem to be operational currently, at least they don't seem to have any noticeable effect.

The "Quit" button cleanly exits the program, but only after any currently running routine has finished.  For example, the program would wait until a time lapse sequence would run through to the end before it terminates.

In addition to buttons in the GUI, I added some sliders, for Brightness, Contrast and Colour Saturation.  There is an additional slider at the bottom for setting the delay in seconds for time lapse sequences.  The maximum time lapse interval I have set is 7200, for 2-hour intervals.  This maximum time of course can be changed in the code.

The breadboard pushbutton is used for the "Take still" and "Take video" modes.  It is pushed to take a single still picture, with the settings displayed.  For recording video, the button is pressed once to start the video exposure, and then pressed again to finish the video sequence.  When the program is expecting a button push, it prints the message "Press the Button!" in the LXTerminal on the left of the screen.

The program uses the Tkinter library to draw the GUI elements such as buttons and sliders etc. The picamera library is invoked for using all the imaging attributes such as camera.brightness and so on.  RPi.GPIO of course is imported as the GPIO17 port is used for the pushbutton.

One nice little addition is the "LED" button which toggles the Camera Module's red LED on and off.  I used the statements camera.led = False and camera.led = True.

Finally you will see near the bottom of the GUI shown above, that there is a Motion detect button, followed by a slider labelled Threshold %.  The slider allows values between 1.5 and 2.5 per cent to be chosen.  The percentage refers to that of the image pixels which have to change before motion is considered to have been detected.  The button starts the motion detection part of the program.  The initial setting at line 20 of the code:

20 threshPercent=1.8 #This value is close to ideal for most cases

is 1.8 per cent, which is useful for detecting significant motion, without filling the Pi's SD card with hundreds of images.

Later I added the ability to zoom in on images, which works quite well, except I haven't found a way to centre the image after zooming,

I also added to my Pi Camera board, a zoom lens.  This gives reasonable magnification, but the image quality is a little disappointing.  I haven't got to the bottom of this yet, but here's an animated GIF image taken through the zoom lens and with motion detection::


Here are a couple of pictures of my 8 x zoom clip-on lens mounted on the Raspberry Pi Camera Board:

You can see in the first picture, the zoom lens clipped on to the camera board (and supported by a foil pie dish).  You can also see that the camera LED is on.  The second view shows the lens, Raspberry Pi in its case, and a push button mounted on a mini breadboard, through the Cyntech B+ 40-way Paddle Board (previously described in my Post 55 at http://smokespark.blogspot.co.uk/2014/10/55-.  The push button is for taking still shots or for starting and ending video recordings. (You can just about see out through the window, a couple of garden birds on my peanut bird feeder).


Here's the Python code:


I will continue to develop this as there are more picamera.PiCamera() attributes that can be built in to this program.  Keep watching this space!