|
import cv2 |
|
import numpy as np |
|
import streamlit as st |
|
from tensorflow.keras.preprocessing.image import img_to_array |
|
from tensorflow.keras.models import load_model |
|
|
|
|
|
model = load_model('eye_detection.h5') |
|
IMG_SIZE = 224 |
|
|
|
|
|
st.title("ποΈ Real-Time Eye Detection") |
|
st.write("Detect whether eyes are open or closed in real-time using your webcam.") |
|
|
|
|
|
st.sidebar.title("π§ Controls") |
|
run = st.sidebar.checkbox("Start Webcam") |
|
st.sidebar.write("Toggle the checkbox to start/stop the webcam.") |
|
st.sidebar.write("Press 'Stop' to end the app.") |
|
st.sidebar.info("Tip: Ensure your webcam is properly connected and accessible.") |
|
|
|
|
|
with st.container(): |
|
st.header("πΉ Webcam Feed") |
|
FRAME_WINDOW = st.image([]) |
|
|
|
|
|
with st.container(): |
|
st.header("π Eye Status") |
|
status_placeholder = st.markdown("**Status:** Waiting for webcam input...") |
|
|
|
|
|
cap = cv2.VideoCapture(0) |
|
|
|
while run: |
|
ret, frame = cap.read() |
|
if not ret: |
|
status_placeholder.error("Failed to capture image. Please check your webcam.") |
|
break |
|
|
|
|
|
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
img_resized = cv2.resize(frame_rgb, (IMG_SIZE, IMG_SIZE)) |
|
|
|
|
|
img_array = img_to_array(img_resized) / 255.0 |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
prediction = model.predict(img_array) |
|
|
|
|
|
if prediction[0][0] > 0.8: |
|
status = "Eye is Open π" |
|
status_color = "green" |
|
else: |
|
status = "Eye is Closed π΄" |
|
status_color = "red" |
|
|
|
|
|
status_placeholder.markdown(f"**Status:** <span style='color:{status_color}'>{status}</span>", unsafe_allow_html=True) |
|
|
|
|
|
FRAME_WINDOW.image(frame_rgb) |
|
|
|
|
|
cap.release() |
|
cv2.destroyAllWindows() |
|
|