Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from keras.models import load_model
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load model once
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_expression_model():
|
| 10 |
+
return load_model("expression_model.h5")
|
| 11 |
+
|
| 12 |
+
model = load_expression_model()
|
| 13 |
+
|
| 14 |
+
# Define class labels (update based on your training)
|
| 15 |
+
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
|
| 16 |
+
|
| 17 |
+
# Resize and preprocess image
|
| 18 |
+
def preprocess_image(img):
|
| 19 |
+
img = img.convert('L') # convert to grayscale
|
| 20 |
+
img = img.resize((48, 48))
|
| 21 |
+
img_array = np.array(img)
|
| 22 |
+
img_array = img_array / 255.0 # normalize
|
| 23 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 24 |
+
img_array = np.expand_dims(img_array, axis=-1)
|
| 25 |
+
return img_array
|
| 26 |
+
|
| 27 |
+
# Streamlit UI
|
| 28 |
+
st.title("Facial Expression Classifier ππ’π ")
|
| 29 |
+
st.write("Upload an image and the model will predict the facial expression.")
|
| 30 |
+
|
| 31 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
|
| 32 |
+
|
| 33 |
+
if uploaded_file is not None:
|
| 34 |
+
img = Image.open(uploaded_file)
|
| 35 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 36 |
+
|
| 37 |
+
with st.spinner('Analyzing...'):
|
| 38 |
+
processed_img = preprocess_image(img)
|
| 39 |
+
prediction = model.predict(processed_img)
|
| 40 |
+
class_index = np.argmax(prediction)
|
| 41 |
+
st.success(f"Predicted Expression: **{class_names[class_index]}**")
|