-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateThresoldTraining.py
416 lines (277 loc) · 12.5 KB
/
CreateThresoldTraining.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 25 20:1 7:29 2022
@author: Alfonso Blanco
"""
######################################################################
# PARAMETERS
######################################################################
dirname = "test4Training\\images"
dirname_labels = "test4Training\\labels"
dirname_thresolds="test4Training\\thresolds"
######################################################################
import pytesseract
import numpy as np
from PIL import Image
import cv2
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
bias=4.3
X_resize=220
Y_resize=70
Incthreshold=1.0
ContLoopMax=400
######################################################################
import os
import re
import imutils
#####################################################################
"""
Copied from https://gist.github.com/endolith/334196bac1cac45a4893#
other source:
https://stackoverflow.com/questions/46084476/radon-transformation-in-python
"""
from skimage.transform import radon
import numpy
from numpy import mean, array, blackman, sqrt, square
from numpy.fft import rfft
try:
# More accurate peak finding from
# https://gist.github.com/endolith/255291#file-parabolic-py
from parabolic import parabolic
def argmax(x):
return parabolic(x, numpy.argmax(x))[0]
except ImportError:
from numpy import argmax
def GetRotationImage(image):
I=image
I = I - mean(I) # Demean; make the brightness extend above and below zero
# Do the radon transform and display the result
sinogram = radon(I)
# Find the RMS value of each row and find "busiest" rotation,
# where the transform is lined up perfectly with the alternating dark
# text and white lines
# rms_flat does no exist in recent versions
#r = array([mlab.rms_flat(line) for line in sinogram.transpose()])
r = array([sqrt(mean(square(line))) for line in sinogram.transpose()])
rotation = argmax(r)
#print('Rotation: {:.2f} degrees'.format(90 - rotation))
#plt.axhline(rotation, color='r')
# Plot the busy row
row = sinogram[:, rotation]
N = len(row)
# Take spectrum of busy row and find line spacing
window = blackman(N)
spectrum = rfft(row * window)
frequency = argmax(abs(spectrum))
return rotation, spectrum, frequency
#####################################################################
#########################################################################
def loadimages (dirname ):
#########################################################################
# adapted from:
# https://www.aprendemachinelearning.com/clasificacion-de-imagenes-en-python/
# by Alfonso Blanco García
########################################################################
imgpath = dirname + "\\"
images = []
Licenses=[]
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):
Cont=Cont+1
filepath = os.path.join(root, filename)
License=filename[:len(filename)-4]
image = cv2.imread(filepath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
images.append(gray)
Licenses.append(License)
return images, Licenses
#########################################################################
def loadlabels (dirname ):
#########################################################################
########################################################################
lblpath = dirname + "\\"
labels = []
print("Reading labels from ",lblpath)
Cont=0
for root, dirnames, filenames in os.walk(lblpath):
for filename in filenames:
if re.search("\.(txt)$", filename):
Cont=Cont+1
#if Cont > 3: break
filepath = os.path.join(root, filename)
filepath = os.path.join(root, filename)
f=open(filepath,"r")
Conta=0
for linea in f:
lineadelTrain =linea.split(" ")
if lineadelTrain[0] == "0":
Conta=Conta+1
labels.append(linea)
break
f.close()
if Conta==0:
print("Rare labels without tag 0 on " + filename )
return labels
# Copied from https://learnopencv.com/otsu-thresholding-with-opencv/
def OTSU_Threshold(image):
# Set total number of bins in the histogram
bins_num = 256
# Get the image histogram
hist, bin_edges = np.histogram(image, bins=bins_num)
# Get normalized histogram if it is required
#if is_normalized:
hist = np.divide(hist.ravel(), hist.max())
# Calculate centers of bins
bin_mids = (bin_edges[:-1] + bin_edges[1:]) / 2.
# Iterate over all thresholds (indices) and get the probabilities w1(t), w2(t)
weight1 = np.cumsum(hist)
weight2 = np.cumsum(hist[::-1])[::-1]
# Get the class means mu0(t)
mean1 = np.cumsum(hist * bin_mids) / weight1
# Get the class means mu1(t)
mean2 = (np.cumsum((hist * bin_mids)[::-1]) / weight2[::-1])[::-1]
inter_class_variance = weight1[:-1] * weight2[1:] * (mean1[:-1] - mean2[1:]) ** 2
# Maximize the inter_class_variance function val
index_of_max_val = np.argmax(inter_class_variance)
threshold = bin_mids[:-1][index_of_max_val]
print("Otsu's algorithm implementation thresholding result: ", threshold)
return threshold
###########################################################
# MAIN
##########################################################
images, Licenses =loadimages(dirname)
labels=loadlabels(dirname_labels)
print("Number of imagenes : " + str(len(images)))
print("Number of labels : " + str(len(labels)))
print("Number of licenses : " + str(len(Licenses)))
TotHits=0
TotFailures=0
NumberImageOrder=0
for i in range (len(images)):
NumberImageOrder=NumberImageOrder+1
lineaLabel =labels[i].split(" ")
# Meaning of fields in files labels
#https://github.com/ultralytics/yolov5/issues/2293
#
x_center=float(lineaLabel[1])
y_center=float(lineaLabel[2])
width=float(lineaLabel[3])
heigh=float(lineaLabel[4])
x_start= x_center - width*0.5
x_end=x_center + width*0.5
y_start= y_center - heigh*0.5
y_end=y_center + heigh*0.5
X_start=int(x_start*416)
X_end=int(x_end*416)
Y_start=int(y_start*416)
Y_end=int(y_end*416)
# Clipping the boxes in two positions helps
# in license plate reading
X_start=X_start + 3
Y_start=Y_start + 2
image=images[i]
License=Licenses[i]
#cv2.imshow("Test ", image)
#cv2.waitKey()
SwEnd=0
lineaw=[]
#lineaw.append(TrueLicenses[i])
SumBrightness=np.sum(image)
lineaw.append(str(SumBrightness))
Desv=np.std(image)
lineaw.append(str(Desv))
if Desv < 45:
print("Image with low standard deviation, will be difficult to be recognized")
#print("Car" + str(NumberImageOrder) + " Brillo : " +str(SumBrightness) +
# " Desviacion : " + str(Desv))
threshold=(SumBrightness/177529.84) + bias
print("SumBrightness = " + str(SumBrightness) + " Desviacion = " + str(Desv))
#print(" threshold " + str(threshold))
gray=image[Y_start:Y_end, X_start:X_end]
gray=cv2.resize(gray,None,fx=1.78,fy=1.78,interpolation=cv2.INTER_CUBIC)
gray = cv2.resize(gray, (X_resize,Y_resize), interpolation = cv2.INTER_AREA)
SumBrightnessLic=np.sum(gray)
DesvLic=np.std(gray)
rotation, spectrum, frquency =GetRotationImage(gray)
rotation=90 - rotation
#print("Car" + str(NumberImageOrder) + " Brillo : " +str(SumBrightnessLic) +
# " Desviacion : " + str(DesvLic))
if (rotation > 0 and rotation < 30) or (rotation < 0 and rotation > -30):
gray=imutils.rotate(gray,angle=rotation)
#else:
# continue
Conta=0
ContLoop=0
SwEnd=0
Llicenses=[]
SwEncontrado=0
while (ContLoop < ContLoopMax):
if ContLoop >ContLoopMax: break
ContLoop=ContLoop+1
#https://java2blog.com/cv2-threshold-python/
#https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html
#https://aicha-fatrah.medium.com/improve-the-quality-of-your-ocr-information-extraction-ebc93d905ac4
ret, gray1=cv2.threshold(gray,threshold,255, cv2.THRESH_BINARY)
#cv2.imshow("Prueba", gray1)
#cv2.waitKey()
text = pytesseract.image_to_string(gray1, lang='eng', \
config='--psm 13 --oem 3')
#new_gray1 = Image.fromarray(gray1)
#text = tesserocr.image_to_text(new_gray1)
text = ''.join(char for char in text if char.isalnum())
print(text)
if (text[0:len(License)]==License) or \
(text[1:len(License)+1]==License) :
with open( dirname_thresolds+"\\" + License +".txt","w") as w:
print("FOUNDED License Plate, thresold = " +str(threshold))
SwEncontrado=1
lineaw.append(str(threshold))
lineaWrite =','.join(lineaw)
lineaWrite=lineaWrite + "\n"
w.write(lineaWrite)
w.close
TotHits=TotHits+1
break
#
# Halfway through the loop,
# it searches in the negative direction for the threshold
#
if ContLoop==ContLoopMax/2:
threshold=threshold- Incthreshold*ContLoop
if ContLoop>=ContLoopMax/2:
threshold=threshold-Incthreshold
else:
threshold=threshold+Incthreshold
#
# if it is not found try to try with the OTSU thresholdr
#
if SwEncontrado==0:
threshold=OTSU_Threshold(image)
ret, gray1=cv2.threshold(gray,threshold,255, cv2.THRESH_BINARY)
#cv2.imshow("Prueba", gray1)
#cv2.waitKey()
text = pytesseract.image_to_string(gray1, lang='eng', \
config='--psm 13 --oem 3 --dpi 300')
text = ''.join(char for char in text if char.isalnum())
print(text)
if (text[0:len(License)]==License) or \
(text[1:len(License)+1]==License) :
with open( dirname_thresolds+"\\" + License +".txt","w") as w:
print("FOUNDED by OTSU thresold = " +str(threshold))
SwEncontrado=1
lineaw.append(str(threshold))
lineaWrite =','.join(lineaw)
lineaWrite=lineaWrite + "\n"
w.write(lineaWrite)
w.close
TotHits=TotHits+1
else:
TotFailures=TotFailures+1
print("")
print(" Total Hits = " + str(TotHits))
print(" Total failures = " + str(TotFailures))