A simple Python script that detects faces in an image using OpenCV's built-in Haar Cascade classifier.
Green rectangles are drawn around each detected face in the image.
- Load the face detection model — Uses OpenCV's pre-trained
haarcascade_frontalface_default.xmlmodel, which knows how to spot human faces. - Read the image — Loads
gp.pngfrom the project folder. - Convert to grayscale — Face detection works on grayscale images (it's faster and more accurate).
- Detect faces — Scans the image and finds all face-like regions.
- Draw rectangles — Draws a green box around every detected face.
- Show the result — Displays the final image in a window and prints the total face count.
- Python 3.x
- OpenCV
Install OpenCV with:
pip install opencv-python- Place your image in the project folder and name it
gp.png(or change the filename inindex.pyline 9). - Run the script:
python index.py- A window will open showing the image with green rectangles drawn around detected faces.
- Press any key to close the window.
Python Face Detection OpenCV/
├── index.py # Main script
├── gp.png # Input image
└── README.md # This file
| 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 |
Total Faces Detected: 5
A window titled "Face Detection" will display with green rectangles around each detected face.
