1
+ import os
2
+
3
+ from detector import Detector
4
+ from paddleocr import PaddleOCR
5
+
6
+ import cv2
7
+ import easyocr
8
+ import requests
9
+
10
+ class number (object ):
11
+ def __init__ (self , gpu = False , times = 2 ):
12
+ assert type (gpu ) is bool
13
+ assert type (times ) is int and times >= 1
14
+ self .gpu = gpu
15
+ self .times = times
16
+
17
+ # Init detector and OCR
18
+ self .__detector = Detector (device = "gpu" if gpu else "cpu" )
19
+ self .__eocr = easyocr .Reader (['ch_sim' , 'en' ], gpu = self .gpu )
20
+ self .__pocr = PaddleOCR (use_angle_cls = True , use_gpu = self .gpu , show_log = False )
21
+
22
+ def recognize (self , image ):
23
+ '''
24
+ Recognize aircraft registration number with detection
25
+ and ocr powered by pytorch and paddlepaddle engine.
26
+ :image Accept numpy array or image file path
27
+ '''
28
+ if type (image ) is str :
29
+ path = os .path .abspath (image )
30
+ image = cv2 .imread (path )
31
+
32
+ result = self .__detector .image (image )
33
+ # Subjective judgment
34
+ area_max = 0
35
+ area_index = 0
36
+ for i in range (len (result [1 ])):
37
+ d = result [1 ][i ]
38
+ this_area = ((d ["box" ][1 ] - d ["box" ][0 ]) ** 2 + (d ["box" ][3 ] - d ["box" ][2 ]) ** 2 ) ** 0.5
39
+ if this_area > area_max and result [1 ][i ]["class" ] == "airplane" :
40
+ area_max = this_area
41
+ area_index = i
42
+
43
+ i = result [1 ][area_index ]
44
+ img = image [int (i ["box" ][1 ]):int (i ["box" ][3 ]), int (i ["box" ][0 ]):int (i ["box" ][2 ])]
45
+
46
+ ocr_result = []
47
+ ocr_filter = []
48
+
49
+ for _ in range (2 ):
50
+ eocr_result = self .__eocr .readtext (img , detail = 1 )
51
+ pocr_result = self .__pocr .ocr (img , cls = True )
52
+
53
+ for e in eocr_result :
54
+ if e [2 ] > 0.6 and e [1 ] not in ocr_filter :
55
+ ocr_result .append (
56
+ (tuple ([tuple (i ) for i in e [0 ]]), e [1 ], e [2 ])
57
+ )
58
+ ocr_filter .append (e [1 ])
59
+ for p in pocr_result [0 ]:
60
+ if p [1 ][1 ] > 0.6 and e [1 ][0 ] not in ocr_filter :
61
+ ocr_result .append (
62
+ (tuple ([tuple (i ) for i in p [0 ]]), e [1 ][0 ], e [1 ][1 ])
63
+ )
64
+ ocr_filter .append (e [1 ][0 ])
65
+
66
+ # Read database
67
+ for i in ocr_result :
68
+ db = requests .post ("http://www.airframes.org/" , data = {"reg1" : i [1 ]}).text
69
+ if "No data found on this query." not in db :
70
+ return i
71
+
72
+ return None
0 commit comments