A real-time face tracking system that uses computer vision to detect faces and control a servo motor to physically track the detected face. The system combines Python with OpenCV for face detection and Arduino for servo motor control.
This project implements an automated face tracking system that:
- Detects faces in real-time using a webcam
- Tracks the position of detected faces
- Controls a servo motor via Arduino to follow the face movement
- Captures and stores face images for later use
- Real-time Face Detection: Uses Haar Cascade Classifier for accurate face detection
- Servo Motor Control: Automatically adjusts servo position based on face location
- Face Capture: Ability to capture and store detected faces as images
- Serial Communication: Seamless communication between Python and Arduino
- Visual Feedback: Live video feed with face detection rectangles
- Arduino board (e.g., Arduino Uno)
- Servo motor (compatible with Arduino)
- USB cable for Arduino connection
- Webcam or camera module
- Connecting wires
- Power supply for servo (if needed)
- Python 3.x
- OpenCV (
cv2) - NumPy
- PySerial
- Arduino IDE
- Servo library (included with Arduino IDE)
git clone https://github.com/gaurovgiri/face_tracking.git
cd face_trackingpip install -r requirements.txtOr install manually:
pip install opencv-python numpy pyserial- Open
arduino.inoin Arduino IDE - Connect your Arduino board via USB
- Select the correct board and port from Tools menu
- Upload the sketch to your Arduino
- Connect the servo motor to Arduino:
- Signal wire to digital pin 9
- Power (VCC) to 5V
- Ground (GND) to GND
-
Configure Serial Port: Edit
track.pyand update the COM port to match your Arduino connection:ser = serial.Serial(port='COM5', baudrate=9600, timeout=1)
- On Windows: Use
COM3,COM4,COM5, etc. - On Linux/Mac: Use
/dev/ttyUSB0,/dev/ttyACM0, or/dev/cu.usbmodem*
- On Windows: Use
-
Run the tracking script:
python track.py
-
Controls:
- The servo will automatically rotate to follow detected faces
- Press
ESCkey to exit
To capture and store face images:
python store_face.py- Detected faces will be saved as
face{n}.pngin the current directory - Press
ESCkey to exit
- Initialization: Opens webcam and loads Haar Cascade classifier
- Detection Loop:
- Captures video frames continuously
- Applies face detection algorithm
- Draws rectangles around detected faces
- Position Analysis: Calculates horizontal position of detected face
- Serial Communication: Sends position data to Arduino
- Serial Protocol:
- Start byte:
255 - Motor ID:
1 - Position command:
1(left) or2(right)
- Start byte:
- Movement Logic:
- Receives commands from Python via serial
- Adjusts servo angle incrementally (±3 degrees)
- Updates servo position
The Python script sends 3 bytes to Arduino:
- Byte 1:
255(start byte/header) - Byte 2: Motor ID (currently
1) - Byte 3: Position command (
1= rotate left,2= rotate right)
face_tracking/
├── track.py # Main face tracking script with servo control
├── store_face.py # Face capture and storage utility
├── arduino.ino # Arduino servo control code
├── haarcascade_frontalface_default.xml # Pre-trained face detection model
├── requirements.txt # Python dependencies
└── README.md # Project documentation
In track.py or store_face.py, modify the detectMultiScale parameters:
faces = face.detectMultiScale(img, 1.2, 5, minSize=(10,10), maxSize=(500,500))1.2: Scale factor (lower = more detections but slower)5: Minimum neighbors (higher = fewer false positives)minSize: Minimum face size to detectmaxSize: Maximum face size to detect
In track.py, modify the angular_rotate_servo function:
if angle > 230: # Face on the right side
prov = 2
servo_rotate(1, prov)
elif angle < 195: # Face on the left side
prov = 1
servo_rotate(1, prov)Adjust these threshold values based on your camera resolution and desired tracking range.
In arduino.ino, adjust servo limits if needed:
int min_pos = 600; // Minimum servo pulse width
int max_pos = 2400; // Maximum servo pulse width- Check if your webcam is properly connected
- Try changing camera index in code:
cv2.VideoCapture(0)→cv2.VideoCapture(1) - Ensure no other application is using the camera
- Port not found: Check device manager (Windows) or
ls /dev/tty*(Linux/Mac) - Permission denied (Linux): Add user to dialout group:
Log out and log back in for changes to take effect
sudo usermod -a -G dialout $USER - Arduino not responding: Reset Arduino and restart the Python script
- Verify correct wiring connections
- Check if Arduino code is uploaded successfully
- Ensure servo has adequate power supply
- Verify serial port in Python script matches Arduino port
- Check baud rate matches in both Python (9600) and Arduino (9600)
- Ensure adequate lighting
- Face the camera directly
- Adjust detection parameters in
detectMultiScale - Verify
haarcascade_frontalface_default.xmlfile exists in the project directory
- Add vertical (tilt) servo control for 2-axis tracking
- Implement multiple face tracking
- Add face recognition capabilities
- Improve tracking smoothness with PID control
- Add GUI for configuration
- Support for different servo types
- Mobile app integration
This project is open source and available for educational and personal use.
Contributions are welcome! Feel free to submit issues and pull requests.
- OpenCV for computer vision capabilities
- Haar Cascade classifier for face detection
- Arduino community for servo control examples