|
import gradio as gr |
|
from deepface import DeepFace |
|
import tempfile |
|
import os |
|
import traceback |
|
|
|
def analyze_image(image): |
|
try: |
|
|
|
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: |
|
image.save(temp_file.name) |
|
temp_path = temp_file.name |
|
|
|
|
|
result = DeepFace.analyze( |
|
img_path=temp_path, |
|
actions=["emotion", "gender"], |
|
enforce_detection=False |
|
)[0] |
|
|
|
os.remove(temp_path) |
|
|
|
emotion = result.get("dominant_emotion", "Unknown") |
|
gender = result.get("dominant_gender", "Unknown") |
|
|
|
return f"Gender: {gender}\nEmotion: {emotion}" |
|
|
|
except Exception as e: |
|
tb = traceback.format_exc() |
|
return f"Error occurred:\n{e}\n\nTraceback:\n{tb}" |
|
|
|
demo = gr.Interface( |
|
fn=analyze_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Textbox(label="Prediction"), |
|
title="DeepFace: Emotion & Gender Detection", |
|
description="Upload a clear face image. Model predicts gender and emotion." |
|
) |
|
|
|
demo.launch() |
|
|