Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,32 @@
|
|
1 |
-
import numpy as np
|
2 |
import streamlit as st
|
|
|
|
|
3 |
from PIL import Image
|
4 |
-
import
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
model
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Define the emotion labels
|
12 |
emotion_labels = ['happy', 'sad', 'neutral']
|
13 |
|
14 |
# Function to preprocess the image for the model
|
15 |
def preprocess_image(image):
|
16 |
-
# Resize the image to match the input size of the model
|
17 |
image = image.resize((48, 48))
|
18 |
-
# Convert the image to grayscale
|
19 |
image = image.convert('L')
|
20 |
-
# Convert the image to a numpy array
|
21 |
image = np.array(image)
|
22 |
-
# Normalize the image
|
23 |
image = image / 255.0
|
24 |
-
# Expand the dimensions to match the input shape of the model
|
25 |
image = np.expand_dims(image, axis=-1)
|
26 |
-
# Expand the dimensions to create a batch of size 1
|
27 |
image = np.expand_dims(image, axis=0)
|
28 |
return image
|
29 |
|
@@ -90,7 +93,7 @@ if image_file is not None:
|
|
90 |
st.markdown(f'<div class="predicted-emotion">Predicted Emotion: {predicted_label}</div>', unsafe_allow_html=True)
|
91 |
|
92 |
# Save the prediction and image to session state
|
93 |
-
image_bytes =
|
94 |
image.save(image_bytes, format='PNG')
|
95 |
st.session_state.predictions.append({
|
96 |
'image': image_bytes.getvalue(),
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
+
# Download the model file from the Hugging Face repository
|
9 |
+
model_url = "https://huggingface.co/Hammad712/Emotion_Detection/resolve/main/emotion_detection_model.h5"
|
10 |
+
response = requests.get(model_url)
|
11 |
+
model_file_path = 'emotion_detection_model.h5'
|
12 |
|
13 |
+
# Save the downloaded model file
|
14 |
+
with open(model_file_path, 'wb') as f:
|
15 |
+
f.write(response.content)
|
16 |
+
|
17 |
+
# Load the saved model
|
18 |
+
model = tf.keras.models.load_model(model_file_path)
|
19 |
|
20 |
# Define the emotion labels
|
21 |
emotion_labels = ['happy', 'sad', 'neutral']
|
22 |
|
23 |
# Function to preprocess the image for the model
|
24 |
def preprocess_image(image):
|
|
|
25 |
image = image.resize((48, 48))
|
|
|
26 |
image = image.convert('L')
|
|
|
27 |
image = np.array(image)
|
|
|
28 |
image = image / 255.0
|
|
|
29 |
image = np.expand_dims(image, axis=-1)
|
|
|
30 |
image = np.expand_dims(image, axis=0)
|
31 |
return image
|
32 |
|
|
|
93 |
st.markdown(f'<div class="predicted-emotion">Predicted Emotion: {predicted_label}</div>', unsafe_allow_html=True)
|
94 |
|
95 |
# Save the prediction and image to session state
|
96 |
+
image_bytes = BytesIO()
|
97 |
image.save(image_bytes, format='PNG')
|
98 |
st.session_state.predictions.append({
|
99 |
'image': image_bytes.getvalue(),
|