Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
# Load the model
|
| 7 |
+
@st.cache_resource
|
| 8 |
+
def load_model():
|
| 9 |
+
return tf.keras.models.load_model('traffic_classifier.h5')
|
| 10 |
+
|
| 11 |
+
model = load_model()
|
| 12 |
+
|
| 13 |
+
# Class labels
|
| 14 |
+
class_names = [
|
| 15 |
+
'Speed limit (20km/h)', 'Speed limit (30km/h)', 'Speed limit (50km/h)',
|
| 16 |
+
'Speed limit (60km/h)', 'Speed limit (70km/h)', 'Speed limit (80km/h)',
|
| 17 |
+
'End of speed limit (80km/h)', 'Speed limit (100km/h)', 'Speed limit (120km/h)',
|
| 18 |
+
'No passing', 'No passing for vehicles over 3.5 metric tons',
|
| 19 |
+
'Right-of-way at the next intersection', 'Priority road', 'Yield', 'Stop',
|
| 20 |
+
'No vehicles', 'Vehicles over 3.5 metric tons prohibited', 'No entry',
|
| 21 |
+
'General caution', 'Dangerous curve to the left', 'Dangerous curve to the right',
|
| 22 |
+
'Double curve', 'Bumpy road', 'Slippery road', 'Road narrows on the right',
|
| 23 |
+
'Road work', 'Traffic signals', 'Pedestrians', 'Children crossing',
|
| 24 |
+
'Bicycles crossing', 'Beware of ice/snow', 'Wild animals crossing',
|
| 25 |
+
'End of all speed and passing limits', 'Turn right ahead', 'Turn left ahead',
|
| 26 |
+
'Ahead only', 'Go straight or right', 'Go straight or left', 'Keep right',
|
| 27 |
+
'Keep left', 'Roundabout mandatory', 'End of no passing',
|
| 28 |
+
'End of no passing by vehicles over 3.5 metric tons'
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
st.title('Traffic Sign Classifier')
|
| 32 |
+
|
| 33 |
+
uploaded_file = st.file_uploader("Upload a traffic sign image", type=["jpg", "jpeg", "png"])
|
| 34 |
+
|
| 35 |
+
if uploaded_file is not None:
|
| 36 |
+
image = Image.open(uploaded_file)
|
| 37 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 38 |
+
|
| 39 |
+
# Preprocess the image
|
| 40 |
+
image = image.resize((30, 30))
|
| 41 |
+
image = np.expand_dims(image, axis=0)
|
| 42 |
+
image = np.array(image)
|
| 43 |
+
|
| 44 |
+
# Make prediction
|
| 45 |
+
pred = model.predict(image)
|
| 46 |
+
predicted_class = np.argmax(pred, axis=-1)[0]
|
| 47 |
+
|
| 48 |
+
st.write(f"Predicted traffic sign: {class_names[predicted_class]}")
|
| 49 |
+
st.write(f"Confidence: {pred[0][predicted_class]*100:.2f}%")
|
| 50 |
+
|
| 51 |
+
st.write("Note: This application uses a pre-trained model. For best results, upload images that are 30x30 pixels and similar to those used in the training dataset.")
|