Bird
0
0
Raspberry Piprogramming~10 mins

Motion detection with camera in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Motion detection with camera
Start Camera
Capture Frame
Compare with Previous Frame
Detect Differences?
NoRepeat Capture
Yes
Motion Detected: Trigger Action
Update Previous Frame
Repeat Capture
The camera captures frames continuously, compares each new frame with the previous one to find differences. If differences exceed a threshold, motion is detected and an action is triggered.
Execution Sample
Raspberry Pi
import cv2
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
diff = cv2.absdiff(frame1, frame2)
This code captures two frames from the camera and calculates the difference between them to detect motion.
Execution Table
StepActionFrame1Frame2Difference ComputedMotion DetectedNext Step
1Start camera and capture first frameCapturedN/AN/ANoCapture next frame
2Capture second frameStored from step 1CapturedComputed pixel differenceNoCheck difference threshold
3Compare difference to thresholdSame as step 1Same as step 2Difference value calculatedYes (if difference > threshold)Trigger motion action or continue
4If motion detected, trigger actionSameSameSameYesUpdate previous frame to current
5Update previous frameUpdated to current frame2N/AN/ANoRepeat capture for next frames
6Repeat capture and detectionUpdated frameNew captureNew differenceDependsLoop continues
💡 Loop runs continuously until program is stopped manually
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
frame1NoneCaptured frameCaptured frameCaptured frameUpdated to frame2Updated continuously
frame2NoneNoneCaptured frameCaptured frameNoneCaptured continuously
diffNoneNonePixel difference arrayPixel difference arrayNoneComputed each loop
motion_detectedFalseFalseDepends on diffTrue or FalseFalseUpdated each loop
Key Moments - 3 Insights
Why do we compare two frames instead of just one?
We compare two frames (see execution_table step 2 and 3) to find changes between them, which indicate motion. A single frame alone cannot show movement.
What happens if the difference is very small?
If the difference is below the threshold (step 3), motion_detected stays False and the program continues capturing frames without triggering actions.
Why do we update frame1 after detecting motion?
Updating frame1 to the current frame (step 5) ensures the next comparison is always between the latest frames, keeping motion detection accurate and continuous.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what determines if motion is detected?
AThe color of frame1
BThe size of the camera frame
CThe difference value compared to a threshold
DThe time between frames
💡 Hint
Check the 'Motion Detected' and 'Difference Computed' columns in step 3 of execution_table
According to variable_tracker, what happens to frame1 after step 5?
AIt is updated to the current frame2
BIt becomes None
CIt remains the same as the first captured frame
DIt stores the difference image
💡 Hint
Look at the 'frame1' row under 'After Step 5' in variable_tracker
If the difference between frames is always zero, what will happen in the loop?
AThe program will stop
BMotion will never be detected and loop continues
CMotion will always be detected
DThe camera will reset
💡 Hint
Refer to execution_table step 3 and the 'motion_detected' variable in variable_tracker
Concept Snapshot
Motion detection with camera:
- Capture two frames continuously
- Compute pixel difference between frames
- Compare difference to threshold
- If difference > threshold, motion detected
- Trigger action and update previous frame
- Repeat loop for continuous detection
Full Transcript
This visual execution shows how motion detection works with a camera on Raspberry Pi. The program starts by capturing a frame from the camera. Then it captures a second frame and calculates the difference between the two frames. If the difference is larger than a set threshold, it means motion is detected. When motion is detected, an action can be triggered, such as saving an image or sending an alert. After that, the previous frame is updated to the current frame to continue detecting motion in the next frames. This loop runs continuously until the program is stopped. Variables like frame1, frame2, and diff change as frames are captured and compared. Motion detection depends on comparing two frames, not just one, and updating frames keeps detection accurate.