SicarioOtsutsuki's picture
Update app.py
ba8da1e verified
import streamlit as st
from keras.models import load_model
import cv2
import numpy as np
import time
from PIL import Image
# Load the pre-trained model
model = load_model('emotion_detection_model_50epochs.h5', compile=False)
class_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
# Function to preprocess each frame for prediction
def preprocess_frame(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert to grayscale
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(48, 48))
if len(faces) > 0:
(x, y, w, h) = faces[0] # Use the first detected face
face = gray[y:y+h, x:x+w]
face = cv2.resize(face, (48, 48))
face = face.astype('float32') / 255.0 # Normalize pixel values
face = np.expand_dims(face, axis=0)
face = np.expand_dims(face, axis=-1) # Reshape for the model (48,48,1)
return face, (x, y, w, h)
return None, None
# Streamlit UI layout
st.title("Real-time Emotion Detection")
run = st.checkbox('Run Camera')
FRAME_WINDOW = st.image([])
# Start the camera and predict emotion
cap = cv2.VideoCapture(0) # Open default camera (change index if necessary)
if run:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Preprocess the frame
face, face_coords = preprocess_frame(frame)
if face is not None:
# Make emotion prediction
predictions = model.predict(face)
emotion_label = class_labels[np.argmax(predictions)]
# Draw a rectangle around the face and display the emotion label
(x, y, w, h) = face_coords
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(frame, emotion_label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# Convert BGR image to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
FRAME_WINDOW.image(frame_rgb)
# Small delay for smooth output
time.sleep(0.03)
else:
cap.release()
cv2.destroyAllWindows()