Showing posts with label sunset. Show all posts
Showing posts with label sunset. Show all posts

Saturday, 24 February 2018

Coding for day or night events

This post was promoted by a post on the  GarysShed blog after a discussion on triggering events based on whether it's day or night time.

To be fair, the whole post is an excuse to include following video from the BBC's 'Walk on the Wildside' series


I needed to modify the behaviour of the illumination LEDs in one of my Raspberry Pi-based  birdboxes based on whether it is light or dark.  This box has IR (Infra-Red) as well as visible light LEDs, I wanted the IR to operate all the time, and the standard LEDs to come on (and IR LED go off) only in the event of a daytime  motion 'event', which could be triggered by any of:  Entrance hole IR beam being broken, PIR insider the box triggered or motion detected on camera.

An IR-cut filter over the camera can then be switched on or off depending based on this too - IR LED off when visible LEDs are on.  (An IR-cut filter turns an IR-sensitive camera into a daylight-only sensitive camera).

The idea was that I didn't want visible LEDs coming on during the night, mainly to disturb any occupants, I also didn't want this happening as it looks a bit odd from the outside:

Possible solutions:
1) Analyse camera image for brightness
2) Use a light-dependent resistor (LDR) to sense ambient light and modify LED behaviour based on this.
3) Implement a software-method to predict day vs night based on longitude and latitude

1) Analyse camera image for brightness.  Borrowed code from here and here and here
convert test.png -colorspace hsb -resize 1x1 txt:- | sed -n 2p | sed 's/.*(//; s/).*//'

If run in the terminal, will output something like: 81.857%,38.706%,17.7462% where these figures are the HSB (hue, saturation, brightness) values from a 1x1 pixel converted version of the original image (test.png in this example)

2) Use LDR to monitor light.  There's loads of examples out there how to do this, a randomly selected recent one here

At one time I did a merge of method 1 & 2 to which I've uploaded to a github repository Ambient-Light-Monitoring, the script is called ambient_lightMonitor.py.  This reads actual light level via an  LDR, and also takes image with a Raspberry Pi Camera and measures image brightness.  All these variables are then written to a file for playing with which could be applied with in a variety of contexts..

3) Software method
I've tried two approaches, both rely on feeding the application the current location via longitude and latitude.

3.1) SunWait.  A stand-alone program, an example use is described in a blog post here.  Essentially you run a crontab job every ?minute that writes to a file to indicate the current light/dark state.  You can then read that file into your application and base the outcome on whether its day (..light) or night (...dark).  I could not get it to work..

3.2) Python module: pyephem, website here
This is a python module, so is used within a python script.  You need to install python-dev first or the install of this module will fail. The module can then be installed with the pip python module installer
sudo apt-get install python-dev
sudo apt-get install python-pip  #if pip not available, e.g. on Raspbian Lite
sudo pip install ephem
Note, the sudo bit is important otherwise if you attempt to run a python script from rc.local  containing this module (to run it at boot) the root user wont be able to find the module since without 'sudo...' it will install ephem under the user 'pi'

This approach worked for me.  Longitude and Latitude can be generated on various websites, eg here, pyephem also uses height above sea level which I think I got off google earth.  This would be aprticularly relevant if you're using it at the top of a mountain....
Refer to the example script light_dark.py in the above mentioned githib repository

3.3) ?Pikrellcam motion capture application
I use the awesome motion capture application PiKrellCam in my birdboxes.  In its .config file is the facility to define location by longitude and latitude.  I spotted a recent forum post querying if it was possible to switch an IR-cut filter on or off by using the day/night status within Pikrellcam to trigger a command to the GPOI pin(s) controlling the IR cut.  See my IR-cut post on how to wire such a thing up.

Sunday, 8 May 2016

Sunset timelapse: Raspistill vs PiCamera

There two ways (that I know of)  that the Raspberry pi camera can be triggered to take a still image, using Raspistill, or PiCamera.  PiCamera is a python interface for the Raspbery Pi camera. Raspistill can be called from the command line [or... within a python script using os.system("raspistill commend etc").

My preliminary attempts at sunrise timelapses using PiCamera have lead to over exposed skies, with all the details blown out.  This side by side comparison compares:  PiCamera (LEFT) to Raspistill (RIGHT).  

These timelapses are made from a consecutive series of alternate jpgs (PiCamera then RaspiStill) on default settings, as follows (I've left out any referenced variables to stop it getting too unwieldy):
PiCamera
def imagePiCamera():

 with picamera.PiCamera() as camera:

  camera.led = False
  camera.resolution = (2592, 1944)
  camera.framerate = (1, 1)
  camera.vflip = False
  camera.hflip = False
  camera.quality = 100
  camera.exposure_mode = 'auto'
  camera.awb_mode = 'auto'
  camera.image_effect = 'none'
  camera.color_effects = None
  camera.start_preview()
  time.sleep(2)
  camera.annotate_bg = True
  camera.annotate_text = dt.now().strftime('%Y-%m-%d %H:%M:%S')
  camera.capture(folderToSave + "/" + s_fileSerialNumber + "_PiCamera.jpg")

RaspiStill
def imageRaspiStill():    
 os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + s_fileSerialNumber + "_imageRaspiStill.jpg --nopreview -awb auto") 

My take on this is that I get a better sky / sun using RaspiStill, which is a bit of a disappointment since I like being able to annotate with text (as you can see in the PiCamera version (LEFT)... which is no use in a timelapse, I just forgot to switch it off...)

For interest, the 'grouped per-method' images are make into a video as follows:
#rename files
aa=0;for i in `ls`; do sudo mv $i `printf "%04d" $aa`.jpg; aa=$(($aa+1));done
#make timelapse
avconv -r 24 -i %04d.jpg -r 24 -vcodec libx264 -crf 20 -g 15 output.mp4

I got fed up waiting for this to run on my Raspberry pi, so run it on a linux virtual machine through virtualBox on a windows PC = much faster.  The RaspPi3 will compile a 800 image timelapse in approx 30 min using this approach.  Takes me approx 1 min using the other approach.

For the comparison... A side-by-side stitch of the two sunset timelapses as follows:
ffmpeg -i timlapse_PiCamera.mp4 -i timlapse_RaspiStill.mp4 -filter_complex \
'[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' \
-map [vid] -c:v libx264 -crf 23 -preset sideBySide.mp4