👤 Human Detection
Human Detection
Learn how to detect faces and people using computer vision - this is the foundation for your human-tracking fan project!
What is Face Detection?
Face detection is when a computer program looks at an image and finds where faces are. It's like teaching the computer to recognize "this is a face" and tell you where it is in the picture!
Introduction to OpenCV
OpenCV (Open Source Computer Vision Library) is a powerful library that makes computer vision easy. It can detect faces, objects, and much more!
Installing OpenCV:
Basic Face Detection
OpenCV comes with pre-trained face detection models. We can use them to detect faces in images!
Simple Face Detection Code:
Understanding the Code:
- CascadeClassifier: Loads the face detection model
- cvtColor: Converts color image to grayscale
- detectMultiScale: Finds faces in the image
- (x, y, w, h): Coordinates and size of detected face
- rectangle: Draws a box around the detected face
Real-Time Face Detection
For the tracking fan, we need to detect faces in real-time from the camera, not just in photos!
Real-Time Face Detection:
Understanding the Code:
- VideoCapture(0): Opens the camera (0 = first camera)
- cap.read(): Reads one frame from camera
- detectMultiScale(): Finds all faces in the frame
- center_x, center_y: Calculates the center point of the face
- The loop continuously captures frames and detects faces
Getting Face Coordinates
For tracking, we need to know where the face is. The coordinates tell us the position!
X, Y Coordinates
Top-left corner of face
x = horizontal position
y = vertical position
Width, Height
Size of detected face
w = width
h = height
Center Point
Middle of the face
Project: Face Detection with Coordinates Display
Let's create a system that detects faces and displays their coordinates!
Materials Needed:
- Raspberry Pi
- Raspberry Pi Camera Module
- Optional: LCD display to show coordinates
Complete Face Detection Code:
Understanding the Code:
- Continuously captures frames from camera
- Detects faces in each frame
- Calculates center coordinates of each face
- Prints coordinates to console
- These coordinates will be used to control the servo motor in the next lesson!
Improving Detection
You can adjust detection parameters to make it work better:
Scale Factor (1.1)
How much to scale image for detection
Lower = more accurate but slower
Min Neighbors (4)
How many neighbors needed to confirm detection
Higher = fewer false positives
Min Size
Minimum face size to detect
Filters out very small detections
Common Mistakes to Avoid
⚠️ Watch Out For:
- Camera Not Enabled: Enable camera in raspi-config first
- OpenCV Not Installed: Install opencv-python library
- Too High Resolution: Lower resolution = faster detection
- Wrong Color Conversion: Face detection needs grayscale images
- Forgetting to Release: Always call
cap.release()when done - Multiple Faces: Code handles multiple faces - use the first one or largest one
Summary
You've learned:
- ✅ Face detection finds faces in images and video
- ✅ OpenCV is a powerful computer vision library
- ✅
CascadeClassifierloads face detection models - ✅
detectMultiScale()finds faces and returns coordinates - ✅ Face coordinates are (x, y, width, height)
- ✅ Center point = (x + w//2, y + h//2)
- ✅ Lower camera resolution = faster face detection
- ✅ Real-time detection uses VideoCapture in a loop
🎮 Try It: Practice Face Detection!
Practice writing face detection code. Try these challenges:
📝 Challenge 1: Detect and Print
Write code that detects faces and prints how many faces are found:
📝 Challenge 2: Calculate Center
Write code that detects a face and prints its center coordinates:
🎯 Activity: Face Detection with Coordinates Display
What You'll Build:
Create a system that detects faces and displays their coordinates!
Step-by-Step Instructions:
- Install OpenCV: Install opencv-python library
- Enable Camera: Make sure camera is enabled in raspi-config
- Connect Camera: Attach Raspberry Pi Camera module
- Write Code: Use the face detection code from Learn tab
- Test: Run code and stand in front of camera
- Observe: Watch coordinates change as you move
- Enhance: Add LCD display to show coordinates
Testing Checklist:
- ✅ Face is detected when you stand in front of camera
- ✅ Coordinates are printed to console
- ✅ Coordinates change when you move
- ✅ System works in real-time (updates continuously)
💪 Practice Challenges
Challenge 1: Face Counter
Modify the code to count how many faces have been detected total (not just in current frame):
Challenge 2: Largest Face
When multiple faces are detected, track only the largest one:
Challenge 3: Face Position Indicator
Print "Left", "Center", or "Right" based on face X coordinate:
- If center_x < frame_width/3: "Left"
- If center_x > 2*frame_width/3: "Right"
- Otherwise: "Center"
Challenge 4: Save Face Photos
When a face is detected, save a photo of just the face (crop the face region):