Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,12 @@ import numpy as np
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import time
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the Keras model (.keras file)
|
| 8 |
-
model = tf.keras.models.load_model("Model.keras")
|
| 9 |
|
| 10 |
# Image parameters for prediction
|
| 11 |
img_height, img_width = 128, 128
|
|
@@ -20,13 +23,27 @@ def predict(img):
|
|
| 20 |
|
| 21 |
# Make prediction using the model
|
| 22 |
prediction = model.predict(img_array)
|
| 23 |
-
label = "The eyes are visibly closed, hinting at a danger ahead for driver." if prediction[0] > 0.5 else "The eyes are visibly open, hinting at
|
| 24 |
|
| 25 |
# Calculate inference time
|
| 26 |
inference_time = time.time() - start_time
|
| 27 |
|
| 28 |
return label, round(inference_time, 4) # Return label and inference time
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Streamlit UI
|
| 31 |
st.title("Eye State Prediction")
|
| 32 |
st.write("Team 18: Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Sidhartha Karjee")
|
|
@@ -45,3 +62,6 @@ if uploaded_image is not None:
|
|
| 45 |
# Display results
|
| 46 |
st.write(f"Prediction: **{label}**")
|
| 47 |
st.write(f"Inference Time: **{inference_time} seconds**")
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import time
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
import tempfile
|
| 8 |
+
import base64
|
| 9 |
|
| 10 |
# Load the Keras model (.keras file)
|
| 11 |
+
model = tf.keras.models.load_model("Model.keras")
|
| 12 |
|
| 13 |
# Image parameters for prediction
|
| 14 |
img_height, img_width = 128, 128
|
|
|
|
| 23 |
|
| 24 |
# Make prediction using the model
|
| 25 |
prediction = model.predict(img_array)
|
| 26 |
+
label = "The eyes are visibly closed, hinting at a danger ahead for the driver." if prediction[0] > 0.5 else "The eyes are visibly open, hinting at the driver's alertness."
|
| 27 |
|
| 28 |
# Calculate inference time
|
| 29 |
inference_time = time.time() - start_time
|
| 30 |
|
| 31 |
return label, round(inference_time, 4) # Return label and inference time
|
| 32 |
|
| 33 |
+
# Function to convert text to speech and auto-play
|
| 34 |
+
def speak_auto(text):
|
| 35 |
+
tts = gTTS(text=text, lang='en')
|
| 36 |
+
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
|
| 37 |
+
tts.save(fp.name)
|
| 38 |
+
audio_bytes = fp.read()
|
| 39 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
| 40 |
+
audio_html = f"""
|
| 41 |
+
<audio autoplay>
|
| 42 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
| 43 |
+
</audio>
|
| 44 |
+
"""
|
| 45 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
# Streamlit UI
|
| 48 |
st.title("Eye State Prediction")
|
| 49 |
st.write("Team 18: Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Sidhartha Karjee")
|
|
|
|
| 62 |
# Display results
|
| 63 |
st.write(f"Prediction: **{label}**")
|
| 64 |
st.write(f"Inference Time: **{inference_time} seconds**")
|
| 65 |
+
|
| 66 |
+
# Convert the result into speech and play it
|
| 67 |
+
speak_auto(f"{label} Inference time: {inference_time} seconds.")
|