-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
207 lines (173 loc) · 7.84 KB
/
test2.py
File metadata and controls
207 lines (173 loc) · 7.84 KB
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
import cv2
import numpy as np
import glob
import time
from yolo import yoloDetection as yolo
from statistics import median
def heuristica_placaBR(detections, classes):
caracter = {}
for i in range(len(classes)):
caracter[classes[i]] = i
# Heurística Conversora para letras
letra = {'0':'O', '1':'I', '2':'Z', '3':'3', '4':'A', '5':'S', '6':'G', '7':'Z', '8':'B', '9':'9',
'A':'A', 'B':'B', 'C':'C', 'D':'D', 'E':'E', 'F':'F', 'G':'G', 'H':'H', 'I':'I',
'J':'J', 'K':'K', 'L':'L', 'M':'M', 'N':'N', 'O':'O', 'P':'P', 'Q':'Q', 'R':'R',
'S':'S', 'T':'T', 'U':'U', 'V':'V', 'W':'W', 'X':'X', 'Y':'Y', 'Z':'Z'}
# Heurística Conversora para números
num = { '0':'0', '1':'1', '2':'2', '3':'3', '4':'4', '5':'5', '6':'6', '7':'7', '8':'8', '9':'9',
'A':'4', 'B':'8', 'C':'C', 'D':'0', 'E':'E', 'F':'F', 'G':'0', 'H':'H', 'I':'1',
'J':'1', 'K':'K', 'L':'L', 'M':'M', 'N':'N', 'O':'0', 'P':'P', 'Q':'0', 'R':'R',
'S':'5', 'T':'T', 'U':'U', 'V':'V', 'W':'W', 'X':'X', 'Y':'Y', 'Z':'7'}
if len(detections) == 7:
for i in range(len(detections)):
if i < 3: # Letras
detections[i]['CLASS_NAME'] = letra[ detections[i]['CLASS_NAME'] ] #Heuristica de Letra
detections[i]['CLASS_ID'] = caracter[ detections[i]['CLASS_NAME'] ] #Atualiza CLASS_ID
else: # Número
detections[i]['CLASS_NAME'] = num[ detections[i]['CLASS_NAME'] ] #Heuristica de Número
detections[i]['CLASS_ID'] = caracter[ detections[i]['CLASS_NAME'] ] #Atualiza CLASS_ID
def loadClassNames(ClassArchive):
with open(ClassArchive, 'r') as f:
class_names = f.read().split('\n')
return class_names
def loadClassColors(ClassColorsArchive):
class_colors = []
with open(ClassColorsArchive, 'r') as f:
rows = f.read().split('\n')
for row in rows:
values = row.split(',')
color = []
for value in values:
color.append(int(value))
class_colors.append(color.copy())
color.clear()
return class_colors
def xywhTOxyixyf(xywh):
x0, y0 = int(xywh[0] - 0.5 * xywh[2]), int(xywh[1] - 0.5 * xywh[3])
x1, y1 = int(xywh[0] + 0.5 * xywh[2]), int(xywh[1] + 0.5 * xywh[3])
return [x0, y0, x1, y1]
def xyxyTOxywh(xyxy):
w, h = int(xyxy[2] - xyxy[0]), int(xyxy[3] - xyxy[1])
x, y = int(xyxy[0] + (w / 2)), int(xyxy[1] + (h / 2))
return [x, y, w, h]
def putRectangleDetection(image, xyxy, class_name, score, color, showScore=True):
cv2.rectangle(image, xyxy[0:2], xyxy[2:], color, 2)
if showScore:
text = "{}: {:.4f}".format(class_name, score)
cv2.putText(image, text, (xyxy[0], xyxy[1] - 5), cv2.FONT_HERSHEY_DUPLEX, 0.6, color, 1)
else:
text = "{}".format(class_name)
cv2.putText(image, text, (xyxy[0], xyxy[1] - 5), cv2.FONT_HERSHEY_DUPLEX, 0.6, color, 1)
def cutDetection(image, xyxy):
return image[xyxy[1]:xyxy[3], xyxy[0]:xyxy[2]]
def sortMotLP(listaDic):
superior = []
inferior = []
y = []
for detection in listaDic:
y.append(detection['BOX'][1])
lim = median(y)
for detection in listaDic:
if detection['BOX'][1] < lim:
superior.append(detection)
else:
inferior.append(detection)
superior = sorted(superior, key= lambda dici: dici['BOX'][0])
inferior = sorted(inferior, key= lambda dici: dici['BOX'][0])
return superior + inferior
def changeReferenceXY(reference, change):
change[0] += reference[0]
change[1] += reference[1]
change[2] += reference[0]
change[3] += reference[1]
def n_max_conf(n, detections):
detections_copy = detections[:]
det = []
while(len(det) < n and len(detections_copy) > 0):
remove = 0
maior = detections_copy[0]['SCORE']
for i in range(len(detections_copy)):
if detections_copy[i]['SCORE'] > maior:
maior = detections_copy[i]['SCORE']
remove = i
det.append(detections_copy[remove])
del detections_copy[remove]
return det
arquivo_1 = './test_predicts/predicts2/lp_det.txt'
arquivo_2 = './test_predicts/predicts2/char_det.txt'
arquivo_3 = './test_predicts/predicts2/final_lp.txt'
Dataset_dir = 'C:/Users/lucas/OneDrive - ifsp.edu.br/Projeto OCR/Bases_Dados/UFPR-ALPR dataset/images/test/'
images_path = glob.glob(Dataset_dir + '*.png')
lp_det = yolo(INPUT_IMGSZ= 640,
SCORE_THRESHOLD= 0.25,
NMS_THRESHOLD= 0.45,
CLASS_NAMES= loadClassNames('./classes/lp_det.txt'),
CLASS_COLORS= loadClassColors('./classes/lp_det_color.txt'),
ONNXArchive= './onnx/yv8s_lp_det.onnx')
char_det = yolo(INPUT_IMGSZ= 384,
SCORE_THRESHOLD= 0.001,
NMS_THRESHOLD= 0.45,
CLASS_NAMES= loadClassNames('./classes/char_det.txt'),
CLASS_COLORS= loadClassColors('./classes/char_det_color.txt'),
ONNXArchive= './onnx/yv8s_char_det.onnx')
det = []
tam = len(images_path)
cont = 0
for img_path in images_path:
aux = []
cont+=1
print('PROGRESSO: {:.2%}'.format(cont/tam))
image_name = img_path[len(Dataset_dir):]
print(image_name)
img = cv2.imread(img_path)
h, w = img.shape[:2]
lp_det.runYOLODetection(img)
for detection in lp_det.DETECTIONS:
aux.append(image_name)
aux.append(detection['CLASS_ID'])
aux.append(detection['BOX'][0] / w)
aux.append(detection['BOX'][1] / h)
aux.append(detection['BOX'][2] / w)
aux.append(detection['BOX'][3] / h)
try:
with open(arquivo_1, "a") as arq:
arq.write(f'{aux[0]} {aux[1]} {aux[2]} {aux[3]} {aux[4]} {aux[5]}\n')
except:
with open(arquivo_1, "w") as arq:
arq.write(f'{aux[0]} {aux[1]} {aux[2]} {aux[3]} {aux[4]} {aux[5]}\n')
aux.clear()
for lp in lp_det.DETECTIONS:
lp_coor = xywhTOxyixyf(lp['BOX'])
char_det.runYOLODetection(cutDetection(img, lp_coor))
char_det.DETECTIONS = n_max_conf(7, char_det.DETECTIONS)
if lp['BOX'][2]/lp['BOX'][3] > 2:
char_det.DETECTIONS = sorted(char_det.DETECTIONS, key= lambda dici: dici['BOX'][0])
elif lp['BOX'][2]/lp['BOX'][3] <= 2:
char_det.DETECTIONS = sortMotLP(char_det.DETECTIONS)
heuristica_placaBR(char_det.DETECTIONS, char_det.CLASS_NAMES)
for detection in char_det.DETECTIONS:
char_coor = xywhTOxyixyf(detection['BOX'])
changeReferenceXY(lp_coor, char_coor)
char_coor_xywh = xyxyTOxywh(char_coor)
aux.append(image_name)
aux.append(detection['CLASS_ID'])
aux.append(char_coor_xywh[0] / w)
aux.append(char_coor_xywh[1] / h)
aux.append(char_coor_xywh[2] / w)
aux.append(char_coor_xywh[3] / h)
try:
with open(arquivo_2, "a") as arq:
arq.write(f'{aux[0]} {aux[1]} {aux[2]} {aux[3]} {aux[4]} {aux[5]}\n')
except:
with open(arquivo_2, "w") as arq:
arq.write(f'{aux[0]} {aux[1]} {aux[2]} {aux[3]} {aux[4]} {aux[5]}\n')
aux.clear()
placa = ''
for detection in char_det.DETECTIONS:
placa += detection['CLASS_NAME']
try:
with open(arquivo_3, "a") as arq:
arq.write(f'{image_name} {placa}\n')
except:
with open(arquivo_3, "w") as arq:
arq.write(f'{image_name} {placa}\n')