camera projects on raspberry pi

10 cool things to do with a camera on your Raspberry Pi

Hey, I just bought a camera for my Raspberry Pi. I wanted to share the experience to help you purchase and install yours.
And also show you some exciting projects to realize with a camera and a Raspberry Pi

So what’s the use of a camera on Raspberry Pi?
As with any computer, a camera can, of course, be used to take pictures, movies or webcam service.
But thanks to the small size of the whole thing, it is possible to imagine exciting projects!

I will explain to you how to:

  • Choose your camera if you do not have one yet
  • Install it in your Raspberry Pi
  • Perform the first basic tests
  • The 10 coolest projects I’ve found to do with a camera

Getting started

Buy a camera

If you do not have a camera yet and you are interested in this subject I advise you to take action
This is an investment of less than $20, compatible with the entire Raspberry Pi range

You have two choices overall when you buy a camera:

  • The official model, available in two variants (standard or NoIR as No Infrared)
  • A compatible model created by another manufacturer

Personally, I opted for a compatible model (check on Amazon), which seemed even more affordable with a night vision function that seemed interesting
You will find my opinion on the recommended products page

It really depends on what you want to do, but again the price is so low that even if you have to change afterward, it’s not a big problem

Do not forget to think about your goal
If you intend to move the set, there are cases with a passage for the camera or even a planned fixation
It is essential, especially outside

For example with this kind of box, you have a slot that allows passing the cable of the camera.
Then place the camera in a module like this to fix it or put it somewhere

Install the camera

Hardware installation

camera cable raspberry piThe installation of the camera on the Raspberry Pi is quite simple since you just have to connect your camera cable on the Raspberry Pi camera port
Remember to remove the plastic film there, and insert the wire into the slot as in the picture

Software installation

By default packets to manage your camera are not installed and the camera port is not enabled

Once your camera is plugged in, you have to change all that:

  • Launch the raspi-config utility
    sudo raspi-config
  • Go to the camera menu
  • Choose to activate the port
  • Apply and quit
  • Restart the Raspberry Pi when asked

That’s all there is to do and after the reboot, your camera is directly usable

We will see in the next paragraphs how to check its operation

First photo

The first thing we can do is take a simple picture of the image seen by the camera

For that, the raspistill command is already installed, and you can use it like this:

raspistill -o image.jpg

The -o parameter is used to set the file name where the image will be saved

Launch the command and open the file to verify that it works well

First video

A command is also available to record a video: raspivid

The basic use is this one:

raspivid -o test.h264

Press CTRL + C to stop recording
You can open the file with VLC (sudo apt-get install vlc)

There are quite a few other options for this command
For example, you can define the duration of the recording:

raspivid -t 6000

Time is in ms,  -t 6000 will create a 6s video

Or the dimensions of the image

raspivid -w 800 -h 600

I invite you to look at the help of the command to discover all the possible options
Just type “raspivid” so that they are displayed

First python script

As you know, the Python language is often used on Raspberry Pi
So I’m going to show you how to create a simple python script to take a picture

First create a text file, either with a graphics editor or with nano:

nano test.py

Then paste this code into the file

import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    camera.capture('/home/pi/Images/python.jpg')
    camera.stop_preview()

Record and execute the script:

  • Either double-click on it to open Thonny, and start the script by pressing the Play button
  • Either in ssh, via the command
    python test.py

Then go to the folder /home/pi/Images and open the file python.jpg which contains the photo you take

Add a timer

Ok, it’s not bad this script, but it does not leave us time to prepare for the shot 🙂

You can quickly add a timer to the previous script, which will shifter the X seconds

Edit your text to look like this

import time
import picamera

with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(5)
    camera.capture('/home/pi/Images/python.jpg')
    camera.stop_preview()

You can even add a sound just before taking the picture if you want 🙂

import pygame
import time
import picamera


with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(4)
    pygame.mixer.init()
    pygame.mixer.music.load("flash.wav")
    pygame.mixer.music.play()
    time.sleep(1)
    camera.capture('/home/pi/Images/python.jpg')
    camera.stop_preview()

Of course, this is only a basic script to give you the instructions to use, but it can be modified and extended to infinity according to your needs

10 cool things to do with a camera on your Raspberry Pi

1 – IP Camera

The first idea of use is to transform the Raspberry Pi IP camera, and watch your apartment, baby, cat, …

For that, we will use motion which allows giving access to the camera via a web page

First, install motion and dependencies

sudo apt-get install -y motion libavcodec-dev libavformat-dev libavutil-dev libjpeg-dev libjpeg62-turbo-dev libpq-dev libswresample-dev

Then activate the camera driver

sudo modprobe bcm2835-v4l2

Check that your camera is visible with the command

v4l2-ctl -V

 

This command should show you the settings of your camera in this form:

Video Capture Format:
Width / Height: 640/480
Pixel Format: 'YU12'
Field: None
Bytes per Line: 640
Size Image: 460800
Colorspace: SMPTE 170M
Transfer Function: Default
YCbCr / HSV Encoding: Default
Quantization: Default
Flags:

Then you’re supposed to post these settings in the motion configuration in /etc/motion/motion.conf
On my side, I did not succeed, probably because I do not have the original camera, so I get a functional configuration on a forum, here are the steps:

wget https://www.dropbox.com/s/6ruqgv1h65zufr6/motion-mmal-lowflyerUK-20151114.tar.gz
tar -zxvf motion-mmal-lowflyerUK-20151114.tar.gz
sudo motion -c motion-mmalcam-both.conf

After that, your camera is now accessible to the URL: http://X.X.X.X:8081
Replace X.X.X.X by the IP address of the Raspberry Pi

You can then redirect a port on your router for access from the outside if needed

2 – Create a timelapse

A timelapse is a video created from images taken at regular intervals that gives the impression to see what happens in an accelerated way
Here is an example :

To do this create a script that will take a picture loop and store everything in the same folder
Then you can use a tool like ffmpeg to gather all that in a video

I show you how to do with the two methods seen previously:

  • the python script with a timer
  • the raspistill command

Timelapse with a python script

  • Create a new script:
    nano timelapse.py
  • Paste this code in the editor:
    import time
    import picamera
    
    TOTAL_IMAGES = 10
    TIME_BETWEEN_IMAGES = 60
    
    with picamera.PiCamera() as camera:
        for image in range(TOTAL_IMAGES):
           camera.start_preview()
           time.sleep(TIME_BETWEEN_IMAGES)
           camera.capture('/home/pi/Images/timelapse/timelapse%04d.jpg' % image)
           camera.stop_preview()
    

    Ok so depending on the desired film time, and the length of the desired final video, we define the variables from the top of the script with the total number of images to take and the time between each image
    For the test, I took small values, but if you want to film a whole day it will be necessary to increase them

    Also, remember to change the %04d by a larger figure if you take more than 10000 images

  • Run the script
    python timelapse.py

    You can also run it in Thonny which is not bad for this kind of little script

Timelapse with a bash script

You can do pretty much the same thing with a bash script

For this follow the following steps:

  • Create a script
    nano timelapse.sh
  • Paste the content of the following script
    #! /bin/bash
    
    TOTAL_IMAGES=10
    TIME_BETWEEN_IMAGES=60
    
    for i in `seq 1 $TOTAL_IMAGES`
    do
    echo $i
    /usr/bin/raspistill -o /home/pi/Images/timelapse/timelapse$i.jpg
    sleep $TIME_BETWEEN_IMAGES
    done

    The same as with python

  • Make the script executable
    chmod +x timelapse.sh
  • Run the script
    ./timelapse.sh

Create the video

In both cases, the script created a lot of images in the folder /home/pi/Images/timelapse
We now need to merge them in a video file

For that, you have to follow the following steps with ffmpeg

  • Install ffmpeg
    sudo apt-get install ffmpeg
  • Run this command
    cd /home/pi/Images/timelapse
    ffmpeg -r 24 -pattern_type glob -i '*.jpg' -s hd1080 -vcodec libx264 -crf 18 -preset slow timelapse.mp4
    
    

    You can find all the documentation on the ffmpeg website if you want to tweak this command

3 – Detector

If you have a motion sensor for your Raspberry Pi, it is possible to operate it with the camera to trigger a shot

You can code this motion detector in Python with the pir library
A tutorial is available on the Raspberry Pi Foundation website if you are interested

4 – Minecraft photobooth

As you know, Minecraft and Python are often nested on Raspberry Pi
And so you can combine the two to create a photo booth in Minecraft that will take you in real life when you get in

For this you must follow this steps:

  • Build a room that looks like a photo booth in the game
  • Note the coordinates of the block on which you will have to be to trigger the photo (x, y, z)
  • Create a piece of code that reads the player’s position at any time
    And that triggers the camera with the commands seen previously when the player reaches the desired position
    The code may look something like this:

    while True:
        x, y, z, = mc.player.getPos()
        if x == 50 and y == 64 and z == -122:
           mc.postToChat("Smile!")
           camera.start_preview()
           sleep(2)
           camera.capture('/home/pi/Images/mc.jpg')
           camera.stop_preview()
           sleep(5)

5 – Security Camera

Home security is an increasingly important market
Manufacturers build sophisticated cameras to monitor your home
But did you know that the Raspberry Pi was quite able to achieve this task?

The first point on this list was about the Motion software that could be used to turn our camera into an IP camera
Well, this software can manage motion detection, and can also trigger commands when saving an image

The basic idea is to activate the motion detection and use the on_picture_save parameter of the configuration to send an email with the image as an attachment

Do not forget to pay attention that your Raspberry Pi is always running
That is well powered even in your absence, automatically start services and maybe set up some alerts to avoid breakdowns (I think of disk space for example)

6 – Solar powered outdoor camera

As you know, the Raspberry Pi consumes very little, and it is possible to power it via batteries (USB power bank or specific)
It is also possible to connect it to a solar panel, which will allow to power (or recharge the batteries) the day

Our camera can become autonomous outdoor
If you set up one of the movement detectors mentioned earlier, you will be able to take photos or videos closer to the animals in your area.

It is a project that can be interesting if you live in the countryside and want to observe the animals that run near your house 🙂

7 – Underwater camera

See the post on Hackaday

Another fantastic project that I discovered recently
Scientists seek to know the seabed better, and to observe them cheaply

A team to build an underwater camera based on a Raspberry Pi, a camera and some LEDs
All for about $ 100 each

If this prototype is validated, it will greatly facilitate research in this area
You can find more information about this project by clicking on the image

8 – Car reversing camera

I kept you the most refreshing ideas for the end 🙂
We are now moving to more significant projects

Do you have a car a little old or low-end?

It’s possible to install a Raspberry Pi as an onboard computer, and therefore enjoy a reversing camera and many other options for less than $ 250.

The video will speak better than me 🙂

9 – Add a camera to your drone

If you are a fan of drones, you should know that these machines can be costly, especially those equipped with HD camera allowing you to film your exploits

If you already have a drone, why not tie him a Raspberry Pi with a camera? 🙂
The guys of SixFab have gone further in the video below. They also integrated a 4G chip, and automate the streaming of video on Youtube

In the same state of mind, you can also attach your Raspberry Pi to a remotely controlled vehicle before starting a course with your friends 🙂

10 –  Photobooth

We saw in this list how to create a photo booth in Minecraft, but it’s also possible to do it in real life!

There are tons of projects on the internet that pushed the concept
Trigger button, built-in screen, instant photo printing, little waiting music, etc.
It must be a hit at a wedding, for a minimal budget!

Here is an example in a video:

Conclusion

So we saw in this article :

  • How to add a camera to your Raspberry Pi
  • How to put it into use with some basic commands and scripts
  • A lot of crazy projects that I hope will give you insights into the possibilities of the Raspberry Pi and a camera

 

 

Leave a Comment