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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import numpy as np
2
  import streamlit as st
 
 
3
  from tensorflow.keras.preprocessing.image import img_to_array
4
  from tensorflow.keras.models import load_model
5
 
@@ -18,32 +20,26 @@ st.sidebar.write("Toggle the checkbox to start/stop the webcam.")
18
  st.sidebar.write("Press 'Stop' to end the app.")
19
  st.sidebar.info("Tip: Ensure your webcam is properly connected and accessible.")
20
 
21
- # Create a container for video feed (first row)
22
- with st.container():
23
- st.header("πŸ“Ή Webcam Feed")
24
- FRAME_WINDOW = st.image([])
25
 
26
- # Create a container for status display (second row)
27
- with st.container():
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"
@@ -51,8 +47,11 @@ if run:
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)
 
 
 
 
1
  import numpy as np
2
  import streamlit as st
3
+ import cv2 # Added import for OpenCV
4
+ from PIL import Image # For image decoding
5
  from tensorflow.keras.preprocessing.image import img_to_array
6
  from tensorflow.keras.models import load_model
7
 
 
20
  st.sidebar.write("Press 'Stop' to end the app.")
21
  st.sidebar.info("Tip: Ensure your webcam is properly connected and accessible.")
22
 
23
+ # Webcam feed and status placeholders
24
+ FRAME_WINDOW = st.image([])
25
+ status_placeholder = st.markdown("**Status:** Waiting for webcam input...")
 
26
 
 
 
 
 
 
 
27
  if run:
28
+ # Capture an image from the webcam
29
  camera_input = st.camera_input("Capture image")
 
 
 
 
30
 
31
+ if camera_input:
32
+ # Decode to PIL Image and convert to RGB
33
+ img = Image.open(camera_input).convert('RGB')
34
+ # Resize and convert to array
35
+ img_resized = img.resize((IMG_SIZE, IMG_SIZE))
36
  img_array = img_to_array(img_resized) / 255.0
37
  img_array = np.expand_dims(img_array, axis=0)
38
 
39
  # Predict eye status
40
  prediction = model.predict(img_array)
41
 
42
+ # Determine status and color
43
  if prediction[0][0] > 0.8:
44
  status = "Eye is Open πŸ‘€"
45
  status_color = "green"
 
47
  status = "Eye is Closed 😴"
48
  status_color = "red"
49
 
50
+ # Update UI
51
+ status_placeholder.markdown(
52
+ f"**Status:** <span style='color:{status_color}'>{status}</span>",
53
+ unsafe_allow_html=True
54
+ )
55
+ FRAME_WINDOW.image(img)
56
+ else:
57
+ st.write("Webcam is stopped. Check the sidebar to start.")