Motion detection with a camera helps you know when something moves in front of the camera. It is useful for security or monitoring.
0
0
Motion detection with camera in Raspberry Pi
Introduction
To watch your home and get alerts if someone moves inside.
To monitor a pet playing in a room and record only when it moves.
To save storage by recording video only when there is motion.
To count people entering or leaving a room.
To trigger lights or alarms when motion is detected.
Syntax
Raspberry Pi
import cv2 # Start camera camera = cv2.VideoCapture(0) # Read first frame ret, frame1 = camera.read() frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) frame1_gray = cv2.GaussianBlur(frame1_gray, (21, 21), 0) while True: ret, frame2 = camera.read() frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) frame2_gray = cv2.GaussianBlur(frame2_gray, (21, 21), 0) # Calculate difference diff = cv2.absdiff(frame1_gray, frame2_gray) _, thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY) # Find contours contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: if cv2.contourArea(contour) > 500: print("Motion detected!") frame1_gray = frame2_gray if cv2.waitKey(10) & 0xFF == 27: # Press ESC to exit break camera.release() cv2.destroyAllWindows()
This example uses OpenCV library to access the camera and process images.
We compare two frames to find changes that show motion.
Examples
Start the camera and prepare the first image in gray and blurred to reduce noise.
Raspberry Pi
import cv2 camera = cv2.VideoCapture(0) ret, frame1 = camera.read() frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) frame1_gray = cv2.GaussianBlur(frame1_gray, (21, 21), 0)
Find the difference between two frames and create a black and white image showing changes.
Raspberry Pi
diff = cv2.absdiff(frame1_gray, frame2_gray) _, thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)
Look for shapes in the difference image that are big enough to count as motion.
Raspberry Pi
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: if cv2.contourArea(contour) > 500: print("Motion detected!")
Sample Program
This program uses your Raspberry Pi camera to detect motion. It compares each new frame to the previous one. If it finds big enough changes, it prints "Motion detected!". Press ESC to stop.
Raspberry Pi
import cv2 camera = cv2.VideoCapture(0) ret, frame1 = camera.read() frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) frame1_gray = cv2.GaussianBlur(frame1_gray, (21, 21), 0) while True: ret, frame2 = camera.read() frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) frame2_gray = cv2.GaussianBlur(frame2_gray, (21, 21), 0) diff = cv2.absdiff(frame1_gray, frame2_gray) _, thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) motion_found = False for contour in contours: if cv2.contourArea(contour) > 500: motion_found = True break if motion_found: print("Motion detected!") frame1_gray = frame2_gray if cv2.waitKey(10) & 0xFF == 27: # ESC key break camera.release() cv2.destroyAllWindows()
OutputSuccess
Important Notes
Make sure your Raspberry Pi camera is enabled and connected.
You need to install OpenCV with: pip install opencv-python.
Adjust the contour area threshold (500) to make detection more or less sensitive.
Summary
Motion detection compares camera images to find changes.
OpenCV helps capture and process images easily.
Detecting motion can trigger alerts or recordings.
