0
0
Computer Visionml~5 mins

DNN-based face detection in Computer Vision

Choose your learning style9 modes available
Introduction

We use deep neural networks (DNN) to find faces in pictures because they can learn to spot faces very well, even if faces look different or are in tricky positions.

When you want to unlock your phone by recognizing your face.
When a camera needs to focus automatically on people's faces.
When you want to count how many people are in a photo or video.
When you want to blur faces in photos to protect privacy.
When you want to add fun filters that stick to your face in apps.
Syntax
Computer Vision
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
blob = cv2.dnn.blobFromImage(image, scalefactor, size, mean)
model.setInput(blob)
detections = model.forward()

This example uses OpenCV's DNN module with a Caffe model for face detection.

The 'blobFromImage' prepares the image for the model by resizing and normalizing it.

Examples
Load the face detection model files.
Computer Vision
model = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'res10_300x300_ssd_iter_140000.caffemodel')
Create a blob from the input image with size 300x300 and mean subtraction for normalization.
Computer Vision
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
Run the model on the prepared image to get face detections.
Computer Vision
model.setInput(blob)
detections = model.forward()
Sample Model

This program loads a DNN face detector, reads an image, detects faces with confidence above 0.5, and prints their locations.

Computer Vision
import cv2

# Load the pre-trained face detection model from OpenCV's DNN module
prototxt_path = 'deploy.prototxt'
model_path = 'res10_300x300_ssd_iter_140000.caffemodel'
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)

# Read an image from file
image = cv2.imread('face.jpg')
(h, w) = image.shape[:2]

# Prepare the image for the model
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
model.setInput(blob)

# Run forward pass to get detections
detections = model.forward()

# Loop over detections and print confidence and bounding box
for i in range(detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > 0.5:
        box = detections[0, 0, i, 3:7] * [w, h, w, h]
        (startX, startY, endX, endY) = box.astype('int')
        print(f'Face {i+1}: Confidence={confidence:.2f}, Box=({startX}, {startY}), ({endX}, {endY})')
OutputSuccess
Important Notes

Make sure the model files ('deploy.prototxt' and '.caffemodel') are downloaded and in the working folder.

The confidence threshold (0.5 here) can be adjusted to detect more or fewer faces.

DNN-based detection works well even if faces are turned or partly covered.

Summary

DNN-based face detection uses a trained neural network to find faces in images.

It is useful for many real-life tasks like unlocking phones or adding filters.

OpenCV's DNN module makes it easy to load models and detect faces with just a few lines of code.