Ahmadkhan12 commited on
Commit
5cde790
·
verified ·
1 Parent(s): d71fc08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -7,6 +7,14 @@ import streamlit as st
7
  # Load ONNX model
8
  onnx_model = ort.InferenceSession("onnx_model.onnx")
9
 
 
 
 
 
 
 
 
 
10
  # Preprocess image function
11
  def preprocess_image(image):
12
  """Preprocess image to match model input requirements"""
@@ -30,7 +38,14 @@ def predict_emotion_onnx(onnx_model, image_input):
30
  input_name = onnx_model.get_inputs()[0].name
31
  output_name = onnx_model.get_outputs()[0].name
32
  prediction = onnx_model.run([output_name], {input_name: image_input})
33
- return prediction
 
 
 
 
 
 
 
34
 
35
  # Streamlit interface
36
  st.title("Emotion Recognition with ONNX")
@@ -45,7 +60,8 @@ if uploaded_file is not None:
45
  image_input = preprocess_image(image)
46
 
47
  # Predict the emotion
48
- emotion_prediction = predict_emotion_onnx(onnx_model, image_input)
49
 
50
- # Display the prediction
51
- st.write(f"Predicted Emotion: {emotion_prediction[0]}")
 
 
7
  # Load ONNX model
8
  onnx_model = ort.InferenceSession("onnx_model.onnx")
9
 
10
+ # Emotion labels (same as the model's output classes)
11
+ emotion_labels = ["Anger", "Disgust", "Fear", "Happy", "Sadness", "Surprise", "Neutral"]
12
+
13
+ # Softmax function to convert logits to probabilities
14
+ def softmax(logits):
15
+ exp_logits = np.exp(logits - np.max(logits)) # Stability trick
16
+ return exp_logits / np.sum(exp_logits)
17
+
18
  # Preprocess image function
19
  def preprocess_image(image):
20
  """Preprocess image to match model input requirements"""
 
38
  input_name = onnx_model.get_inputs()[0].name
39
  output_name = onnx_model.get_outputs()[0].name
40
  prediction = onnx_model.run([output_name], {input_name: image_input})
41
+
42
+ # Apply softmax to the output logits
43
+ probabilities = softmax(prediction[0][0]) # We assume batch size of 1
44
+
45
+ # Get the predicted emotion label (index of the highest probability)
46
+ predicted_class = np.argmax(probabilities)
47
+
48
+ return emotion_labels[predicted_class], probabilities[predicted_class]
49
 
50
  # Streamlit interface
51
  st.title("Emotion Recognition with ONNX")
 
60
  image_input = preprocess_image(image)
61
 
62
  # Predict the emotion
63
+ emotion_label, probability = predict_emotion_onnx(onnx_model, image_input)
64
 
65
+ # Display the predicted emotion and probability
66
+ st.write(f"Predicted Emotion: {emotion_label}")
67
+ st.write(f"Confidence: {probability:.2f}")