Skip to content

sushensanthush/Face-detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Easy Face Detection with OpenCV

Python Version OpenCV Version License Platform Maintained

A beginner-friendly Python project that detects human faces in images using OpenCV's Haar Cascade Classifier. This project is designed to help newcomers understand core computer vision concepts through a simple, well-documented example.


Preview

Project Screenshot

Table of Contents


Overview

This program opens a file picker dialog, loads a user-selected image, runs face detection using a pre-trained Haar Cascade model, draws red rectangles around detected faces, and displays the result using Matplotlib.

No datasets, no model training, and no internet connection required. OpenCV includes the pre-trained face detection model out of the box.


How It Works

User selects image  -->  Load image with OpenCV  -->  Convert to Grayscale
                                                              |
                                                              v
Display result  <--  Draw rectangles on faces  <--  Run Haar Cascade Detector

Libraries Used

Library Version Purpose
opencv-python >= 4.x Image loading, processing, and face detection
matplotlib >= 3.x Rendering and displaying the output image
tkinter Built-in GUI file picker dialog (no install needed)

Prerequisites

  • Python 3.7 or higher
  • pip (Python package manager)
  • A terminal or command prompt

Installation

Clone or download this repository, then install the required dependencies:

pip install opencv-python matplotlib

tkinter is included with standard Python installations and does not require a separate install.


Usage

Run the script from your terminal:

python face_detector.py

A file picker window will open. Select any image file in .jpg, .jpeg, .png, .bmp, or .webp format. The program will detect faces and display the result with red rectangles drawn around each detected face.


Code Walkthrough

Below is the full source code with a detailed explanation of every section.

import cv2
import matplotlib.pyplot as plt
from tkinter import Tk
from tkinter.filedialog import askopenfilename

What this does:

  • cv2 is the OpenCV library, used for all image processing tasks.
  • matplotlib.pyplot is used to display the final image in a window.
  • Tk and askopenfilename are from the built-in tkinter library and are used to create a file picker dialog so the user can browse and select an image.

Tk().withdraw()
print("Opening file selector... Please select an image.")
uploaded_image_path = askopenfilename(
    title="Select an Image for Face Detection",
    filetypes=[("Image Files", "*.jpg *.jpeg *.png *.bmp *.webp")]
)

What this does:

  • Tk().withdraw() creates a Tkinter application instance and immediately hides its default blank window. Without this line, an unwanted empty window would appear behind the file picker.
  • askopenfilename() opens the operating system's native file browser dialog. The title sets the dialog heading, and filetypes restricts the selectable files to common image formats.
  • The selected file's full path is stored in uploaded_image_path as a string.

if not uploaded_image_path:
    print("Error: No image was selected. Exiting...")
    exit()

What this does:

  • If the user closes the dialog without selecting a file, askopenfilename() returns an empty string. This check catches that case and exits the program cleanly instead of crashing further down.

img = cv2.imread(uploaded_image_path)

if img is None:
    print("Error: Could not load the selected image.")
    exit()

What this does:

  • cv2.imread() reads the image from the given file path and loads it into memory as a NumPy array (a 3D grid of pixel values).
  • If the file is corrupt or unreadable, imread() returns None instead of raising an exception. The if img is None check handles this case gracefully.

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

What this does:

  • Loads a pre-trained face detection model called a Haar Cascade Classifier.
  • cv2.data.haarcascades is the path to the folder where OpenCV stores its built-in trained models.
  • haarcascade_frontalface_default.xml is the XML file containing the trained model for detecting frontal human faces. It ships with every installation of opencv-python and requires no additional download.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

What this does:

  • Converts the color image (which has 3 channels: Blue, Green, Red) into a grayscale image (1 channel representing brightness).
  • The Haar Cascade Classifier works on grayscale images because it analyses pixel intensity patterns, not color. This conversion also reduces the amount of data the algorithm needs to process, making detection faster.
  • Note that OpenCV loads images in BGR order by default, not the more common RGB order.

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=7, minSize=(50, 50))

What this does:

  • detectMultiScale() scans the grayscale image at multiple scales to find faces of different sizes within the same image.
  • It returns a list of rectangles, where each rectangle represents a detected face described by four values: (x, y, width, height).
  • If no faces are detected, it returns an empty list and the loop below simply does nothing.

For a detailed explanation of the parameters, see the Parameters Explained section.


for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 3)

What this does:

  • Loops through each detected face rectangle.
  • x, y is the top-left corner of the face bounding box. w, h are the width and height.
  • cv2.rectangle() draws a rectangle on the original color image.
    • (x, y) is the top-left corner.
    • (x+w, y+h) is the bottom-right corner.
    • (0, 0, 255) is the color in BGR format, which produces red.
    • 3 is the border thickness in pixels.

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

What this does:

  • Converts the image from BGR (OpenCV's default channel order) to RGB (Matplotlib's expected channel order).
  • Without this step, the colors in the displayed image would be wrong — reds would appear blue and blues would appear red.

plt.imshow(img_rgb)
plt.axis('off')
plt.show()

What this does:

  • plt.imshow() renders the image inside a Matplotlib figure.
  • plt.axis('off') hides the axis ticks and labels so only the image is visible.
  • plt.show() opens the display window.

Parameters Explained

The most important line in this project is detectMultiScale(). Understanding its parameters is key to tuning detection accuracy.

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=7, minSize=(50, 50))
Parameter Value Used Meaning
scaleFactor 1.1 How much the image is scaled down at each scanning step. A value of 1.1 means a 10% reduction per step. Lower values are more thorough but slower.
minNeighbors 7 How many neighboring rectangles must confirm a detection before it is accepted. Higher values reduce false positives but may miss some real faces.
minSize (50, 50) The minimum pixel size a detected region must be. Any face smaller than 50x50 pixels is ignored.

Tuning guidance:

  • Too many false detections: increase minNeighbors (try 8 or 9).
  • Missing real faces: decrease minNeighbors (try 5 or 6).
  • Working with high-resolution images with small faces: reduce minSize to (30, 30).

Common Errors and Fixes

No module named 'cv2'

OpenCV is not installed. Run:

pip install opencv-python

Image displays with wrong colors (red rectangles appear blue)

You forgot to convert from BGR to RGB before displaying. Ensure this line exists before plt.imshow():

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

No faces detected on a photo that clearly contains faces

Lower the minNeighbors value and reduce minSize:

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

Also note that this classifier is trained for frontal faces only. Faces that are turned sideways or tilted significantly may not be detected.


_tkinter.TclError on Linux

Tkinter may not be installed on some Linux distributions. Install it with:

sudo apt-get install python3-tk

Key Concepts for Beginners

What is a Haar Cascade Classifier?

It is a machine learning-based method for object detection, introduced by Viola and Jones in 2001. It works by scanning an image with a sliding window and checking for patterns (called Haar features) that are characteristic of human faces. for example, the fact that eye regions are darker than the forehead, or that the nose bridge is brighter than the sides. The model in this project was pre-trained by the OpenCV team and is ready to use with no additional setup.

What is BGR vs RGB?

OpenCV reads and stores images with color channels in the order Blue, Green, Red (BGR). Most other tools, including Matplotlib and standard image editors, use the order Red, Green, Blue (RGB). Whenever you pass an OpenCV image to another library for display, you need to swap the channel order using cv2.cvtColor().

What is grayscale conversion and why is it needed?

A grayscale image has a single channel representing brightness, compared to three channels in a color image. The Haar Cascade Classifier analyses brightness intensity patterns rather than color, so converting to grayscale is a requirement for the algorithm to work. It also reduces the volume of data being processed, which speeds up detection.

What is a NumPy array in the context of images?

When OpenCV loads an image, it stores it as a NumPy array, essentially a 3D grid of numbers. The dimensions represent height, width, and color channels. Each pixel is a set of three numbers (one per channel) ranging from 0 to 255. All image manipulations in OpenCV, including drawing rectangles and converting colors, are operations on this array.


Project Structure

face-detection/
│
├── face_detector.py      # Main Python script
└── README.md             # Project documentation

License

This project is open source and available under the MIT License.

Developed by Sushen Santhush

About

A beginner-friendly Python project that detects human faces in images using OpenCV's Haar Cascade Classifier. This project is designed to help newcomers understand core computer vision concepts through a simple, well-documented example.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages