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