12
12
import requests
13
13
14
14
class number (object ):
15
- def __init__ (self , gpu = False , times = 2 , ua = "ARNRS" ):
15
+ def __init__ (self , gpu = False , times = 1 , filter = 0.8 , ua = "ARNRS" , debug = False ):
16
16
assert type (gpu ) is bool
17
17
assert type (times ) is int and times >= 1
18
18
self .gpu = gpu
19
19
self .times = times
20
- self .similar = {"8" : "B" , "o " : "0" , "-" : "—" }
20
+ self .similar = {"8" : "B" , "O " : "0" , "-" : "—" , "1" : "/" , "l" : "I" , "2" : "Z" , "4" : "A " }
21
21
self .ua = ua
22
+ self .filter = filter
23
+ self .debug = debug
22
24
23
25
# Init detector and OCR
24
26
self .__detector = Detector (device = "gpu" if gpu else "cpu" )
25
27
self .__eocr = easyocr .Reader (['ch_sim' , 'en' ], gpu = self .gpu )
26
- self .__pocr = PaddleOCR (use_angle_cls = True , use_gpu = self .gpu , show_log = False )
28
+ self .__pocr = PaddleOCR (use_angle_cls = True , use_gpu = self .gpu , show_log = debug )
27
29
28
30
# Init database
29
- self .__database = []
31
+ self .__database = {}
30
32
i = 0
31
33
with open ('aircraftDatabase.csv' , "r" , encoding = 'utf-8' ) as fb :
32
34
for row in csv .reader (fb , skipinitialspace = True ):
33
35
if not i :
34
36
keys = row
35
37
else :
36
- self .__database .append (dict (zip (keys , row )))
38
+ self .__database [row [1 ]] = dict (zip (keys , row ))
39
+ self .__database [row [1 ].replace ("-" , "" )] = dict (zip (keys , row ))
37
40
i += 1
38
41
39
42
# Try to update database
43
+ '''
40
44
update_database_daemon_thread = threading.Thread(target=self.__update_database_daemon, name="Update Database Daemon Thread")
41
45
update_database_daemon_thread.daemon = True
42
46
update_database_daemon_thread.start()
47
+ '''
43
48
44
49
def __update_database_daemon (self ):
45
50
while True :
@@ -78,9 +83,8 @@ def search(self, keyword):
78
83
Search a plane by it registration number
79
84
:keyword Registration number
80
85
'''
81
- for i in self .__database :
82
- if i ["registration" ] == keyword :
83
- return i
86
+ if keyword .upper () in self .__database .keys () and not keyword .isdigit ():
87
+ return self .__database [keyword .upper ()]
84
88
85
89
# Similar characters replace
86
90
self .similar = {** self .similar , ** dict (zip (self .similar .values (), self .similar .keys ()))}
@@ -91,10 +95,12 @@ def search(self, keyword):
91
95
for c in condition :
92
96
for c_i in c :
93
97
keyword_temp = keyword .replace (c_i [0 ], c_i [1 ])
94
- for i in self .__database :
95
- if i ["registration" ] == keyword_temp :
96
- return i
98
+ if keyword_temp .upper () in self .__database .keys ():
99
+ return self .__database [keyword_temp .upper ()]
97
100
101
+ if keyword .upper () in self .__database .keys () and keyword .isdigit ():
102
+ return self .__database [keyword .upper ()]
103
+
98
104
return None
99
105
100
106
def recognize (self , image ):
@@ -124,27 +130,44 @@ def recognize(self, image):
124
130
ocr_result = []
125
131
ocr_filter = []
126
132
127
- for _ in range (2 ):
133
+ for _ in range (self . times ):
128
134
eocr_result = self .__eocr .readtext (img , detail = 1 )
135
+ if self .debug :
136
+ print (eocr_result )
137
+ print ("------------------------------" )
129
138
pocr_result = self .__pocr .ocr (img , cls = True )
130
-
139
+ if self .debug :
140
+ print (pocr_result )
141
+ print ("------------------------------" )
131
142
for e in eocr_result :
132
- if e [2 ] > 0.6 and e [1 ] not in ocr_filter :
143
+ if e [2 ] > self . filter and e [1 ] not in ocr_filter :
133
144
ocr_result .append (
134
145
(tuple ([tuple (i ) for i in e [0 ]]), e [1 ], e [2 ])
135
146
)
136
147
ocr_filter .append (e [1 ])
137
148
for p in pocr_result [0 ]:
138
- if p [1 ][1 ] > 0.6 and e [1 ][0 ] not in ocr_filter :
149
+ if p [1 ][1 ] > self . filter and p [1 ][0 ] not in ocr_filter :
139
150
ocr_result .append (
140
- (tuple ([tuple (i ) for i in p [0 ]]), e [1 ][0 ], e [1 ][1 ])
151
+ (tuple ([tuple (i ) for i in p [0 ]]), p [1 ][0 ], p [1 ][1 ])
141
152
)
142
- ocr_filter .append (e [1 ][0 ])
153
+ ocr_filter .append (p [1 ][0 ])
154
+
155
+ # OCR result tidy up
156
+
157
+
143
158
144
159
# Read database
145
160
for i in ocr_result :
146
161
r = self .search (i [1 ])
147
162
if r :
148
163
return r
149
164
150
- return None
165
+ if self .debug :
166
+ print (ocr_result )
167
+ return None
168
+
169
+ if __name__ == "__main__" :
170
+ num = number ()
171
+ for pic in os .listdir ("test" ):
172
+ print (pic , ":" )
173
+ print (num .recognize (os .path .join ("test" , pic )))
0 commit comments