Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load the trained model
|
| 8 |
+
MODEL_PATH = "image_model.h5"
|
| 9 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 10 |
+
|
| 11 |
+
# Define image dimensions
|
| 12 |
+
IMG_WIDTH, IMG_HEIGHT = 64, 48
|
| 13 |
+
|
| 14 |
+
# Class labels with descriptions
|
| 15 |
+
CLASS_LABELS = {
|
| 16 |
+
0: "Normal driving (safe)",
|
| 17 |
+
1: "Texting - right",
|
| 18 |
+
2: "Talking on the phone - right",
|
| 19 |
+
3: "Texting - left",
|
| 20 |
+
4: "Talking on the phone - left",
|
| 21 |
+
5: "Operating the radio",
|
| 22 |
+
6: "Drinking",
|
| 23 |
+
7: "Reaching behind",
|
| 24 |
+
8: "Hair and makeup",
|
| 25 |
+
9: "Talking to passenger"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def predict_image(image):
|
| 29 |
+
"""
|
| 30 |
+
Predict the class of a given image.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
image (PIL.Image.Image): Input image.
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
str: Predicted class description.
|
| 37 |
+
float: Confidence score of the prediction.
|
| 38 |
+
"""
|
| 39 |
+
# Preprocess the image
|
| 40 |
+
img_array = img_to_array(image)
|
| 41 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 42 |
+
img_array = img_array / 255.0 # Normalize pixel values to [0, 1]
|
| 43 |
+
|
| 44 |
+
# Make a prediction
|
| 45 |
+
predictions = model.predict(img_array)
|
| 46 |
+
predicted_class = np.argmax(predictions, axis=1)[0]
|
| 47 |
+
confidence = np.max(predictions)
|
| 48 |
+
|
| 49 |
+
return CLASS_LABELS[predicted_class], confidence
|
| 50 |
+
|
| 51 |
+
# Streamlit app
|
| 52 |
+
st.title("Driver Distraction Detection")
|
| 53 |
+
st.markdown("Team18 Image Project : Sayandip Bhattacharyya, Purnendu Rudrapal, Sridatta Das, Siddharta Karjee")
|
| 54 |
+
|
| 55 |
+
# File upload
|
| 56 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
| 57 |
+
|
| 58 |
+
if uploaded_file is not None:
|
| 59 |
+
# Display the uploaded image
|
| 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_column_width=True)
|
| 63 |
+
|
| 64 |
+
# Predict the class
|
| 65 |
+
with st.spinner("Predicting..."):
|
| 66 |
+
predicted_class, confidence = predict_image(resized_image)
|
| 67 |
+
|
| 68 |
+
# Display the result
|
| 69 |
+
st.subheader("Prediction")
|
| 70 |
+
st.write(f"**Class:** {predicted_class}")
|
| 71 |
+
st.write(f"**Confidence:** {confidence:.2%}")
|