Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
from tensorflow.keras.preprocessing.image import
|
| 5 |
from PIL import Image
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
|
|
|
| 8 |
|
| 9 |
# Load the trained model
|
| 10 |
MODEL_PATH = "image_model.h5"
|
| 11 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
IMG_WIDTH, IMG_HEIGHT = 64, 48
|
| 15 |
|
| 16 |
-
# Class labels
|
| 17 |
CLASS_LABELS = {
|
| 18 |
0: "Normal driving (safe)",
|
| 19 |
1: "Texting - right",
|
|
@@ -28,50 +29,43 @@ CLASS_LABELS = {
|
|
| 28 |
}
|
| 29 |
|
| 30 |
def predict_image(image):
|
| 31 |
-
# Preprocess the image
|
| 32 |
img_array = img_to_array(image)
|
| 33 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 34 |
-
img_array = img_array / 255.0
|
| 35 |
-
|
| 36 |
-
# Make a prediction
|
| 37 |
predictions = model.predict(img_array)
|
| 38 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 39 |
confidence = np.max(predictions)
|
| 40 |
-
|
| 41 |
return CLASS_LABELS[predicted_class], confidence
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
# Generate speech using gTTS
|
| 45 |
tts = gTTS(text=text, lang='en')
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Streamlit app
|
| 54 |
st.title("Driver Distraction Detection")
|
| 55 |
st.markdown("Team18 Image Project : Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Sidhartha Karjee")
|
| 56 |
|
| 57 |
-
# File upload
|
| 58 |
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
| 59 |
|
| 60 |
if uploaded_file is not None:
|
| 61 |
-
# Display the uploaded image
|
| 62 |
image = Image.open(uploaded_file).convert("RGB")
|
| 63 |
resized_image = image.resize((IMG_WIDTH, IMG_HEIGHT))
|
| 64 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 65 |
|
| 66 |
-
# Predict the class
|
| 67 |
with st.spinner("Predicting..."):
|
| 68 |
predicted_class, confidence = predict_image(resized_image)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
prediction_text = f"Class: {predicted_class}\nConfidence: {confidence:.2%}"
|
| 72 |
st.subheader("Prediction")
|
| 73 |
st.write(prediction_text)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
audio_path = speak(prediction_text)
|
| 77 |
-
st.audio(audio_path, format="audio/mp3")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
from PIL import Image
|
| 6 |
from gtts import gTTS
|
| 7 |
import tempfile
|
| 8 |
+
import base64
|
| 9 |
|
| 10 |
# Load the trained model
|
| 11 |
MODEL_PATH = "image_model.h5"
|
| 12 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 13 |
|
| 14 |
+
# Image dimensions
|
| 15 |
IMG_WIDTH, IMG_HEIGHT = 64, 48
|
| 16 |
|
| 17 |
+
# Class labels
|
| 18 |
CLASS_LABELS = {
|
| 19 |
0: "Normal driving (safe)",
|
| 20 |
1: "Texting - right",
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
def predict_image(image):
|
|
|
|
| 32 |
img_array = img_to_array(image)
|
| 33 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 34 |
+
img_array = img_array / 255.0
|
|
|
|
|
|
|
| 35 |
predictions = model.predict(img_array)
|
| 36 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 37 |
confidence = np.max(predictions)
|
|
|
|
| 38 |
return CLASS_LABELS[predicted_class], confidence
|
| 39 |
|
| 40 |
+
def speak_auto(text):
|
|
|
|
| 41 |
tts = gTTS(text=text, lang='en')
|
| 42 |
+
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
|
| 43 |
+
tts.save(fp.name)
|
| 44 |
+
audio_bytes = fp.read()
|
| 45 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
| 46 |
+
audio_html = f"""
|
| 47 |
+
<audio autoplay>
|
| 48 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
| 49 |
+
</audio>
|
| 50 |
+
"""
|
| 51 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
| 52 |
|
| 53 |
# Streamlit app
|
| 54 |
st.title("Driver Distraction Detection")
|
| 55 |
st.markdown("Team18 Image Project : Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Sidhartha Karjee")
|
| 56 |
|
|
|
|
| 57 |
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
| 58 |
|
| 59 |
if uploaded_file is not None:
|
|
|
|
| 60 |
image = Image.open(uploaded_file).convert("RGB")
|
| 61 |
resized_image = image.resize((IMG_WIDTH, IMG_HEIGHT))
|
| 62 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 63 |
|
|
|
|
| 64 |
with st.spinner("Predicting..."):
|
| 65 |
predicted_class, confidence = predict_image(resized_image)
|
| 66 |
|
| 67 |
+
prediction_text = f"{predicted_class}. Confidence: {confidence:.2%}"
|
|
|
|
| 68 |
st.subheader("Prediction")
|
| 69 |
st.write(prediction_text)
|
| 70 |
|
| 71 |
+
speak_auto(prediction_text) # Auto-play speech
|
|
|
|
|
|