-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_infer.py
463 lines (403 loc) · 17.5 KB
/
single_infer.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import argparse
import json
import math
import os
import sys
import time
import cv2
import matplotlib.pyplot as plt
import numpy as np
import torch
sys.path.append("/root/VinMap")
import shutil
# classification
import mmcv
from classification.mmcls.apis import inference_model, init_model, show_result_pyplot
from classification.mmcls.apis.inference import data_infer
from classification.mmcls.utils import auto_select_device
# vocab_filtering
from eval import check, mapper
from mmcv import Config
from mmocr.utils.ocr import MMOCR
from PIL import Image, ImageDraw, ImageFont
from tools.minimum_hull import minimum_bounding_rectangle
# segmentation
from tqdm import tqdm
from unidecode import unidecode
from vietocr.tool.config import Cfg
# recognition
from vietocr.tool.predictor import Predictor
def parse_args():
parser = argparse.ArgumentParser(description="Release-21.01.16")
parser.add_argument(
"-configCls",
"--configCls",
default="classification/configs/efficientnet/efficientnet-b4_8xb32_in1k.py",
help="Map Classification Configuration",
)
parser.add_argument(
"-configSegment",
"--configSegment",
default="configuration/maskrcnn_resnext101_DCN_160e_icdar.py",
help="Text Segmentation Configuration",
)
parser.add_argument(
"-cls_weights", "--cls_weights", default="../weight/effnetb4_vnmap.pth", help="Map Classification Weight"
)
parser.add_argument(
"-det_weights",
"--det_weights",
default="../weight/resnext101_DCN_160e_epoch_150.pth",
help="Text Segmentation Weight",
)
parser.add_argument(
"-rec_weights", "--rec_weights", default="../weight/transformerocr_btc.pth", help="Text Recognition Weight"
)
parser.add_argument("--root", "--root", default="None", help="Root Workdirectory")
parser.add_argument("-input_images", "--input_images", default="None", help="Input images path")
parser.add_argument("-output_destination", "--output_destination", default="None", help="Output path")
parser.add_argument("-single_infer", "--single_infer", default="True", help="Type of single infering")
parser.add_argument(
"-single_infer_image", "--single_infer_image", default="None", help="Input path of single image"
)
parser.add_argument(
"-single_infer_path", "--single_infer_path", default="None", help="Output path of single infer"
)
args = parser.parse_args()
return args
def excecute(root, folder_path, image_folder_path):
print("Excecuting OCR")
os.makedirs(folder_path + "/" + "predicted")
for f in tqdm(os.listdir(image_folder_path)):
file_type = f.split(".")[1]
file_name = f.split(".")[0]
image = cv2.imread(os.path.join(image_folder_path, f))
ff = None
try:
ff = open(os.path.join(folder_path, "out_" + file_name + ".json"), "r")
except:
continue
all_data = json.load(ff)
boundary_results = all_data["boundary_result"]
results = []
ii = Image.fromarray(np.array(image))
draw = ImageDraw.Draw(ii)
for boundary_result in boundary_results:
np_image = np.array(image)
info = []
if boundary_result[-1] < 0.1:
continue
points = []
for i in range(0, len(boundary_result) - 2, 2):
points.append(tuple([int(boundary_result[i]), int(boundary_result[i + 1])]))
points = np.array(points)
try:
four_points = minimum_bounding_rectangle(points)
except:
continue
four_points = np.array(four_points).astype(int)
rect = cv2.minAreaRect(four_points)
box = cv2.boxPoints(rect)
oriented_rec = np.int0(box)
x_tl, y_tl = min(oriented_rec[:, 0]), min(oriented_rec[:, 1])
x_br, y_br = max(oriented_rec[:, 0]), max(oriented_rec[:, 1])
if x_tl < 0 or y_tl < 0 or x_br >= np_image.shape[1] or y_br >= np_image.shape[0]:
np_image = cv2.copyMakeBorder(np_image, 500, 500, 500, 500, cv2.BORDER_CONSTANT, value=[0, 0, 0])
np_image = np_image[y_tl + 500 : y_br + 500, x_tl + 500 : x_br + 500]
else:
np_image = np_image[y_tl:y_br, x_tl:x_br]
try:
s = detector.predict(Image.fromarray(np_image))
except:
continue
font_path = os.path.join(cv2.__path__[0], "qt", "fonts", "DejaVuSans.ttf")
font = ImageFont.truetype(font_path, size=16)
draw.text((x_tl, y_tl), str(s), fill="red", font=font)
draw.rectangle([x_tl, y_tl, x_br, y_br], outline="blue")
clockwise = np.flip(oriented_rec, axis=0)
for p in clockwise:
info.append(str(p[0]))
info.append(str(p[1]))
info.append(str(s))
results.append(",".join(info))
# ii.save(folder_path + '/drive/' + file_name + '_txt_.jpg')
file_submit_name = os.path.join(folder_path + "/" + "predicted", file_name + ".txt")
with open(file_submit_name, "w") as file_submit:
for line_string in results:
file_submit.write(line_string)
file_submit.write("\n")
class ModelWrapper:
def __init__(self, args):
self.configCls = args.configCls
self.configSegment = args.configSegment
self.cls_weights = args.cls_weights
self.det_weights = args.det_weights
self.rec_weights = args.rec_weights
self.root = args.root
self.input_images = args.input_images
self.output_destination = args.output_destination
self.device = "cuda:0"
# Setting up configuration
self.SegmentationModel = MMOCR(
det="MaskRCNN_IC15",
det_config=self.configSegment,
recog=None,
det_ckpt=self.det_weights,
device=self.device,
)
print("Completed Loading Det Model")
def e2e_infer(self, filename, folder_path):
"""
e2e Inferences
filename: image file path
folder_path: prediction archive
Output: Resulting
Postitive:
(VN map) and
do not contain (Truong Sa and Hoang Sa)
Negative:
(not VN map) or
(VN map and contain (Truong Sa or Hoang Sa))
Results:
0: Positive
1: Negative1
2: Negative2
"""
if os.path.exists(folder_path + "/" + "predicted") == False:
os.makedirs(folder_path + "/" + "predicted")
else:
shutil.rmtree(folder_path + "/" + "predicted")
os.makedirs(folder_path + "/" + "predicted")
if os.path.exists(folder_path + "/" + "fig") == False:
os.makedirs(folder_path + "/" + "fig")
else:
shutil.rmtree(folder_path + "/" + "fig")
os.makedirs(folder_path + "/" + "fig")
seg_res = self.SegmentationModel.readtext(
filename, output=folder_path + "/" + "fig", export=folder_path + "/" + "fig"
)
self.SegmentationModel = None
torch.cuda.empty_cache()
## Recognition
device = self.device
config_rec = Cfg.load_config_from_name("vgg_transformer")
config_rec["weights"] = self.rec_weights
config_rec["cnn"]["pretrained"] = False
config_rec["device"] = device
## Classification
cfg = Config.fromfile(self.configCls)
cfg.device = device
cfg.model.head.num_classes = 2
cfg.model.head.topk = (1,)
cfg.model.backbone.init_cfg = dict(type="Pretrained", checkpoint=self.cls_weights, prefix="backbone")
cfg.model.head.num_classes = 2
cfg.model.head.topk = (1,)
# Model load weights
self.ClsModel = init_model(cfg, self.cls_weights, device=device)
self.ClsModel.CLASSES = ["notvietnam", "vietnam"]
self.ClsModel.cfg = cfg
print("Completed Loading Cls Model")
self.OCRModel = Predictor(config_rec)
print("Completed Loading Rec Model")
results = 0
image_folder_path = folder_path + "/" + "fig"
for f in os.listdir(image_folder_path):
# Read image
try:
image = cv2.imread(filename)
except:
continue
file_type = f.split(".")[-1]
if file_type != "json":
continue
## CLASSIFICATION
data = data_infer(self.ClsModel.cfg, filename, device)
# forward the model
with torch.no_grad():
scores = self.ClsModel(return_loss=False, **data)
pred_score = np.max(scores, axis=1)[0]
pred_label = np.argmax(scores, axis=1)[0]
result = {"pred_label": pred_label, "pred_score": float(pred_score)}
result["pred_class"] = self.ClsModel.CLASSES[result["pred_label"]]
if result["pred_label"] == 0: # Not VietNam
results = 1
continue
ff = None
try:
ff = open(os.path.join(image_folder_path, f), "r")
except:
results = 0
continue
all_data = json.load(ff)
boundary_results = all_data["boundary_result"]
result_str = []
ii = Image.fromarray(np.array(image))
for boundary_result in boundary_results:
np_image = np.array(image)
info = []
if boundary_result[-1] < 0.1:
continue
points = []
for i in range(0, len(boundary_result) - 2, 2):
points.append(tuple([int(boundary_result[i]), int(boundary_result[i + 1])]))
points = np.array(points)
try:
four_points = minimum_bounding_rectangle(points)
except:
continue
four_points = np.array(four_points).astype(int)
rect = cv2.minAreaRect(four_points)
box = cv2.boxPoints(rect)
oriented_rec = np.int0(box)
x_tl, y_tl = min(oriented_rec[:, 0]), min(oriented_rec[:, 1])
x_br, y_br = max(oriented_rec[:, 0]), max(oriented_rec[:, 1])
if x_tl < 0 or y_tl < 0 or x_br >= np_image.shape[1] or y_br >= np_image.shape[0]:
np_image = cv2.copyMakeBorder(np_image, 500, 500, 500, 500, cv2.BORDER_CONSTANT, value=[0, 0, 0])
np_image = np_image[y_tl + 500 : y_br + 500, x_tl + 500 : x_br + 500]
else:
np_image = np_image[y_tl:y_br, x_tl:x_br]
try:
s = self.OCRModel.predict(Image.fromarray(np_image))
except:
continue
clockwise = np.flip(oriented_rec, axis=0)
for p in clockwise:
info.append(str(p[0]))
info.append(str(p[1]))
info.append(str(s))
result_str.append(unidecode(info[-1].lower()))
if check(result_str) == 1:
results = 2
else:
results = 0
# Postitive:
# (VN map) and
# do not contain (Truong Sa and Hoang Sa)
# Negative:
# (not VN map) or
# (VN map and contain (Truong Sa or Hoang Sa))
if results == 0:
print("This is VN map. It does not contain (Truong Sa and Hoang Sa).. ALERT")
elif results == 1:
print("This is not VN map.. SKIPPED")
elif results == 2:
print("This is VN map, It contains (Truong Sa or Hoang Sa).. OK")
def process_image(self):
"""
Mass Inference
Outputing logging files -> final results from eval.py
"""
folder_path = self.output_destination
image_folder_path = self.input_images
if os.path.exists(folder_path + "/" + "predicted") == False:
os.makedirs(folder_path + "/" + "predicted")
seg_res = self.SegmentationModel.readtext(
self.input_images, output=self.output_destination, export=self.output_destination
)
self.SegmentationModel = None
torch.cuda.empty_cache()
## Recognition
device = "cuda:0"
config_rec = Cfg.load_config_from_name("vgg_transformer")
config_rec["weights"] = self.rec_weights
config_rec["cnn"]["pretrained"] = False
config_rec["device"] = device
## Classification
cfg = Config.fromfile(self.configCls)
cfg.device = auto_select_device()
cfg.model.head.num_classes = 2
cfg.model.head.topk = (1,)
cfg.model.backbone.init_cfg = dict(type="Pretrained", checkpoint=self.cls_weights, prefix="backbone")
cfg.model.head.num_classes = 2
cfg.model.head.topk = (1,)
# Model load weights
self.ClsModel = init_model(cfg, self.cls_weights, device=device)
self.ClsModel.CLASSES = ["notvietnam", "vietnam"]
self.ClsModel.cfg = cfg
print("Completed Loading Cls Model")
self.OCRModel = Predictor(config_rec)
print("Completed Loading Rec Model")
for f in tqdm(os.listdir(image_folder_path)):
file_type = f.split(".")[-1]
file_name = ".".join(f.split(".")[0:-1])
## CLASSIFICATION
device = "cuda:0"
data = data_infer(self.ClsModel.cfg, os.path.join(image_folder_path, f), device)
# forward the model
with torch.no_grad():
scores = self.ClsModel(return_loss=False, **data)
pred_score = np.max(scores, axis=1)[0]
pred_label = np.argmax(scores, axis=1)[0]
result = {"pred_label": pred_label, "pred_score": float(pred_score)}
result["pred_class"] = self.ClsModel.CLASSES[result["pred_label"]]
if result["pred_label"] == 0: # Not VietNam
file_submit_name = os.path.join(folder_path + "/" + "predicted", file_name + ".txt")
with open(file_submit_name, "w") as file_submit:
file_submit.write("notvnmap")
file_submit.write("\n")
continue
image = cv2.imread(os.path.join(image_folder_path, f))
ff = None
try:
ff = open(os.path.join(folder_path, "out_" + file_name + ".json"), "r")
except:
file_submit_name = os.path.join(folder_path + "/" + "predicted", file_name + ".txt")
with open(file_submit_name, "a") as file_submit:
file_submit.write("vnmap")
file_submit.write("\n")
continue
all_data = json.load(ff)
boundary_results = all_data["boundary_result"]
results = []
ii = Image.fromarray(np.array(image))
# draw = ImageDraw.Draw(ii)
for boundary_result in boundary_results:
np_image = np.array(image)
info = []
if boundary_result[-1] < 0.1:
continue
points = []
for i in range(0, len(boundary_result) - 2, 2):
points.append(tuple([int(boundary_result[i]), int(boundary_result[i + 1])]))
points = np.array(points)
try:
four_points = minimum_bounding_rectangle(points)
except:
continue
four_points = np.array(four_points).astype(int)
rect = cv2.minAreaRect(four_points)
box = cv2.boxPoints(rect)
oriented_rec = np.int0(box)
x_tl, y_tl = min(oriented_rec[:, 0]), min(oriented_rec[:, 1])
x_br, y_br = max(oriented_rec[:, 0]), max(oriented_rec[:, 1])
if x_tl < 0 or y_tl < 0 or x_br >= np_image.shape[1] or y_br >= np_image.shape[0]:
np_image = cv2.copyMakeBorder(np_image, 500, 500, 500, 500, cv2.BORDER_CONSTANT, value=[0, 0, 0])
np_image = np_image[y_tl + 500 : y_br + 500, x_tl + 500 : x_br + 500]
else:
np_image = np_image[y_tl:y_br, x_tl:x_br]
try:
s = self.OCRModel.predict(Image.fromarray(np_image))
except:
continue
# font_path = os.path.join(cv2.__path__[0],'qt','fonts','DejaVuSans.ttf')
# font = ImageFont.truetype(font_path, size=16)
# draw.text((x_tl, y_tl), str(s), fill="red", font=font)
# draw.rectangle([x_tl, y_tl, x_br, y_br], outline="blue")
clockwise = np.flip(oriented_rec, axis=0)
for p in clockwise:
info.append(str(p[0]))
info.append(str(p[1]))
info.append(str(s))
results.append(",".join(info))
file_submit_name = os.path.join(folder_path + "/" + "predicted", file_name + ".txt")
with open(file_submit_name, "w") as file_submit:
for line_string in results:
file_submit.write(line_string)
file_submit.write("\n")
if __name__ == "__main__":
args = parse_args()
wrapper = ModelWrapper(args)
if args.single_infer == "False":
wrapper.process_image()
else:
wrapper.e2e_infer(args.single_infer_image, args.single_infer_path)