alperugurcan commited on
Commit
68a8260
·
verified ·
1 Parent(s): 8314d9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -17
app.py CHANGED
@@ -2,19 +2,7 @@ import gradio as gr
2
  from deepface import DeepFace
3
  from PIL import Image
4
  import numpy as np
5
- import base64
6
 
7
- # Function to encode images in base64
8
- def encode_image(image_path):
9
- with open(image_path, "rb") as image_file:
10
- encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
11
- return f"data:image/jpeg;base64,{encoded_string}"
12
-
13
- # Example images as base64 strings (You need to download the images first)
14
- example_image_1 = encode_image("e.jpeg") # Replace with your local image path
15
- example_image_2 = encode_image("k.jpeg") # Replace with your local image path
16
-
17
- # Analyze the uploaded image
18
  def analyze_face(image):
19
  analysis_result = DeepFace.analyze(img_path=np.array(image), actions=['age', 'gender', 'race', 'emotion'])[0]
20
 
@@ -24,12 +12,10 @@ def analyze_face(image):
24
  race = analysis_result['dominant_race']
25
  emotion = analysis_result['dominant_emotion']
26
 
27
- # Format emotion breakdown as a string
28
  emotions_detail = ', '.join([f"{k}: {v:.2f}%" for k, v in analysis_result['emotion'].items()])
29
 
30
  return age, f"{gender} ({gender_prob})", race, emotion, emotions_detail
31
 
32
- # Define the Gradio interface
33
  iface = gr.Interface(
34
  fn=analyze_face,
35
  inputs=gr.Image(type="pil"),
@@ -37,9 +23,7 @@ iface = gr.Interface(
37
  gr.Text(label="Gender Probability"),
38
  gr.Text(label="Race"),
39
  gr.Text(label="Dominant Emotion"),
40
- gr.Text(label="Emotion Breakdown")],
41
- examples=[[example_image_1], [example_image_2]] # Adding example images
42
  )
43
 
44
- # Launch the Gradio app
45
  iface.launch()
 
2
  from deepface import DeepFace
3
  from PIL import Image
4
  import numpy as np
 
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  def analyze_face(image):
7
  analysis_result = DeepFace.analyze(img_path=np.array(image), actions=['age', 'gender', 'race', 'emotion'])[0]
8
 
 
12
  race = analysis_result['dominant_race']
13
  emotion = analysis_result['dominant_emotion']
14
 
 
15
  emotions_detail = ', '.join([f"{k}: {v:.2f}%" for k, v in analysis_result['emotion'].items()])
16
 
17
  return age, f"{gender} ({gender_prob})", race, emotion, emotions_detail
18
 
 
19
  iface = gr.Interface(
20
  fn=analyze_face,
21
  inputs=gr.Image(type="pil"),
 
23
  gr.Text(label="Gender Probability"),
24
  gr.Text(label="Race"),
25
  gr.Text(label="Dominant Emotion"),
26
+ gr.Text(label="Emotion Breakdown")]
 
27
  )
28
 
 
29
  iface.launch()