dilkushsingh's picture
Update app.py
c2b4128 verified
import gradio as gr
from huggingface_hub import hf_hub_download
from keras.models import load_model
from keras.preprocessing.image import img_to_array
import numpy as np
import os
import tensorflow as tf
# Define custom BatchNormalization layer with get_config() and from_config() methods
class CustomBatchNormalization(tf.keras.layers.BatchNormalization):
def get_config(self):
config = super().get_config()
return config
@classmethod
def from_config(cls, config):
# Extract and convert the axis parameter (assuming it's a list in the config)
if isinstance(config['axis'], list):
config['axis'] = int(config['axis'][0]) # Extract the first element as integer
return cls(**config)
# Define custom Dropout layer with get_config() and from_config() methods
class CustomDropout(tf.keras.layers.Dropout):
def get_config(self):
config = super().get_config()
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# Define custom Flatten layer with get_config() and from_config() methods
class CustomFlatten(tf.keras.layers.Flatten):
def get_config(self):
config = super().get_config()
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# Define custom Dense layer with get_config() and from_config() methods
class CustomDense(tf.keras.layers.Dense):
def get_config(self):
config = super().get_config()
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# Replace with your model repository and filename
model_file = hf_hub_download(
repo_id="dilkushsingh/Facial_Emotion_Recognizer",
filename="ResNet50_Model.h5",
local_dir="./models"
)
# Load the model with custom objects
custom_objects = {
'BatchNormalization': CustomBatchNormalization,
'Dropout': CustomDropout,
'Flatten': CustomFlatten,
'Dense': CustomDense
}
model = load_model(model_file, custom_objects=custom_objects)
# Emotion labels dictionary
emotion_labels = {'angry': 0, 'disgust': 1, 'fear': 2, 'happy': 3, 'neutral': 4, 'sad': 5, 'surprise': 6}
index_to_emotion = {v: k for k, v in emotion_labels.items()}
def prepare_image(img_pil):
"""Preprocess the PIL image to fit model's input requirements."""
# Convert the PIL image to a numpy array with the target size
img = img_pil.resize((224, 224))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
return img_array
# Define the Gradio interface
def predict_emotion(image):
"""Predict emotion from an uploaded image."""
# Preprocess the image
processed_image = prepare_image(image)
# Make prediction using the model
prediction = model.predict(processed_image)
# Get the emotion label with the highest probability
predicted_class = np.argmax(prediction, axis=1)
predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
return predicted_emotion
interface = gr.Interface(
fn=predict_emotion, # Your prediction function
inputs=gr.Image(type="pil"), # Input for uploading an image, directly compatible with PIL images
outputs="text", # Output as text displaying the predicted emotion
title="Facial Emotion Recognizer",
description="Upload an image and see the predicted emotion."
)
# Launch the Gradio interface
interface.launch()