Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
from deepface import DeepFace
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
uploaded_file = st.file_uploader("Upload an image for face analysis:", type=["jpg", "png", "jpeg"])
|
| 7 |
-
|
| 8 |
-
if uploaded_file is not None:
|
| 9 |
# Perform DeepFace analysis
|
| 10 |
-
analysis_result = DeepFace.analyze(img_path=
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from deepface import DeepFace
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Function to analyze the uploaded image
|
| 7 |
+
def analyze_face(image):
|
| 8 |
+
# Convert the image to a numpy array
|
| 9 |
+
image_array = np.array(image)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
# Perform DeepFace analysis
|
| 12 |
+
analysis_result = DeepFace.analyze(img_path=image_array, actions=['age', 'gender', 'race', 'emotion'])
|
| 13 |
+
|
| 14 |
+
# Extract results
|
| 15 |
+
age = analysis_result['age']
|
| 16 |
+
gender = analysis_result['gender']
|
| 17 |
+
race = analysis_result['dominant_race']
|
| 18 |
+
emotion = analysis_result['dominant_emotion']
|
| 19 |
+
emotions_detail = analysis_result['emotion']
|
| 20 |
+
|
| 21 |
+
# Return the results
|
| 22 |
+
return age, gender, race, emotion, emotions_detail
|
| 23 |
+
|
| 24 |
+
# Define the Gradio interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=analyze_face,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=[gr.Number(label="Age"),
|
| 29 |
+
gr.Text(label="Gender"),
|
| 30 |
+
gr.Text(label="Race"),
|
| 31 |
+
gr.Text(label="Dominant Emotion"),
|
| 32 |
+
gr.JSON(label="Emotion Breakdown")]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the Gradio app
|
| 36 |
+
iface.launch()
|