Himanshu2003's picture
Create app.py
619cfae verified
import streamlit as st
from PIL import Image
import numpy as np
import cv2
from tensorflow.keras.models import load_model
import os
# Ensure the 'upload' directory exists
upload_folder = 'uploads'
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
# Load the pre-trained model
model = load_model("emotion_detector.keras")
def get_result(img_path):
img = cv2.imread(img_path)
img_resize = cv2.resize(img, (224, 224))
img_resize = np.array(img_resize, dtype=np.float32)
img_resize /= 255.0
img_input = img_resize.reshape(1, 224, 224, 3)
prediction = model.predict(img_input)
emotion_dict = {0: 'angry 😡',
1: 'disgust 🤢',
2: 'fear 😱',
3: 'happy 😀',
4: 'neutral 😐',
5: 'sad 😢',
6: 'surprise 😲'}
max_index = np.argmax(np.array(prediction[0]))
pred=int(np.round(prediction[0][max_index]))
emotion = emotion_dict[max_index]
return f"He/she is feeling {emotion}"
st.title("Let\'s detect the Emotion 😀 😢 😡 😱 🤢 😲 😐 ")
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
image = Image.open(uploaded_image)
image_path = os.path.join(upload_folder, uploaded_image.name)
image.save(image_path)
output = get_result(image_path)
st.write(output)
st.image(image, use_container_width=True)