Some days ago my wife brought planting soil with seeds of some kind and said: “wouldn’t that be nice if you and the boys plant these?”
I thought to myself “ohhh yeah that could be nice… but it would be much nicer if I’ll document the plant growth and make a time lapse clip… in fact that will be awesome.”
I have a couple of RaspBerry PIs lying around in my man cave… Prefect, I thought to myself.
I spent a couple of hours googling and searching for a python script that will shoot a time lapse series, a one that will auto adjust the exposure and ISO values for day and night shooting, and guess what, on the third or fourth google page i found something that could fit exactly for my needs, some dude with a PhD in computer science wrote something that seems perfect.
Happy and cheered up I downloaded the script from the GitHub repository that it is stored on and tested it on my RaspBerry PI…
the first functionality was OK but when I simulated a low light conditions, I was heartbroken… the script fails to switch to night mode and exits with an error.
I’ve tried to contact the author by Email, opened an issue on GitHub, tried to look for him on FaceBook (he’s name is common like “Mike O’niel” and I got about ten thousand results so I gave up on that really quickly), and even tried to contact him on LinkedIn profile that was linked to the GitHub repo…
nada de nada… no reply from MR PhD on any of the channels.
Then I thought to myself “OK, I’m familiar with python, I’m using it regularly, why won’t I just try to deep dive in those scripts and try to trace the reason for the failure…”
I wore my diving suit and dived in.
After an hour or two in which I practiced frustration I decided that MR PhDs scripts are not for me, it will be easier to write my own scripts.
First I searched for a method so the picamera could detect if the conditions are day conditions or night conditions, somehow I stumbled upon this GitHub repo (https://github.com/NestBoxTech/Ambient-Light-Monitoring/blob/master/ambient_lightMonitor.py) from which I adopted the first three functions, added another function to retrieve the needed file for brightness analyzation (by my design it was the last file created in a destination folder) and added all those functions to light.py file, perfect, now I know to calculate the brightness of a latest photo stored in a given directory.
Next I need to start a time lapse shooting loop and implement the light detection functions.
For that matter I navigated to picamera recipes and used the standard time laps loop that would be used for Day Mode (https://picamera.readthedocs.io/en/release-1.10/recipes1.html#capturing-timelapse-sequences) I enriched it with timestamped output and a custom location to store the images.
From the same source I took the “Capturing in low light” piece of code and manipulated it to run as a loop, just replaced the “camera.capture to camera.capture_continuouse… blab la bla…” and that would be my Night Mode, which if very similar to the Day Mode just some other parameters.
I’ve placed both Day and Night modes in the same timelapse.py file and defined them as functions, initially I thought to use the “GOTO” method (like in bash scripts) but after reading the following Stack Overflow (https://stackoverflow.com/questions/18863309/the-equivalent-of-a-goto-in-python/18863545) I changed my mind, although GOTO could be very helpful for my usecase.
Next I’ve added conditions to functions loops, so that after every photo that is taken the light detection function will process the photo and determine if the light conditions are night or day conditions.
After I tested that this works correctly, I’ve added another functionality to time lapse functions, based on the calculation of the light detection function a day mode loop was stopped and night mode loop was triggered, and vice versa.
Then I performed a tiny little smoke test and VIOLLA, I got myself a Day and Night time lapse mechanism.
But wait a second… now I got myself a bunch of photos, now I need to stich them into a video.
For that matter I was hoping to use OpenCV, I followed a tutorial I found and as soon as I executed the “stitching” script my RPI almost immediately got stuck… hmmm
I dunno if it’s because of the Raspberry PI that is not powerful enough to perform the stitching or whatever was the reason for the PI to get stuck, anyway I tried it one more time with less photos (about200), and it crashed again, so I’ve moved to another method and continued my search on google which led me to this wonderful script (https://tsaith.github.io/combine-images-into-a-video-with-python-3-and-opencv-3.html) it has to be changed a little cuz by default it brings an array that is ordered in arbitrary order which makes the time lapse video unusable, I replaced “listdir” with “glob” which from my experience is better for that matter and now I’m all good.
* (FYI, if you interested in more fancy script you can go over the comments and look for “ButAlsoHam”s comment, he created two more versions of the script)
I am ready for production!
where were those seeds that she brought…
Conclusions:
this mechanism works very well, some improvements will be made though when I’ll create the second version.
there are some tweaks that could make it more elegant.
if you have an idea please post a comment below.
For more detailed information and instructions please visit the project page: https://eli-bukin.com/home/projects/day-and-night-timelapse-with-raspberrypi/