Setting up a Raspberry Pi Camera

The first part in a bigger project in order to build a weather detector

Project Image
Project Owner:

John N. G. Samarasinghe

Project Outcome:

Set up the Raspberry pi and its camera to take photos at a constant interval

Project End Date:

2020-11-01

Project tags:

Raspberry Pi, Hobby, Camera, Project, Progress, Camera

Dive in to details

I had a small generic Raspberry Pi Camera Rev 1.3 lying around in my parts box for some time and I have always put aside working on it. So I decided to finally take it out and one of my Raspberry Pi zeros to test out the Raspberry Pi Camera.

RPi Camera

Setting up the Pi camera isn't that difficult. All you need to do is to connect the ribbon cable to the camera connector on the Raspberry Pi and enable the camera through that Raspberry Pi Configuration.

I took some photos using the raspistill command on the terminal and obtained a few decent photos.

The picture below is very low light example picture with the subject at distance about a 3/4 of a foot from the camera.

RPi Camera

View this post on Instagram

A post shared by iamJohnnySam (@iamjohnnysam)

Once I set-up my Raspberry Pi camera, I wrote a simple script to take a picture from my camera every 2 minutes.

Option 1: Writing a Python Script

Initially I wrote a small Python code which takes a photo every 2 minutes from my Raspberry Pi Camera. The code can be found on my GitHub here.

Next I wrote a small shell script which can run my Python program and added a command to Crontab to run the shell script at start up.

Option 2: Run from Shell Script

The problem with Option 1 is that the camera is always on. To avoid this I ditched the python script and prepared a shell script that can directly take a picture from my camera.

For this I wrote a small shell script which can take a picture and save it to my desktop and added a command to Crontab to run the shell script every 2 minutes. (instead of @reboot, use */2 * * * *)

I attached the Raspberry Pi in my balcony as in the post below. A sample of half a day of photos is shown in the video above.

View this post on Instagram

A post shared by iamJohnnySam (@iamjohnnysam)


Update - I made a mistake!

When I set-up the Raspberry Pi camera to take pictures every 2 minutes, I did not check up on the files to see if they were saving properly. My goal was to leave the Pi running for at-least 10 days to get a large amount of images to work on some image processing and convolutional neural networks.

When I did my initial test for 1 days I calculated my image file size to be very small (max of about 200kB). So, if there a photo every 2 minutes, that's about 720 photos a day and 7200 photos in 10 days. Which is only 1.4-1.5GB of space - not a problem. Since my Raspberry Pi zero was did not come with WiFi I didn't bother looking at the progress as I didn't want to disturb the camera.

So where did I go wrong? When I did my first test I had a small Python script which took a photo every 5 minutes, but since the python script tends to leave the camera on as long as it is running, I decided to write a small shell script which can also do the same thing and I scheduled the script from Crontab to run every 2 minutes, but I did not check on the new file size. Apparently running the camera this way actually saves an Image of roughly 2.5MB instead of 200kB - Oops. The rest is simple, my Raspberry Pi exceeded by initial estimates by the end of Day 1 and the file system crashed at the end of Day 2. However, I did manage to extract the files from the SD card.

But turns out this is not the reason why my file system crashed. At the end of day 2 I only had about 2.5GB of space on an 8GB SD card. I knew there was no way my files could have exceeded this limit. So why then? When I did some reading I realized that this was to do with the block size. The block size on my Raspbian OS is about 4096 bytes, which means that any file is saved on fixed blocks of 4MB. Whether my image was 200kB or 2.5MB it would have taken up 4MB to save the file. So, looking at it this way it can be easily seen that to save my 2 days worth of images it had actually taken close to 5GB of disk space.

So technically, this attempt would have been a bust either way. Time to look in to a new method of saving files.

Similar Posts

See other projects and posts in the same category as this post

blog item Kivy based Semi-automatic image classification helper.
The second part in a bigger project in order to build a weather detector
Image Classifier, Kivy, Python 2020-11-11 | Read More....
blog item Setting up a Raspberry Pi Camera
The first part in a bigger project in order to build a weather detector
Raspberry Pi, Hobby, Camera, Project, Progress, Camera 2020-11-01 | Read More....
Comment Box is loading comments...