Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,24 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
from PIL import Image
|
5 |
import streamlit as st
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
# Emotion labels
|
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"""
|
21 |
# Convert the image to grayscale
|
@@ -48,8 +53,9 @@ def predict_emotion_onnx(onnx_model, image_input):
|
|
48 |
return emotion_labels[predicted_class], probabilities[predicted_class]
|
49 |
|
50 |
# Streamlit interface
|
51 |
-
st.title("Emotion Recognition with ONNX")
|
52 |
|
|
|
53 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
54 |
|
55 |
if uploaded_file is not None:
|
@@ -65,3 +71,4 @@ if uploaded_file is not None:
|
|
65 |
# Display the predicted emotion and probability
|
66 |
st.write(f"Predicted Emotion: {emotion_label}")
|
67 |
st.write(f"Confidence: {probability:.2f}")
|
|
|
|
3 |
import cv2
|
4 |
from PIL import Image
|
5 |
import streamlit as st
|
6 |
+
import kagglehub # Import kagglehub to load the AffectNet dataset
|
7 |
|
8 |
+
# Download the AffectNet dataset
|
9 |
+
path = kagglehub.dataset_download("fatihkgg/affectnet-yolo-format")
|
10 |
+
print("Path to AffectNet dataset:", path)
|
11 |
|
12 |
+
# Emotion labels for AffectNet
|
13 |
emotion_labels = ["Anger", "Disgust", "Fear", "Happy", "Sadness", "Surprise", "Neutral"]
|
14 |
|
15 |
+
# Load ONNX model
|
16 |
+
onnx_model = ort.InferenceSession("onnx_model.onnx")
|
17 |
+
|
18 |
# Softmax function to convert logits to probabilities
|
19 |
def softmax(logits):
|
20 |
exp_logits = np.exp(logits - np.max(logits)) # Stability trick
|
21 |
return exp_logits / np.sum(exp_logits)
|
22 |
|
23 |
+
# Preprocess image function for ONNX model
|
24 |
def preprocess_image(image):
|
25 |
"""Preprocess image to match model input requirements"""
|
26 |
# Convert the image to grayscale
|
|
|
53 |
return emotion_labels[predicted_class], probabilities[predicted_class]
|
54 |
|
55 |
# Streamlit interface
|
56 |
+
st.title("Emotion Recognition with ONNX and AffectNet")
|
57 |
|
58 |
+
# File uploader to upload images
|
59 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
60 |
|
61 |
if uploaded_file is not None:
|
|
|
71 |
# Display the predicted emotion and probability
|
72 |
st.write(f"Predicted Emotion: {emotion_label}")
|
73 |
st.write(f"Confidence: {probability:.2f}")
|
74 |
+
|