0
0
Computer Visionml~5 mins

Face detection with deep learning in Computer Vision

Choose your learning style9 modes available
Introduction

Face detection helps computers find faces in pictures or videos. It is useful for many apps like unlocking phones or tagging friends in photos.

To find faces in photos for organizing albums.
To unlock a phone using your face.
To detect people in security camera footage.
To add fun filters on faces in social media apps.
To count how many people are in a room.
Syntax
Computer Vision
import cv2

# Load pre-trained face detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Read image
image = cv2.imread('image.jpg')

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

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

# Show image
cv2.imshow('Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

This example uses OpenCV's pre-trained Haar Cascade model for face detection.

Faces are detected as rectangles with coordinates (x, y, width, height).

Examples
Detect faces with a lower minNeighbors value to find more faces but with more false positives.
Computer Vision
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
Use a smaller scaleFactor to detect smaller faces but it will be slower.
Computer Vision
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)
Draw green rectangles with thicker lines around detected faces.
Computer Vision
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 3)
Sample Model

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

Computer Vision
import cv2

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

# Read the image
image = cv2.imread(cv2.samples.findFile('lena.jpg'))

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

print(f'Number of faces detected: {len(faces)}')

# Draw rectangles around 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('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
OutputSuccess
Important Notes

Haar Cascade is a fast and simple method for face detection but newer methods like SSD or MTCNN can be more accurate.

Make sure the image path is correct or use sample images from OpenCV to avoid errors.

Face detection works best on clear, front-facing images.

Summary

Face detection finds faces in images using models like Haar Cascade.

It is useful for apps like phone unlocking, photo tagging, and security.

OpenCV provides easy tools to detect and draw faces in pictures.