-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuI.py
119 lines (100 loc) · 3.19 KB
/
GuI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import PySimpleGUI as sg
import cv2
from pathlib import Path
import numpy as np
from keras.models import load_model
import tensorflow as tf
from tensorflow import keras
import os
from PIL import Image
# Load the model to classify sign
model = load_model('CNN_model.h5')
size = 300
image_to_classify_path = ""
# List of sign names
names = {1: 'Speed limit (20km/h)',
2: 'Speed limit (30km/h)',
3: 'Speed limit (50km/h)',
4: 'Speed limit (60km/h)',
5: 'Speed limit (70km/h)',
6: 'Speed limit (80km/h)',
7: 'Stop',
8: 'Speed limit (100km/h)',
9: 'No Entry',
10: 'No passing',
11: 'No passing for trucks',
12: 'Right-of-way at intersection',
13: 'Priority road',
14: 'Yield priority',
15: 'Traffic lights',
16: 'Pedestrians',
17: 'Bicycles',
18: 'Go straight',
19: 'Turn right',
20: 'Turn left',
21: 'Roundabout',
}
# functions
def classify():
img = keras.preprocessing.image.load_img(image_to_classify_path, target_size=(30, 30))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
pred = np.argmax(model.predict([img_array]), axis=-1)[0]
window["SignName"].update(names[pred + 1])
folder_path_reference_signs = 'Reference_photos'
image_name_reference_sign = "{0}.png".format(pred)
image_path_reference_sign = os.path.join(folder_path_reference_signs,image_name_reference_sign)
try:
imgage_reference_sign = Image.open(image_path_reference_sign)
except:
print("cant find image on given path")
try:
res, img_to_show = cv2.imencode(".png", cv2.imread(image_path_reference_sign))
except:
window["Status"].update("Cannot load refence sign image")
return
window["Result"].update(data=img_to_show.tobytes())
def read_file():
global image_to_classify_path
image_to_classify_path = sg.popup_get_file("", no_window=True)
if image_to_classify_path == "":
return
window["Status"].update("Choose image")
if not Path(image_to_classify_path).is_file():
window["Status"].update("Image not found")
return
try:
res, image = cv2.imencode(".png", cv2.imread(image_to_classify_path))
except:
window["Status"].update("Cannot identify image")
return
window["Image"].update(data=image.tobytes())
# gui
gui = [
[sg.Text("Choose image", expand_x=True, key="Status")],
[sg.Button("Browse"), sg.Button("Classify")],
[sg.Image(size=(size, size), key="Image")]
]
# result gui
result_gui = [
[sg.Text(key="SignName")],
[sg.Image(size=(size, size), key="Result")]
]
layout = [
[
sg.Column(gui, vertical_alignment="center", justification="center"),
sg.VSeperator(),
sg.Column(result_gui, vertical_alignment="center", justification="center")
]
]
window = sg.Window("Project", layout)
# Event loop
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
elif event == "Browse":
read_file()
elif event == "Classify":
classify()
window.close()