Spaces:
Sleeping
Sleeping
File size: 2,866 Bytes
59a25b9 6787c48 59a25b9 9eccf64 6787c48 59a25b9 6787c48 59a25b9 6787c48 59a25b9 049d4a9 4eacd06 59a25b9 6787c48 59a25b9 6787c48 f0b8841 6787c48 9eccf64 59a25b9 3e903c9 59a25b9 651be06 59a25b9 4eacd06 2511789 9eccf64 f0b8841 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import streamlit as st
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
from PIL import Image
from gtts import gTTS
import tempfile
import base64
# Load the trained model
MODEL_PATH = "image_model.h5"
model = tf.keras.models.load_model(MODEL_PATH)
# Image dimensions
IMG_WIDTH, IMG_HEIGHT = 64, 48
# Class labels
CLASS_LABELS = {
0: "The person in the uploaded image is driving safely",
1: "The person in the uploaded image is texting in the right direction and thus, distracted",
2: "The person in the uploaded image is talking on the phone in the right direction and thus, distracted",
3: "The person in the uploaded image is texting in the left direction and thus, distracted",
4: "The person in the uploaded image is talking on the phone in the left direction and thus, distracted",
5: "The person in the uploaded image is operating the radio and thus, distracted",
6: "The person in the uploaded image is drinking and thus, distracted",
7: "The person in the uploaded image is reaching behind and thus, distracted",
8: "The person in the uploaded image is doing hair and makeup and thus, distracted",
9: "The person in the uploaded image is talking to a passenger and thus, distracted"
}
def predict_image(image):
img_array = img_to_array(image)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
predictions = model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)[0]
confidence = np.max(predictions)
return CLASS_LABELS[predicted_class], confidence
def speak_auto(text):
tts = gTTS(text=text, lang='en')
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
tts.save(fp.name)
audio_bytes = fp.read()
b64 = base64.b64encode(audio_bytes).decode()
audio_html = f"""
<audio autoplay>
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
</audio>
"""
st.markdown(audio_html, unsafe_allow_html=True)
# Streamlit app
st.title("Driver Distraction Detection")
st.markdown("Team18 Image Project : Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Sidhartha Karjee")
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
resized_image = image.resize((IMG_WIDTH, IMG_HEIGHT))
st.image(image, caption="Uploaded Image", use_container_width=True)
with st.spinner("Predicting..."):
predicted_class, confidence = predict_image(resized_image)
prediction_text = f"{predicted_class}. Prediction confidence: {confidence:.2%}"
st.subheader("Prediction")
st.write(prediction_text)
speak_auto(prediction_text) # Auto-play speech |