Ahmadkhan12 commited on
Commit
7ffdd20
·
verified ·
1 Parent(s): 6b9468a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -31
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 the page config
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
- # 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)
 
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}")