teragron commited on
Commit
37a62a4
1 Parent(s): 349d57c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ from deepface import DeepFace
4
+
5
+ def analyze_fn(img_path):
6
+ # Load the image only once for better performance
7
+ img = cv2.imread(img_path)
8
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9
+
10
+ objs = DeepFace.analyze(img_path=img_path, actions=['age', 'gender', 'race', 'emotion'])
11
+ obj = objs[0]
12
+
13
+ age = obj["age"]
14
+ gender = obj["dominant_gender"]
15
+ race = obj["dominant_race"]
16
+ emotion = obj["dominant_emotion"]
17
+
18
+ region = obj["region"]
19
+ x, y, w, h = region["x"], region["y"], region["w"], region["h"]
20
+
21
+ # Draw the rectangle on the image
22
+ cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
23
+
24
+ return [img, age, gender, race, emotion]
25
+
26
+ with gr.Blocks() as demo:
27
+ title = """<p><h1 align="center" style="font-size: 36px;">Facial Attribute Analyzer</h1></p>"""
28
+ gr.HTML(title)
29
+ with gr.Row():
30
+ with gr.Column():
31
+ image = gr.Image(label="Upload Image", type="filepath")
32
+ analyze = gr.Button("Analyze")
33
+ with gr.Column():
34
+ age_box = gr.Textbox(label="Age")
35
+ gender_box = gr.Textbox(label="Gender")
36
+ race_box = gr.Textbox(label="Race")
37
+ emotion_box = gr.Textbox(label="Emotion")
38
+
39
+ analyze.click(fn=analyze_fn, inputs=image, outputs=[image, age_box, gender_box, race_box, emotion_box], api_name="greet")
40
+
41
+ demo.launch()