MeloRFTW commited on
Commit
401bbd0
·
verified ·
1 Parent(s): f46c1ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from deepface import DeepFace
3
  import tempfile
4
  import os
5
- import json
6
 
7
  def analyze_image(image):
8
  try:
@@ -11,28 +11,30 @@ def analyze_image(image):
11
  image.save(temp_file.name)
12
  temp_path = temp_file.name
13
 
14
- # Run DeepFace analysis
15
  result = DeepFace.analyze(
16
  img_path=temp_path,
17
- actions=["gender", "emotion"],
18
  enforce_detection=False
19
- )
20
 
21
- os.remove(temp_path) # clean up
22
 
23
- # Convert output to readable JSON
24
- return json.dumps(result[0], indent=2)
 
 
25
 
26
  except Exception as e:
27
- return f"Error: {str(e)}"
 
28
 
29
- # Gradio app
30
  demo = gr.Interface(
31
  fn=analyze_image,
32
  inputs=gr.Image(type="pil"),
33
- outputs=gr.Textbox(label="Analysis Result"),
34
- title="DeepFace Analyzer",
35
- description="Upload a face image. Model returns age, gender, emotion, and race."
36
  )
37
 
38
  demo.launch()
 
2
  from deepface import DeepFace
3
  import tempfile
4
  import os
5
+ import traceback
6
 
7
  def analyze_image(image):
8
  try:
 
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()