Skip to content

Estimate the distance of a face from a camera using real-time face detection and computer vision. This project leverages OpenCV and Haar Cascade to detect faces and measure their distance from the camera. It's simple, efficient, and ideal for educational purposes, DIY projects, and smart system integrations. πŸš€

Notifications You must be signed in to change notification settings

HaiderManzoor/FaceDistanceMeasure

Repository files navigation

Face Distance Measurement Using OpenCV πŸŽ₯πŸ“

This project calculates the distance between a face and the camera in real-time using OpenCV’s Haar Cascade face detector and focal length estimation.


Features ✨

  • Real-time face detection via webcam πŸ‘οΈβ€πŸ—¨οΈ
  • Calculates camera focal length using a reference image 🎯
  • Estimates distance from camera to detected face in centimeters πŸ“
  • Displays distance on live video feed πŸ–₯️

Requirements πŸ“‹

  • Python 3.x 🐍
  • OpenCV (opencv-python) 🧰
  • Webcam πŸ“·
  • Reference face image (rf.png) with known face width and distance πŸ–ΌοΈ
  • Haar Cascade XML file (haarcascade_frontalface_default.xml in data/ directory) πŸ“‚

Installation πŸš€

  1. Clone or download the repo. πŸ“₯

  2. Install OpenCV:

pip install opencv-python
  1. Download Haar Cascade XML from OpenCV GitHub and put it in the data/ folder. πŸ“‚

Usage ▢️

  1. Update the script with your known reference values:
Know_distance = 30          # Known distance (cm) πŸ“  
Know_width_face = 14.3      # Known real face width (cm) πŸ‘€
  1. Run the script:
python your_script_name.py
  1. Press q to quit. ❌

How It Works βš™οΈ

  • Calculates the focal length using the known reference distance and face width. 🎯
  • Detects faces in the webcam feed. πŸ‘οΈβ€πŸ—¨οΈ
  • Measures the face width in pixels and estimates distance with the formula:

[ \text{Distance} = \frac{\text{Real Face Width} \times \text{Focal Length}}{\text{Face Width in Image}} ]


Output Example πŸ“Έ

Distance Measurement Output


Notes πŸ“

  • Use the same camera for calibration and live detection. πŸ“·
  • Adjust Haar Cascade parameters if needed for better face detection. πŸ”§
  • Good lighting improves detection accuracy. πŸ’‘

License πŸ“„

Open source, free to use. πŸš€


Connect with Me on LinkedIn πŸ”—

LinkedIn

About

Estimate the distance of a face from a camera using real-time face detection and computer vision. This project leverages OpenCV and Haar Cascade to detect faces and measure their distance from the camera. It's simple, efficient, and ideal for educational purposes, DIY projects, and smart system integrations. πŸš€

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages