Spaces:
Sleeping
Sleeping
SakibRumu
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,30 +26,37 @@ model = load_learner(model_path)
|
|
| 26 |
# Define the emotion classes
|
| 27 |
emotion_classes = list(class_mapping.values()) # Get emotion classes from the class mapping
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Upload an image
|
| 33 |
-
file = st.file_uploader("Upload an image of a face", type=["jpeg", "jpg", "png"])
|
| 34 |
-
|
| 35 |
-
if file is None:
|
| 36 |
-
st.write("Please upload an image to detect the emotion.")
|
| 37 |
-
else:
|
| 38 |
-
# Display the uploaded image
|
| 39 |
-
image = Image.open(file)
|
| 40 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 41 |
-
|
| 42 |
-
# Convert the image to a format that the model can accept
|
| 43 |
-
img = PILImage.create(file)
|
| 44 |
-
|
| 45 |
-
# Predict the emotion
|
| 46 |
-
st.write("Classifying the emotion...")
|
| 47 |
pred_class, pred_idx, outputs = model.predict(img)
|
| 48 |
-
|
| 49 |
-
# Get the predicted label and confidence
|
| 50 |
predicted_emotion = emotion_classes[pred_idx]
|
| 51 |
confidence = outputs[pred_idx] * 100 # Convert to percentage
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = PILImage.create(image) # Convert the uploaded image into a PIL image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
pred_class, pred_idx, outputs = model.predict(img)
|
|
|
|
|
|
|
| 33 |
predicted_emotion = emotion_classes[pred_idx]
|
| 34 |
confidence = outputs[pred_idx] * 100 # Convert to percentage
|
| 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
|
| 44 |
+
* Fear
|
| 45 |
+
* Happiness
|
| 46 |
+
* Sadness
|
| 47 |
+
* Surprise
|
| 48 |
+
* Neutral
|
| 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")
|
| 57 |
+
|
| 58 |
+
# Button to predict the emotion
|
| 59 |
+
image_input.upload(predict_emotion, image_input, [label_output, confidence_output])
|
| 60 |
+
|
| 61 |
+
# Launch the app
|
| 62 |
+
demo.launch(share=True)
|