Motion detection with a camera helps you know when something moves in front of the camera. It is useful for security or monitoring.
Motion detection with camera in 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.
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)
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) for contour in contours: if cv2.contourArea(contour) > 500: print("Motion detected!")
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.
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()
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.
Motion detection compares camera images to find changes.
OpenCV helps capture and process images easily.
Detecting motion can trigger alerts or recordings.
Practice
Solution
Step 1: Understand motion detection basics
Motion detection works by comparing images to find differences or changes.Step 2: Identify the main goal of motion detection
The goal is to detect movement by noticing changes between frames, not to change resolution or save power.Final Answer:
To find changes between consecutive images -> Option BQuick Check:
Motion detection = find changes [OK]
- Confusing motion detection with camera settings
- Thinking motion detection saves power
- Assuming it connects camera to internet
Solution
Step 1: Recall the correct OpenCV import statement
OpenCV in Python is imported using 'import cv2'.Step 2: Check other options for correctness
Options 'opencv', 'cv', and 'camera' are incorrect or outdated module names.Final Answer:
import cv2 -> Option DQuick Check:
OpenCV import = cv2 [OK]
- Using 'import opencv' instead of 'import cv2'
- Confusing 'cv' with 'cv2'
- Trying to import 'camera' module for OpenCV
import cv2 cap = cv2.VideoCapture(0) ret, frame1 = cap.read() ret, frame2 = cap.read() diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY) print(thresh.sum() > 10000)
Solution
Step 1: Understand the code logic for motion detection
The code compares two frames, converts difference to grayscale, thresholds it, then sums pixels to detect motion.Step 2: Interpret the print statement
If the sum of thresholded pixels is greater than 10000, it prints True (motion detected), else False.Final Answer:
True if motion detected, False otherwise -> Option AQuick Check:
Threshold sum > 10000 means motion True [OK]
- Assuming it always prints True or False
- Confusing thresholding with color conversion
- Expecting syntax errors from correct code
import cv2
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
if thresh.sum() > 10000:
print("Motion detected")
cap.release()Solution
Step 1: Check code flow for continuous motion detection
The code reads frames only once, so it cannot detect motion continuously.Step 2: Identify missing loop for repeated frame capture
Motion detection requires a loop to keep reading frames and comparing them over time.Final Answer:
Missing loop to continuously read frames -> Option AQuick Check:
Continuous detection needs a loop [OK]
- Thinking threshold value is wrong
- Confusing absdiff with subtract
- Releasing capture too early
Solution
Step 1: Understand the requirement for continuous motion detection
We need to detect motion lasting at least 3 seconds, not just a single frame.Step 2: Choose method to track motion duration
Counting consecutive frames with motion and triggering alert after reaching a threshold time is the best approach.Final Answer:
Count consecutive frames with motion and trigger alert after threshold -> Option CQuick Check:
Continuous motion needs counting frames [OK]
- Triggering alert immediately on any motion
- Using timer without checking motion
- Ignoring motion detection logic
Ready to prove it?
Pass the test and this pattern moves unknown
