CoolFace
Apppublic

sumit74/Emotion_Recognition

sourceHugging Faceupdated 1y agoView on Hugging Face
2likes
app.py44 linesDownload Raw Back to root
1import streamlit as st2import tensorflow as tf3import numpy as np4import cv25from PIL import Image6 7# Load trained model8def load_model():9    return tf.keras.models.load_model("emotion_model.h5", compile=False)10 11model = load_model()12 13# Labels (match training class order)14labels = ['Angry','Disgust','Fear','Happy','Sad','Surprise','Neutral']15 16# Streamlit page setup17st.set_page_config(page_title="Emotion Recognition App", layout='centered')18st.title("Emotion Recognition App")19st.write("Upload a face image and get predicted emotion.")20 21# File uploader22upload_file = st.file_uploader("Choose an image...", type=['jpg','jpeg','png'])23if upload_file is not None:24    image = Image.open(upload_file).convert("RGB")25    st.image(image, caption='Uploaded Image', use_container_width=True)26 27    # Convert to numpy array28    img = np.array(image)29 30    # Resize to 96x96 and keep 3 channels (RGB)31    img_resized = cv2.resize(img, (96, 96))32    img_resized = img_resized / 255.0  # Normalize33    img_resized = np.expand_dims(img_resized, axis=0)  # Shape: (1, 96, 96, 3)34 35    # Prediction36    pred = model.predict(img_resized)37    pred = pred.tolist()   # convert to Python list38 39    # Map prediction to labels40    result = {labels[i]: float(pred[0][i]) for i in range(len(labels))}41    emotion = labels[np.argmax(pred[0])]42 43    st.success(f"Predicted Emotion: {emotion}")44    st.bar_chart(result)