Spaces:
Sleeping
Sleeping
added app.py file
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from deepface import DeepFace
|
| 3 |
+
|
| 4 |
+
# Function to analyze face
|
| 5 |
+
def analyze_face(image):
|
| 6 |
+
try:
|
| 7 |
+
results = DeepFace.analyze(
|
| 8 |
+
img_path = image,
|
| 9 |
+
actions = ['emotion'],
|
| 10 |
+
detector_backend = 'retinaface', # more accurate than opencv
|
| 11 |
+
enforce_detection = False
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
if results is None or len(results) == 0:
|
| 15 |
+
return "Not a Face"
|
| 16 |
+
|
| 17 |
+
outputs = []
|
| 18 |
+
for i, face in enumerate(results):
|
| 19 |
+
emotion = face['dominant_emotion']
|
| 20 |
+
outputs.append(f"Face {i+1}: {emotion.capitalize()}")
|
| 21 |
+
|
| 22 |
+
return "\n".join(outputs)
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return "Not a Face"
|
| 26 |
+
|
| 27 |
+
# Gradio UI
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn = analyze_face,
|
| 30 |
+
inputs = [
|
| 31 |
+
gr.Image(type="filepath", label="Upload or Capture Image")
|
| 32 |
+
],
|
| 33 |
+
outputs = [
|
| 34 |
+
gr.Textbox(label="Prediction")
|
| 35 |
+
],
|
| 36 |
+
title = "DeepFace Emotion Detector",
|
| 37 |
+
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'."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|