Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from deepface import DeepFace
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
import traceback
|
6 |
+
|
7 |
+
def analyze_image(image):
|
8 |
+
try:
|
9 |
+
# Save image to temp file
|
10 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
11 |
+
image.save(temp_file.name)
|
12 |
+
temp_path = temp_file.name
|
13 |
+
|
14 |
+
# Run analysis
|
15 |
+
result = DeepFace.analyze(
|
16 |
+
img_path=temp_path,
|
17 |
+
actions=["emotion", "gender"],
|
18 |
+
enforce_detection=False
|
19 |
+
)[0]
|
20 |
+
|
21 |
+
os.remove(temp_path)
|
22 |
+
|
23 |
+
emotion = result.get("dominant_emotion", "Unknown")
|
24 |
+
gender = result.get("dominant_gender", "Unknown")
|
25 |
+
|
26 |
+
return f"Gender: {gender}\nEmotion: {emotion}"
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
tb = traceback.format_exc()
|
30 |
+
return f"Error occurred:\n{e}\n\nTraceback:\n{tb}"
|
31 |
+
|
32 |
+
demo = gr.Interface(
|
33 |
+
fn=analyze_image,
|
34 |
+
inputs=gr.Image(type="pil"),
|
35 |
+
outputs=gr.Textbox(label="Prediction"),
|
36 |
+
title="DeepFace: Emotion & Gender Detection",
|
37 |
+
description="Upload a clear face image. Model predicts gender and emotion."
|
38 |
+
)
|
39 |
+
|
40 |
+
demo.launch()
|