File size: 2,164 Bytes
a58bc70 152fa4b f8f9179 a58bc70 f8f9179 a58bc70 4f8044a 2be9d10 a432ed6 4f8044a a432ed6 4f8044a a432ed6 4f8044a a432ed6 4f8044a a432ed6 4f8044a a432ed6 4f8044a a432ed6 4f8044a a432ed6 2be9d10 a432ed6 2be9d10 a432ed6 2be9d10 a432ed6 f8f9179 57e2f3b f8f9179 2be9d10 a58bc70 a432ed6 a58bc70 a432ed6 a58bc70 4f8044a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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
# Download ONNX-modellen
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 -> NL mapping (lowercase) incl. varianten/typo's
EN_TO_NL = {
"neutral": "neutraal",
"happy": "blij",
"happiness": "blij",
"sad": "verdrietig",
"sadness": "verdrietig",
"surprise": "verrast",
"surprised": "verrast",
"supprised": "verrast", # veelvoorkomende typo
"surprized": "verrast",
"angry": "boos",
"anger": "boos",
"disgust": "walging",
"fear": "angstig",
"fearful": "angstig",
"fearfull": "angstig", # veelvoorkomende typo
"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)
# In-memory statistieken
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, (_
|