Skip to content

Instantly share code, notes, and snippets.

@j-adamczyk
Created February 16, 2021 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-adamczyk/bdf56466abcf3803216e4a814928de3d to your computer and use it in GitHub Desktop.
Save j-adamczyk/bdf56466abcf3803216e4a814928de3d to your computer and use it in GitHub Desktop.
Face detection using Viola-Jones method
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier()
face_cascade.load("haarcascade_frontalface_alt.xml")
def viola_jones_detect(img: np.ndarray) -> np.ndarray:
frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
frame_gray = cv2.equalizeHist(frame_gray)
faces = face_cascade.detectMultiScale(frame_gray)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment