Ars135 commited on
Commit
7bcd50b
·
verified ·
1 Parent(s): 790a1e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
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['dominant_emotion']
9
- return f"Emotion: {emotion}"
 
 
 
 
 
 
 
10
  except Exception as e:
11
- return "Error: " + str(e) + "\n" + traceback.format_exc()
12
 
13
- gr.Interface(
14
  fn=detect_emotion,
15
- inputs=gr.Image(type="numpy"),
16
- outputs="text",
17
  title="Emotion Detection (DeepFace)",
18
- description="Upload an image to detect emotion using pretrained DeepFace model."
19
- ).launch()
 
 
 
 
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()