Face Detection with Computer Vision

In this notebook, you’ll use a neural network to detect faces in real time using your webcam. You’ll learn how computers “see” and locate faces in an image, and think about how this technology is used in the real world.

Open In Jupyter K-12

What Is Face Detection?

Face detection is the task of finding where faces are located in an image or video frame. The output is a bounding box — a rectangle drawn around each face — plus a confidence score that says how certain the model is.

This is not the same as face recognition, which tries to identify who a person is. Face detection only says: “there is a face at this location.” It has no idea who you are.

Under the hood, modern face detectors are small neural networks trained on millions of face images. These are the same kind of networks you learned about today.

Step 1: Live Face Detection

This platform uses BlazeFace: a lightweight neural network from Google that detects faces in real time. Each face it finds is returned as a dictionary with these fields:

Field Meaning
x, y Top-left corner of the bounding box (pixels)
w, h Width and height of the bounding box (pixels)
confidence How certain the model is (0.0 – 1.0)

Run the cell below — your browser will ask for webcam access, click Allow.

The detector will run continuously and draw a box around any face it finds.

Click Stop (■) in the toolbar when you’re ready to move on.

import cv
import graphics

canvas = graphics.canvas()
camera = cv.start_camera(canvas)
detector = cv.start_face_detector(camera)

try:
  while True:
    detections = detector.get_detections()
    canvas.draw_bounding_boxes(detections)
finally:
  detector.stop()
  camera.stop()
  print("Camera stopped.")

Step 2: Explore the Detection Data

The cell below runs the same live detection loop, but also prints the raw numbers for every face it finds, updated constantly.

Try moving closer to the camera, tilting your head, or partially covering your face. Watch how the position, size, and confidence values change in real time.

Click Stop (■) when you’re done.

import cv
import graphics
import time

canvas = graphics.canvas()
camera = cv.start_camera(canvas)
detector = cv.start_face_detector(camera)

frame = 0
try:
  while True:
    detections = detector.get_detections()
    canvas.draw_bounding_boxes(detections)
    if frame % 240 == 0:
      print(f"--- {len(detections)} face(s) detected ---")
      for i, face in enumerate(detections):
        print(f"  Face {i + 1}: x={face['x']}, y={face['y']}  "
            f"size={face['w']}x{face['h']}  confidence={face['confidence']:.1%}")
    frame += 1
    time.sleep(0.001)
finally:
  detector.stop()
  camera.stop()
  print("Camera stopped.")

Step 3: Experiment — Confidence Threshold

A confidence threshold filters out detections the model isn’t sure about.

  • High threshold (e.g., 0.90) → only very certain detections pass through
  • Low threshold (e.g., 0.20) → more detections, including uncertain ones

Set the slider to your desired threshold, then run the cell. The loop will filter detections in real time using that value. Try a very high threshold. Can you make the detector lose track of your face?

Click Stop (■) when you’re done.

import cv
import graphics

CONFIDENCE_THRESHOLD = 0.7 #@param {type:"slider", min:0.1, max:1.0, step:0.05}

canvas = graphics.canvas()
camera = cv.start_camera(canvas)
detector = cv.start_face_detector(camera)

try:
  while True:
    detections = detector.get_detections()
    filtered = [d for d in detections if d['confidence'] >= CONFIDENCE_THRESHOLD]
    canvas.draw_bounding_boxes(filtered)
finally:
  detector.stop()
  camera.stop()
  print("Camera stopped.")

Face detection is used in schools, airports, stadiums, and social media platforms. Where do you think it’s helpful? Are there places where you’d want limits on how it’s used — and why?

Check for Understanding

Answer the questions below to check what you’ve learned.

{ “question_type”: “true_false”, “question”: “Face detection can identify who a person is.”, “answer”: “False”, “submitted_answer”: “” }

{ “question_type”: “multiple_choice”, “question”: “What does a confidence score of 0.95 mean for a face detection?”, “options”: [ { “key”: “a”, “text”: “95 faces were found in the image” }, { “key”: “b”, “text”: “The model is 95% certain it found a face” }, { “key”: “c”, “text”: “The face is 95 pixels wide” }, { “key”: “d”, “text”: “The detection took 0.95 seconds” } ], “answer”: “b”, “submitted_answer”: “” }

{ “question_type”: “true_false”, “question”: “Setting a higher confidence threshold causes the detector to find more faces.”, “answer”: “False”, “submitted_answer”: “” }

{ “question_type”: “multiple_choice”, “question”: “Which four values describe a bounding box around a detected face?”, “options”: [ { “key”: “a”, “text”: “x, y, width, height” }, { “key”: “b”, “text”: “top, bottom, left, right” }, { “key”: “c”, “text”: “row, column, depth, color” }, { “key”: “d”, “text”: “red, green, blue, alpha” } ], “answer”: “a”, “submitted_answer”: “” }