CoolFace
Apppublic

A-Celsius/Traffic_Sign_Classifier

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py79 linesDownload Raw Back to root
1import cv22import numpy as np3import gradio as gr4from keras.models import load_model5 6names = [7    'Speed limit (20km/h)',8    'Speed limit (30km/h)',9    'Speed limit (50km/h)',10    'Speed limit (60km/h)',11    'Speed limit (70km/h)',12    'Speed limit (80km/h)',13    'End of speed limit (80km/h)',14    'Speed limit (100km/h)',15    'Speed limit (120km/h)',16    'No passing',17    'No passing for vechiles over 3.5 metric tons',18    'Road Block',19    'Priority road',20    'Yield',21    'Stop',22    'No vehicles',23    'Vechiles over 3.5 metric tons prohibited',24    'No entry',25    'General caution',26    'Double curve',27    'Bumpy Road',28    'Slippery road',29    'Road narrows on the right',30    'Road Work',31    'Traffic Signals',32    'Pedestrians',33    'Children crossing',34    'Bicycles crossing',35    'Beware of ice/snow',36    'Wild animals crossing',37    'End of all speed and passing limits',38    'Turn right ahead',39    'Turn left ahead',40    'Ahead only',41    'Go straight or right',42    'Go straight or left',43    'Keep right',44    'Keep left',45    'Roundabout mandatory',46    'End of no passing',47    'End of no passing by vechiles over 3.5 metric tons'48]49 50# Load the saved model51model = load_model('model.h5')52 53 54# Preprocess the input image55def preprocess_image(img):56    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)57    img = cv2.equalizeHist(img)58    img = img / 25559    img = cv2.resize(img, (32, 32))60    img = img.reshape(1, 32, 32, 1)61    return img62 63 64# Define the prediction function65def predict_image(image):66    preprocessed_image = preprocess_image(image)67    predictions = model.predict(preprocessed_image)68    class_index = np.argmax(predictions)69    class_label = names[class_index]70    accuracy = predictions[0][class_index]71    return f"Prediction: {class_label}, Accuracy: {accuracy:.2%}"72 73 74# Create the Gradio interface75iface = gr.Interface(fn=predict_image, inputs="image", outputs="text")76 77# Run the interface78iface.launch()79