|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog |
| 3 | +from tkinter import * |
| 4 | +from PIL import ImageTk, Image |
| 5 | + |
| 6 | +import numpy |
| 7 | +#load the trained model to classify sign |
| 8 | +from keras.models import load_model |
| 9 | +model = load_model('traffic_classifier.h5') |
| 10 | + |
| 11 | +#dictionary to label all traffic signs class. |
| 12 | +classes = { 1:'Speed limit (20km/h)', |
| 13 | + 2:'Speed limit (30km/h)', |
| 14 | + 3:'Speed limit (50km/h)', |
| 15 | + 4:'Speed limit (60km/h)', |
| 16 | + 5:'Speed limit (70km/h)', |
| 17 | + 6:'Speed limit (80km/h)', |
| 18 | + 7:'End of speed limit (80km/h)', |
| 19 | + 8:'Speed limit (100km/h)', |
| 20 | + 9:'Speed limit (120km/h)', |
| 21 | + 10:'No passing', |
| 22 | + 11:'No passing veh over 3.5 tons', |
| 23 | + 12:'Right-of-way at intersection', |
| 24 | + 13:'Priority road', |
| 25 | + 14:'Yield', |
| 26 | + 15:'Stop', |
| 27 | + 16:'No vehicles', |
| 28 | + 17:'Veh > 3.5 tons prohibited', |
| 29 | + 18:'No entry', |
| 30 | + 19:'General caution', |
| 31 | + 20:'Dangerous curve left', |
| 32 | + 21:'Dangerous curve right', |
| 33 | + 22:'Double curve', |
| 34 | + 23:'Bumpy road', |
| 35 | + 24:'Slippery road', |
| 36 | + 25:'Road narrows on the right', |
| 37 | + 26:'Road work', |
| 38 | + 27:'Traffic signals', |
| 39 | + 28:'Pedestrians', |
| 40 | + 29:'Children crossing', |
| 41 | + 30:'Bicycles crossing', |
| 42 | + 31:'Beware of ice/snow', |
| 43 | + 32:'Wild animals crossing', |
| 44 | + 33:'End speed + passing limits', |
| 45 | + 34:'Turn right ahead', |
| 46 | + 35:'Turn left ahead', |
| 47 | + 36:'Ahead only', |
| 48 | + 37:'Go straight or right', |
| 49 | + 38:'Go straight or left', |
| 50 | + 39:'Keep right', |
| 51 | + 40:'Keep left', |
| 52 | + 41:'Roundabout mandatory', |
| 53 | + 42:'End of no passing', |
| 54 | + 43:'End no passing veh > 3.5 tons' } |
| 55 | + |
| 56 | +#initialise GUI |
| 57 | +top=tk.Tk() |
| 58 | +top.geometry('800x600') |
| 59 | +top.title('Traffic sign classification') |
| 60 | +top.configure(background='#CDCDCD') |
| 61 | + |
| 62 | +label=Label(top,background='#CDCDCD', font=('arial',15,'bold')) |
| 63 | +sign_image = Label(top) |
| 64 | + |
| 65 | +def classify(file_path): |
| 66 | + global label_packed |
| 67 | + image = Image.open(file_path) |
| 68 | + image = image.resize((30,30)) |
| 69 | + image = numpy.expand_dims(image, axis=0) |
| 70 | + image = numpy.array(image) |
| 71 | + print(image.shape) |
| 72 | + pred = model.predict_classes([image])[0] |
| 73 | + sign = classes[pred+1] |
| 74 | + print(sign) |
| 75 | + label.configure(foreground='#011638', text=sign) |
| 76 | + |
| 77 | + |
| 78 | +def show_classify_button(file_path): |
| 79 | + classify_b=Button(top,text="Classify Image",command=lambda: classify(file_path),padx=10,pady=5) |
| 80 | + classify_b.configure(background='#364156', foreground='white',font=('arial',10,'bold')) |
| 81 | + classify_b.place(relx=0.79,rely=0.46) |
| 82 | + |
| 83 | +def upload_image(): |
| 84 | + try: |
| 85 | + file_path=filedialog.askopenfilename() |
| 86 | + uploaded=Image.open(file_path) |
| 87 | + uploaded.thumbnail(((top.winfo_width()/2.25),(top.winfo_height()/2.25))) |
| 88 | + im=ImageTk.PhotoImage(uploaded) |
| 89 | + |
| 90 | + sign_image.configure(image=im) |
| 91 | + sign_image.image=im |
| 92 | + label.configure(text='') |
| 93 | + show_classify_button(file_path) |
| 94 | + except: |
| 95 | + pass |
| 96 | + |
| 97 | +upload=Button(top,text="Upload an image",command=upload_image,padx=10,pady=5) |
| 98 | +upload.configure(background='#364156', foreground='white',font=('arial',10,'bold')) |
| 99 | + |
| 100 | +upload.pack(side=BOTTOM,pady=50) |
| 101 | +sign_image.pack(side=BOTTOM,expand=True) |
| 102 | +label.pack(side=BOTTOM,expand=True) |
| 103 | +heading = Label(top, text="Know Your Traffic Sign",pady=20, font=('arial',20,'bold')) |
| 104 | +heading.configure(background='#CDCDCD',foreground='#364156') |
| 105 | +heading.pack() |
| 106 | +top.mainloop() |
0 commit comments