File size: 1,104 Bytes
c2e4268
 
f46c1ae
c2e4268
401bbd0
c2e4268
 
 
f46c1ae
 
 
 
 
401bbd0
f46c1ae
 
401bbd0
f46c1ae
401bbd0
f46c1ae
401bbd0
f46c1ae
401bbd0
 
 
 
f46c1ae
c2e4268
401bbd0
 
c2e4268
 
 
 
401bbd0
 
 
c2e4268
 
 
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
import gradio as gr
from deepface import DeepFace
import tempfile
import os
import traceback

def analyze_image(image):
    try:
        # Save image to temp file
        with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
            image.save(temp_file.name)
            temp_path = temp_file.name

        # Run analysis
        result = DeepFace.analyze(
            img_path=temp_path,
            actions=["emotion", "gender"],
            enforce_detection=False
        )[0]

        os.remove(temp_path)

        emotion = result.get("dominant_emotion", "Unknown")
        gender = result.get("dominant_gender", "Unknown")

        return f"Gender: {gender}\nEmotion: {emotion}"

    except Exception as e:
        tb = traceback.format_exc()
        return f"Error occurred:\n{e}\n\nTraceback:\n{tb}"

demo = gr.Interface(
    fn=analyze_image,
    inputs=gr.Image(type="pil"),
    outputs=gr.Textbox(label="Prediction"),
    title="DeepFace: Emotion & Gender Detection",
    description="Upload a clear face image. Model predicts gender and emotion."
)

demo.launch()