Update app.py
Browse files
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 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 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
|
| 55 |
-
status_placeholder.markdown(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|