0
0
Computer Visionml~5 mins

Haar cascade face detection in Computer Vision

Choose your learning style9 modes available
Introduction

Haar cascade face detection helps computers find faces in pictures or videos quickly and easily.

When you want to find faces in photos on your phone.
When building a security camera that alerts if it sees a face.
When making a fun app that adds filters to faces in real time.
When sorting pictures by people's faces automatically.
When you need a simple way to detect faces without heavy computing.
Syntax
Computer Vision
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=5)

detectMultiScale finds faces and returns their positions as rectangles.

scaleFactor controls how much the image size is reduced at each image scale.

Examples
Detect faces in a grayscale image with default settings.
Computer Vision
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
Detect faces with a smaller scale step and fewer neighbors for more detections but possibly more false positives.
Computer Vision
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.05, minNeighbors=3)
Detect faces with a larger scale step and more neighbors for fewer but more confident detections.
Computer Vision
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.2, minNeighbors=6)
Sample Model

This program loads a sample image, detects faces using Haar cascade, prints how many faces it found, draws blue rectangles around them, and shows the result.

Computer Vision
import cv2

# Load the pre-trained Haar cascade for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Read an image from file
image = cv2.imread(cv2.samples.findFile('lena.jpg'))

# Convert the image to grayscale (required for Haar cascade)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Print number of faces found
print(f'Number of faces detected: {len(faces)}')

# Draw rectangles around detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

# Save the result image
cv2.imwrite('faces_detected.jpg', image)

# Show the image with detected faces
cv2.imshow('Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
OutputSuccess
Important Notes

Haar cascades work best on clear, frontal faces and may miss faces at angles or in poor light.

Always convert images to grayscale before detection for best results.

You can adjust scaleFactor and minNeighbors to balance between detecting more faces and avoiding false detections.

Summary

Haar cascade is a fast way to find faces in images using simple patterns.

It works by scanning the image at different sizes and looking for face-like features.

Adjusting parameters helps control detection accuracy and speed.