File size: 2,239 Bytes
514da58
1c2da80
 
8295315
1c2da80
 
8295315
1c2da80
ba8da1e
8295315
 
1c2da80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514da58
1c2da80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()