@@ -78,6 +78,37 @@ def __update_database(self):
78
78
i += 1
79
79
break
80
80
81
+ def __distance (self , p1 , p2 ):
82
+ return ((p1 [0 ] - p2 [0 ])** 2 + (p1 [1 ] - p2 [1 ])** 2 ) ** 0.5
83
+
84
+ def __closest_pair (self , X , Y ):
85
+ if len (X ) <= 3 :
86
+ return min ([self .__distance (X [i ], X [j ]) for i in range (len (X )) for j in range (i + 1 , len (X ))])
87
+
88
+ mid = len (X )// 2
89
+ XL , XR = X [:mid ], X [mid :]
90
+ YL , YR = [p for p in Y if p in XL ], [p for p in Y if p in XR ]
91
+
92
+ d = min (self .__closest_pair (XL , YL ), self .__closest_pair (XR , YR ))
93
+
94
+ line = (X [mid ][0 ] + X [mid - 1 ][0 ]) / 2
95
+ YS = [p for p in Y if abs (p [0 ]- line ) < d ]
96
+
97
+ return min (d , self .__closest_split_pair (YS , d ))
98
+
99
+ def __closest_split_pair (self , Y , d ):
100
+ n = len (Y )
101
+ for i in range (n - 1 ):
102
+ for j in range (i + 1 , min (i + 8 , n )):
103
+ if self .__distance (Y [i ], Y [j ]) < d :
104
+ d = self .__distance (Y [i ], Y [j ])
105
+ return d
106
+
107
+ def __dis (self , p1 , p2 ):
108
+ X = p1 + p2
109
+ Y = sorted (X , key = lambda p : (p [0 ], p [1 ]))
110
+ return self .__closest_pair (X , Y )
111
+
81
112
def search (self , keyword ):
82
113
'''
83
114
Search a plane by it registration number
@@ -131,30 +162,73 @@ def recognize(self, image):
131
162
ocr_filter = []
132
163
133
164
for _ in range (self .times ):
165
+ # OCR recognize
166
+ pocr_result = self .__pocr .ocr (img , cls = True )
167
+ if self .debug :
168
+ print (pocr_result )
169
+ print ("------------------------------B-" )
134
170
eocr_result = self .__eocr .readtext (img , detail = 1 )
135
171
if self .debug :
136
172
print (eocr_result )
137
- print ("------------------------------" )
138
- pocr_result = self .__pocr .ocr (img , cls = True )
173
+ print ("------------------------------A-" )
174
+
175
+ # OCR results tidy up
176
+ for i in range (len (pocr_result [0 ])):
177
+ for j in range (len (pocr_result [0 ])):
178
+ if self .debug :
179
+ print ("------------------------------D-" )
180
+ print (pocr_result [0 ][i ][0 ], pocr_result [0 ][j ][0 ])
181
+ if i != j and len (pocr_result [0 ][i ][0 ]) == 4 and len (pocr_result [0 ][j ][0 ]) == 4 and self .__dis (pocr_result [0 ][i ][0 ], pocr_result [0 ][j ][0 ]) < 5 :
182
+ if self .debug :
183
+ print ("D Appended" )
184
+ pocr_result .append (((pocr_result [0 ][i ][0 ], pocr_result [0 ][j ][0 ]), pocr_result [0 ][i ][1 ][1 ] + pocr_result [0 ][j ][1 ][1 ], (pocr_result [0 ][i ][1 ][2 ] + pocr_result [0 ][j ][1 ][2 ]) / 2 ))
185
+ pocr_result .append (((pocr_result [0 ][j ][0 ], pocr_result [0 ][i ][0 ]), pocr_result [0 ][j ][1 ][1 ] + pocr_result [0 ][i ][1 ][1 ], (pocr_result [0 ][j ][1 ][2 ] + pocr_result [0 ][i ][1 ][2 ]) / 2 ))
186
+ else :
187
+ if self .debug :
188
+ disout = 0
189
+ if len (eocr_result [i ][0 ]) == 4 and len (eocr_result [j ][0 ]) == 4 :
190
+ disout = self .__dis (pocr_result [0 ][i ][0 ], pocr_result [0 ][j ][0 ])
191
+ print (i != j , len (pocr_result [0 ][i ][0 ]) == 4 , len (pocr_result [0 ][j ][0 ]) == 4 , disout )
192
+ print ("------------------------------D-" )
193
+
194
+ for i in range (len (eocr_result )):
195
+ for j in range (len (eocr_result )):
196
+ if self .debug :
197
+ print ("------------------------------C-" )
198
+ print (eocr_result [i ][0 ], eocr_result [j ][0 ])
199
+ if i != j and len (eocr_result [i ][0 ]) == 4 and len (eocr_result [j ][0 ]) == 4 and self .__dis (eocr_result [i ][0 ], eocr_result [j ][0 ]) < 5 :
200
+ if self .debug :
201
+ print ("C Appended" )
202
+ eocr_result .append (((eocr_result [i ][0 ], eocr_result [j ][0 ]), eocr_result [i ][1 ] + eocr_result [j ][1 ], (eocr_result [i ][2 ] + eocr_result [j ][2 ]) / 2 ))
203
+ eocr_result .append (((eocr_result [j ][0 ], eocr_result [i ][0 ]), eocr_result [j ][1 ] + eocr_result [i ][1 ], (eocr_result [j ][2 ] + eocr_result [i ][2 ]) / 2 ))
204
+ else :
205
+ if self .debug :
206
+ disout = 0
207
+ if len (eocr_result [i ][0 ]) == 4 and len (eocr_result [j ][0 ]) == 4 :
208
+ disout = self .__dis (eocr_result [i ][0 ], eocr_result [j ][0 ])
209
+ print (i != j , len (eocr_result [i ][0 ]) == 4 , len (eocr_result [j ][0 ]) == 4 , disout )
210
+ print ("------------------------------C-" )
211
+
212
+ pocr_result = [sorted (pocr_result [0 ], key = lambda x : len (x [1 ][0 ]), reverse = True )]
213
+ eocr_result = sorted (eocr_result , key = lambda x : len (x [1 ]), reverse = True )
214
+
139
215
if self .debug :
140
216
print (pocr_result )
141
- print ("------------------------------" )
142
- for e in eocr_result :
143
- if e [2 ] > self .filter and e [1 ] not in ocr_filter :
144
- ocr_result .append (
145
- (tuple ([tuple (i ) for i in e [0 ]]), e [1 ], e [2 ])
146
- )
147
- ocr_filter .append (e [1 ])
217
+ print (eocr_result )
218
+
219
+ # OCR results sum up
148
220
for p in pocr_result [0 ]:
149
221
if p [1 ][1 ] > self .filter and p [1 ][0 ] not in ocr_filter :
150
222
ocr_result .append (
151
223
(tuple ([tuple (i ) for i in p [0 ]]), p [1 ][0 ], p [1 ][1 ])
152
224
)
153
225
ocr_filter .append (p [1 ][0 ])
154
-
155
- # OCR result tidy up
156
-
157
-
226
+ for e in eocr_result :
227
+ if e [2 ] > self .filter and e [1 ] not in ocr_filter :
228
+ ocr_result .append (
229
+ (tuple ([tuple (i ) for i in e [0 ]]), e [1 ], e [2 ])
230
+ )
231
+ ocr_filter .append (e [1 ])
158
232
159
233
# Read database
160
234
for i in ocr_result :
@@ -167,7 +241,9 @@ def recognize(self, image):
167
241
return None
168
242
169
243
if __name__ == "__main__" :
244
+ import json
170
245
num = number ()
246
+ os .makedirs ("out" , exist_ok = True )
171
247
for pic in os .listdir ("test" ):
172
- print ( pic , ":" )
173
- print ( num .recognize (os .path .join ("test" , pic )))
248
+ with open ( os . path . join ( "out" , f" { pic } .json" ) , "w+" ) as fb :
249
+ fb . write ( json . dumps ( num .recognize (os .path .join ("test" , pic ) )))
0 commit comments