Spaces:
Runtime error
Runtime error
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() | |