Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from deepface import DeepFace | |
| from PIL import Image | |
| import numpy as np | |
| def analyze_face(image): | |
| analysis_result = DeepFace.analyze(img_path=np.array(image), actions=['age', 'gender', 'race', 'emotion'])[0] | |
| age = analysis_result['age'] | |
| gender = max(analysis_result['gender'], key=analysis_result['gender'].get) | |
| gender_prob = f"{analysis_result['gender'][gender]:.2f}%" | |
| race = analysis_result['dominant_race'] | |
| emotion = analysis_result['dominant_emotion'] | |
| emotions_detail = ', '.join([f"{k}: {v:.2f}%" for k, v in analysis_result['emotion'].items()]) | |
| return age, f"{gender} ({gender_prob})", race, emotion, emotions_detail | |
| iface = gr.Interface( | |
| fn=analyze_face, | |
| inputs=gr.Image(type="pil"), | |
| outputs=[gr.Number(label="Age"), | |
| gr.Text(label="Gender Probability"), | |
| gr.Text(label="Race"), | |
| gr.Text(label="Dominant Emotion"), | |
| gr.Text(label="Emotion Breakdown")] | |
| ) | |
| iface.launch() |