faceexpression / App.py
awais0300's picture
Create App.py
1ba3b61 verified
import streamlit as st
from keras.models import load_model
from PIL import Image
import numpy as np
import cv2
# Load model once
@st.cache_resource
def load_expression_model():
return load_model("expression_model.h5")
model = load_expression_model()
# Define class labels (update based on your training)
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
# Resize and preprocess image
def preprocess_image(img):
img = img.convert('L') # convert to grayscale
img = img.resize((48, 48))
img_array = np.array(img)
img_array = img_array / 255.0 # normalize
img_array = np.expand_dims(img_array, axis=0)
img_array = np.expand_dims(img_array, axis=-1)
return img_array
# Streamlit UI
st.title("Facial Expression Classifier 😊😒😠")
st.write("Upload an image and the model will predict the facial expression.")
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, caption="Uploaded Image", use_column_width=True)
with st.spinner('Analyzing...'):
processed_img = preprocess_image(img)
prediction = model.predict(processed_img)
class_index = np.argmax(prediction)
st.success(f"Predicted Expression: **{class_names[class_index]}**")