Skip to main content

Computer vision: color detection and face tracking

Time: 11:35 AM to 12:15 PM
Your robot’s camera can detect specific colors and track faces. In this section you will build two programs from scratch: a color detector with a live web stream, and a face tracker that moves the camera servos to follow you. Both use classical computer vision — fast, offline, but only able to detect what you explicitly program.

Program 1: color detection

Build a script that detects colored objects in the camera feed and streams the annotated video to your browser.

Step 1 — Imports

You need four libraries: picamera2 for the camera, cv2 (OpenCV) for image processing, numpy for arrays, and Python’s built-in http.server and threading for the web stream:

Step 2 — Define color ranges

Colors are defined in HSV (hue/saturation/value) color space. HSV separates the color itself (hue) from brightness, which makes detection more reliable under different lighting:
Red has two ranges because red wraps around in HSV — hue 0 and hue 180 are both red.

Step 3 — Detection function

Convert the frame to HSV, create a mask of matching pixels, find the largest group, and draw a bounding box:
cv2.inRange checks every pixel: is it within the HSV range? cv2.findContours groups the matching pixels into shapes. cv2.contourArea measures the size — we ignore anything smaller than 500 pixels to filter out noise.

Step 4 — MJPEG web stream

Serve the annotated frames as a live video stream in any browser:
This uses the MJPEG (Motion JPEG) format — each frame is sent as a separate JPEG image, and the browser displays them in sequence like a video. The threading.Lock prevents the camera loop and the web server from accessing the frame at the same time.

Step 5 — Main loop

Start the camera, start the web server, and run the detection loop:

Run it

Open your browser to:
Hold a red object in front of the camera. You should see a bounding box appear. Try other colors:
Detection works best in consistent lighting. If the robot does not detect your object, try a larger or more saturated colored item, or adjust the room lighting.

Final code: color_detect.py


Program 2: face tracker

Build a script that detects faces and moves the pan/tilt servos to keep the face centered in the frame.

Step 1 — Face detection with Haar cascades

OpenCV includes a pre-trained Haar cascade classifier for face detection. Load it at the top of the script:
A Haar cascade is a series of simple image features (edges, lines, rectangles) trained to distinguish “face” from “not face”. It is not a neural network — it is a decades-old technique that runs fast on low-power hardware.

Step 2 — Servo helper functions

The PiCar-X API for camera servos varies between library versions. These helpers handle both:

Step 3 — Proportional tracking

The tracking algorithm uses proportional control: measure how far the face is from the center of the frame, multiply by a small gain, and adjust the servo by that amount.

Step 4 — The tracking loop

Detect faces, find the largest one, calculate the error from frame center, and adjust servos:
If the face is 100 pixels to the right of center and the gain is 0.035, the servo moves 3.5°. If the face is within 60 pixels of center (the dead zone), the servo does not move at all. This prevents the jittery back-and-forth motion that happens with no dead zone.

Run it

Open http://picar.local:9000 in your browser. Stand in front of the camera and move around — the camera should physically follow your face.

Final code: face_tracker.py


Classical CV vs. AI

Everything today used explicit rules. The color detector only finds colors you defined in COLOR_RANGES. The face tracker only recognizes the pattern “face” using a pre-built classifier. Neither system can learn, adapt, or recognize new objects.On Day 4, you will replace these rules with an AI that can recognize any object and describe what it sees in natural language.

Day 2 wrap-up

Recap: your robot now moves, speaks, follows lines, detects colors, and tracks faces — all without any AI. Every program today used explicit rules and thresholds that you wrote. What’s coming tomorrow: connect a large language model, give the robot a microphone, and have a real conversation with it. The LLM will control the robot using tool calls — the same mechanism that powers ChatGPT plugins.