File size: 21,016 Bytes
1865436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# import sys
# sys.path.insert(0, '../Pipeline_POI_Engineering/src/sts')
# Copyright (c) Facebook, Inc. and its affiliates.
import argparse
import glob
import multiprocessing as mp
import os
import time
import cv2
import tqdm
import torch
import numpy as np
import json
from shapely.geometry import Point, Polygon
from genericmask import GenericMask
from detectron2.config import get_cfg
from detectron2.data.detection_utils import read_image
from detectron2.utils.logger import setup_logger
from detectron2.structures import BitMasks, Boxes, BoxMode, Keypoints, PolygonMasks, RotatedBoxes
from predictor import VisualizationDemo
# from beamsearch import beam_search
from editdistance import eval
import operator
# constants
WINDOW_NAME = "COCO detections"

dictionary = "aàáạảãâầấậẩẫăằắặẳẵAÀÁẠẢÃĂẰẮẶẲẴÂẦẤẬẨẪeèéẹẻẽêềếệểễEÈÉẸẺẼÊỀẾỆỂỄoòóọỏõôồốộổỗơờớợởỡOÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠiìíịỉĩIÌÍỊỈĨuùúụủũưừứựửữƯỪỨỰỬỮUÙÚỤỦŨyỳýỵỷỹYỲÝỴỶỸ"


def make_groups():
    groups = []
    i = 0
    while i < len(dictionary) - 5:
        group = [c for c in dictionary[i: i + 6]]
        i += 6
        groups.append(group)
    return groups


groups = make_groups()

TONES = ["", "ˋ", "ˊ", "﹒", "ˀ", "˜"]
SOURCES = ["ă", "â", "Ă", "Â", "ê", "Ê",
           "ô", "ơ", "Ô", "Ơ", "ư", "Ư", "Đ", "đ"]
TARGETS = ["aˇ", "aˆ", "Aˇ", "Aˆ", "eˆ", "Eˆ",
           "oˆ", "o˒", "Oˆ", "O˒", "u˒", "U˒", "D‑", "d‑"]


def parse_tone(word):
    res = ""
    tone = ""
    for char in word:
        if char in dictionary:
            for group in groups:
                if char in group:
                    if tone == "":
                        tone = TONES[group.index(char)]
                    res += group[0]
        else:
            res += char
    res += tone
    return res


def full_parse(word):
    word = parse_tone(word)
    res = ""
    for char in word:
        if char in SOURCES:
            res += TARGETS[SOURCES.index(char)]
        else:
            res += char
    return res


def correct_tone_position(word):
    word = word[:-1]
    if len(word) < 2:
        pass
    first_ord_char = ""
    second_order_char = ""
    for char in word:
        for group in groups:
            if char in group:
                second_order_char = first_ord_char
                first_ord_char = group[0]
    if word[-1] == first_ord_char and second_order_char != "":
        pair_chars = ["qu", "Qu", "qU", "QU", "gi", "Gi", "gI", "GI"]
        for pair in pair_chars:
            if pair in word and second_order_char in ["u", "U", "i", "I"]:
                return first_ord_char
        return second_order_char
    return first_ord_char


def decoder(recognition):
    for char in TARGETS:
        recognition = recognition.replace(char, SOURCES[TARGETS.index(char)])
    if len(recognition) < 1:
        return recognition
    if recognition[-1] in TONES:
        if len(recognition) < 2:
            return recognition
        replace_char = correct_tone_position(recognition)
        tone = recognition[-1]
        recognition = recognition[:-1]
        for group in groups:
            if replace_char in group:
                recognition = recognition.replace(
                    replace_char, group[TONES.index(tone)])
    return recognition


def decode_recognition(rec):
    CTLABELS = [" ", "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "ˋ", "ˊ", "﹒", "ˀ", "˜", "ˇ", "ˆ", "˒", "‑", ]
    last_char = False
    s = ''
    for c in rec:
        c = int(c)
        if 0 < c < 107:
            s += CTLABELS[c-1]
            last_char = c
        elif c == 0:
            s += u''
        else:
            last_char = False
    if len(s) == 0:
        s = ' '
    s = decoder(s)
    return s


def get_mini_boxes(contour, max_x, min_x, thr):
    bounding_box = cv2.minAreaRect(contour)
    # print('bbox', bounding_box)
    bounding_box = list(bounding_box)
    bounding_box[1] = list(bounding_box[1])
    if bounding_box[2] <= 45:
        bounding_box[1][0] = bounding_box[1][0]*thr
    else:
        bounding_box[1][1] = bounding_box[1][1]*thr
    bounding_box[1] = tuple(bounding_box[1])
    bounding_box = tuple(bounding_box)
    points = sorted(list(cv2.boxPoints(bounding_box)), key=lambda x: x[0])
    index_1, index_2, index_3, index_4 = 0, 1, 2, 3
    if points[1][1] > points[0][1]:
        index_1 = 0
        index_4 = 1
    else:
        index_1 = 1
        index_4 = 0
    if points[3][1] > points[2][1]:
        index_2 = 2
        index_3 = 3
    else:
        index_2 = 3
        index_3 = 2
    # p1 = np.array([min_x, points[index_1][1]])
    # p2 = np.array([max_x, points[index_2][1]])
    # p3 = np.array([max_x, points[index_3][1]])
    # p4 = np.array([min_x, points[index_4][1]])
    # box = [p1, p2, p3, p4]
    box = [
        points[index_1], points[index_2], points[index_3], points[index_4]
    ]
    return box


def get_mini_boxes_1(contour):
    bounding_box = cv2.minAreaRect(contour)
    points = sorted(list(cv2.boxPoints(bounding_box)), key=lambda x: x[0])
    index_1, index_2, index_3, index_4 = 0, 1, 2, 3
    if points[1][1] > points[0][1]:
        index_1 = 0
        index_4 = 1
    else:
        index_1 = 1
        index_4 = 0
    if points[3][1] > points[2][1]:
        index_2 = 2
        index_3 = 3
    else:
        index_2 = 3
        index_3 = 2

    box = [
        points[index_1], points[index_2], points[index_3], points[index_4]
    ]
    return box


def calculate_iou(box_1, box_2):
    # print(box_1, box_2)
    poly_1 = Polygon(box_1)
    poly_2 = Polygon(box_2)
    # print(poly_1.union(poly_2).area)
    try:
        iou = poly_1.intersection(poly_2).area / poly_1.union(poly_2).area
    except:
        iou = 0
    return iou

# def get_key(val, my_dict):
#     for key, value in my_dict.items():
#         if val in value:
#             return key


def merge_boxes(boxes, recs, trh):
    dict_bbox = {}
    x = 0
    for i in range(len(boxes)-2):
        tmp_box = [i]
        db_copy1 = dict_bbox.copy()
        for key, value in db_copy1.items():
            if i in value:
                tmp_box = db_copy1[key]
                del dict_bbox[key]
                break
        for j in range(i+1, len(boxes)-1):
            ba = cv2.minAreaRect(boxes[i].reshape(-1, 1, 2).astype(int))
            bb = cv2.minAreaRect(boxes[j].reshape(-1, 1, 2).astype(int))
            iou = calculate_iou(boxes[i], boxes[j])
            # scr = min(ba[1][0], bb[1][0])/max(ba[1][0], bb[1][0])
            if iou > trh:
                db_copy = dict_bbox.copy()
                check = False
                for key, value in db_copy.items():
                    if i in value:
                        check = True
                        tmp_box.remove(i)
                        tmp_box.extend(db_copy[key])
                        del dict_bbox[key]
                        break
                if check == False:
                    tmp_box.append(j)
        dict_bbox[x] = tmp_box
        x += 1
    recs_out = []
    db_clone = {}
    for key, value in dict_bbox.items():
        db_clone[key] = list(set(value))
    for key, value in db_clone.items():
        tmp_str = []
        for i in value:
            tmp_str.append([recs[i], cv2.minAreaRect(
                boxes[i].reshape(-1, 1, 2).astype(int))[0][0]])
        recs_out.append(tmp_str)
    return db_clone, recs_out


def combine(dict_box, h, w, boxes):
    bboxs = []
    for key, db in dict_box.items():
        list_box = []
        for j in db:
            list_box.append(boxes[j])
        h1 = h
        h2 = 0
        h3 = 0
        h4 = h
        w1 = w
        w2 = w
        w3 = 0
        w4 = 0
        for box in list_box:
            if box[0, 0] < h1:
                h1 = box[0, 0]
            if box[1, 0] > h2:
                h2 = box[1, 0]
            if box[2, 0] > h3:
                h3 = box[2, 0]
            if box[3, 0] < h4:
                h4 = box[3, 0]
            if box[0, 1] < w1:
                w1 = box[0, 1]
            if box[1, 1] < w2:
                w2 = box[1, 1]
            if box[2, 1] > w3:
                w3 = box[2, 1]
            if box[3, 1] > w4:
                w4 = box[3, 1]
            tmp = np.array([[h1, w1], [h2, w2], [h3, w3], [h4, w4]])
        bboxs.append(tmp.astype(np.int16))
    return bboxs


def rec_to_str(recs):
    rec_1 = []
    for rec in recs:
        i = sorted(rec, key=lambda x: x[1])
        print(i)
        i = " ".join(decoder(item[0]) for item in i)
        rec_1.append(i)
    return rec_1


def scale_points(mask):
    mask_tmp = mask.copy()
    for i in range(2, len(mask_tmp)-2):
        for j in range(2, len(mask_tmp[i])-2):
            if mask_tmp[i][j] != 0:
                mask[i-2][j-2] = mask[i-2][j-1] = mask[i-2][j] = mask[i-2][j+1] = mask[i-2][j+2] = mask[i-1][j-2] = mask[i-1][j-1] = mask[i-1][j] = mask[i-1][j+1] = mask[i-1][j+2] = mask[i][j-2] = mask[i][j-1] = mask[i][j +
                                                                                                                                                                                                                            1] = mask[i][j+2] = mask[i+1][j-2] = mask[i+1][j-1] = mask[i+1][j] = mask[i+1][j+1] = mask[i+1][j+2] = mask[i+2][j-2] = mask[i+2][j-1] = mask[i+2][j] = mask[i+2][j+1] = mask[i+2][j+2] = mask_tmp[i][j]
    return mask


def convert_boxes(boxes):
    if isinstance(boxes, Boxes) or isinstance(boxes, RotatedBoxes):
        return boxes.tensor.numpy()
    else:
        return np.asarray(boxes)


def convert_masks(masks_or_polygons, h, w):
    m = masks_or_polygons
    if isinstance(m, PolygonMasks):
        m = m.polygons
    if isinstance(m, BitMasks):
        m = m.tensor.numpy()
    if isinstance(m, torch.Tensor):
        m = m.numpy()
    ret = []
    for x in m:
        if isinstance(x, GenericMask):
            ret.append(x)
        else:
            ret.append(GenericMask(x, h, w))
    return ret


def setup_cfg(args):
    # load config from file and command-line arguments
    cfg = get_cfg()

    from projects.SWINTS.swints import add_SWINTS_config
    add_SWINTS_config(cfg)
    # -----

    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    # Set score_threshold for builtin models
    cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold
    cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold
    cfg.freeze()
    return cfg


def get_parser():
    parser = argparse.ArgumentParser(
        description="Detectron2 demo for builtin configs")
    parser.add_argument(
        "--config-file",
        default="src/sts/projects/SWINTS/configs/SWINTS-swin-finetune-vintext.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument("--inputfile", nargs="+",
                        help="A list of array of segmentation", default=["output/ss"])
    parser.add_argument(
        "--input",
        nargs="+",
        help="A list of space separated input images; "
        "or a single glob pattern such as 'directory/*.jpg'", default=["images"]
    )
    parser.add_argument(
        "--output",
        help="A file or directory to save output visualizations. "
        "If not given, will show output in an OpenCV window.", default="output/sts/"
    )
    parser.add_argument(
        "--output-visualize",
        help="A file or directory to save output visualizations. "
        "If not given, will show output in an OpenCV window.", default="output/visualize/"
    )
    parser.add_argument(
        "--confidence-threshold",
        type=float,
        default=0.5,
        help="Minimum score for instance predictions to be shown",
    )
    parser.add_argument(
        "--opts",
        help="Modify config options using the command-line 'KEY VALUE' pairs",
        default=[],
        nargs=argparse.REMAINDER,
    )
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn", force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    demo = VisualizationDemo(cfg)
    hh = []
    if args.inputfile:
        path_segment = args.inputfile[0]
    if args.input:
        if os.path.isdir(args.input[0]):
            args.input = [os.path.join(args.input[0], fname)
                          for fname in os.listdir(args.input[0])]
        elif len(args.input) == 1:
            args.input = glob.glob(os.path.expanduser(args.input[0]))
            assert args.input, "The input path(s) was not found"
        for path in tqdm.tqdm(args.input, disable=not args.output):
            # use PIL, to be consistent with evaluation
            print(path)
            txt_name = str(path.split("/")[-1].split(".")[0]) + '.txt'
            txt_file = os.path.join(path_segment, txt_name)
            img = read_image(path, format="BGR")
            h, w, _ = img.shape
            start_time = time.time()
            predictions, visualized_output = demo.run_on_image(
                img, args.confidence_threshold, path)
            # time_1 = time.time()-start_time

            mask = np.loadtxt(txt_file,  dtype=np.int32)
            # time_2 = time.time()-time_1
            mmax = np.amax(mask)
            if mmax == 0:
                mmax = 1
            mask = scale_points(mask)
            # time_3 = time.time()-time_2

            outs = cv2.findContours(
                (mask * int(255/mmax)).astype(np.uint8), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            if len(outs) == 3:
                img, contours, _ = outs[0], outs[1], outs[2]
            elif len(outs) == 2:
                contours, _ = outs[0], outs[1]

            box_sign = []
            for contour in contours:
                points = get_mini_boxes_1(contour)
                points = np.array(points)
                box_sign.append(points)
            # time_4 = time.time()-time_3
            dict_box_sign = {}
            dict_box_sign_out = {}
            dict_rec_sign = {}
            dict_rec_sign_out = {}
            in_signboard = 0
            # full_box = 0

            for i in range(len(box_sign)):
                dict_box_sign[i] = []
                dict_box_sign_out[i] = []
                dict_rec_sign[i] = []
                dict_rec_sign_out[i] = []
            list_limit = []
            for sig in box_sign:
                # print(sig)
                max_x = max(sig[0][0], sig[1][0], sig[2][0], sig[3][0])
                min_x = min(sig[0][0], sig[1][0], sig[2][0], sig[3][0])
                list_limit.append([max_x, min_x])
            if "instances" in predictions:

                beziers = []
                segments = []
                recc = []
                scoress = []
                instances = predictions["instances"].to(torch.device("cpu"))
                # print("instance",type(instances))
                instances = instances[instances.scores >
                                      args.confidence_threshold]
                boxes = instances.pred_boxes if instances.has(
                    "pred_boxes") else None
                scores = instances.scores if instances.has("scores") else None
                # classes = instances.pred_classes if instances.has("pred_classes") else None
                recs = instances.pred_rec if instances.has(
                    "pred_rec") else None
                # rec_score = instances.pred_rec_score if instances.has("pred_rec_score") else None

                masks = np.asarray(instances.pred_masks)
                masks = [GenericMask(x, h, w) for x in masks]
                masks = convert_masks(masks, h, w)
                polys = []
                for mask in masks:
                    polys.append(np.concatenate(
                        mask.polygons).reshape(-1, 2).tolist())

                # text box into signboard box
                for bezier, rec, score in zip(polys, recs, scores):
                    # print(bezier)
                    if score >= 0.5:
                        bezier = np.array(
                            bezier, dtype='int').reshape(-1, 1, 2)
                        bounding_box = cv2.minAreaRect(bezier)
                        midpoint = Point(bounding_box[0])
                        for i in range(len(box_sign)):
                            poly = Polygon(box_sign[i])
                            if midpoint.within(poly):
                                in_signboard += 1
                                dict_box_sign[i].append(bezier)
                                dict_rec_sign[i].append(
                                    full_parse(decode_recognition(rec)))
                # time_5 = time.time()-time_4
                for i in range(len(dict_box_sign)):
                    boxes = []
                    reces = []
                    for bezier, rec in zip(dict_box_sign[i], dict_rec_sign[i]):
                        unclip_ratio = 1.0
                        bezier = bezier.reshape(-1, 1, 2)
                        points = get_mini_boxes(
                            bezier, list_limit[i][0], list_limit[i][1], 3)
                        box = np.array(points, dtype=np.int16)

                        box[:, 0] = np.clip(np.round(box[:, 0]), 0, w)
                        box[:, 1] = np.clip(np.round(box[:, 1]), 0, h)

                        boxes.append(box.astype(np.int16))
                        reces.append(rec)

                    dict_box, rec_out = merge_boxes(boxes, reces, 0.1)

                    rec_outs = rec_to_str(rec_out)
                    bboxs = combine(dict_box, h, w, boxes)
                    # print(rec_outs)
                    dict_box_sign_out[i] = bboxs
                    dict_rec_sign_out[i] = rec_outs
                # time_6 = time.time()-time_5
            # Visualize image after merge boxes
            img_draw = cv2.imread(path)
            for i in range(len(dict_box_sign_out)):
                for j in range(len(dict_box_sign_out[i])):
                    pts = dict_box_sign_out[i][j]
                    x, y = pts[0][0], pts[0][1]
                    pts = np.array(pts, np.int32).reshape((-1, 1, 2))
                    isClosed = True
                    color = (255, 0, 0)
                    thickness = 2
                    img_draw = cv2.polylines(
                        img_draw, [pts], isClosed, color, thickness)
                    cv2.putText(img_draw, dict_rec_sign_out[i][j], (
                        x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
            # print(time_1, time_2, time_3, time_4, time_5, time_6)
            txt_name1 = str(path.split("/")[-1].split(".")[0]) + '_box.txt'
            txt_name2 = str(path.split("/")[-1].split(".")[0]) + '_text.txt'
            img_name = str(path.split("/")[-1].split(".")[0]) + '.jpg'
            if args.output:
                output_path_box = os.path.join(args.output, txt_name1)
                output_path_text = os.path.join(args.output, txt_name2)
                with open(output_path_box, 'w+', encoding='utf-8') as output_file_box:
                    for index, box in dict_box_sign_out.items():
                        arr = []
                        for box_ in box:
                            arr.append(box_.tolist())
                        dict_box_sign_out[index] = arr
                    dict_box_sign_out["file_name"] = str(path.split("/")[-1])
                    json.dump(dict_box_sign_out, output_file_box,
                              ensure_ascii=False)
                with open(output_path_text, 'w+', encoding='utf-8') as output_file_box:
                    dict_rec_sign_out["file_name"] = str(path.split("/")[-1])
                    json.dump(dict_rec_sign_out, output_file_box,
                              ensure_ascii=False)
            if args.output_visualize:
                cv2.imwrite(os.path.join(
                    args.output_visualize, img_name), img_draw)
            logger.info(
                "{}: detected {} instances in {:.2f}s".format(
                    path, len(predictions["instances"]
                              ), time.time() - start_time
                )
            )