You are on page 1of 27

Raspberry Pi Camera Interface

1
Technical Specifications System On Chip (SoC) processor
Introduced in March 2016

2
Raspberry Pi 3 [3] Board layout.
RasPi Camera Module
• 8 MP camera module exclusively made for Raspberry Pi
• Video resolution = 1080p High Definition
• Picture resolution = 768p-1080p (still images).
• Small in size can be connected directly to the Raspberry Pi camera port

Camera Port

3
Configuring the Camera Module

• Switch off the Rpi before connecting


• Locate the camera port on your Raspberry
pi and connect the camera module
• Start the RPi. Provide the power supply
• Open the Raspberry Pi Configuration
Tool from the main menu on the Desktop
• Make sure that the camera software is
enabled in configuration settings as follows:

4
Configuring the Camera Module

Step 1: Connect the Step 2: Open the Raspberry Pi Step 3: Enable the
camera Configuration window Camera interface

Footer Text 5
RPi Camera Usage
• There are numerous third-party libraries built for accessing the Rpi Camera, including the
Picamera Python library which we will use.

• From the picamera library, we will use the class, “PiCamera” that provides a pure Python
interface to the Raspberry Pi’s camera module.

• Upon construction of an instance using this class, this PiCamera class initializes the
camera. Since there is only a single camera supported by the Raspberry Pi, this means that
only a single instance of this PiCamera class can exist at any given time in the program .

• No camera preview or recording is started automatically upon construction of an instance:


• Use start_preview() method to start live display of the camera’s input.
• Use capture() method to capture image,
• Use start_recording() method to begin recording a video.

• Several attributes are provided by the picamera library to adjust the camera’s configuration.
Some of these can be adjusted during a preview or recording is running, such as brightness,
resolution, etc. More functions and properties are shown in the following slides.

6
Python Picamera library has following functions and properties
Function with their default values Description

start_preview() To start a live camera preview use the start_preview() method. It


starts a preview session over the current display that can viewed
on the Rpi monitor screen. The preview typically overrides
whatever is currently visible on the display. To stop the preview
and reveal the display again, call stop_preview() method. The
preview can be started and stopped multiple times during the
lifetime of the PiCamera object.

capture(photo_path) Capture an image from the camera, storing it as a JPEG in output.


Takes photo_path as a paramter in string to specify where the
captured image should be saved

stop_preview() To stop camera preview after the image is captured. Stops the
preview session and shuts down the preview window of the
camera, restoring the original RPi display on the monitor screen.
Function with their default values Description
Start recording video from the camera, storing it as an H264 stream. Output is
start_recording(output) a string with the path and name of the video file
e. g:
start_recording(‘/home/pi
/Desktop/myVideo.h264’)
stop_recording() Stop recording video from the camera. After calling this method the video
encoder will be shut down and output will stop being written to the path
specified with start_recording() 7
PiCamera Properties Description
with their default
values
sharpness = 0 Retrieves or sets the sharpness setting of the camera. When queried
for this, the sharpness property returns the sharpness level of the
camera (a measure of the amount of post-processing to reduce or
increase image sharpness) as an integer between -100 and 100.When
set, the property adjusts the sharpness (focus) of the camera.
Sharpness can be adjusted while previews or recordings are in
progress.
contrast =0 Retrieves or sets the contrast setting of the camera. Contrast level of
the camera is an integer between -100 and 100.
brightness = 50 Retrieves or sets the brightness setting of the camera=
0= black , 100 = white, without having the brightness in the code, it
will be default =50
saturation = 0 Retrieves or sets the color saturation setting of the camera.
Range = -100 to 100 i.e. the quality of the image: grey scale.
exposure_mode = The possible exposure modes for the camera can be set in a range
'auto' from { 'off‘, 'auto‘,'night‘, 'nightpreview', 'backlight‘, 'spotlight',
'sports', 'snow‘, 'beach', 'fixedfps‘, 'antishake', 'fireworks‘}
image_effect = 'none' sets the current image effect applied by the camera. Can vary from
{‘none' 'negative' 'solarize' 'sketch' 'denoise' 'emboss' 'oilpaint'
'hatch' 'gpen' 'pastel' 'watercolor' 'film' 'blur' 'saturation' 'colorswap'
'washedout' 'posterise' 'colorpoint' 'colorbalance' 'cartoon'
'deinterlace1' 'deinterlace2‘} 8
PiCamera Properties with Description
their default values

rotation = 0 This property retrieves or sets the current rotation of the camera’s image. On querying ,
this rotation property returns the rotation applied to the image. Valid values range from
0, 90, 180, and 270.
On setting , the property changes the rotation applied to the camera’s input. The property
can be set while recordings or previews are in progress.

hflip = False You can apply a horizontal and vertical flip if your camera is positioned upside-down.
This is done by changing the hflip and vflip properties directly.
The hflip property retrieves or sets whether the camera’s output is horizontally flipped.
print hflip, will return When queried, the hflip property returns a Boolean value indicating whether or not the
camera’s output is horizontally flipped.
True or False
vflip = False Retrieves or sets whether the camera’s output is horizontally flipped. When queried, the
vflip property returns a Boolean value indicating whether or not the camera’s output is
vertically flipped.

zoom= (0.0, 0.0, 1.0, 1.0) Retrieves or sets the zoom applied to the camera’s input.
When queried, this property returns a (x, y, w, h) tuple of floating point values ranging
from 0.0 to 1.0, indicating the proportion of the image to include in the output ( also
known as the “Region of Interest” or ROI). The default value is (0.0, 0.0, 1.0, 1.0) which
indicates that everything should be included.

resolution(1280,720) The resolution of the capture is configurable. By default it's set to the resolution of
your monitor, but the maximum resolution is 2592 x 1944 for still photos and 1920 x
1080 for video recording.
The minimum resolution allowed is 64 x 64
The recommended resolution setting is 640X480 so that the camera preview does not
cover the full monitor screen but only a part of it.

annotate_text = “COE410 Add text to your image with annotate_text camera property.
Image"
9
Image Capture

• Step 1: First, at the Python prompt or at the top of a Python script, enter:
o from picamera import PiCamera #From the picamera library import
PiCamera class
• Step 2: Creating a camera object of PiCamera class:
o Mycamera = PiCamera() # Create an instance of PiCamera class
called Mycamera
• Step 3: Capture image using the capture function by using object Mycamera.
Mycamera is an instance (object) of PiCamera class created above:
o Mycamera.capture(‘COE410.jpg') # And take a picture

10
Creating a live preview

from picamera import PiCamera # This will make the PiCamera library available to
the script

import time # This will import time library to use delay functions

Mycamera = PiCamera() # Create an instance of PiCamera class called


Mycamera

Mycamera.start_preview() # start_preview() function is defined in PiCamera class that


we imported above. Starts a preview session over the
current display that can be viewed on the RPi monitor `
screen. The preview typically overrides whatever is
currently visible on the display screen

time.sleep(5) # Add a delay of few seconds after camera preview


has started . The delay is important as the camera
sensor has to adjust to the ambient light levels. Delay
time of 2 seconds is minimum

Mycamera.stop_preview() # Stops the preview session and shuts down the preview
window of the camera , restoring the original RPi
display on the monitor screen.
11
Creating a live preview Contd.
If the camera is upside down and the preview is flipped, it can be
rotated by rotate property of the camera as follows:

from picamera import PiCamera # This will make the PiCamera library available to
the script
import time # This will import time library to use delay functions
Mycamera = PiCamera() # Create an instance of PiCamera class called
Mycamera
Mycamera.rotation = 180 # Setting the “rotation” property to flip the
image. Options are to rotate the image by 90, 180, or 270
degrees, or set it to 0 to reset.
Mycamera.start_preview() # Starts a preview session over the current display that
can viewed on the RPi monitor screen. The preview
typically overrides whatever is currently visible on the
display screen
time.sleep(5) # Add a delay of few seconds after camera preview
has started . The delay is important as the camera
sensor has to adjust to the ambient light levels. Delay
time of 2 seconds is minimum
Mycamera.stop_preview() # Stops the preview session and shuts down the preview
window of the camera , restoring the original RPi
display on the monitor screen.
12
Creating a live preview and image capture
from picamera import PiCamera # Import the python camera library called
“picamera” into your program.
import time # Can add pauses between commands using the sleep
class from time library
Mycamera = PiCamera() # Create an instance of PiCamera class called Mycamera

Mycamera.start_preview() # This will display a preview showing the


live camera feed on the screen
time.sleep(5) # Adds a pause before capturing the image; gives
time to the sensor to adjust. In seconds
Mycamera.capture(‘/home/pi/Desktop/myImage.jpg’)
# Captures the image and stores it in /home/pi/Desktop path
with the name myImage.jpg. IN other words, the capture
function can be used as
Mycamera.capture(“photo_path”), photo_path is a variable
in string defined by user.
# Raspberry pi Camera supports different image formats such
as JPEG, PNG,JPG, GIF, BMP, RGB photos

Mycamera.stop_preview() # This will remove the camera preview and restore the original
Rpi desktop screen
Mycamera.close() # Closes the camera instance and cleans up the resources
stops all recording and preview activities and releases
13
all resources associated with the camera;
Video Recording
from picamera import PiCamera # Import the python camera library called
“picamera” into your program.
import time # Can add pauses between commands
using the sleep class from time library
Mycamera = PiCamera() # Create an instance of PiCamera class
called mycamera
Mycamera.start_preview() # Start the camera preview to display mycamera
input on the monitor screen of RPi
Mycamera.start_recording('/home/pi/video.h264')
# Raspberry Pi by default supportsH.264 format for video.
For high –profile formats like vlc. Need to install the vlc
media player on Raspberry Pi first
# Call the start_recording() method specifying the
video_path with the name of the video file you want to
save with. Use the h264 extension for the
video type by default.
time.sleep(10) # Record a video of 10 seconds
Mycamera.stop_recording() # Terminates the camera recording
Mycamera.stop_preview() # Stops the camera preview display and
restores the original Rpi display screen
Mycamera.close() # Release the camera resources.

14
• By default, Raspberry Pi has the omxplayer installed which is the video
player specifically made for the RPi GPU
• To play the video, you'll need to open a terminal window.
• Type the following command and press Enter to play the video

o omxplayer myvideo.h264

15
Program to adjust brightness in a loop and
annotate display with current brightness level
This program shows that some camera properties can be adjusted “live” while a
preview is running. In this case, the brightness is increased steadily during display :

from picamera import PiCamera # Import the python camera library called
“picamera” into your program.
import time # Can add pauses between commands using the sleep
class from time library
Mycamera = PiCamera() # Create an instance of PiCamera class called Mycamera

Mycamera.start_preview() # This will display a preview showing the live camera feed
on the screen
for i in range(100):
Mycamera.annotate_text = “Current Brightness: %s" % i
# Add text to Camera preview display that shows the current
brightness level value ranging from 0 to 99
Mycamera.brightness = i # set the camera.brightness property from 0 to 99 in the loop
time.sleep(1)
Mycamera.stop_preview() # stop displaying the camera preview on monitor
Mycamera.close() # Release the camera resources

#Similar effects can be seen for contrast, image_effects, and exposure_mode properties of
camera.
16
Program to capture multiple images at a given time using a loop:

from picamera import PiCamera


import time

Mycamera = PiCamera() # Create an instance of PiCamera class called Mycamera

Mycamera.start_preview() # This will display a preview showing the


live camera feed on the screen
for image_number in range (5): # Create a loop to take 5 images
time.sleep(5) # Adds a pause before capturing the image; gives
time to the sensor to adjust. In seconds
Mycamera.capture(‘/home/pi/Desktop/myImage %s.jpg’ %image_number)
# Captures the image and stores it in /home/pi/Desktop path with the
name myImage followed by the image_number from 0 to 4.

Mycamera.stop_preview() # This will remove the camera preview and restore the original
RPi desktop screen
Mycamera.close()

17
Using RPi Camera for Home Security
Write a program that can use RPi camera module for home security purposes. The program should work as
following:

1. At the press of the Door Bell (DIP switch) the Rpi camera module should start with a preview and
capture an image of the guest.
2. Store the captured image along with a Timestamp
3. This image should be sent to the Home Owner Client who can access this image from his mobile
phone.
4. On identifying the authenticated person, the Home Owner should send a command to open the door
by turning ON the Red LED remotely.
5. Hints:
• Use Flask to send captured image to the client using the send_file() method.
The syntax for this function is:
• send_file(photo_path, mimetype=‘image/jpeg’) /=‘video/h.264’
# where photo_path is the path in String where the image is stored after capturing.
# mimetype is used to tell the client browser that the media sent to it could be image or
video etc.
• To get the Timestamp, use the datetime.now.ISOformat() method.

• Use Dynamic Routing for controlling the LED status

18
Connection Diagram

Door

19
Footer Text 3/13/2017 20
Footer Text 3/13/2017 21
Footer Text 3/13/2017 22
Footer Text 3/13/2017 23
24
Output
On Raspberry Pi’s browser you
can view image by accessing
“0.0.0.0:5020/showImage” route
Timestamp will be attached to
the image captured

Footer Text 3/13/2017 25


Route to “0.0.0.0:5020/showImage/1” to turn
LED ON from server’s web browser

Raspberry Pi IP address: 192.168.1.114


(found using ‘ifconfig’ command on Raspi
terminal)
To access from mobile, route to
“192.168.1.114:5020/showImage” path on
your phone’s browser and view the image
with its timestamp.

Footer Text 3/13/2017 26


References

• http://picamera.readthedocs.io/en/release-1.10/api_camera.html
• http://picamera.readthedocs.io/en/release-
1.10/api_camera.html#picamera.camera.PiCamera.resolution
• https://picamera.readthedocs.io/en/release-0.1/api.html
• http://picamera.readthedocs.io/en/release-0.6/quickstart.html
• https://www.raspberrypi.org/documentation/usage/camera/python/README.md
• https://www.raspberrypi.org/learning/getting-started-with-picamera/worksheet/
• http://picamera.readthedocs.io/en/release-1.9/recipes1.html#capturing-to-a-network-stream
• https://www.raspberrypi.org/products/camera-module/

27

You might also like