Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the FER model from the current directory (make sure the model is uploaded to Hugging Face Space)
|
| 7 |
+
model = tf.keras.models.load_model("FER_DATA.keras")
|
| 8 |
+
|
| 9 |
+
# Function to predict emotion
|
| 10 |
+
def predict_emotion(image):
|
| 11 |
+
# Convert the image to grayscale as the model expects grayscale images
|
| 12 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 13 |
+
|
| 14 |
+
# Resize image to (48, 48) as expected by the model
|
| 15 |
+
image = cv2.resize(image, (48, 48))
|
| 16 |
+
|
| 17 |
+
# Normalize the image
|
| 18 |
+
image = image / 255.0
|
| 19 |
+
|
| 20 |
+
# Add a channel dimension (model expects a single channel)
|
| 21 |
+
image = np.expand_dims(image, axis=-1)
|
| 22 |
+
|
| 23 |
+
# Add a batch dimension (model expects a batch of images)
|
| 24 |
+
image = np.expand_dims(image, axis=0)
|
| 25 |
+
|
| 26 |
+
# Make a prediction
|
| 27 |
+
prediction = model.predict(image)
|
| 28 |
+
|
| 29 |
+
# Get the predicted emotion
|
| 30 |
+
emotion = np.argmax(prediction)
|
| 31 |
+
emotions = ['Angry', 'Happy', 'Sad', 'Neutral']
|
| 32 |
+
|
| 33 |
+
# Return the predicted emotion
|
| 34 |
+
return emotions[emotion]
|
| 35 |
+
|
| 36 |
+
# Set up Gradio Interface
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=predict_emotion, # Function that makes predictions
|
| 39 |
+
inputs=gr.Image(shape=(224, 224), image_mode="RGB"), # Input is an image
|
| 40 |
+
outputs="text", # Output is a text (emotion)
|
| 41 |
+
title="MoodSync - Emotion Detection", # Title of the app
|
| 42 |
+
description="This app detects emotions from images using a deep learning model.", # Description
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the app
|
| 46 |
+
iface.launch()
|