Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,27 @@ import traceback
|
|
| 4 |
|
| 5 |
def detect_emotion(img):
|
| 6 |
try:
|
|
|
|
| 7 |
result = DeepFace.analyze(img, actions=['emotion'], enforce_detection=False)
|
| 8 |
-
emotion = result
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
-
return "Error
|
| 12 |
|
| 13 |
-
gr.Interface(
|
| 14 |
fn=detect_emotion,
|
| 15 |
-
inputs=gr.Image(type="numpy"),
|
| 16 |
-
outputs="
|
| 17 |
title="Emotion Detection (DeepFace)",
|
| 18 |
-
description="
|
| 19 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def detect_emotion(img):
|
| 6 |
try:
|
| 7 |
+
# DeepFace handles preprocessing and detection; enforce_detection=False avoids crashes on non-perfect faces
|
| 8 |
result = DeepFace.analyze(img, actions=['emotion'], enforce_detection=False)
|
| 9 |
+
emotion = result.get('dominant_emotion', None)
|
| 10 |
+
scores = result.get('emotion', {})
|
| 11 |
+
lines = []
|
| 12 |
+
if emotion:
|
| 13 |
+
lines.append(f"Dominant emotion: {emotion}")
|
| 14 |
+
if scores:
|
| 15 |
+
for k, v in sorted(scores.items(), key=lambda x: -x[1]):
|
| 16 |
+
lines.append(f"{k}: {v:.2f}")
|
| 17 |
+
return "\\n".join(lines) if lines else "No result"
|
| 18 |
except Exception as e:
|
| 19 |
+
return "Error during analysis:\\n" + str(e) + "\\n" + traceback.format_exc()
|
| 20 |
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
fn=detect_emotion,
|
| 23 |
+
inputs=gr.Image(type="numpy", label="Upload face image"),
|
| 24 |
+
outputs=gr.Textbox(label="Emotion Output"),
|
| 25 |
title="Emotion Detection (DeepFace)",
|
| 26 |
+
description="Detect facial emotions using DeepFace pretrained models."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
iface.launch()
|