|
import cv2 as cv |
|
import numpy as np |
|
import gradio as gr |
|
from pathlib import Path |
|
from collections import Counter, defaultdict |
|
from huggingface_hub import hf_hub_download |
|
|
|
from facial_fer_model import FacialExpressionRecog |
|
from yunet import YuNet |
|
|
|
|
|
FD_MODEL_PATH = hf_hub_download(repo_id="opencv/face_detection_yunet", filename="face_detection_yunet_2023mar.onnx") |
|
FER_MODEL_PATH = hf_hub_download(repo_id="opencv/facial_expression_recognition", filename="facial_expression_recognition_mobilefacenet_2022july.onnx") |
|
|
|
backend_id = cv.dnn.DNN_BACKEND_OPENCV |
|
target_id = cv.dnn.DNN_TARGET_CPU |
|
|
|
fer_model = FacialExpressionRecog(modelPath=FER_MODEL_PATH, backendId=backend_id, targetId=target_id) |
|
detect_model = YuNet(modelPath=FD_MODEL_PATH) |
|
|
|
|
|
EN_TO_NL = { |
|
"neutral": "neutraal", |
|
|
|
"happy": "blij", |
|
"happiness": "blij", |
|
|
|
"sad": "verdrietig", |
|
"sadness": "verdrietig", |
|
|
|
"surprise": "verrast", |
|
"surprised": "verrast", |
|
"supprised": "verrast", |
|
"surprized": "verrast", |
|
|
|
"angry": "boos", |
|
"anger": "boos", |
|
|
|
"disgust": "walging", |
|
|
|
"fear": "angstig", |
|
"fearful": "angstig", |
|
"fearfull": "angstig", |
|
|
|
"contempt": "minachting", |
|
|
|
"unknown": "onbekend", |
|
} |
|
|
|
def to_dutch_lower(label: str) -> str: |
|
"""Zet emotielabel om naar NL en lowercase (fallback: originele lowercase).""" |
|
if not label: |
|
return "onbekend" |
|
key = label.strip().lower() |
|
return EN_TO_NL.get(key, key) |
|
|
|
|
|
emotion_stats = defaultdict(int) |
|
|
|
def visualize(image, det_res, fer_res): |
|
"""Tekent bbox + NL-lowercase emotielabel op de output.""" |
|
output = image.copy() |
|
landmark_color = [(255, 0, 0), (0, 0, 255), (0, 255, 0), (255, 0, 255), (0, 255, 255)] |
|
for det, fer_type in zip(det_res, fer_res): |
|
bbox = det[0:4].astype(np.int32) |
|
fer_type_str_nl = to_dutch_lower(FacialExpressionRecog.getDesc(fer_type)) |
|
|
|
cv.rectangle(output, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (0, 255, 0), 2) |
|
cv.putText(output, fer_type_str_nl, (_ |
|
|