CoolFace
Apppublic

Clocksp/face-emotion-recognition

sourceHugging Facemitupdated 6mo agoView on Hugging Face
2likes
util.py55 linesDownload Raw Back to root
1import cv22import mediapipe as mp3 4from mediapipe.tasks.python import vision5from mediapipe.tasks.python import BaseOptions6from mediapipe.tasks.python.vision import RunningMode7 8def get_detector():9    options = vision.FaceLandmarkerOptions(10        base_options=BaseOptions(model_asset_path="face_landmarker.task"),11        running_mode=RunningMode.IMAGE,12        num_faces=113    )14    return vision.FaceLandmarker.create_from_options(options)15 16 17def get_face_landmarks(image, draw=False):18    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)19    mp_image = mp.Image(20        image_format=mp.ImageFormat.SRGB,21        data=image_rgb22    )23 24    detector = get_detector()25    result = detector.detect(mp_image)26 27 28    if not result.face_landmarks:29        return None30 31    face_landmarks = result.face_landmarks[0] 32 33    xs_, ys_, zs_ = [], [], []34 35    for lm in face_landmarks[:468]:36        xs_.append(lm.x)37        ys_.append(lm.y)38        zs_.append(lm.z)39 40    min_x, min_y, min_z = min(xs_), min(ys_), min(zs_)41 42    image_landmarks = []43    for i in range(len(xs_)):44        image_landmarks.append(xs_[i] - min_x)45        image_landmarks.append(ys_[i] - min_y)46        image_landmarks.append(zs_[i] - min_z)47 48    if draw:49        h, w, _ = image.shape50        for lm in face_landmarks:51            x = int(lm.x * w)52            y = int(lm.y * h)53            cv2.circle(image, (x, y), 1, (0, 255, 0), -1)54 55    return image_landmarks