A small Python project that demonstrates two simple face-related applications using OpenCV:
facedetection.py
— Detects faces from the webcam and shows the number of faces detected in the video feed.facetracking.py
— Detects a face and maps its position to two Arduino-controlled servos so a camera or mechanism can track the face.
This README explains how to install the required packages, run each script, and troubleshoot common issues.
- Real-time face detection using OpenCV's Haar cascade classifier.
- Visual overlays (bounding boxes, target marker, position text).
- Optional Arduino servo control (via
pyfirmata
) to physically track the detected face.
- Python 3.8+ (3.10/3.11 recommended)
- A webcam (built-in or USB)
- If you want servo tracking: an Arduino board, two servos, and a USB connection to a Windows COM port.
Install the required Python packages. From PowerShell you can run:
python -m pip install opencv-python numpy cvzone pyfirmata
Notes:
cvzone
bundles some useful helpers but is optional for these scripts —opencv-python
andnumpy
are essential.- If you run into binary wheel errors for
opencv-python
, try upgrading pip first:python -m pip install --upgrade pip
.
facedetection.py
— Simple face detection and on-screen face count.facetracking.py
— Face tracking with servo control (requires Arduino + pyfirmata).haarcascade_frontalface_default.xml
— Haar cascade model used by both scripts (must be in the same folder).
Open PowerShell in the repository folder (e.g., D:\Face-Detection
) and run one of the scripts.
Run the face detection demo:
python facedetection.py
Press the q
key in the video window to quit.
Run the face tracking demo (hardware required):
- Connect your Arduino to the PC and confirm the COM port (Windows Device Manager). Update
port = "COM7"
infacetracking.py
to match your COM port. - Connect two servos to the Arduino pins you configured (the script uses digital pins 9 and 10 by default).
- Start the script:
python facetracking.py
Press q
in the video window to quit. The script will continuously write servo angles to the Arduino using pyfirmata.
- Use a common ground between the Arduino and the servos.
- Power servos from a suitable power supply if they draw more current than the Arduino's regulator can safely provide.
- Typical wiring: servo signal wires to Arduino D9 and D10, servo VCC to external 5V (or Arduino 5V if current is low), servo GND to Arduino GND.
- "Camera couldn't Access" or "The camera cannot be accessed." — Ensure no other program is using the webcam and that the correct device index (0) is valid for your system.
- Cascade file not found — Make sure
haarcascade_frontalface_default.xml
is in the same folder as the scripts. - Arduino
pyfirmata
errors — Verify the COM port string and that the Arduino is running the StandardFirmata sketch. Install Firmata on the Arduino using the Arduino IDE: open Examples → Firmata → StandardFirmata and upload it.
- The scripts use OpenCV's Haar cascade classifier which is fast but less accurate than modern deep-learning based detectors; it's good for simple demos and low-latency use.
cvzone
is optional; you can remove it and rely directly onopencv-python
+numpy
.