Face detection (Haar cascade) — computer vision in the browser
Haar cascades detect faces by scanning for simple light/dark patterns at many scales — the original real-time face detector in computer vision. This fetches a portrait and the cascade file, then runs cv2.CascadeClassifier.detectMultiScale. Run it live and tweak the parameters.
Example code (Python / OpenCV)
"""
Face detection (Haar cascade)
Haar cascades detect faces by scanning for simple light/dark patterns at many scales — the original real-time face detector in computer vision. This fetches a portrait and the cascade file, then runs cv2.CascadeClassifier.detectMultiScale. Run it live and tweak the parameters.
"""
import cv2
import requests
import numpy as np
# A real photo, fetched live from the web — nothing is uploaded
url = "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?w=480&q=70"
# no-cache: always fetch a fresh copy so a stale browser-cache entry can't break decoding
resp = requests.get(url, headers={"Cache-Control": "no-cache"})
img = cv2.imdecode(np.frombuffer(resp.content, np.uint8), cv2.IMREAD_COLOR)
# The Haar cascade (an OpenCV XML) is bundled with TinkerCV — load it straight from disk
face = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
found = face.detectMultiScale(gray, 1.1, 5) # scaleFactor, minNeighbors
for (x, y, w, h) in found:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
print(len(found), "face(s) found")
cv2.imshow("faces", img)