wko_anime-face-similarity/face_detect.py

16 lines
484 B
Python
Raw Normal View History

2023-02-01 19:55:12 +01:00
import cv2
import numpy as np
def find_face_bbox(data: np.ndarray, classifier_file='haarcascades/haarcascade_frontalface_default.xml'):
data_gray = cv2.cvtColor(data, cv2.COLOR_RGB2GRAY)
face_cascade = cv2.CascadeClassifier(classifier_file)
face_coords = face_cascade.detectMultiScale(data_gray, 1.1, 3)
return max(face_coords, key=len)
def crop_face(data: np.ndarray, bounding_box) -> np.ndarray:
x, y, w, h = bounding_box
face = data[y:y + h, x:x + w]
return face