liveBook · Manning

Face-detection algorithm

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

Face detection is the computer-vision task of locating faces in images or video.

A face-detection algorithm locates faces in camera images and returns their positions. In a face-following camera, the useful result is the center of a detected face: that coordinate tells the robot whether to move its camera left or right. The detector can also draw a rectangle and center marker so its result is visible before motors are involved. section 9.0 section 9.3 section 9.3.2

OpenCV uses Haar feature-based cascade classifiers for this task. A pretrained classifier is supplied as an XML model in OpenCV’s data directory, so detection starts by loading it with CascadeClassifier rather than training a model. Before calling detectMultiScale, convert the camera frame to grayscale and apply histogram equalization to improve contrast. section 9.3.1 section 9.3.2

A reusable detection function can follow this small pipeline:

  • Prepare the frame by converting it to grayscale and equalizing its histogram. section 9.3.2
  • Run detectMultiScale on the prepared image and check whether any matching rectangles were returned. section 9.3.1 section 9.3.2
  • Take a match’s x, y, width, and height values; calculate its center; then return that coordinate. section 9.3 section 9.3.2
  • Draw the matching rectangle and center marker on the original frame for a visual check. section 9.3.2

From still image to live camera

First, load photo.jpg, detect and mark its face, and print the center coordinate. For live video, open VideoCapture(0), read a frame on each loop, call the same detection function, and display the marked frame until Q or Esc exits. Reusing the function keeps the camera loop focused on capture and display. section 9.3.2 section 9.4.4

Live detection must keep pace with the camera. At 30 frames per second, measure repeated detection calls and calculate average FPS. If detection is too slow, resize the image before classification: scaling both dimensions to 20% reduces a 640×480 image from 307,200 to 12,288 pixels, a 25× reduction. This setting preserves successful detection here, while substantially smaller scales can hurt accuracy. section 9.4.1 section 9.4.2 section 9.6

Figure 9.1 Hardware stack: servo motors will be used to move the camera toward a detected face.

The hardware stack shows how a detected face becomes physical behavior: servo motors pan the camera toward the face. section 9.1

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Builds reusable OpenCV detection functions, live-video loops, timing measurements, and image-scaling optimization without training a classifier.
Examples
Uses a Raspberry Pi camera photo, marked detection rectangles, a live preview loop, and a pan-tilt camera mount.
What it assumes
Uses Python and OpenCV while supplying pretrained classifier models rather than requiring classifier training.
What you can do after
Apply it via OpenCV to detect, mark, time, and speed up face detection in camera frames.
How it connects
Connects OpenCV, Haar cascade classifiers, histogram equalization, live video, servo motors, and camera control.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.