import os import gradio as gr import cv2 from insightface.app import FaceAnalysis from hsemotion_onnx.facial_emotions import HSEmotionRecognizer def facial_emotion_recognition(img): faces = face_detector.get(img) if len(faces) > 0: highest_score_box = (0, 0, 0, 0) # x, y, w, h highest_score = 0 for face in faces: if face['det_score'] > highest_score: highest_score = face['det_score'] x1, y1, x2, y2 = face['bbox'].astype(int) x_margin = int((x2 - x1) * face_margin) y_margin = int((y2 - y1) * face_margin) x = max(0, x1 - x_margin) y = max(0, y1 - y_margin) w = min(x2 + x_margin, img.shape[1]) - x h = min(y2 + y_margin, img.shape[0]) - y highest_score_box = (x, y, w, h) x, y, w, h = highest_score_box emotion, _ = hse_emo_model.predict_emotions(img[y:y+h, x:x+w], logits=True) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2) cv2.putText(img, emotion, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) return img face_margin = 0.1 model_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'buffalo_sc') face_detector = FaceAnalysis(name=model_name, allowed_modules=['detection'], providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) face_detector.prepare(ctx_id=0, det_size=(640, 640)) hse_emo_model = HSEmotionRecognizer(model_name='enet_b0_8_best_vgaf') webcam = gr.Image(image_mode='RGB', type='numpy', source='webcam', label='Input Image') output = gr.Image(image_mode='RGB', type='numpy', label='Output Image') app = gr.Interface(facial_emotion_recognition, inputs=webcam, outputs=output) app.launch()