One of the main attractions of the Raspberry Pi platform is its camera module. Currently, the Raspberry Pi Foundation offers three camera modules:
• Camera Module 2
• 2 NoIR camera module
• High quality Raspberry Pi camera
Camera module 2 is a replacement for the original camera module as of April 2016. Module V2 has an 8-megapixel Sony IMX219 sensor compared to the 5-megapixel OmniVision OV5647 sensor in the original module. The module is compatible with Raspberry Pi 1, 2, 3 and 4. It can be easily connected to the camera serial interface (CSI port) of any Raspberry Pi. The V2 camera module supports 1080p30, 720p60 and VGA90 video modes. The new module is not just high-resolution. It's much better in image quality, low-light performance, and color fidelity. The infrared camera module 2 is the same as the regular module 2, except it does not use an infrared filter. The Pi NoIR camera is very useful for night photography and video capture. The high-quality camera is a 12.3-megapixel Sony IMX477 sensor that supports C- and CS-mount lenses.
You can develop many interesting projects with the Raspberry Pi camera module. Well, just by installing a camera module into your tiny pocket computer, you can explore the vast world of image processing, video processing, and even machine learning. In this article, we explore how to get started with the camera module and control it using Python.
What do you need
All you need is a Raspberry Pi (1/2/3/4) and the camera module. The default camera module is used to take photos under light. The NoIR Pi camera takes photos in the dark and requires an additional infrared light source. In this article, we are working with the standard camera module. The camera module must come with a flexible cable for connecting to the CSI port. To connect the camera to the Raspberry Pi Zero, a smaller cable is required.
How to connect the camera module
All Raspberry Pis have a serial camera interface (CSI port). The camera module is connected to the CSI port. It is common to confuse the CSI port with the display port. The location of the CSI port on the Raspberry Pi is indicated below.
To install the camera module, locate the CSI port. Carefully remove the plastic clip from the door. Insert the flat cable from the camera module into the CSI port so that the connectors on the bottom of the cable face the contacts on the port. Put the plastic clip back so that the ribbon cable is secured. The camera module connected to the Raspberry Pi looks like the one shown below.
Enable Raspberry Pi Camera
The camera module is not enabled by default. After connecting the camera module, boot the Raspberry Pi. Go to the main menu and open the Raspberry Pi configuration tool. You can also open the configuration tool in the terminal by typing sudo raspi-config.
In the configuration tool, select the interfaces tab and make sure the camera is enabled.
If it is not enabled, enable the camera and restart the Raspberry Pi.
Taking snapshots of the command line
Once the camera module is installed and enabled, it's time to test it. The best place to test the camera module is the Raspberry Pi command line, i.e. the Bash shell. Open the terminal window by clicking the terminal icon on the taskbar.
To take a photo, type the following command and press Enter.
raspistill -o Desktop/image.jpg
The above command takes a photo using the camera module and saves it to the desktop. The camera view opens for five seconds when the above command is executed and closes when a still image is captured. Images can be saved to any other location as long as the specified folder exists in the Raspberry Pi's directory structure.
Capturing a video from the command line
To record a video using the camera module, type the following command and press Enter.
raspivid -o Desktop/video.h264
The above command captures a video recording and saves the recorded video to a specified folder when stopped. Just like images, videos can also be saved to any location as long as the specified folder exists in the Raspberry Pi's directory structure. Saved videos can be played using a VLC player.
Controlling the Camera Module with Python
The camera module can be controlled using Python. This requires the picamera library. On Raspberry Pi 3/4, the picamera library is already installed with IDLE or Thonny Python IDE. If the picamera library is missing, it can be installed for all users using the following commands.
$ sudo apt-get install python-picamera
Remember to update your installation after installing the picamera library using the following commands.
$ sudo apt-get update
$ sudo apt-get update
The picamera library can also be installed for all users using PIP.
$ pip install picamera
In a Python script, the picamera library can be imported using the following commands.
importing picamera PiCamera
To use the camera module in a Python script, an object of the picamera class needs to be instantiated as follows.
camera = PiCamera
Testing the camera module with Python
To test the camera module with Python, the best way is to run the camera preview for some time. If the camera module is installed correctly and working with the Raspberry Pi board, a red LED will flash on the camera module whenever the Raspberry Pi board boots up. The red LED stays on when the camera is active.
Save the following Python script to a .py file and run the code to see the camera view.
importing picamera PiCamera
of time matter sleep
camera = PiCamera
camera.start_preview
sleep (5)
camera.stop_preview
It should be noted that the file should never be saved as picamera.py. It should also be noted that the camera view only works when a monitor is connected to the Raspberry Pi. It does not work with remote access, whether SSH or VNC.
The start_preview method starts a camera preview. When the camera view is active, the entire monitor screen is occupied by the camera view. The camera preview can be stopped in Python code using the stop_preview method. If you forgot to add a line of code to stop the camera preview, it can only be stopped by exiting the script execution. Since the entire screen is occupied by the camera view, press Ctrl+F2 to stop code execution. This will safely exit the camera view.
Taking photos with Raspberry Pi Camera using Python
Photos can be taken using the capture method of the picamera class. The method requires a file path to store the image with a correctly specified image format. Run the following code to capture a still image using Python.
importing picamera PiCamera
of time matter sleep
camera = PiCamera
camera.start_preview
sleep (5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview
The sleep method must be called for at least 2 seconds before capturing the image. The camera sensor requires some time to detect light levels. A few seconds after starting the camera preview gives the module some time to adjust to the ambient light.
The above code runs the camera preview for 5 seconds. It takes a photo and saves it in the specified folder. The folder must exist in the Raspberry Pi directory structure and the image name must be specified in a recognized format such as jpg, jpeg, or png.
To take multiple photos, a for loop can be used as follows.
importing picamera PiCamera
of time matter 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 code above opens a camera view, takes five photos at an interval of 5 seconds and finally closes the camera view.
Recording video with Raspberry Pi camera using Python
A video can be recorded using the start_recording and stop_recording methods of the picamera library. The start_recording method starts recording a video. A sleep method can be called after it for the intended duration of the recorded video. Video recording can be stopped using the stop_recording method. Run the following code to record a video using Python.
importing picamera PiCamera
of time matter sleep
camera = PiCamera
camera.start_preview
camera.start_recording('/home/pi/Desktop/video.h264')
sleep (10)
camera.stop_recording
camera.stop_preview
The code above records a video using a Raspberry Pi camera for 10 seconds. The recorded video is saved to the specified location. The specified location must exist in the Raspberry Pi directory structure. The video name must be specified in the appropriate file format. The picamera library supports h264 and mjpeg video formats.
Rotating the camera view
It is possible to get a disoriented camera view on the monitor due to awkward installation of the camera module. The camera view can be rotated to 90˚, 180˚ and 270˚ using the camera object rotation property. If the camera view is upside down, use the following code.
camera = PiCamera
camera rotation = 180
Making the camera view transparent
You can make the camera view transparent. It is useful to see code compilation errors while the camera view is active. This is done by setting the alpha level of the camera preview. It is passed as an argument to the start_preview method. The value of an alpha parameter can be set between 0 and 255.
camera = PiCamera
camera.start_preview(alpha=200)
Changing camera view brightness
The brightness of the camera preview can be set by changing the brightness property of the camera object. Brightness can be set between 0 and 100.
camera = PiCamera
camera.start_preview
camera.brightness = 70
Changing the Camera View Contrast
The contrast of the camera view can be set by changing the contrast property of the camera object. Contrast can be set between 0 and 100.
camera = PiCamera
camera.start_preview
camera.contrast = 70
Changing resolution and frame rate
By default, the resolution of captured images is set according to the monitor resolution. The maximum resolution for still images can be 2596×1944 and for video recording it is 1920×1080. The minimum resolution is 64×64. The resolution can be changed by changing the resolution property of the camera object. It should be noted that if high resolution is selected, the frame rate must be reduced for proper operation by changing the frame rate property of the camera object.
camera = PiCamera
camera.resolution = (2592, 1944)
camera.framerate = 15
Adding text to images
You can add text to still images by specifying the annotate_text property of the camera object.
camera = PiCamera
camera.start_preview
camera.annotate_text = “EEWORLDONLINE”
Change the font and color of added text
The font size, color, and background color of added text can be changed by specifying the annotate_text_size, annotate_foreground, and annotate_background properties of the camera object. To add color to text, the color class also needs to be imported from picamera. The text size can be set between 6 and 160. The default text size is 32.
importing picamera PiCamera, Color
of time matter sleep
camera = PiCamera
camera.start_preview
camera.annotate_text_size = 50
camera.annotate_background = Color('blue')
camera.annotate_foreground = Color('yellow')
camera.annotate_text = “EEWORLDONLINE”
sleep (5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview
Changing the exposure of captured images
The exposure of captured images can be set by changing the position_mode property of the camera object. The default mode is automatic. The exposure modes supported by the picamera library are off, auto, night, night view, backlight, spotlight, sports, snow, beach, very long, fixed fps, anti-shake, and fireworks.
camera = PiCamera
camera.start_preview
camera.exposure_mode = 'beach'
sleep (5)
camera.capture('/home/pi/Desktop/beach.jpg')
camera.stop_preview
Run the following code to cycle through all exposure modes in the camera view.
camera = PiCamera
camera.start_preview
for exmode in camera.EXPOSURE_MODES:
camera.exposure_mode = exmode
camera.annotate_text = “Exposure mode: %s” % exmode
sleep (5)
camera.stop_preview
Changing the white balance of the image
The white balance of captured images can be set by changing the awb_mode property of the camera object. The default mode is automatic. The white balance preset modes supported by the picamera library are off, auto, night, sunlight, cloudy, shadow, tungsten, fluorescent, incandescent, flash, and horizon.
camera = PiCamera
camera.start_preview
camera.awb_mode = 'sunlight'
sleep (5)
camera.capture('/home/pi/Desktop/beach.jpg')
camera.stop_preview
Run the following code to cycle through all white balance modes in the camera view.
camera = PiCamera
camera.start_preview
for awbmode in camera.AWB_MODES:
Camera. awb_mode = awbmode
camera.annotate_text = “White balance mode: %s”% awbmode
sleep (5)
camera.stop_preview
Adding Image Effects
The picamera library also supports adding image effects to captured images. To add an image effect, the image_effect property of the camera object needs to be changed. Supported image effects are none, negative, solarization, sketch, noise reduction, emboss, oil painting, hatching, gpen, pastel, watercolor, film, blur, saturation, color shift, faded, posterization, colorpoint, image balance colors, cartoon, deinterlacing1 and deinterlacing2.
camera = PiCamera
camera.start_preview
camera.image_effect = 'oil painting'
sleep (5)
camera.capture('/home/pi/Desktop/beach.jpg')
camera.stop_preview
Run the following code to cycle through all image effects in the camera view.
camera = PiCamera
camera.start_preview
for in-camera effect.IMAGE_EFFECTS:
camera.image_effect = effect
camera.annotate_text = “Effect: %s” % effect
sleep (5)
camera.stop_preview
Conclusion
The Raspberry Pi camera module is a lot of fun. The picamera library is well written and powerful. It allows you to take photos, record videos, stream videos, stream videos to a network and even process images with openCV. The camera module is easy to install and offers endless possibilities to do many interesting things.