Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,68 @@
|
|
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
|
8 |
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
|
9 |
-
|
10 |
st.title("Emotion Recognition App")
|
11 |
|
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 |
-
|
19 |
-
model_path = "emotion_model.onnx"
|
20 |
return ort.InferenceSession(model_path)
|
21 |
|
22 |
-
# Load the model
|
23 |
emotion_model = load_model()
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
|
28 |
-
# Preprocess image to match model input requirements
|
29 |
def preprocess_image(image):
|
30 |
-
|
31 |
-
|
32 |
-
image_resized = cv2.resize(
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
return
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if uploaded_file is not None:
|
40 |
-
# Open and
|
41 |
-
image = Image.open(uploaded_file)
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
# Display the
|
51 |
-
st.
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import onnxruntime as ort
|
3 |
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
|
7 |
+
# Set page configuration
|
8 |
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
|
|
|
9 |
st.title("Emotion Recognition App")
|
10 |
|
|
|
|
|
|
|
11 |
# Load the ONNX model using onnxruntime
|
12 |
@st.cache_resource
|
13 |
def load_model():
|
14 |
+
model_path = "onnx_model.onnx" # Ensure this is the correct path to your uploaded ONNX model
|
|
|
15 |
return ort.InferenceSession(model_path)
|
16 |
|
17 |
+
# Load the emotion detection model
|
18 |
emotion_model = load_model()
|
19 |
|
20 |
+
# Process the uploaded image
|
21 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
22 |
|
|
|
23 |
def preprocess_image(image):
|
24 |
+
"""Preprocess image to match model input requirements"""
|
25 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
26 |
+
image_resized = cv2.resize(image, (224, 224)) # Resize image to model input size
|
27 |
+
image_input = np.transpose(image_resized, (2, 0, 1)) # Change data format for the model
|
28 |
+
image_input = image_input.astype(np.float32) / 255.0 # Normalize the image
|
29 |
+
image_input = np.expand_dims(image_input, axis=0) # Add batch dimension
|
30 |
+
return image_input
|
31 |
|
32 |
+
def predict_emotion(image_input):
|
33 |
+
"""Run inference and predict the emotion"""
|
34 |
+
input_name = emotion_model.get_inputs()[0].name
|
35 |
+
output_name = emotion_model.get_outputs()[0].name
|
36 |
+
prediction = emotion_model.run([output_name], {input_name: image_input})
|
37 |
+
emotion = np.argmax(prediction[0]) # Get the class with the highest probability
|
38 |
+
return emotion
|
39 |
+
|
40 |
+
# Define a function to display emotion text
|
41 |
+
def display_emotion(emotion):
|
42 |
+
"""Map emotion index to a human-readable emotion"""
|
43 |
+
emotion_map = {
|
44 |
+
0: "Anger",
|
45 |
+
1: "Disgust",
|
46 |
+
2: "Fear",
|
47 |
+
3: "Happiness",
|
48 |
+
4: "Sadness",
|
49 |
+
5: "Surprise",
|
50 |
+
6: "Neutral"
|
51 |
+
}
|
52 |
+
return emotion_map.get(emotion, "Unknown")
|
53 |
+
|
54 |
+
# If an image is uploaded
|
55 |
if uploaded_file is not None:
|
56 |
+
# Open and display the uploaded image
|
57 |
+
image = Image.open(uploaded_file)
|
58 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
59 |
|
60 |
+
# Preprocess the image
|
61 |
+
image_input = preprocess_image(image)
|
62 |
+
|
63 |
+
# Predict the emotion
|
64 |
+
emotion = predict_emotion(image_input)
|
65 |
+
emotion_label = display_emotion(emotion)
|
66 |
|
67 |
+
# Display the predicted emotion
|
68 |
+
st.write(f"Detected Emotion: {emotion_label}")
|