Advanced Level • Lesson 9

👤 Human Detection

⏱️ 60 minutes 📚 Advanced 🎯 Computer Vision
👤

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!

💡 Real-World Example: Your phone uses face detection to unlock with Face ID. Security cameras use it to detect people. Your human-tracking fan will use it to find where people are!

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:

# In terminal, type: sudo apt-get update sudo apt-get install python3-opencv # Or using pip: pip3 install opencv-python

Basic Face Detection

OpenCV comes with pre-trained face detection models. We can use them to detect faces in images!

Simple Face Detection Code:

import cv2 import numpy as np # Load the face detection model (comes with OpenCV) face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Read an image (or capture from camera) image = cv2.imread('photo.jpg') # Or use camera # Convert to grayscale (face detection works on grayscale) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw rectangles around detected faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) print(f"Face detected at: x={x}, y={y}, width={w}, height={h}") # Save or display the image cv2.imwrite('faces_detected.jpg', image) print(f"Found {len(faces)} face(s)!")

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:

import cv2 # Load face detection model face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Start camera (0 = default camera) cap = cv2.VideoCapture(0) try: while True: # Read frame from camera ret, frame = cap.read() if not ret: break # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw rectangles and get coordinates for (x, y, w, h) in faces: # Draw rectangle around face cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # Calculate center of face center_x = x + w // 2 center_y = y + h // 2 # Print coordinates print(f"Face center: ({center_x}, {center_y})") # Display the frame (optional - comment out if no display) # cv2.imshow('Face Detection', frame) # Break on 'q' key (if display is shown) # if cv2.waitKey(1) & 0xFF == ord('q'): # break except KeyboardInterrupt: cap.release() cv2.destroyAllWindows() print("Face detection stopped!")

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

center_x = x + w // 2 center_y = y + h // 2

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:

import cv2 import time # Load face detection model face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Start camera cap = cv2.VideoCapture(0) # Set camera resolution (lower = faster processing) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) print("Face detection started! Press Ctrl+C to stop.") try: while True: ret, frame = cap.read() if not ret: break # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Process each detected face for (x, y, w, h) in faces: # Calculate center coordinates center_x = x + w // 2 center_y = y + h // 2 # Draw rectangle cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # Draw center point cv2.circle(frame, (center_x, center_y), 5, (0, 0, 255), -1) # Print coordinates print(f"Face detected! Center: ({center_x}, {center_y}), Size: {w}x{h}") if len(faces) == 0: print("No face detected") # Small delay to prevent too much output time.sleep(0.1) except KeyboardInterrupt: cap.release() cv2.destroyAllWindows() print("Face detection stopped!")

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
  • CascadeClassifier loads 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
🎉 Excellent Progress! You can now detect faces! In the next lesson, you'll learn to use these coordinates to control a servo motor that follows the face.

🎮 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:

💡 Tip: Remember to convert images to grayscale before detection, and always release the camera when done!

🎯 Activity: Face Detection with Coordinates Display

What You'll Build:

Create a system that detects faces and displays their coordinates!

Step-by-Step Instructions:

  1. Install OpenCV: Install opencv-python library
  2. Enable Camera: Make sure camera is enabled in raspi-config
  3. Connect Camera: Attach Raspberry Pi Camera module
  4. Write Code: Use the face detection code from Learn tab
  5. Test: Run code and stand in front of camera
  6. Observe: Watch coordinates change as you move
  7. 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)
🏆 Bonus Challenge: Can you modify the code to track the largest face when multiple faces are detected? (Hint: find the face with the largest width × height)

💪 Practice Challenges

Challenge 1: Face Counter

Modify the code to count how many faces have been detected total (not just in current frame):

# Keep a running count of all faces detected # Print total count every 10 detections

Challenge 2: Largest Face

When multiple faces are detected, track only the largest one:

# Calculate area = width × height # Find face with largest area # Use that face's coordinates

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):

# When face detected: # Crop the face region from frame # Save as "face_1.jpg", "face_2.jpg", etc.