abhinavyadav11 commited on
Commit
bb3e999
Β·
verified Β·
1 Parent(s): 4a5b7f8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import streamlit as st
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
+ from tensorflow.keras.models import load_model
6
+
7
+ # Load your trained model
8
+ model = load_model('/Users/abhinavyadav/Downloads/DSMP 1.0/eye_detection.h5')
9
+ IMG_SIZE = 224 # Resize the image to the input size of your model (e.g., 224x224)
10
+
11
+ # Streamlit App Title
12
+ st.title("πŸ‘οΈ Real-Time Eye Detection")
13
+ st.write("Detect whether eyes are open or closed in real-time using your webcam.")
14
+
15
+ # Sidebar
16
+ st.sidebar.title("πŸ”§ Controls")
17
+ run = st.sidebar.checkbox("Start Webcam")
18
+ st.sidebar.write("Toggle the checkbox to start/stop the webcam.")
19
+ st.sidebar.write("Press 'Stop' to end the app.")
20
+ st.sidebar.info("Tip: Ensure your webcam is properly connected and accessible.")
21
+
22
+ # Create a container for video feed (first row)
23
+ with st.container():
24
+ st.header("πŸ“Ή Webcam Feed")
25
+ FRAME_WINDOW = st.image([])
26
+
27
+ # Create a container for status display (second row)
28
+ with st.container():
29
+ st.header("πŸ” Eye Status")
30
+ status_placeholder = st.markdown("**Status:** Waiting for webcam input...")
31
+
32
+ # Initialize webcam
33
+ cap = cv2.VideoCapture(0)
34
+
35
+ while run:
36
+ ret, frame = cap.read()
37
+ if not ret:
38
+ status_placeholder.error("Failed to capture image. Please check your webcam.")
39
+ break
40
+
41
+ # Convert frame to RGB
42
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
43
+
44
+ # Resize the frame for model input
45
+ img_resized = cv2.resize(frame_rgb, (IMG_SIZE, IMG_SIZE))
46
+
47
+ # Preprocess the image
48
+ img_array = img_to_array(img_resized) / 255.0
49
+ img_array = np.expand_dims(img_array, axis=0)
50
+
51
+ # Predict eye status
52
+ prediction = model.predict(img_array)
53
+
54
+ # Update prediction status
55
+ if prediction[0][0] > 0.8:
56
+ status = "Eye is Open πŸ‘€"
57
+ status_color = "green"
58
+ else:
59
+ status = "Eye is Closed 😴"
60
+ status_color = "red"
61
+
62
+ # Update UI with the prediction status
63
+ status_placeholder.markdown(f"**Status:** <span style='color:{status_color}'>{status}</span>", unsafe_allow_html=True)
64
+
65
+ # Display the video feed
66
+ FRAME_WINDOW.image(frame_rgb)
67
+
68
+ # Release resources when the checkbox is unchecked
69
+ cap.release()
70
+ cv2.destroyAllWindows()