0% found this document useful (0 votes)
4 views

app.py

Uploaded by

mi539919
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

app.py

Uploaded by

mi539919
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import pickle

import cv2
import streamlit as st
from utils import get_face_landmarks
import tempfile
import os

# Load the model


with open('./model', 'rb') as f:
model = pickle.load(f)

emotions = ['HAPPY', 'SAD', 'SURPRISED']

# Streamlit app title


st.title("Real-Time Face Emotion Detection")

# Webcam input
frame_holder = st.empty()

cap = cv2.VideoCapture(0)

if not cap.isOpened():
st.error("Error: Could not open webcam.")
else:
st.text("Press 'q' to stop the webcam feed.")

while True:
ret, frame = cap.read()
if not ret:
st.error("Failed to capture image")
break

# Process frame for face landmarks


face_landmarks = get_face_landmarks(frame, draw=True,
static_image_mode=False)
if face_landmarks:
output = model.predict([face_landmarks])

# Display emotion
cv2.putText(frame,
emotions[int(output[0])],
(10, frame.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2)

# Streamlit displays the video frame


frame_holder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB")

# Stop the loop if 'q' is pressed


if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

st.success("Webcam feed stopped.")

You might also like