abhinavyadav11 commited on
Commit
3933506
Β·
verified Β·
1 Parent(s): c4d8620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import cv2
2
  import numpy as np
3
  import streamlit as st
4
  from tensorflow.keras.preprocessing.image import img_to_array
@@ -29,42 +28,31 @@ 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()
 
 
1
  import numpy as np
2
  import streamlit as st
3
  from tensorflow.keras.preprocessing.image import img_to_array
 
28
  st.header("πŸ” Eye Status")
29
  status_placeholder = st.markdown("**Status:** Waiting for webcam input...")
30
 
31
+ # Webcam input using Streamlit's camera_input widget
32
+ if run:
33
+ camera_input = st.camera_input("Capture image")
34
+
35
+ if camera_input:
36
+ # Convert the image to RGB format and resize it for prediction
37
+ img_resized = cv2.resize(camera_input, (IMG_SIZE, IMG_SIZE))
38
+
39
+ # Preprocess the image
40
+ img_array = img_to_array(img_resized) / 255.0
41
+ img_array = np.expand_dims(img_array, axis=0)
42
+
43
+ # Predict eye status
44
+ prediction = model.predict(img_array)
45
+
46
+ # Update prediction status
47
+ if prediction[0][0] > 0.8:
48
+ status = "Eye is Open πŸ‘€"
49
+ status_color = "green"
50
+ else:
51
+ status = "Eye is Closed 😴"
52
+ status_color = "red"
53
+
54
+ # Update UI with the prediction status
55
+ status_placeholder.markdown(f"**Status:** <span style='color:{status_color}'>{status}</span>", unsafe_allow_html=True)
56
+
57
+ # Display the webcam feed
58
+ FRAME_WINDOW.image(camera_input)