Skip to content

Commit e9113cc

Browse files
committed
first version beta
1 parent 8e3b628 commit e9113cc

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
# arnrs
22

3-
Aircraft Registration Number Recognition System
3+
Aircraft Registration Number Recognition System
4+
5+
## Installation
6+
7+
1. Install [Pytorch Engine](https://pytorch.org/get-started/locally/) (CUDA, ROCm or CPU) with anaconda
8+
9+
2. Clone Detectron2 Framework from Github with follow command:
10+
11+
`git clone https://github.com/facebookresearch/detectron2.git`
12+
13+
3. Run Detectron2 Installtion with follow command:
14+
15+
`python -m pip install -e detectron2 -i https://pypi.tuna.tsinghua.edu.cn/simple`
16+
17+
4. Install [PaddlePaddle Engine](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/conda/windows-conda.html) (CUDA, ROCm or CPU) with anaconda
18+
19+
5. Install easyocr package with follow command:
20+
21+
`pip install easyocr -i https://pypi.tuna.tsinghua.edu.cn/simple`
22+
23+
6. Install paddleocr package with follow command:
24+
25+
`pip install paddleocr -i https://pypi.tuna.tsinghua.edu.cn/simple`
26+
27+
7. To solve opencv version issue, reinstall opencv and opencv-headless with follow commands (There may be some errors happen, you can ignore it)
28+
29+
`pip install opencv-python==4.2.0.34 -i https://pypi.tuna.tsinghua.edu.cn/simple`
30+
31+
`pip install opencv-python-headless==4.2.0.34 -i https://pypi.tuna.tsinghua.edu.cn/simple`
32+
33+
8. Replace packages rely in paddleocr from "tools.infer" to "paddleocr.tools.infer" with text editor, it's a bug from paddleocr
34+
35+
9. Finally enjoy it :)

detector.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from detectron2.engine import DefaultPredictor
2+
from detectron2.config import get_cfg
3+
from detectron2.data import MetadataCatalog
4+
from detectron2.utils.visualizer import ColorMode, Visualizer
5+
from detectron2 import model_zoo
6+
7+
class Detector:
8+
def __init__(self, mode = "zoo", model = "COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml", device = "cpu") -> None:
9+
self.cfg = get_cfg()
10+
11+
if mode == "zoo":
12+
# Load model config and pretrained model
13+
self.cfg.merge_from_file(model_zoo.get_config_file(model))
14+
self.cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model)
15+
16+
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
17+
self.cfg.MODEL.DEVICE = device
18+
19+
self.predictor = DefaultPredictor(self.cfg)
20+
21+
def image(self, image):
22+
predictions = self.predictor(image)
23+
24+
viz = Visualizer(image[:,:,::-1], metadata = MetadataCatalog.get(self.cfg.DATASETS.TRAIN[0]))
25+
#instance_mode = ColorMode.IMAGE_BW
26+
27+
output = viz.draw_instance_predictions(predictions["instances"].to("cpu"))
28+
29+
result = []
30+
for i in range(len(predictions["instances"].scores)):
31+
result.append(
32+
{
33+
"box": tuple([float(pos) for pos in [obj for obj in predictions["instances"].pred_boxes[i]][0]]),
34+
"score": float(predictions["instances"].scores[i]),
35+
"class": MetadataCatalog.get(self.cfg.DATASETS.TRAIN[0]).thing_classes[predictions["instances"].pred_classes[i]]
36+
}
37+
)
38+
39+
return output.get_image()[:,:,::-1], tuple(result)

main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)