Getting Started with Raspberry Pi Camera

barIn this tutorial, you’ll learn how to use and connect a camera to Raspberry Pi 4 Model B.

Brief overview

At the heart of Raspberry Pi Camera v2.1 lies Sony’s IMX219 image sensor. This 8 MP powerhouse has significantly improved focusing and is compatible with all Raspberry Pi models. Capture crystal-clear still images at 3280 x 2464 pixels, record smooth videos in resolutions ranging from 1080p30, 720p60 to 640x480p90, or do a heap of other fun tasks—this little device is for capturing the moments!

What You Will Learn

You will see how to use and connect a camera to the Raspberry Pi 4 board in this tutorial.

  • How to control the camera using Python
  • How to use raspivid, raspistill and raspiyuv utilities
  • How to use start_preview() and stop_preview() to display the image from the camera
  • How to take photos using the capture() command
  • How to record video using start_recording() and stop_recording()
  • How to view video via omxplayer
  • How to change the brightness, contrast and resolution in photos
  • How to apply visual effects to video

Items for Getting Started

You can buy Raspberry Pi Camera Board and Raspberry Pi 4 Model B on Seeed Studio store. Check it out.

Preparation

Make sure you have the latest version of the Raspbian operating system You can download the latest version from the official Raspberry website. I’ve already published a quick start guide recently. Check it out.

  • After installing the OS, we will check whether the latest drivers are installed with the following commands.
sudo apt-get update
  • This command updates the list of available packages and their versions.
sudo apt-get upgrade

Now enable the camera interface by opening the Raspberry Pi configuration menu:

  • Go to Preferences menu -> Raspberry Pi Configuration

Enable the camera from Interfaces option and click OK.

  • After turning on the camera, the system will ask you to restart it. Reboot the system.

How to Connect a Camera to the Raspberry Pi

Many options are available for video output, and the Raspberry Pi board can accept this camera module using a special CSI (Camera Serial Interface) port; video data transfer up to 1080p at 30 frames per second and up to 720p at a maximum of 60 frames per second.

Unlike USB cameras, the CSI interface does not burden the Raspberry Pi’s processor, enabling more efficient use of the camera.

To connect the camera to the Raspberry Pi board, follow these steps:

  1. Turn off the Raspberry Pi.
  2. Carefully locate the camera port and lift the tabs.
  3. Gently insert the flex cable from the camera into the connector and lock the tabs.

Now the cable should be securely clamped in the CSI connector, and you can turn on the Raspberry Pi.

Taking photo using using Raspbian Terminal

For basic manipulations with the camera, there are 3 command line utilities that are preinstalled on the system:

  • raspivid – video capture utility
  • raspistill – photo capture utility
  • raspiyuv is a utility similar to raspistill, but instead of jpg files, as a result it generates raw files (uncompressed, unprocessed).

A complete list of parameters for each utility can be obtained if you run the utility without parameters

raspistill

either execute the utility with the –help parameter:

raspistill --help

The following are examples of utilities:

raspistill -t 2000 -o image.jpg -w 640 -h 480 -v

Capture an image with a delay of 2 seconds for a digital cam with a resolution of 640 x 480 pixels, outputting information during the operation of the utility (-v) and save with the name “image”. undefined

raspivid -t 10000 -o video.h264

Record a video 10 seconds long and save to video.h264.

Taking pictures using Python library – PiCamera

In addition to standard utilities, the camera can be used by software methods. For example, when building security systems, when it is necessary to activate recording, when an event occurs or according to a schedule.

Before using the camera in Python, you will require the PiCamera package that comes preloaded in the system. If for some reason it is not there, then you can install the library with the following command:

sudo apt-get install python3-picamera

Sketches with the name picamera.py cannot be used – this will make it impossible to use the PiCamera library in Python.

When the library is installed, it must be imported in the sketch:

import picamera

The following code enables the camera in preview mode for 10 seconds,

import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()

The following code enables the camera in preview mode for 5 seconds, then take a photo and save it as “image” on your desktop.

import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()

Recording video using python library – PiCamera

To shoot the video, we will use the start_recording() and stop_recording() commands.

import picamera
from time import sleep
camera.start_preview()
camera.start_recording('/home/pi/video.h264')
sleep(10)
camera.stop_recording()
camera.stop_preview()

After 10 seconds, the video will end and will be saved to the video.h264 file in your user’s root folder. To watch the video, use the omxplayer program.

omxplayer video.h264

Effects

The Picamera library for Python allows you to use a large number of settings and filters that can be applied to both the preview and the photo itself.

Adding the text

You can add any text to the photo using the annotate_text command. You can also change the text size with annotate_text_size command. See the following example.

import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_preview()
camera.annotate_text = "Hello world!!!"
camera.annotate_text_size = 60
sleep(10)
camera.stop_preview()
Change image resolution

By default, a photo is taken of the resolution that is configured on your monitor, but you can change it using the camera.resolution() command.

camera.resolution = (2592, 1944)
Adjust the brightness or contrast of a picture

You can adjust the brightness on the photo by setting it from 0 to 100. By default, 50 is used. If you want to set the brightness, for example, to 70, specify the following code after starting the preview:

camera.brightness = 70

To set the contrast, use the camera.contrast command.

Visual effects

You can use camera.image_effect to overlay a large number of different visual effects: negative, solarize, sketch, denoise, emboss, oilpaint, hatch, gpen, pastel, watercolor, film, blur, saturation, colorswap, washedout, posterise, colorpoint, colorbalance, cartoon, deinterlace1, deinterlace2, none.

With the help of the following program you can see all available filters. The code will change visual effects every 5 seconds:

import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_preview()
for effect in camera.IMAGE_EFFECTS:
camera.image_effect = effect
camera.annotate_text = "Effect: %s" % effect
sleep(5)
camera.stop_preview()

You can find a complete list of functions and features of the picamera library on the official website.

I hope you found this guide useful and thanks for reading. If you have any questions or feedback? Leave a comment below. Stay tuned!

Source: Getting Started with Raspberry Pi Camera


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *