Lesson 24: Face Recognition and Biometric Systems

Teaching Robots to Recognize People and Faces

🎯 Learning Objectives

👤 Introduction to Biometrics

What are Biometrics?

Biometrics are unique biological characteristics that can be used to identify individuals. Unlike passwords or ID cards that can be lost or stolen, biometric features are part of who you are and are very difficult to fake or duplicate.

Common Types of Biometrics:

  • Facial recognition: Analyzing facial features and structure
  • Fingerprints: Unique patterns on fingertips
  • Iris scanning: Patterns in the colored part of the eye
  • Voice recognition: Unique characteristics of speech patterns
  • Hand geometry: Size and shape of hands and fingers

Face Detection vs. Face Recognition

It's important to understand the difference between these two related technologies:

  • Face Detection: Finding and locating faces in an image (answers "Is there a face here?")
  • Face Recognition: Identifying who the person is (answers "Whose face is this?")
  • Face Verification: Confirming if a face matches a specific person (answers "Is this John?")

🔍 How Face Recognition Works

The Face Recognition Process

Face recognition systems work through several steps:

  1. Image Capture: Camera takes a photo or video frame
  2. Face Detection: System finds faces in the image
  3. Face Alignment: Adjusts face position and angle for consistency
  4. Feature Extraction: Measures key facial features (eyes, nose, mouth distances)
  5. Face Encoding: Converts measurements into a unique "face print"
  6. Comparison: Compares face print to known faces in database
  7. Decision: Determines if there's a match and who it is

Key Facial Features Measured:

  • Distance between eyes
  • Width of nose
  • Depth of eye sockets
  • Shape of cheekbones
  • Length of jaw line
  • Overall face shape and proportions

Challenges in Face Recognition

  • Lighting conditions: Shadows, bright light, or darkness affect accuracy
  • Facial expressions: Smiling, frowning, or talking changes facial features
  • Angles and poses: Profile views or tilted heads are harder to recognize
  • Aging and changes: People's faces change over time
  • Accessories: Glasses, hats, or masks can hide facial features
  • Image quality: Blurry or low-resolution images reduce accuracy

💻 Programming ESP32-S3 Face Recognition

Setting Up the Camera

The ESP32-S3 has built-in camera support that makes face recognition possible. Here's how to initialize the camera:

#include "esp_camera.h"
#include "esp_face_detect.h"
#include "esp_face_recognition.h"

void setupCamera() {
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_QVGA; // 320x240
  config.jpeg_quality = 10;
  config.fb_count = 1;

  // Initialize camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
  
  Serial.println("Camera initialized successfully!");
}

Basic Face Detection

Let's start with detecting faces in the camera image:

void detectFaces() {
  // Capture image from camera
  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Camera capture failed");
    return;
  }

  // Convert image format for face detection
  dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  if (!image_matrix) {
    esp_camera_fb_return(fb);
    Serial.println("Memory allocation failed");
    return;
  }

  // Convert JPEG to RGB
  bool converted = fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  esp_camera_fb_return(fb);
  
  if (!converted) {
    dl_matrix3du_free(image_matrix);
    Serial.println("Image conversion failed");
    return;
  }

  // Detect faces
  box_array_t *net_boxes = face_detect(image_matrix);
  
  if (net_boxes) {
    Serial.printf("Found %d faces\n", net_boxes->len);
    
    // Print information about each detected face
    for (int i = 0; i < net_boxes->len; i++) {
      box_t *box = &(net_boxes->box[i]);
      Serial.printf("Face %d: x=%d, y=%d, w=%d, h=%d\n", 
                   i+1, box->box_p[0], box->box_p[1], 
                   box->box_p[2] - box->box_p[0], 
                   box->box_p[3] - box->box_p[1]);
    }
    
    free(net_boxes->box);
    free(net_boxes);
  } else {
    Serial.println("No faces detected");
  }
  
  dl_matrix3du_free(image_matrix);
}

Face Recognition System

Now let's add face recognition to identify specific people:

// Face recognition variables
face_id_list id_list = {0};
String knownNames[10]; // Store names for recognized faces
int enrolledFaces = 0;

void enrollNewFace(String name) {
  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) return;

  dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  esp_camera_fb_return(fb);

  // Detect face for enrollment
  box_array_t *net_boxes = face_detect(image_matrix);
  
  if (net_boxes && net_boxes->len > 0) {
    // Use the first detected face for enrollment
    int8_t left_sample_face = enroll_face(&id_list, image_matrix, &(net_boxes->box[0]));
    
    if (left_sample_face >= 0) {
      knownNames[enrolledFaces] = name;
      enrolledFaces++;
      Serial.println("Face enrolled successfully: " + name);
    } else {
      Serial.println("Face enrollment failed");
    }
    
    free(net_boxes->box);
    free(net_boxes);
  }
  
  dl_matrix3du_free(image_matrix);
}

void recognizeFaces() {
  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) return;

  dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  esp_camera_fb_return(fb);

  // Detect faces
  box_array_t *net_boxes = face_detect(image_matrix);
  
  if (net_boxes) {
    for (int i = 0; i < net_boxes->len; i++) {
      // Try to recognize each detected face
      int8_t face_id = recognize_face(&id_list, image_matrix, &(net_boxes->box[i]));
      
      if (face_id >= 0) {
        Serial.println("Recognized: " + knownNames[face_id]);
        greetPerson(knownNames[face_id]);
      } else {
        Serial.println("Unknown person detected");
        askForIntroduction();
      }
    }
    
    free(net_boxes->box);
    free(net_boxes);
  }
  
  dl_matrix3du_free(image_matrix);
}

void greetPerson(String name) {
  Serial.println("Hello, " + name + "!");
  flashLED(3); // Flash LED 3 times
}

void askForIntroduction() {
  Serial.println("I don't recognize you. What's your name?");
  flashLED(1); // Single flash for unknown person
}

🛠️ Hands-On Activity: Personal Security Robot

Project Overview

Create a security robot that can recognize family members or classmates and respond differently to known vs. unknown people.

Implementation Steps:

  1. Setup camera: Initialize ESP32-S3 camera module
  2. Test face detection: Verify camera can detect faces
  3. Enroll faces: Add 2-3 known people to the system
  4. Program responses: Different actions for known/unknown people
  5. Test system: Verify recognition accuracy and responses
  6. Add features: LED patterns, sounds, or movement responses

⚠️ Privacy and Safety Considerations

  • Consent: Always ask permission before enrolling someone's face
  • Data storage: Understand that face data is stored on the device
  • Lighting: Ensure good lighting for accurate recognition
  • Testing: Test with different angles and expressions
  • Backup plan: Include manual override for system access

📝 Assessment & Homework

Reflection Questions

  1. Explain the difference between face detection and face recognition.
  2. What are three challenges that can make face recognition less accurate?
  3. Describe the seven steps in the face recognition process.
  4. Why is consent important when using face recognition technology?
  5. What are two advantages and two disadvantages of biometric security systems?

Programming Challenge

Smart Doorbell System: Design a program for a robot doorbell that can recognize family members and visitors.

Requirements:

  • Automatically detect when someone approaches
  • Recognize family members and greet them by name
  • Ask unknown visitors to identify themselves
  • Send different notifications for family vs. visitors
  • Include privacy protections and consent mechanisms

Research Project

Research a real-world application of face recognition technology and write a one-page report covering:

  • How the technology is being used (security, convenience, etc.)
  • What benefits it provides to users
  • What privacy or ethical concerns have been raised
  • How the organization addresses these concerns
  • Your opinion on whether this use is appropriate and why
← Lesson 23: Robot Swarms 📚 Semester Overview Take Quiz 📝