Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,62 @@
|
|
1 |
-
from
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
|
11 |
-
prompt = prompt["generated_text"]
|
12 |
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
app.launch()
|
|
|
1 |
+
from deepface import DeepFace
|
2 |
+
import pandas as pd
|
3 |
import gradio as gr
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
|
8 |
+
def faceAnalyzer(image_path):
|
9 |
+
def analyze(image_path, attribute):
|
10 |
+
analysis = DeepFace.analyze(img_path=image_path, actions=['gender', 'race', 'emotion', 'age'])
|
11 |
+
df = pd.DataFrame(analysis[0])
|
12 |
+
plot = df[attribute].plot(kind='line', figsize=(9, 5), title=attribute).get_figure()
|
13 |
+
_, temp_filename = tempfile.mkstemp(suffix=".png")
|
14 |
+
plot.savefig(temp_filename, dpi=600)
|
15 |
+
plt.close(plot)
|
16 |
+
return temp_filename
|
17 |
|
18 |
+
attributes = ['gender', 'race', 'emotion']
|
19 |
+
images = [analyze(image_path, attribute) for attribute in attributes]
|
20 |
|
21 |
+
return [gr.Image(image) for attribute, image in zip(attributes, images)]
|
22 |
|
|
|
23 |
|
24 |
+
def faceAnalyzer2(image_path, attribute):
|
25 |
|
26 |
+
analysis = DeepFace.analyze(img_path=image_path, actions=['age', 'gender', 'race', 'emotion'])
|
27 |
+
# convert the resulting dictionary to a DataFrame
|
28 |
+
df = pd.DataFrame(analysis[0])
|
29 |
+
|
30 |
+
if attribute == "gender":
|
31 |
+
gender = df['gender'].plot(kind = 'line', figsize = (9, 5), title = 'Gender').get_figure()
|
32 |
+
return gender
|
33 |
+
|
34 |
+
elif attribute == "race":
|
35 |
+
race = df['race'].plot(kind = 'line', figsize = (9, 5), title = 'Race').get_figure()
|
36 |
+
return race
|
37 |
+
|
38 |
+
elif attribute == "emotion":
|
39 |
+
emotion = df['emotion'].plot(kind = 'line', figsize = (9, 5), title = 'Emotion').get_figure()
|
40 |
+
return emotion
|
41 |
+
|
42 |
+
|
43 |
+
app1 = gr.Interface(faceAnalyzer,
|
44 |
+
inputs=gr.Image(label="Upload Photo"),
|
45 |
+
outputs=[gr.Image(label="Gender Analysis"),
|
46 |
+
gr.Image(label="Race Analysis"),
|
47 |
+
gr.Image(label="Emotion Analysis")],
|
48 |
+
theme=gr.themes.Soft())
|
49 |
+
|
50 |
+
app2 = gr.Interface(faceAnalyzer2,
|
51 |
+
inputs=[gr.Image(label="Upload Photo"),gr.Radio(choices=["gender","race","emotion"],
|
52 |
+
value="gender",
|
53 |
+
label="Attributes",
|
54 |
+
info="Select an attribute")],
|
55 |
+
outputs=gr.Plot(label="Analysis Output"),
|
56 |
+
theme=gr.themes.Soft())
|
57 |
+
|
58 |
+
application = gr.TabbedInterface([app1,app2],["Full Analysis","Select Analysis"],theme=gr.themes.Soft())
|
59 |
+
|
60 |
+
|
61 |
+
application.launch()
|
62 |
|
|