Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
-
|
|
|
5 |
|
6 |
# Set the page config
|
7 |
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
|
@@ -11,32 +12,40 @@ st.title("Emotion Recognition App")
|
|
11 |
# Upload an image
|
12 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
13 |
|
14 |
-
#
|
15 |
-
@st.cache_resource
|
16 |
def load_model():
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Process the uploaded image
|
22 |
if uploaded_file is not None:
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if predictions:
|
36 |
-
top_prediction = predictions[0] # Assuming the model returns a list of predictions
|
37 |
-
emotion = top_prediction["label"]
|
38 |
-
confidence = top_prediction["score"]
|
39 |
-
|
40 |
-
st.image(image, caption=f"Detected Emotion: {emotion} (Confidence: {confidence:.2f})", use_column_width=True)
|
41 |
-
else:
|
42 |
-
st.warning("Unable to determine emotion. Try another image.")
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
+
import onnxruntime as ort
|
5 |
+
import cv2
|
6 |
|
7 |
# Set the page config
|
8 |
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
|
|
|
12 |
# Upload an image
|
13 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
14 |
|
15 |
+
# Load the ONNX model using onnxruntime
|
16 |
+
@st.cache_resource
|
17 |
def load_model():
|
18 |
+
# Path to the uploaded ONNX model (should be the name of the model file you uploaded)
|
19 |
+
model_path = "emotion_model.onnx"
|
20 |
+
return ort.InferenceSession(model_path)
|
21 |
+
|
22 |
+
# Load the model
|
23 |
+
emotion_model = load_model()
|
24 |
+
|
25 |
+
# Class labels for facial emotions (based on the training dataset)
|
26 |
+
emotion_labels = ['Anger', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise', 'Neutral']
|
27 |
+
|
28 |
+
# Preprocess image to match model input requirements
|
29 |
+
def preprocess_image(image):
|
30 |
+
# Convert image to grayscale and resize to match the input size expected by the model
|
31 |
+
image_gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
32 |
+
image_resized = cv2.resize(image_gray, (64, 64)) # The model expects 64x64 input
|
33 |
+
image_normalized = image_resized / 255.0 # Normalize to [0, 1] range
|
34 |
+
image_reshaped = np.expand_dims(image_normalized, axis=0) # Add batch dimension
|
35 |
+
image_reshaped = np.expand_dims(image_reshaped, axis=0) # Add channel dimension (1 channel for grayscale)
|
36 |
+
return image_reshaped.astype(np.float32)
|
37 |
|
38 |
# Process the uploaded image
|
39 |
if uploaded_file is not None:
|
40 |
+
# Open and preprocess the image
|
41 |
+
image = Image.open(uploaded_file).convert("RGB")
|
42 |
+
processed_image = preprocess_image(image)
|
43 |
+
|
44 |
+
# Perform inference
|
45 |
+
input_name = emotion_model.get_inputs()[0].name
|
46 |
+
outputs = emotion_model.run(None, {input_name: processed_image})
|
47 |
+
predicted_class = np.argmax(outputs[0], axis=1)[0] # Get the index of the highest probability
|
48 |
+
predicted_emotion = emotion_labels[predicted_class]
|
49 |
+
|
50 |
+
# Display the results
|
51 |
+
st.image(image, caption=f"Detected Emotion: {predicted_emotion}", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|