File size: 845 Bytes
36ee343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load the face emotion recognition model
emotion_classifier = pipeline("image-classification", model="dima806/facial_emotions_image_detection")

def detect_emotion(image):
    # Perform emotion detection
    results = emotion_classifier(image)
    
    # Format and return the results
    return {result["label"]: f"{result['score']:.4f}" for result in results}

# Create the Gradio interface
demo = gr.Interface(
    fn=detect_emotion,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=7),
    title="Facial Expression Recognition",
    description="Upload an image with a face to detect the emotion/expression. The model can recognize: anger, disgust, fear, happiness, neutral, sadness, and surprise."
)

# Launch the app
if __name__ == "__main__":
    demo.launch()