Field: Intelligent Systems & Robotics
Date: 2025
Repository: Face_Direction_Tracker_Project
This project demonstrates how to use OpenCV to detect a human face in real time, track its movement direction (Left, Right), and calculate the movement speed in pixels per second.
The system visually overlays:
- A green bounding box around the detected face
- Direction text (Left, Right, Up, Down)
- Speed (px/s) on the live video feed
โ
Real-time face detection via OpenCV Haar Cascade
โ
Dynamic direction tracking between consecutive frames
โ
Speed estimation based on pixel displacement per second
โ
Overlay visualization (text + bounding box)
โ
Well-documented, readable, and modular code structure
| Step | Operation | Description |
|---|---|---|
| 1๏ธโฃ | Frame Capture | Webcam feed captures live video frames. |
| 2๏ธโฃ | Preprocessing | Each frame converted to grayscale for faster detection. |
| 3๏ธโฃ | Face Detection | Haar Cascade locates the face rectangle (x, y, w, h). |
| 4๏ธโฃ | Center Calculation | Center = (x + w/2, y + h/2) gives face midpoint. |
| 5๏ธโฃ | Tracking | Compare center positions between current & previous frame. |
| 6๏ธโฃ | Direction Decision | Based on displacement (dx, dy): determines Left/Right/Up/Down. |
| 7๏ธโฃ | Speed Computation | Uses Euclidean distance & time delta โ speed = distance / ฮt. |
| 8๏ธโฃ | Display Overlay | Direction and speed are displayed on live video. |
| Tool | Purpose |
|---|---|
| Python 3.x | Core programming language |
| OpenCV (cv2) | Image & video processing |
| time module | Measuring frame intervals for speed |
| Git & GitHub | Version control and collaboration |
Make sure you have Python 3.x and OpenCV installed:
1.pip install opencv-python 2.Step 2 โ Run the Script 3.Execute the main script in your terminal: python face_direction_tracker.py โ Step 4 โ Exit To close the live webcam window, press:
๐ง Working Logic (Step-by-Step):
1.Capture Frame: The webcam captures each video frame in real-time. 2.Convert to Grayscale: Makes detection faster and easier for OpenCV. 3.Face Detection: Haar Cascade identifies the bounding box of your face. 4.Calculate Center: Compute (cx, cy) โ the midpoint of the face box. 5.Compare Frames: Measure how much the center moved since the last frame. 6.Compute Direction:
If cxโ > cxโ โ Face moved Right
If cxโ < cxโ โ Face moved Left
If cyโ > cyโ โ Face moved Down
If cyโ < cyโ โ Face moved Up
7.Compute Speed: Calculate pixels moved per second.
8.Overlay Results: Display direction and speed text on the live feed.
( ๐ ๐ฅ ) 2 + ( ๐ ๐ฆ ) 2 ฮ ๐ก Speed= ฮt (dx) 2 +(dy) 2
Where:
dx = cxโ - cxโ โ horizontal movement
dy = cyโ - cyโ โ vertical movement
ฮt โ time difference between frames
๐ Example Output When you move your face, the terminal and live video display: Direction: Left Speed: 120.54 px/s โ A green rectangle surrounds your face and updates continuously.
๐ Visualization Overview Frame Detected Face Computed Center Output Frame 1 Yes (320, 240) โ Frame 2 Yes (380, 240) Direction: Right, Speed: 60 px/s Frame 3 Yes (380, 280) Direction: Down, Speed: 40 px/s
๐ง What I Learned:
From this project, I developed practical understanding of: ๐งฉ How OpenCV detects and tracks moving objects ๐งฎ How to measure position change for motion detection โฑ๏ธ How to calculate speed based on time intervals ๐ง The concept of direction vectors in computer vision
๐ Future Improvements: Improvement and Description ๐น Use Mediapipe or Dlib For more accurate face landmarks ๐น Add 3D Head Pose Estimation Estimate angles (pitch, yaw, roll) ๐น Integrate with servo motors Make a robot head follow user movement ๐น Data Logging Save motion data for ML training or analytics ๐น Optimize frame rate Use multi-threading for smoother tracking
๐ References & Learning Materials: 1.OpenCV Official Documentation 2.PyImageSearch Tutorials 3.Real-Time Object Tracking (LearnOpenCV)
Field: Intelligent Systems & Robotics
Year: 2025