Sayandip commited on
Commit
2511789
·
verified ·
1 Parent(s): 8e71115

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
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
- with tempfile.NamedTemporaryFile(delete=False) as temp_file:
47
- temp_file_path = temp_file.name + ".mp3"
48
- tts.save(temp_file_path)
49
- os.system(f"start {temp_file_path}") # On Windows, use `start`, on Mac/Linux use `open`
 
 
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
- # Speak the prediction
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")