|
import os |
|
import gradio as gr |
|
import cv2 |
|
from insightface.app import FaceAnalysis |
|
from hsemotion_onnx.facial_emotions import HSEmotionRecognizer |
|
|
|
|
|
def resize(image, target_size): |
|
|
|
height, width = image.shape[0], image.shape[1] |
|
|
|
|
|
scaling_factor = min(target_size[0] / width, target_size[1] / height) |
|
|
|
resized_image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_LINEAR) |
|
|
|
return resized_image |
|
|
|
|
|
def facial_emotion_recognition(img): |
|
|
|
img = resize(img, target_size) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return img |
|
|
|
face_margin = 0.1 |
|
target_size = (640, 640) |
|
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') |
|
webcam_output = gr.Image(image_mode='RGB', type='numpy', label='Output Image') |
|
webcam_interface = gr.Interface(facial_emotion_recognition, inputs=webcam, outputs=webcam_output) |
|
|
|
upload = gr.Image(image_mode='RGB', type='numpy', source='upload', label='Input Image') |
|
upload_output = gr.Image(image_mode='RGB', type='numpy', label='Output Image') |
|
upload_interface = gr.Interface(facial_emotion_recognition, inputs=upload, outputs=upload_output, examples='examples') |
|
|
|
demo = gr.TabbedInterface(interface_list=[upload_interface, webcam_interface], tab_names=['Upload', 'Webcam']) |
|
demo.launch() |
|
|