Skip to content

SasadaSaumya/Python-Face-Detection-OpenCV

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Face Detection with OpenCV

A simple Python script that detects faces in an image using OpenCV's built-in Haar Cascade classifier.


Preview

Face Detection Output

Green rectangles are drawn around each detected face in the image.


How It Works

  1. Load the face detection model — Uses OpenCV's pre-trained haarcascade_frontalface_default.xml model, which knows how to spot human faces.
  2. Read the image — Loads gp.png from the project folder.
  3. Convert to grayscale — Face detection works on grayscale images (it's faster and more accurate).
  4. Detect faces — Scans the image and finds all face-like regions.
  5. Draw rectangles — Draws a green box around every detected face.
  6. Show the result — Displays the final image in a window and prints the total face count.

Requirements

  • Python 3.x
  • OpenCV

Install OpenCV with:

pip install opencv-python

Usage

  1. Place your image in the project folder and name it gp.png (or change the filename in index.py line 9).
  2. Run the script:
python index.py
  1. A window will open showing the image with green rectangles drawn around detected faces.
  2. Press any key to close the window.

Project Structure

Python Face Detection OpenCV/
├── index.py       # Main script
├── gp.png         # Input image
└── README.md      # This file

Key Parameters Explained

Parameter Value Meaning
scaleFactor 1.1 How much the image is scaled down at each step — lower = more accurate but slower
minNeighbors 6 How many neighboring detections are needed to confirm a face — higher = fewer false positives
minSize (30, 30) Minimum face size in pixels — ignores anything smaller

Example Output

Total Faces Detected: 5

A window titled "Face Detection" will display with green rectangles around each detected face.

About

A simple Python script that detects faces in an image using OpenCV's built-in Haar Cascade classifier.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages