File size: 1,353 Bytes
1ba3b61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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]}**")