Spaces:
Sleeping
Sleeping
# app.py | |
import streamlit as st | |
import cv2 | |
import numpy as np | |
import tensorflow as tf | |
import time | |
import os | |
# --- Streamlit Page Configuration (MUST BE THE FIRST STREAMLIT COMMAND) --- | |
st.set_page_config(page_title="Real-time Emotion Recognition", layout="wide") | |
# --- 1. Load Model and Face Detector (Cached for Performance) --- | |
def load_emotion_model(): | |
model_path = 'models/emotion_model_best.h5' # Path to your trained model | |
if not os.path.exists(model_path): | |
st.error(f"Error: Model file not found at {model_path}. Please ensure training was successful and the file exists.") | |
st.stop() | |
try: | |
model = tf.keras.models.load_model(model_path) | |
return model | |
except Exception as e: | |
st.error(f"Error loading model from {model_path}: {e}") | |
st.stop() | |
def load_face_detector(): | |
cascade_path = 'haarcascade_frontalface_default.xml' # Path to your Haar Cascade file | |
if not os.path.exists(cascade_path): | |
st.error(f"Error: Haar Cascade file not found at {cascade_path}.") | |
st.markdown("Please download `haarcascade_frontalface_default.xml` from:") | |
st.markdown("[https://github.com/opencv/opencv/blob/4.x/data/haarcascades/haarcascade_frontalface_default.xml](https://github.com/opencv/opencv/blob/4.x/data/haarcascades/haarcascade_frontalface_default.xml)") | |
st.markdown("And place it in a `cascades` folder next to `app.py`.") | |
st.stop() | |
face_cascade = cv2.CascadeClassifier(cascade_path) | |
if face_cascade.empty(): | |
st.error(f"Error: Could not load Haar Cascade classifier from {cascade_path}. Check file integrity.") | |
st.stop() | |
return face_cascade | |
# Load the model and face detector when the app starts | |
model = load_emotion_model() | |
face_detector = load_face_detector() | |
# --- 2. Define Constants and Labels --- | |
IMG_HEIGHT = 48 | |
IMG_WIDTH = 48 | |
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise'] | |
label_colors = { | |
'angry': (0, 0, 255), # BGR Red | |
'disgust': (0, 165, 255), # BGR Orange | |
'fear': (0, 255, 255), # BGR Yellow | |
'happy': (0, 255, 0), # BGR Green | |
'neutral': (255, 255, 0), # BGR Cyan | |
'sad': (255, 0, 0), # BGR Blue | |
'surprise': (255, 0, 255) # BGR Magenta | |
} | |
# --- 3. Streamlit App Layout --- | |
st.title("Live Facial Emotion Recognition") | |
st.markdown(""" | |
This application uses a deep learning model (trained on FER-2013) to detect emotions from faces in real-time. | |
It requires access to your computer's webcam. | |
""") | |
stframe = st.empty() | |
st_status = st.empty() | |
col1, col2 = st.columns([1,1]) | |
with col1: | |
start_button = st.button("Start Camera", key="start_camera") | |
with col2: | |
stop_button = st.button("Stop Camera", key="stop_camera") | |
# Initialize session state for camera control and performance tracking | |
if "camera_started" not in st.session_state: | |
st.session_state.camera_started = False | |
if "cap" not in st.session_state: | |
st.session_state.cap = None | |
if "last_process_time" not in st.session_state: | |
st.session_state.last_process_time = 0.0 | |
# --- Performance Configuration --- | |
DESIRED_FPS = 15 # Aim for 15 frames per second for processing | |
FRAME_INTERVAL_SECONDS = 1.0 / DESIRED_FPS | |
FACE_DETECTION_DOWNSCALE = 0.5 # Scale factor for face detection (e.g., 0.5 means half size) | |
# --- 4. Main Camera Loop Logic --- | |
if start_button: | |
st.session_state.camera_started = True | |
if stop_button: | |
st.session_state.camera_started = False | |
st_status.info("Camera stopped.") | |
if st.session_state.cap is not None and st.session_state.cap.isOpened(): | |
st.session_state.cap.release() | |
st.session_session.cap = None | |
stframe.empty() | |
# Updated: use_container_width instead of use_column_width | |
stframe.image(np.zeros((480, 640, 3), dtype=np.uint8), channels="RGB", use_container_width=True) | |
if st.session_state.camera_started: | |
st_status.info("Starting camera... Please allow camera access if prompted.") | |
if st.session_state.cap is None or not st.session_state.cap.isOpened(): | |
st.session_state.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) | |
if not st.session_state.cap.isOpened(): | |
st_status.error("Failed to open camera. Please check if it's connected and not in use.") | |
st.session_state.camera_started = False | |
st.stop() | |
while st.session_state.camera_started: | |
ret, frame = st.session_state.cap.read() | |
if not ret: | |
st_status.error("Failed to read frame from camera. It might be disconnected or an error occurred.") | |
st.session_state.camera_started = False | |
break | |
current_time = time.time() | |
if current_time - st.session_state.last_process_time >= FRAME_INTERVAL_SECONDS: | |
st.session_state.last_process_time = current_time | |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
small_frame = cv2.resize(gray_frame, (0, 0), fx=FACE_DETECTION_DOWNSCALE, fy=FACE_DETECTION_DOWNSCALE) | |
faces = face_detector.detectMultiScale(small_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) | |
original_faces = [] | |
for (x, y, w, h) in faces: | |
x_orig = int(x / FACE_DETECTION_DOWNSCALE) | |
y_orig = int(y / FACE_DETECTION_DOWNSCALE) | |
w_orig = int(w / FACE_DETECTION_DOWNSCALE) | |
h_orig = int(h / FACE_DETECTION_DOWNSCALE) | |
original_faces.append((x_orig, y_orig, w_orig, h_orig)) | |
for (x, y, w, h) in original_faces: | |
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) | |
face_roi = gray_frame[max(0, y):min(gray_frame.shape[0], y+h), max(0, x):min(gray_frame.shape[1], x+w)] | |
if face_roi.size == 0: | |
continue | |
face_roi = cv2.resize(face_roi, (IMG_WIDTH, IMG_HEIGHT)) | |
face_roi = np.expand_dims(face_roi, axis=0) | |
face_roi = np.expand_dims(face_roi, axis=-1) | |
face_roi = face_roi / 255.0 | |
predictions = model.predict(face_roi, verbose=0)[0] | |
emotion_index = np.argmax(predictions) | |
predicted_emotion = emotion_labels[emotion_index] | |
confidence = predictions[emotion_index] * 100 | |
text_color = label_colors.get(predicted_emotion, (255, 255, 255)) | |
text = f"{predicted_emotion} ({confidence:.2f}%)" | |
text_y = y - 10 if y - 10 > 10 else y + h + 20 | |
cv2.putText(frame, text, (x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.9, text_color, 2, cv2.LINE_AA) | |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
# Updated: use_container_width instead of use_column_width | |
stframe.image(frame_rgb, channels="RGB", use_container_width=True) | |
time.sleep(0.001) # Small sleep to yield control, can be adjusted or removed | |
if st.session_state.cap is not None and st.session_state.cap.isOpened(): | |
st.session_state.cap.release() | |
st.session_state.cap = None | |
st_status.info("Camera released.") |