In: Electrical Engineering
I am working on an IOT BASED HOME AUTOMATION
PROJECT...I want to make use of a camera in this system to be able
to capture movements or happenings in the house...
Can this be accomplished by using NODEMCU,ARDUINO,RASPBERRY
PIE..
if yes...I need detailed ways on how to do it using each of
them.
Its better to use Raspberry pie because it has inbuilt Camera module connector. NODEMCU and ARDUINO does not have inbuilt Camera module connector.
Connect Pi Camera to CSI interface of Raspberry Pi board
Now, we can use Pi Camera for capturing images and videos using Raspberry Pi.
Before using Pi Camera, we need to enable camera for its working.
How to Enable Camera functionality on Raspberry Pi
For enabling camera in Raspberry Pi, open raspberry pi configuration using following command,
sudo raspi-config
then select Interfacing options in which select camera option to enable its functionality.
reboot Raspberry Pi.
Now we can access camera on Raspberry Pi.
Now we can capture images and videos using Pi Camera on Raspberry Pi.
Example
Capture images and save it to the specified directory.
We can capture images using Python. Here, we will write a Python program to capture images using Pi Camera on Raspberry Pi.
Here, we have used picamera package(library) which provides different classes for Raspberry Pi. Out of which we are mainly interested in PiCamera class which is for camera module.
Python Program for Image Capture
import picamera from time import sleep #create object for PiCamera class camera = picamera.PiCamera() #set resolution camera.resolution = (1024, 768) camera.brightness = 60 camera.start_preview() #add text on image camera.annotate_text = 'Hi Pi User' sleep(5) #store image camera.capture('image1.jpeg') camera.stop_preview()
Functions Used
To use picamera python based library we have to include it in our program as given below
import picamera
This picamera library has PiCamera class for camera module. So, we have to create object for PiCamera class.
PiCamera Class
To use Pi Camera in Python on Raspberry Pi, we can use PiCamera class which has different APIs for camera functionality. We need to create object for PiCamera class.
E.g. Camera = picamera.PiCamera()
The above PiCamera class has different member variables and functions which we can access by simply inserting a dot (.) in between object name and member name.
E.g. Camera.resolution = (1080, 648)
capture()
It is used to capture images using Pi Camera.
E.g. Camera.capture(“/home/pi/image.jpeg”)
The capture() function has different parameters which we can pass for different operations like resize, format, use_video_port, etc.
E.g. Camera.capture(“/home/pi/image.jpeg”, resize=(720, 480))
resolution= (width,height)
It sets the resolution of camera at which image captures, video records and preview will display. The resolution can be specified as (width, height) tuple, as a string formatted WIDTHxHEIGHT, or as a string containing commonly recognised display resolution name e.g. “HD”, “VGA”, “1080p”, etc.
E.g.
Camera.resolution = (720, 480)
Camera.resolution = “720 x 480”
Camera.resolution = “720p”
Camera.resolution = “HD”
Annotate_text = “Text”
It is used to add text on image, video, etc.
E.g. Camera.annotate_text = “Hi Pi User”
start_preview()
It displays the preview overlay of default or specified resolution.
Example Camera.start_preview()
stop_preview()
It is used to close the preview overlay.
E.g. Camera.stop_preview()
Python Program for Video Recording
import picamera from time import sleep camera = picamera.PiCamera() camera.resolution = (640, 480) print() #start recording using pi camera camera.start_recording("/home/pi/demo.h264") #wait for video to record camera.wait_recording(20) #stop recording camera.stop_recording() camera.close() print("video recording stopped")
Functions used
We have to create object for PiCamera class. Here, we have create object as camera.
start_recording()
It is used to start video recordingand store it.
E.g. Camera.start_recording(‘demo.h264’)
It records video named demo of h264 format.
wait_recording(timeout)
Wait on video encoder for specified timeout seconds.
E.g. Camera.wait_recording(60)
stop_recording()
It is used to stop video recording.
E.g. Camera.stop_recording()
Play Recorded Video
To open video, we can use omxplayer by using following command,
omxplayer video_name