- Interface a Raspberry Pi-cam to the RPi3
- Writing a python code to store the image captured via camera
- Writing a python code to store the video captured via camera
- Playing the video using omxplayer
For this project, you will need
- Raspberry Pi 3 [RPi 3] board (quantity: 1 no.),
- Raspberry Pi Camera (quantity 1 no.),
- Micro SD card (quantity 1 no.),
- Power Adapter (quantity 1 no.),
- VNC Viewer software on your computer
The Raspberry Pi Camera V2 features an 8 megapixel Sony IMX219 image sensor with fixed focus lens, it is capable of 3280×2464 pixel static images and supports 1080P30, 720P60, and 640×480P90 video.
Connecting the Raspberry Pi to the Camera Module
To connect the camera to the Raspberry Pi, switch off the Raspberry Pi by pressing shut down in the menu. Locate the port for connecting the camera module.
Connecting the camera module
Turn on the Raspberry Pi. Check whether the camera is enabled in the Preferences -> Raspberry Pi Configuration Tools -> Camera. If it’s not enabled, enable it and reboot your Pi to begin.
Testing the Camera
Let’s test the camera whether it works or not. Open the terminal window and type the following command
sudo nano camera.py
Here you can name your file according to your wish but do not name it as picamera.py because picamera.py is pre-defined and may cause issues with system files. Enter the following code.
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() sleep(10) camera.stop_preview()
Save the code while exiting the editor and type the following command into a terminal to run it.
This will show you the camera preview for 10 seconds and it will close. The above code contains a predefined function called preview. This will enable us to see the preview of the camera vision.
- The camera preview only works when a monitor is connected to the Pi.
- Remote access (such as SSH and VNC) will not allow you to see the camera preview.
If your preview was upside-down, you can rotate it with the following code:
camera.rotation = 180 camera.start_preview() sleep(10) camera.stop_preview()
You can rotate the image by 90, 180, or 270 degrees, or you can set it to 0 to reset.
All the editing can be done by opening the python program using the command
sudo nano filename.py
Here the “filename” is the name given to the program.
Taking Still Images using the RPi Cam
Let’s take a few still images with the raspberry pi camera. To do this, amend your code to reduce the sleep and add a camera.capture() line.
camera.start_preview() sleep(5) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview()
In the above code, the camera starts preview and sleep of 5 seconds are given. It’s important to sleep for at least 2 seconds before capturing, to give the sensor time to set its light levels.
Run the code and you’ll see the camera preview open for 5 seconds before capturing a still picture. You’ll see the preview adjust to a different resolution momentarily as the picture is taken. You’ll see your photo on the desktop. Double-click the file icon to open it.
Now add a loop to take five pictures in a row.
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() for i in range(5): sleep(5) camera.capture('/home/pi/Desktop/image%s.jpg' % i) camera.stop_preview()
The variable i contains the current iteration number, from 0 to 4, so the images will be saved as image0.jpg, image1.jpg, and so on.
Run the code again and hold the camera in position. It will take one picture every five seconds. Once the fifth picture is taken, the preview will close. Now look at the images on your desktop and you’ll see five new pictures.
There you go!
Recording a video using the RPi Cam
Now that you have used the camera to take still pictures, let us move on to recording a video. To do this, amend your code to replace capture() with start_recording() and stop_recording().
from picamera import PiCamera from time import sleep camera = PiCamera() camera.start_preview() camera.start_recording('/home/pi/video.h264') sleep(10) camera.stop_recording() camera.stop_preview()
Run the code. This will record 10 seconds of video and then close the preview.
There you go!
To play the video, you’ll need to open a terminal window by clicking the black monitor icon in the taskbar.
Type the following command and press Enter to play the video.
The video will play. It may actually play at a faster speed than what has been recorded, due to omxplayer’s fast frame rate. Now that you know how to operate the camera, go ahead and explore using the camera in your projects for different applications like image processing, object detection etc.