Spaces:
Sleeping
Sleeping
SakibRumu
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastai.vision.all import load_learner, PILImage
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Define the class mapping for the RAF-DB dataset
|
| 7 |
class_mapping = {
|
|
@@ -19,25 +21,37 @@ def get_raf_label(file_path):
|
|
| 19 |
# Use the class mapping to get the label
|
| 20 |
return class_mapping[str(file_path.parent.name)]
|
| 21 |
|
| 22 |
-
# Load the model
|
| 23 |
-
model_path = "
|
| 24 |
-
model =
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Define the emotion classes
|
| 27 |
emotion_classes = list(class_mapping.values()) # Get emotion classes from the class mapping
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Function for Emotion Prediction
|
| 30 |
def predict_emotion(image):
|
| 31 |
-
img =
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
return predicted_emotion, f"{confidence:.2f}%"
|
| 36 |
|
| 37 |
# Gradio interface with xkcd theme
|
| 38 |
with gr.Blocks(theme="gstaff/xkcd") as demo:
|
| 39 |
gr.Markdown("# Emotion Recognition Classifier")
|
| 40 |
-
gr.Markdown("""
|
| 41 |
This app uses a deep learning model to recognize emotions in facial images.
|
| 42 |
The model has been trained on a dataset to classify images into different emotion categories:
|
| 43 |
* Anger
|
|
@@ -45,12 +59,12 @@ with gr.Blocks(theme="gstaff/xkcd") as demo:
|
|
| 45 |
* Happiness
|
| 46 |
* Sadness
|
| 47 |
* Surprise
|
| 48 |
-
*
|
| 49 |
""")
|
| 50 |
-
|
| 51 |
# Upload image widget
|
| 52 |
image_input = gr.Image(type="pil", label="Upload an image of a face")
|
| 53 |
-
|
| 54 |
# Outputs
|
| 55 |
label_output = gr.Textbox(label="Predicted Emotion")
|
| 56 |
confidence_output = gr.Textbox(label="Confidence Percentage")
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import timm
|
| 4 |
+
from torch import nn
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
# Define the class mapping for the RAF-DB dataset
|
| 9 |
class_mapping = {
|
|
|
|
| 21 |
# Use the class mapping to get the label
|
| 22 |
return class_mapping[str(file_path.parent.name)]
|
| 23 |
|
| 24 |
+
# Load the SeresNet50 model using timm
|
| 25 |
+
model_path = "custom_seresnet50_emotion_model.pth" # Replace with your actual .pth model path
|
| 26 |
+
model = timm.create_model('seresnet50', pretrained=False, num_classes=7) # Load the SeresNet50 model with 7 classes
|
| 27 |
+
model.load_state_dict(torch.load(model_path)) # Load the saved model weights
|
| 28 |
+
model.eval() # Set the model to evaluation mode
|
| 29 |
|
| 30 |
# Define the emotion classes
|
| 31 |
emotion_classes = list(class_mapping.values()) # Get emotion classes from the class mapping
|
| 32 |
|
| 33 |
+
# Define the transformation pipeline
|
| 34 |
+
transform = transforms.Compose([
|
| 35 |
+
transforms.Resize((224, 224)), # Resize image to 224x224 (adjust as needed)
|
| 36 |
+
transforms.ToTensor(),
|
| 37 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Standard normalization for models trained on ImageNet
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
# Function for Emotion Prediction
|
| 41 |
def predict_emotion(image):
|
| 42 |
+
img = Image.open(image).convert('RGB') # Convert the uploaded image into RGB
|
| 43 |
+
img = transform(img).unsqueeze(0) # Apply transformations and add batch dimension
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model(img) # Get model output
|
| 46 |
+
_, pred_idx = torch.max(outputs, 1) # Get the predicted class index
|
| 47 |
+
predicted_emotion = emotion_classes[pred_idx.item()]
|
| 48 |
+
confidence = torch.nn.functional.softmax(outputs, dim=1)[0][pred_idx].item() * 100 # Get confidence
|
| 49 |
return predicted_emotion, f"{confidence:.2f}%"
|
| 50 |
|
| 51 |
# Gradio interface with xkcd theme
|
| 52 |
with gr.Blocks(theme="gstaff/xkcd") as demo:
|
| 53 |
gr.Markdown("# Emotion Recognition Classifier")
|
| 54 |
+
gr.Markdown("""
|
| 55 |
This app uses a deep learning model to recognize emotions in facial images.
|
| 56 |
The model has been trained on a dataset to classify images into different emotion categories:
|
| 57 |
* Anger
|
|
|
|
| 59 |
* Happiness
|
| 60 |
* Sadness
|
| 61 |
* Surprise
|
| 62 |
+
* Contempt
|
| 63 |
""")
|
| 64 |
+
|
| 65 |
# Upload image widget
|
| 66 |
image_input = gr.Image(type="pil", label="Upload an image of a face")
|
| 67 |
+
|
| 68 |
# Outputs
|
| 69 |
label_output = gr.Textbox(label="Predicted Emotion")
|
| 70 |
confidence_output = gr.Textbox(label="Confidence Percentage")
|