Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load your trained model
|
| 8 |
+
model = load_model("model.h5")
|
| 9 |
+
|
| 10 |
+
# Define class names (update according to your model)
|
| 11 |
+
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
|
| 12 |
+
|
| 13 |
+
# Prediction function
|
| 14 |
+
def predict_expression(image):
|
| 15 |
+
image = image.convert("L") # Convert to grayscale
|
| 16 |
+
image = image.resize((48, 48)) # Resize to match model input
|
| 17 |
+
img_array = np.array(image) / 255.0 # Normalize
|
| 18 |
+
img_array = img_array.reshape(1, 48, 48, 1) # Add batch and channel dims
|
| 19 |
+
|
| 20 |
+
prediction = model.predict(img_array)
|
| 21 |
+
class_index = np.argmax(prediction)
|
| 22 |
+
confidence = np.max(prediction)
|
| 23 |
+
|
| 24 |
+
return f"Expression: {class_names[class_index]} ({confidence:.2%} confidence)"
|
| 25 |
+
|
| 26 |
+
# Gradio Interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=predict_expression,
|
| 29 |
+
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="Facial Expression Classifier",
|
| 32 |
+
description="Upload a face image and get the predicted emotion"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
iface.launch()
|