class RetinaFaceDetector: def __init__(self, min_face_size=0): """Face detector based on RetinaFace. Args: min_face_size (int): minimum size of a face to be considered a match. Compared against min(width, height) of the face bounding box. """ self.min_size = min_face_size self.model = RetinaFace(quality="normal") def detect(self, img: np.array): assert len(img.shape) == 3 and img.shape[2] == 3 bbs = self.model.predict(img, threshold=0.95) return [{ "box": [b["x1"], b["y1"], b["x2"], b["y2"]], "keypoints": { "left_eye": (int(b["left_eye"][0]), int(b["left_eye"][1])), "right_eye": (int(b["right_eye"][0]), int(b["right_eye"][1])), "nose": (int(b["nose"][0]), int(b["nose"][1])), "mouth_left": (int(b["left_lip"][0]), int(b["left_lip"][1])), "mouth_right": (int(b["right_lip"][0]), int(b["right_lip"][1])), } } for b in bbs if min(b["x2"] - b["x1"], b["y2"] - b["y1"]) >= self.min_size]
# pip3 install opencv-python import cv2 from retinaface import RetinaFace # init with normal accuracy option detector = RetinaFace(quality="normal") # same with cv2.imread,cv2.cvtColor rgb_image = detector.read("data/hian.jpg") faces = detector.predict(rgb_image) # faces is list of face dictionary # each face dictionary contains x1 y1 x2 y2 left_eye right_eye nose left_lip right_lip # faces=[{"x1":20,"y1":32, ... }, ...] result_img = detector.draw(rgb_image, faces) # save ([...,::-1] : rgb -> bgr ) cv2.imwrite("data/result_img.jpg", result_img[..., ::-1])