Skip to content

gaurovgiri/face_tracking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Face Tracking System

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.

Overview

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

Features

  • 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

Hardware Requirements

  • 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)

Software Requirements

Python Dependencies

  • Python 3.x
  • OpenCV (cv2)
  • NumPy
  • PySerial

Arduino

  • Arduino IDE
  • Servo library (included with Arduino IDE)

Installation

1. Clone the Repository

git clone https://github.com/gaurovgiri/face_tracking.git
cd face_tracking

2. Install Python Dependencies

pip install -r requirements.txt

Or install manually:

pip install opencv-python numpy pyserial

3. Arduino Setup

  1. Open arduino.ino in Arduino IDE
  2. Connect your Arduino board via USB
  3. Select the correct board and port from Tools menu
  4. Upload the sketch to your Arduino

4. Hardware Connections

  1. Connect the servo motor to Arduino:
    • Signal wire to digital pin 9
    • Power (VCC) to 5V
    • Ground (GND) to GND

Usage

Face Tracking with Servo Control

  1. Configure Serial Port: Edit track.py and 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*
  2. Run the tracking script:

    python track.py
  3. Controls:

    • The servo will automatically rotate to follow detected faces
    • Press ESC key to exit

Face Capture Mode

To capture and store face images:

python store_face.py
  • Detected faces will be saved as face{n}.png in the current directory
  • Press ESC key to exit

How It Works

Face Detection (Python)

  1. Initialization: Opens webcam and loads Haar Cascade classifier
  2. Detection Loop:
    • Captures video frames continuously
    • Applies face detection algorithm
    • Draws rectangles around detected faces
  3. Position Analysis: Calculates horizontal position of detected face
  4. Serial Communication: Sends position data to Arduino

Servo Control (Arduino)

  1. Serial Protocol:
    • Start byte: 255
    • Motor ID: 1
    • Position command: 1 (left) or 2 (right)
  2. Movement Logic:
    • Receives commands from Python via serial
    • Adjusts servo angle incrementally (±3 degrees)
    • Updates servo position

Communication Protocol

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)

Project Structure

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

Configuration

Adjusting Face Detection Sensitivity

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 detect
  • maxSize: Maximum face size to detect

Adjusting Tracking Thresholds

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.

Servo Configuration

In arduino.ino, adjust servo limits if needed:

int min_pos = 600;   // Minimum servo pulse width
int max_pos = 2400;  // Maximum servo pulse width

Troubleshooting

Camera Not Opening

  • 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

Serial Port Issues

  • Port not found: Check device manager (Windows) or ls /dev/tty* (Linux/Mac)
  • Permission denied (Linux): Add user to dialout group:
    sudo usermod -a -G dialout $USER
    Log out and log back in for changes to take effect
  • Arduino not responding: Reset Arduino and restart the Python script

Servo Not Moving

  • 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)

Face Not Detected

  • Ensure adequate lighting
  • Face the camera directly
  • Adjust detection parameters in detectMultiScale
  • Verify haarcascade_frontalface_default.xml file exists in the project directory

Future Enhancements

  • 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

License

This project is open source and available for educational and personal use.

Contributing

Contributions are welcome! Feel free to submit issues and pull requests.

Acknowledgments

  • OpenCV for computer vision capabilities
  • Haar Cascade classifier for face detection
  • Arduino community for servo control examples

About

Face tracking system using webcam and servo motor controlled by arduino

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors