-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecognizetTrafficSignDetectedAvishkaSandeepa.py
291 lines (234 loc) · 9.62 KB
/
RecognizetTrafficSignDetectedAvishkaSandeepa.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
"""
Created on feb 2024
@author: Alfonso Blanco using the model downloaded from
https://github.com/AvishkaSandeepa/Traffic-Signs-Recognition
"""
#######################################################################
# PARAMETERS
######################################################################
dir=""
dirname= "Test2"
#dirnameYolo="runs\\detect\\train2\\weights\\best.pt"
dirnameYolo="bestDetectTrafficSign.pt"
from keras.models import Sequential, load_model
# Downloaded from https://github.com/faeya/traffic-sign-classification
#modelTrafficSignRecognition=load_model('traffic_classifier.h5')
# https://github.com/AvishkaSandeepa/Traffic-Signs-Recognition
modelTrafficSignRecognition=load_model('model.h5')
#creating dictionary to label all traffic sign classes.
classes = { 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:'End of speed limit (80km/h)',
8:'Speed limit (100km/h)',
9:'Speed limit (120km/h)',
10:'No passing',
11:'No passing veh over 3.5 tons',
12:'Right-of-way at intersection',
13:'Priority road',
14:'Yield',
15:'Stop',
16:'No vehicles',
17:'Veh > 3.5 tons prohibited',
18:'No entry',
19:'General caution',
20:'Dangerous curve left',
21:'Dangerous curve right',
22:'Double curve',
23:'Bumpy road',
24:'Slippery road',
25:'Road narrows on the right',
26:'Road work',
27:'Traffic signals',
28:'Pedestrians',
29:'Children crossing',
30:'Bicycles crossing',
31:'Beware of ice/snow',
32:'Wild animals crossing',
33:'End speed + passing limits',
34:'Turn right ahead',
35:'Turn left ahead',
36:'Ahead only',
37:'Go straight or right',
38:'Go straight or left',
39:'Keep right',
40:'Keep left',
41:'Roundabout mandatory',
42:'End of no passing',
43:'End no passing veh > 3.5 tons' }
import cv2
import time
TimeIni=time.time()
# https://docs.ultralytics.com/python/
from ultralytics import YOLO
model_yolo = YOLO(dirnameYolo)
class_list = model_yolo.model.names
print(class_list)
TabTrafficSign =[ 'Speed limit (20km/h)',
'Speed limit (30km/h)',
'Speed limit (50km/h)',
'Speed limit (60km/h)',
'Speed limit (70km/h)',
'Speed limit (80km/h)',
'End of speed limit (80km/h)',
'Speed limit (100km/h)',
'Speed limit (120km/h)',
'No passing',
'No passing veh over 3.5 tons',
'Right-of-way at intersection',
'Priority road',
'Yield',
'Stop',
'No vehicles',
'Veh > 3.5 tons prohibited',
'No entry',
'General caution',
'Dangerous curve left',
'Dangerous curve right',
'Double curve',
'Bumpy road',
'Slippery road',
'Road narrows on the right',
'Road work',
'Traffic signals',
'Pedestrians',
'Children crossing',
'Bicycles crossing',
'Beware of ice/snow',
'Wild animals crossing',
'End speed + passing limits',
'Turn right ahead',
'Turn left ahead',
'Ahead only',
'Go straight or right',
'Go straight or left',
'Keep right',
'Keep left',
'Roundabout mandatory',
'End of no passing',
'End no passing veh > 3.5 tons' ]
import torch
from torch import nn
import os
import re
import cv2
import numpy as np
import keras
import functools
import time
inicio=time.time()
import numpy as np
import numpy
########################################################################
def loadimages(dirname):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirname + "\\"
images = []
TabFileName=[]
print("Reading imagenes from ",imgpath)
NumImage=-2
Cont=0
for root, dirnames, filenames in os.walk(imgpath):
NumImage=NumImage+1
for filename in filenames:
if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
filepath = os.path.join(root, filename)
image = cv2.imread(filepath)
images.append(image)
TabFileName.append(filename)
Cont+=1
return images, TabFileName
# ttps://medium.chom/@chanon.krittapholchai/build-object-detection-gui-with-yolov8-and-pysimplegui-76d5f5464d6c
def DetectTrafficSignWithYolov8 (img):
TabcropTrafficSign=[]
y=[]
yMax=[]
x=[]
xMax=[]
Tabclass_name=[]
results = model_yolo.predict(img)
for i in range(len(results)):
# may be several plates in a frame
result=results[i]
xyxy= result.boxes.xyxy.numpy()
confidence= result.boxes.conf.numpy()
class_id= result.boxes.cls.numpy().astype(int)
print(class_id)
out_image = img.copy()
for j in range(len(class_id)):
con=confidence[j]
label=class_list[class_id[j]] + " " + str(con)
box=xyxy[j]
cropTrafficSign=out_image[int(box[1]):int(box[3]),int(box[0]):int(box[2])]
TabcropTrafficSign.append(cropTrafficSign)
y.append(int(box[1]))
yMax.append(int(box[3]))
x.append(int(box[0]))
xMax.append(int(box[2]))
#Tabclass_name.append(class_name)
print(label)
Tabclass_name.append(label)
return TabcropTrafficSign, y,yMax,x,xMax, Tabclass_name
###########################################################
# MAIN
##########################################################
imagesComplete, TabFileName=loadimages(dirname)
print("Number of imagenes : " + str(len(imagesComplete)))
ContDetected=0
ContNoDetected=0
for i in range (len(imagesComplete)):
gray=imagesComplete[i]
#cv2.imshow('Gray', gray)
#cv2.waitKey(0)
TabImgSelect, y, yMax, x, xMax, Tabclass_name =DetectTrafficSignWithYolov8(gray)
if TabImgSelect==[]:
print(TabFileName[i] + " NON DETECTED")
ContNoDetected=ContNoDetected+1
continue
else:
ContDetected=ContDetected+1
print(TabFileName[i] + " DETECTED ")
for z in range(len(TabImgSelect)):
#if TabImgSelect[z] == []: continue
gray1=TabImgSelect[z]
#cv2.waitKey(0)
start_point=(x[z],y[z])
end_point=(xMax[z], yMax[z])
color=(0,0,255)
# Using cv2.rectangle() method
# Draw a rectangle with blue line borders of thickness of 5 px
img = cv2.rectangle(gray, start_point, end_point,(255,0,0), 5)
# Put text
text_location = (x[z], y[z])
text_color = (255,255,255)
cv2.imwrite("pp.jpg",gray1)
data=[]
SignalToRecognize=cv2.resize(gray1,(32,32))
data.append(np.array(SignalToRecognize))
X_test=np.array(data)
predict_x=modelTrafficSignRecognition.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
NameTrafficSignPredicted_model2=str(classes[int(classes_x)+1])
print(NameTrafficSignPredicted_model2)
cv2.putText(img, NameTrafficSignPredicted_model2 ,text_location
, cv2.FONT_HERSHEY_SIMPLEX , 1
, text_color, 2 ,cv2.LINE_AA)
cv2.putText(gray1, NameTrafficSignPredicted_model2 ,text_location
, cv2.FONT_HERSHEY_SIMPLEX , 1
, text_color, 2 ,cv2.LINE_AA)
cv2.imshow('Trafic Sign', gray1)
cv2.waitKey(0)
#
show_image=cv2.resize(img,(1000,700))
cv2.imshow('Frame', show_image)
cv2.waitKey(0)
print("")
print( " Time in seconds "+ str(time.time()-TimeIni))