selva1909's picture
Update app.py
6815f49 verified
import gradio as gr
from deepface import DeepFace
# Function to analyze face
def analyze_face(image):
try:
results = DeepFace.analyze(
img_path = image,
actions = ['emotion'],
detector_backend = 'retinaface', # more accurate than opencv
enforce_detection = False
)
if results is None or len(results) == 0:
return "Not a Face"
outputs = []
for i, face in enumerate(results):
emotion = face['dominant_emotion']
outputs.append(f"Face {i+1}: {emotion.capitalize()}")
return "\n".join(outputs)
except Exception as e:
return "Not a Face"
# Gradio UI
iface = gr.Interface(
fn = analyze_face,
inputs = [
gr.Image(type="filepath", label="Upload or Capture Image")
],
outputs = [
gr.Textbox(label="Prediction")
],
title = "Face Emotion Detector application 😊",
description = "Upload an image or take a photo. The app detects if a face is present, then predicts the dominant emotion. If no face is detected, it returns 'Not a Face'."
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)