📷 Camera and Vision Basics
Camera and Vision Basics
Learn how to use the Raspberry Pi Camera to take pictures and detect motion - this is the first step toward human tracking!
Raspberry Pi Camera
The Raspberry Pi Camera is a small camera module that connects directly to your Raspberry Pi. It can take photos, record videos, and with the right software, detect objects and people!
Setting Up the Camera
Hardware Setup:
- Enable Camera: Run
sudo raspi-config→ Interface Options → Camera → Enable - Connect Camera: Insert camera ribbon cable into the camera port (between Ethernet and HDMI)
- Secure Connection: Make sure the ribbon cable is inserted correctly (metal contacts facing away from Ethernet port)
- Reboot: Restart Raspberry Pi after enabling camera
Taking Pictures
The simplest thing you can do with a camera is take pictures!
Basic Photo Capture:
Understanding the Code:
- PiCamera(): Creates a camera object
- start_preview(): Shows what camera sees (like a live view)
- capture(): Takes a photo and saves it
- stop_preview(): Closes the preview
Recording Video
You can also record videos with the camera:
Video Recording:
Motion Detection
Motion detection compares consecutive frames to see if anything has moved. This is simpler than full object detection but very useful!
Simple Motion Detection:
Note: This is a simplified example. Real motion detection compares pixel differences between frames.
Project: Motion Detection Camera
Let's build a system that takes a photo when it detects motion!
Materials Needed:
- Raspberry Pi
- Raspberry Pi Camera Module
- Optional: LED to indicate when motion is detected
Complete Motion Detection Code:
Understanding the Code:
- Camera continuously monitors for motion
- When motion detected, takes a photo with timestamp
- LED flashes to indicate photo was taken
- Photos are saved with unique filenames
Introduction to Computer Vision
Computer vision is when computers "see" and understand images. It's what allows cameras to detect faces, objects, and people!
Image Capture
Camera takes pictures
Image Processing
Computer analyzes the image
Object Detection
Finds faces, people, objects
Common Mistakes to Avoid
⚠️ Watch Out For:
- Camera Not Enabled: Must enable camera in raspi-config first
- Wrong Cable Orientation: Camera ribbon must be inserted correctly
- Missing Library: Install picamera with
pip3 install picamera - Too High Resolution: Higher resolution = slower processing - use lower for motion detection
- Forgetting to Close: Always call
camera.close()when done
Summary
You've learned:
- ✅ Raspberry Pi Camera can take photos and record videos
- ✅ Camera must be enabled in raspi-config before use
- ✅ Use
PiCamera()to create camera object - ✅
capture()takes photos,start_recording()records video - ✅ Motion detection compares frames to detect movement
- ✅ Computer vision lets computers "see" and understand images
- ✅ Lower resolution = faster processing for motion detection
🎮 Try It: Practice with Camera!
Practice writing code for the camera. Try these challenges:
📝 Challenge 1: Take a Photo
Write code that takes a single photo and saves it:
📝 Challenge 2: Timelapse
Write code that takes 10 photos, one every 2 seconds:
camera.close() when you're done.
🎯 Activity: Motion Detection Camera
What You'll Build:
Create a motion detection system that takes photos when movement is detected!
Step-by-Step Instructions:
- Enable Camera: Run
sudo raspi-configand enable camera interface - Connect Camera: Insert camera ribbon cable into camera port
- Install Library: Install picamera library if needed
- Write Code: Create motion detection code
- Test: Run code and wave in front of camera
- Enhance: Add LED indicator when motion detected
Testing Checklist:
- ✅ Camera takes photos successfully
- ✅ Photos are saved with unique filenames
- ✅ System detects motion (or simulates detection)
- ✅ LED flashes when photo is taken
💪 Practice Challenges
Challenge 1: Photo with Timestamp
Take a photo and save it with the current date and time in the filename:
Challenge 2: Continuous Photos
Take 5 photos, one every 3 seconds, and print a message each time:
Challenge 3: Button-Triggered Photo
Combine camera with button - take a photo when button is pressed:
Challenge 4: Low-Resolution Motion Detection
Set camera to low resolution (160x120) for faster motion detection processing: