Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Load the saved model
|
| 7 |
+
from tensorflow.keras.models import load_model
|
| 8 |
+
|
| 9 |
+
# Load the model architecture and weights
|
| 10 |
+
model = load_model('/content/drive/MyDrive/ICT Project/Emotion_detection.h5')
|
| 11 |
+
|
| 12 |
+
# Define the emotion labels
|
| 13 |
+
emotion_labels = ['happy', 'sad', 'neutral']
|
| 14 |
+
|
| 15 |
+
# Function to preprocess the image for the model
|
| 16 |
+
def preprocess_image(image):
|
| 17 |
+
# Resize the image to match the input size of the model
|
| 18 |
+
image = image.resize((48, 48))
|
| 19 |
+
# Convert the image to grayscale
|
| 20 |
+
image = image.convert('L')
|
| 21 |
+
# Convert the image to a numpy array
|
| 22 |
+
image = np.array(image)
|
| 23 |
+
# Normalize the image
|
| 24 |
+
image = image / 255.0
|
| 25 |
+
# Expand the dimensions to match the input shape of the model
|
| 26 |
+
image = np.expand_dims(image, axis=-1)
|
| 27 |
+
# Expand the dimensions to create a batch of size 1
|
| 28 |
+
image = np.expand_dims(image, axis=0)
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
# Custom CSS for styling
|
| 32 |
+
st.markdown("""
|
| 33 |
+
<style>
|
| 34 |
+
.title {
|
| 35 |
+
font-size: 3em;
|
| 36 |
+
color: #4CAF50; /* Green */
|
| 37 |
+
text-align: center;
|
| 38 |
+
}
|
| 39 |
+
.description {
|
| 40 |
+
font-size: 1.2em;
|
| 41 |
+
color: #777777; /* Gray */
|
| 42 |
+
text-align: center;
|
| 43 |
+
}
|
| 44 |
+
.header {
|
| 45 |
+
font-size: 1.5em;
|
| 46 |
+
color: #ff6f61; /* Red */
|
| 47 |
+
}
|
| 48 |
+
.predicted-emotion {
|
| 49 |
+
font-size: 1.5em;
|
| 50 |
+
color: #1e90ff; /* Blue */
|
| 51 |
+
}
|
| 52 |
+
.spinner-text {
|
| 53 |
+
font-size: 1.2em;
|
| 54 |
+
color: #ffa500; /* Orange */
|
| 55 |
+
}
|
| 56 |
+
</style>
|
| 57 |
+
""", unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
# Title and description
|
| 60 |
+
st.markdown('<div class="title">Emotion Detection</div>', unsafe_allow_html=True)
|
| 61 |
+
st.markdown('<div class="description">Upload an image to detect emotions</div>', unsafe_allow_html=True)
|
| 62 |
+
|
| 63 |
+
# File uploader for images
|
| 64 |
+
st.markdown('<div class="header">Upload Image</div>', unsafe_allow_html=True)
|
| 65 |
+
image_file = st.file_uploader("Choose an image", type=["png", 'jpg'])
|
| 66 |
+
|
| 67 |
+
# Sidebar to show previous predictions
|
| 68 |
+
if 'predictions' not in st.session_state:
|
| 69 |
+
st.session_state.predictions = []
|
| 70 |
+
|
| 71 |
+
st.sidebar.header("Previous Predictions")
|
| 72 |
+
for pred in st.session_state.predictions:
|
| 73 |
+
st.sidebar.image(pred['image'], caption=pred['emotion'], use_column_width=True)
|
| 74 |
+
|
| 75 |
+
# Display the uploaded image and predict emotions
|
| 76 |
+
if image_file is not None:
|
| 77 |
+
image = Image.open(image_file)
|
| 78 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 79 |
+
|
| 80 |
+
with st.spinner('Processing...'):
|
| 81 |
+
st.markdown('<div class="spinner-text">Processing...</div>', unsafe_allow_html=True)
|
| 82 |
+
|
| 83 |
+
# Preprocess the image
|
| 84 |
+
preprocessed_image = preprocess_image(image)
|
| 85 |
+
|
| 86 |
+
# Make predictions
|
| 87 |
+
predictions = model.predict(preprocessed_image)
|
| 88 |
+
predicted_label = emotion_labels[np.argmax(predictions)]
|
| 89 |
+
|
| 90 |
+
# Display the predicted emotion
|
| 91 |
+
st.markdown(f'<div class="predicted-emotion">Predicted Emotion: {predicted_label}</div>', unsafe_allow_html=True)
|
| 92 |
+
|
| 93 |
+
# Save the prediction and image to session state
|
| 94 |
+
image_bytes = io.BytesIO()
|
| 95 |
+
image.save(image_bytes, format='PNG')
|
| 96 |
+
st.session_state.predictions.append({
|
| 97 |
+
'image': image_bytes.getvalue(),
|
| 98 |
+
'emotion': predicted_label
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
# Update the sidebar with the new prediction
|
| 102 |
+
st.sidebar.image(image_bytes.getvalue(), caption=predicted_label, use_column_width=True)
|