Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import numpy as np
|
|
| 4 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 5 |
from PIL import Image
|
| 6 |
from gtts import gTTS
|
| 7 |
-
import os
|
| 8 |
import tempfile
|
| 9 |
|
| 10 |
# Load the trained model
|
|
@@ -42,11 +41,14 @@ def predict_image(image):
|
|
| 42 |
return CLASS_LABELS[predicted_class], confidence
|
| 43 |
|
| 44 |
def speak(text):
|
|
|
|
| 45 |
tts = gTTS(text=text, lang='en')
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Streamlit app
|
| 52 |
st.title("Driver Distraction Detection")
|
|
@@ -66,9 +68,10 @@ if uploaded_file is not None:
|
|
| 66 |
predicted_class, confidence = predict_image(resized_image)
|
| 67 |
|
| 68 |
# Display the result
|
| 69 |
-
st.subheader("Prediction")
|
| 70 |
prediction_text = f"Class: {predicted_class}\nConfidence: {confidence:.2%}"
|
|
|
|
| 71 |
st.write(prediction_text)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
speak(prediction_text)
|
|
|
|
|
|
| 4 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 5 |
from PIL import Image
|
| 6 |
from gtts import gTTS
|
|
|
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
# Load the trained model
|
|
|
|
| 41 |
return CLASS_LABELS[predicted_class], confidence
|
| 42 |
|
| 43 |
def speak(text):
|
| 44 |
+
# Generate speech using gTTS
|
| 45 |
tts = gTTS(text=text, lang='en')
|
| 46 |
+
# Save the audio file in a temporary location
|
| 47 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile:
|
| 48 |
+
tmpfile_path = tmpfile.name
|
| 49 |
+
tts.save(tmpfile_path)
|
| 50 |
+
# Return the path of the temporary file so Streamlit can play it
|
| 51 |
+
return tmpfile_path
|
| 52 |
|
| 53 |
# Streamlit app
|
| 54 |
st.title("Driver Distraction Detection")
|
|
|
|
| 68 |
predicted_class, confidence = predict_image(resized_image)
|
| 69 |
|
| 70 |
# Display the result
|
|
|
|
| 71 |
prediction_text = f"Class: {predicted_class}\nConfidence: {confidence:.2%}"
|
| 72 |
+
st.subheader("Prediction")
|
| 73 |
st.write(prediction_text)
|
| 74 |
|
| 75 |
+
# Generate and play the audio
|
| 76 |
+
audio_path = speak(prediction_text)
|
| 77 |
+
st.audio(audio_path, format="audio/mp3")
|