File size: 2,013 Bytes
bb3e999
 
a020f50
 
bb3e999
 
 
 
90e341a
bb3e999
 
 
 
 
 
 
 
 
 
 
 
 
a020f50
 
 
bb3e999
3933506
a020f50
3933506
 
a020f50
 
 
 
 
3933506
 
 
 
 
 
a020f50
3933506
 
 
 
 
 
 
a020f50
 
 
 
 
 
 
 
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
import numpy as np
import streamlit as st
import cv2                        # Added import for OpenCV
from PIL import Image             # For image decoding
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model

# Load your trained model
model = load_model('eye_detection.h5')
IMG_SIZE = 224  # Resize the image to the input size of your model (e.g., 224x224)

# Streamlit App Title
st.title("πŸ‘οΈ Real-Time Eye Detection")
st.write("Detect whether eyes are open or closed in real-time using your webcam.")

# Sidebar
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.")

# Webcam feed and status placeholders
FRAME_WINDOW = st.image([])
status_placeholder = st.markdown("**Status:** Waiting for webcam input...")

if run:
    # Capture an image from the webcam
    camera_input = st.camera_input("Capture image")

    if camera_input:
        # Decode to PIL Image and convert to RGB
        img = Image.open(camera_input).convert('RGB')
        # Resize and convert to array
        img_resized = img.resize((IMG_SIZE, IMG_SIZE))
        img_array = img_to_array(img_resized) / 255.0
        img_array = np.expand_dims(img_array, axis=0)

        # Predict eye status
        prediction = model.predict(img_array)

        # Determine status and color
        if prediction[0][0] > 0.8:
            status = "Eye is Open πŸ‘€"
            status_color = "green"
        else:
            status = "Eye is Closed 😴"
            status_color = "red"

        # Update UI
        status_placeholder.markdown(
            f"**Status:** <span style='color:{status_color}'>{status}</span>",
            unsafe_allow_html=True
        )
        FRAME_WINDOW.image(img)
else:
    st.write("Webcam is stopped. Check the sidebar to start.")