0
0
Computer-visionHow-ToBeginner ยท 4 min read

How to Use MediaPipe for Face Detection in Computer Vision

Use mediapipe.solutions.face_detection.FaceDetection to detect faces in images or video frames. Initialize the detector, process the input image, and extract face bounding boxes from the results for your computer vision tasks.
๐Ÿ“

Syntax

The main class for face detection in MediaPipe is FaceDetection from mediapipe.solutions.face_detection. You create an instance with optional parameters like model_selection (to choose detection model) and min_detection_confidence (to filter weak detections). Then call process() on an RGB image to get detection results.

  • FaceDetection(model_selection=0, min_detection_confidence=0.5): Initializes the detector.
  • process(image): Runs detection on the input image.
  • results.detections: List of detected faces with bounding boxes and keypoints.
python
import mediapipe as mp

mp_face_detection = mp.solutions.face_detection
face_detection = mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5)

# To detect faces:
# results = face_detection.process(rgb_image)
# faces = results.detections
๐Ÿ’ป

Example

This example shows how to use MediaPipe to detect faces in a webcam video stream. It captures frames, converts them to RGB, runs face detection, and draws bounding boxes around detected faces.

python
import cv2
import mediapipe as mp

mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils

cap = cv2.VideoCapture(0)

with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
    while cap.isOpened():
        success, frame = cap.read()
        if not success:
            break

        # Convert BGR to RGB
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        image.flags.writeable = False

        # Detect faces
        results = face_detection.process(image)

        # Draw detections
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        if results.detections:
            for detection in results.detections:
                mp_drawing.draw_detection(image, detection)

        cv2.imshow('MediaPipe Face Detection', image)
        if cv2.waitKey(5) & 0xFF == 27:
            break

cap.release()
cv2.destroyAllWindows()
Output
A window opens showing webcam video with rectangles around detected faces in real-time.
โš ๏ธ

Common Pitfalls

  • Not converting the input image from BGR (OpenCV default) to RGB before processing causes detection to fail or give wrong results.
  • Forgetting to set image.flags.writeable = False before processing can slow down detection.
  • Using a very low min_detection_confidence may produce false positives; too high may miss faces.
  • Not releasing the video capture or destroying windows causes the program to hang.
python
import cv2
import mediapipe as mp

mp_face_detection = mp.solutions.face_detection

cap = cv2.VideoCapture(0)

with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
    while cap.isOpened():
        success, frame = cap.read()
        if not success:
            break

        # WRONG: Not converting BGR to RGB
        # results = face_detection.process(frame)

        # RIGHT: Convert BGR to RGB
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        image.flags.writeable = False
        results = face_detection.process(image)

        # Process results here

cap.release()
cv2.destroyAllWindows()
๐Ÿ“Š

Quick Reference

Here is a quick summary of key MediaPipe Face Detection parameters and methods:

Parameter / MethodDescription
model_selection0 for short-range, 1 for full-range face detection
min_detection_confidenceThreshold to filter weak detections (0 to 1)
process(image)Run face detection on an RGB image
results.detectionsList of detected faces with bounding boxes and keypoints
mp_drawing.draw_detection(image, detection)Draw bounding box and keypoints on image
โœ…

Key Takeaways

Always convert images from BGR to RGB before passing to MediaPipe FaceDetection.
Use FaceDetection's process() method to get face detections from images.
Adjust min_detection_confidence to balance detection sensitivity and accuracy.
Use mp_drawing utilities to visualize detected faces easily.
Release resources like video capture and windows to avoid program hangs.