📖 Scenario: You have a Raspberry Pi with a camera module. You want to detect when something moves in front of the camera. This project will help you write a simple program to detect motion by comparing images.
🎯 Goal: Build a Python program that captures images from the Raspberry Pi camera, compares them to detect motion, and prints a message when motion is detected.
📋 What You'll Learn
Use the picamera library to capture images.
Use the PIL (Pillow) library to process images.
Compare two images to detect changes.
Print Motion detected! when movement is found.
💡 Why This Matters
🌍 Real World
Motion detection is used in home security cameras to alert owners when someone moves in front of the camera.
💼 Career
Understanding how to capture and process images on devices like Raspberry Pi is useful for jobs in IoT, embedded systems, and security technology.
Progress0 / 4 steps
1
Set up initial variables and import libraries
Import the picamera and PIL.Image libraries. Create two variables called image1_path and image2_path with values 'image1.jpg' and 'image2.jpg' respectively.
Raspberry Pi
Hint
Use import picamera and from PIL import Image. Then assign the file names as strings to the variables.
2
Capture two images with the camera
Create a picamera.PiCamera() object called camera. Use camera.capture() to take two pictures saved to image1_path and image2_path.
Raspberry Pi
Hint
Create the camera with picamera.PiCamera(). Use camera.capture() twice with the file paths.
3
Compare the two images to detect motion
Open the images from image1_path and image2_path using Image.open(). Convert them to grayscale with .convert('L'). Calculate the difference by subtracting pixel values and count how many pixels differ by more than 30. Store this count in a variable called motion_pixels.
Raspberry Pi
Hint
Use Image.open() and .convert('L') to get grayscale images. Use getdata() to get pixels. Loop through pixels and count differences over 30.
4
Print motion detection result
If motion_pixels is greater than 1000, print Motion detected!. Otherwise, print No motion detected.
Raspberry Pi
Hint
Use an if statement to check if motion_pixels is greater than 1000. Print the correct message accordingly.
Practice
(1/5)
1. What is the main purpose of motion detection when using a camera on a Raspberry Pi?
easy
A. To increase the camera resolution automatically
B. To find changes between consecutive images
C. To save battery life of the Raspberry Pi
D. To connect the camera to the internet
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 B
Quick Check:
Motion detection = find changes [OK]
Hint: Motion detection means spotting changes between pictures [OK]
Common Mistakes:
Confusing motion detection with camera settings
Thinking motion detection saves power
Assuming it connects camera to internet
2. Which of the following Python code snippets correctly imports OpenCV for motion detection on Raspberry Pi?
easy
A. import camera
B. import opencv
C. import cv
D. import cv2
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 D
Quick Check:
OpenCV import = cv2 [OK]
Hint: OpenCV Python module is always imported as cv2 [OK]
Common Mistakes:
Using 'import opencv' instead of 'import cv2'
Confusing 'cv' with 'cv2'
Trying to import 'camera' module for OpenCV
3. What will be the output of this code snippet when motion is detected?