CoolFace
Apppublic

Manni-HUB-2/Traffic-Sign-Predictor

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py339 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""dlp_project_new2 (1).ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7    https://colab.research.google.com/drive/1fdN3aTQEe_UPkxqD8BwqzGrWn-3o_bGW8"""9 10import numpy as np11import pandas as pd12import matplotlib.pyplot as plt13import cv214import tensorflow as tf15from PIL import Image16import os17from sklearn.model_selection import train_test_split18from keras.utils import to_categorical19from keras.models import Sequential, load_model20from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout, BatchNormalization21 22pip install gradio23pip install -q kaggle24mkdir ~/.kaggle25cp kaggle.json ~/.kaggle/26chmod 600 ~/.kaggle/kaggle.json27kaggle datasets list28 29kaggle datasets download -d meowmeowmeowmeowmeow/gtsrb-german-traffic-sign30 31mkdir Dataset32unzip -q gtsrb-german-traffic-sign.zip -d /content/Dataset33 34"""**Preprocessing Image**"""35 36images = []37labels = []38classes = 4339 40for i in range(classes):41    path = '/content/Dataset/train/'+ str(i)42    raw_images = os.listdir(path)43    for img in raw_images:44        try:45            image = Image.open(path + '/'+ img)46            image = image.resize((50,50))47            image = np.array(image)48            images.append(image)49            labels.append(i)50        except Exception as e:51            print(e)52 53#converting to numpy array54images = np.array(images)55labels = np.array(labels)56 57classes = { 0:'Speed limit (20km/h)',58            1:'Speed limit (30km/h)', 59            2:'Speed limit (50km/h)', 60            3:'Speed limit (60km/h)',61            4:'Speed limit (70km/h)', 62            5:'Speed limit (80km/h)', 63            6:'End of speed limit (80km/h)', 64            7:'Speed limit (100km/h)', 65            8:'Speed limit (120km/h)', 66            9:'No passing', 67            10:'No passing veh over 3.5 tons', 68            11:'Right-of-way at intersection', 69            12:'Priority road', 70            13:'Yield', 71            14:'Stop', 72            15:'No vehicles', 73            16:'Veh > 3.5 tons prohibited', 74            17:'No entry', 75            18:'General caution', 76            19:'Dangerous curve left', 77            20:'Dangerous curve right', 78            21:'Double curve', 79            22:'Bumpy road', 80            23:'Slippery road', 81            24:'Road narrows on the right', 82            25:'Road work', 83            26:'Traffic signals', 84            27:'Pedestrians', 85            28:'Children crossing',86            29:'Bicycles crossing', 87            30:'Beware of ice/snow',88            31:'Wild animals crossing', 89            32:'End speed + passing limits', 90            33:'Turn right ahead', 91            34:'Turn left ahead', 92            35:'Ahead only', 93            36:'Go straight or right', 94            37:'Go straight or left', 95            38:'Keep right', 96            39:'Keep left', 97            40:'Roundabout mandatory', 98            41:'End of no passing', 99            42:'End no passing veh > 3.5 tons' }100 101print('Shape of Image Data: ' + str(images.shape))102print('Shape of Label Data: ' + str(labels.shape))103 104x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.25, random_state = 42, shuffle=True)105 106print("X_train.shape", x_train.shape)107print("X_valid.shape", x_test.shape)108print("y_train.shape", y_train.shape)109print("y_valid.shape", y_test.shape)110 111y_train = to_categorical(y_train, 43)112y_test = to_categorical(y_test, 43)113 114model = Sequential()115 116model.add(Conv2D(filters=16, kernel_size=(8,8), activation='relu', input_shape=(50,50,3)))117model.add(Conv2D(filters=32, kernel_size=(8,8), activation='relu'))118model.add(MaxPool2D(pool_size=(2, 2)))119model.add(BatchNormalization(axis=-1))120 121model.add(Dropout(rate=0.5))122 123model.add(Conv2D(filters=64, kernel_size=(4, 4), activation='relu'))124model.add(Conv2D(filters=128, kernel_size=(4, 4), activation='relu'))125model.add(MaxPool2D(pool_size=(2, 2)))126model.add(BatchNormalization(axis=-1))127 128model.add(Dropout(rate=0.5))129 130model.add(Flatten())131model.add(Dense(512, activation='relu'))132 133 134# We have 43 classes that's why we have defined 43 in the dense135model.add(Dense(43, activation='softmax'))136 137model.summary()138 139from tensorflow.keras.optimizers import Adam140opt= Adam(learning_rate=0.001)141 142model.compile(loss = 'categorical_crossentropy', optimizer=opt, metrics=['accuracy'])143 144epochs = 20145batch_size = 32146history = model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, validation_data = (x_test, y_test))147 148#Accuracy and Loss149 150plt.figure(0)151plt.plot(history.history['accuracy'], label='training accuracy')152plt.plot(history.history['val_accuracy'], label='val accuracy')153plt.title('Accuracy')154plt.xlabel('epochs')155plt.ylabel('accuracy')156plt.legend()157plt.show()158 159 160plt.plot(history.history['loss'], label='training loss')161plt.plot(history.history['val_loss'], label='val loss')162plt.title('Loss')163plt.xlabel('epochs')164plt.ylabel('loss')165plt.legend()166plt.show()167 168"""**Testing And Accuracy**"""169 170# Importing the test dataset171y_test = pd.read_csv('/content/Dataset/Test.csv')172 173labels = y_test["ClassId"].values174imgs = y_test["Path"].values175 176data=[]177 178# Retreiving the images179with tf.device('/GPU:0'):180    for img in imgs:181        image = Image.open('/content/Dataset/'+img)182        image = image.resize([50, 50])183        data.append(np.array(image))184 185X_test = np.array(data)186 187with tf.device('/GPU:0'):188    pred = np.argmax(model.predict(X_test), axis=-1)189 190#Accuracy with the test data191from sklearn.metrics import accuracy_score192print('Accuracy: ' + str(accuracy_score(labels, pred)*100) + ' %')193 194"""**Confusion Matrix**"""195 196from sklearn.metrics import confusion_matrix197cm = confusion_matrix(labels, pred)198np.savetxt("confusion_matrix.csv", cm, delimiter=",")199cm = np.loadtxt("confusion_matrix.csv", delimiter=",")200 201import seaborn as sns202df= pd.DataFrame(cm, index = classes,  columns = classes)203print(df)204plt.figure(figsize = (15,15))205sns.heatmap(df, annot=True)206 207model.save("./trained/Dlpproj.h5")208 209"""**Loading the model**"""210 211from keras.models import load_model212model = load_model('./trained/Dlpproj.h5')213 214import random215plt.figure(figsize = (50, 50))216 217start_index = random.randint(0, 12360)218for i in range(5):219    start_index = random.randint(0, 12360)220 221    plt.subplot(10,10, i + 1)222    plt.grid(False)223    plt.xticks([])224    plt.yticks([])225    prediction = pred[start_index + i]226    actual = labels[start_index + i]227    col = 'g'228    if prediction != actual:229        col = 'r'230    plt.xlabel('\n Actual Sign:{} \n Predicted Sign:{}'.format(classes[actual],classes[prediction]), color = col)231    plt.imshow(X_test[start_index + i])232plt.show()233 234"""#INPUT IMAGE TO CLASSIFY235 236"""237 238from PIL import Image, ImageOps239import numpy as np240import matplotlib.pyplot as plt241from google.colab import files242 243def test_on_img():244    uploaded = files.upload()245    data = []246    for filename in uploaded.keys():247        image = Image.open(filename)248        249        image=rgb_image = image.convert("RGB")250        image = image.resize((50,50))251        252        data.append(np.array(image))253    254    x_test = np.array(data)255    y_pred = np.argmax(model.predict(x_test), axis=-1)256 257    return image, y_pred258 259pic, res = test_on_img()260pic = ImageOps.scale(pic, 4)  # Scale up the image by a factor of 2261pic.show()262print(res)263print("Sign={}".format(classes[res.item()]))264 265 266 267import gradio as gr268from PIL import Image, ImageOps269import numpy as np270 271# Define the image classes272classes = {0:'Speed limit (20km/h)',273            1:'Speed limit (30km/h)', 274            2:'Speed limit (50km/h)', 275            3:'Speed limit (60km/h)',276            4:'Speed limit (70km/h)', 277            5:'Speed limit (80km/h)', 278            6:'End of speed limit (80km/h)', 279            7:'Speed limit (100km/h)', 280            8:'Speed limit (120km/h)', 281            9:'No passing', 282            10:'No passing veh over 3.5 tons', 283            11:'Right-of-way at intersection', 284            12:'Priority road', 285            13:'Yield', 286            14:'Stop', 287            15:'No vehicles', 288            16:'Veh > 3.5 tons prohibited', 289            17:'No entry', 290            18:'General caution', 291            19:'Dangerous curve left', 292            20:'Dangerous curve right', 293            21:'Double curve', 294            22:'Bumpy road', 295            23:'Slippery road', 296            24:'Road narrows on the right', 297            25:'Road work', 298            26:'Traffic signals', 299            27:'Pedestrians', 300            28:'Children crossing',301            29:'Bicycles crossing', 302            30:'Beware of ice/snow',303            31:'Wild animals crossing', 304            32:'End speed + passing limits', 305            33:'Turn right ahead', 306            34:'Turn left ahead', 307            35:'Ahead only', 308            36:'Go straight or right', 309            37:'Go straight or left', 310            38:'Keep right', 311            39:'Keep left', 312            40:'Roundabout mandatory', 313            41:'End of no passing', 314            42:'End no passing veh > 3.5 tons'}315 316model = load_model('/content/trained/Dlpproj.h5')317 318def classify_image(image):319    image = Image.fromarray(image.astype('uint8'), 'RGB')320    321    image = ImageOps.scale(image, 4)  # Scale up the image by a factor of 2322    image = image.convert("RGB")323    image = image.resize((50,50))324    image_array = np.array(image)325    image_array = np.expand_dims(image_array, axis=0)326 327    y_pred = model.predict(image_array)328    y_pred = np.argmax(y_pred, axis=-1)329    330    return classes[y_pred.item()]331 332inputs = gr.inputs.Image(label="Input Image")333outputs = gr.outputs.Label(label="Predicted Sign")334title = "Traffic Sign Classifier"335 336description = "Upload an image of a traffic sign "337theme = "default"338iface = gr.Interface(fn=classify_image, inputs=inputs, outputs=outputs, title=title, description=description, theme=theme)339iface.launch(share=True)