-
Notifications
You must be signed in to change notification settings - Fork 2
/
recognize.py
71 lines (64 loc) · 3.95 KB
/
recognize.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
import numpy as np
import argparse
import sys
from keras.preprocessing import image
from keras.models import load_model
def main():
parser = argparse.ArgumentParser(
description='Road sign recognition (classification) based on model created e.g. by Keras library.')
parser.add_argument("-m", "--model", type=str, nargs='?', const='classifier.h5', default='classifier.h5', help="the name of the h5 file with the model, default value is classifier.h5")
parser.add_argument("file", type=str, help="the name of the image file with road sign (obligatory)")
args = parser.parse_args()
try:
model = load_model(args.model)
except FileNotFoundError:
print("recognize: ", args.model, ": No such file of Model")
sys.exit()
except PermissionError:
print("recognize:", args.model, ": Permission to Model denied")
sys.exit()
except IsADirectoryError:
print("recognize:", args.model, ": Is Directory but not Model")
print("0\t0\t0 ", args.model)
sys.exit()
labels = ["Uneven road", "Speed bump", "Slippery road", "Dangerous curve to the left",
"Dangerous curve to the right", "Double dangerous curve to the left",
"Double dangerous curve to the right", "Presence of children", "Bicycle crossing", "Cattle crossing",
"Road works ahead", "Traffic signals ahead", "Guarded railroad crossing", "Indefinite danger",
"Road narrows", "Road narrows from the left", "Road narrows from the right",
"Priority at the next intersection", "Intersection where the priority from the right is applicable",
"Yield right of way", "Yield to oncoming traffic", "Stop", "No entry for all drivers",
"No bicycles allowed", "Maximum weights allowed (including load)", "No cargo vehicles allowed",
"Maximum width allowed", "Maximum height allowed", "No traffic allowed in both directions",
"No left turn", "No right turn",
"No passing to the left vehicles having more than 2 wheels and horse drawn vehicles",
"Maximum speed limit", "Mandatory way for pedestrians and bicycles", "Mandatory direction (straight on)",
"Mandatory direction (to the right or to the left)", "Mandatory directions(straight on and to the right)",
"Mandatory traffic circle", "Mandatory bicycle path",
"Path shared between pedestrians, bicycles and mopeds class A", "No parking", "No waiting or parking",
"No parking from the 1 st to the 15th of the month", "No parking from the 16th till the end of the month",
"Priority over oncoming traffic", "Parking allowed", "Additional parking sign for handicap only",
"Parking exclusively for motorcars", "Parking exclusively for trucks",
"Parking exclusively for buses/coaches", "Parking on sidewalk or verge mandatory",
"Beginning of a residential area", "End of a residential area", "One way traffic", "Dead end",
"End of road works", "Pedestrian crosswalk", "Bicycles and mopeds crossing", "Parking ahead",
"Speed bump", "End of priority road", "Priority road"]
try:
test_image = image.load_img(args.file, target_size=(64, 64))
except FileNotFoundError:
print("recognize: ", args.file, ": No such file or directory")
sys.exit()
except PermissionError:
print("recognize:", args.file, ": Permission denied")
sys.exit()
except IsADirectoryError:
print("recognize:", args.file, ": Is Directory")
print("0\t0\t0 ", args.file)
sys.exit()
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
y_pred = np.argmax(result, axis=-1)
print("Road sign recognized as ", labels[int(y_pred)])
if __name__ == '__main__':
main()