utility.py 5.1 KB
Newer Older
W
WenmuZhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
文幕地方's avatar
文幕地方 已提交
14
import random
文幕地方's avatar
文幕地方 已提交
15
import ast
文幕地方's avatar
文幕地方 已提交
16
from PIL import Image, ImageDraw, ImageFont
W
WenmuZhou 已提交
17
import numpy as np
18
from tools.infer.utility import draw_ocr_box_txt, str2bool, init_args as infer_args
W
WenmuZhou 已提交
19 20 21 22 23 24


def init_args():
    parser = infer_args()

    # params for output
25
    parser.add_argument("--output", type=str, default='./output')
W
WenmuZhou 已提交
26
    # params for table structure
W
WenmuZhou 已提交
27
    parser.add_argument("--table_max_len", type=int, default=488)
文幕地方's avatar
文幕地方 已提交
28
    parser.add_argument("--table_algorithm", type=str, default='TableAttn')
W
WenmuZhou 已提交
29
    parser.add_argument("--table_model_dir", type=str)
文幕地方's avatar
文幕地方 已提交
30
    parser.add_argument(
文幕地方's avatar
文幕地方 已提交
31
        "--merge_no_span_structure", type=str2bool, default=True)
32 33 34
    parser.add_argument(
        "--table_char_dict_path",
        type=str,
Z
zhoujun 已提交
35
        default="../ppocr/utils/dict/table_structure_dict_ch.txt")
36
    # params for layout
A
an1018 已提交
37
    parser.add_argument("--layout_model_dir", type=str)
38
    parser.add_argument(
A
an1018 已提交
39
        "--layout_dict_path",
40
        type=str,
文幕地方's avatar
文幕地方 已提交
41
        default="../ppocr/utils/dict/layout_dict/layout_publaynet_dict.txt")
文幕地方's avatar
文幕地方 已提交
42
    parser.add_argument(
A
an1018 已提交
43
        "--layout_score_threshold",
A
an1018 已提交
44 45 46 47
        type=float,
        default=0.5,
        help="Threshold of score.")
    parser.add_argument(
文幕地方's avatar
文幕地方 已提交
48 49 50 51
        "--layout_nms_threshold",
        type=float,
        default=0.5,
        help="Threshold of nms.")
52 53
    # params for kie
    parser.add_argument("--kie_algorithm", type=str, default='LayoutXLM')
54
    parser.add_argument("--ser_model_dir", type=str)
文幕地方's avatar
文幕地方 已提交
55 56
    parser.add_argument("--re_model_dir", type=str)
    parser.add_argument("--use_visual_backbone", type=str2bool, default=True)
57 58 59 60
    parser.add_argument(
        "--ser_dict_path",
        type=str,
        default="../train_data/XFUND/class_list_xfun.txt")
littletomatodonkey's avatar
littletomatodonkey 已提交
61 62
    # need to be None or tb-yx
    parser.add_argument("--ocr_order_method", type=str, default=None)
63
    # params for inference
64 65 66
    parser.add_argument(
        "--mode",
        type=str,
文幕地方's avatar
文幕地方 已提交
67
        choices=['structure', 'kie'],
68
        default='structure',
69
        help='structure and kie is supported')
70 71 72 73 74
    parser.add_argument(
        "--image_orientation",
        type=bool,
        default=False,
        help='Whether to enable image orientation recognition')
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    parser.add_argument(
        "--layout",
        type=str2bool,
        default=True,
        help='Whether to enable layout analysis')
    parser.add_argument(
        "--table",
        type=str2bool,
        default=True,
        help='In the forward, whether the table area uses table recognition')
    parser.add_argument(
        "--ocr",
        type=str2bool,
        default=True,
        help='In the forward, whether the non-table area is recognition by ocr')
A
an1018 已提交
90
    # param for recovery
A
an1018 已提交
91 92
    parser.add_argument(
        "--recovery",
A
an1018 已提交
93
        type=str2bool,
A
an1018 已提交
94
        default=False,
文幕地方's avatar
文幕地方 已提交
95
        help='Whether to enable layout of recovery')
A
an1018 已提交
96

W
WenmuZhou 已提交
97 98 99 100 101 102 103 104
    return parser


def parse_args():
    parser = init_args()
    return parser.parse_args()


105
def draw_structure_result(image, result, font_path):
W
WenmuZhou 已提交
106 107 108
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    boxes, txts, scores = [], [], []
U
user1018 已提交
109 110 111 112 113 114 115 116 117

    img_layout = image.copy()
    draw_layout = ImageDraw.Draw(img_layout)
    text_color = (255, 255, 255)
    text_background_color = (80, 127, 255)
    catid2color = {}
    font_size = 15
    font = ImageFont.truetype(font_path, font_size, encoding="utf-8")

W
WenmuZhou 已提交
118
    for region in result:
U
user1018 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        if region['type'] not in catid2color:
            box_color = (random.randint(0, 255), random.randint(0, 255),
                         random.randint(0, 255))
            catid2color[region['type']] = box_color
        else:
            box_color = catid2color[region['type']]
        box_layout = region['bbox']
        draw_layout.rectangle(
            [(box_layout[0], box_layout[1]), (box_layout[2], box_layout[3])],
            outline=box_color,
            width=3)
        text_w, text_h = font.getsize(region['type'])
        draw_layout.rectangle(
            [(box_layout[0], box_layout[1]),
             (box_layout[0] + text_w, box_layout[1] + text_h)],
            fill=text_background_color)
        draw_layout.text(
            (box_layout[0], box_layout[1]),
            region['type'],
            fill=text_color,
            font=font)

文幕地方's avatar
文幕地方 已提交
141
        if region['type'] == 'table':
W
WenmuZhou 已提交
142 143
            pass
        else:
文幕地方's avatar
文幕地方 已提交
144 145 146 147
            for text_result in region['res']:
                boxes.append(np.array(text_result['text_region']))
                txts.append(text_result['text'])
                scores.append(text_result['confidence'])
U
user1018 已提交
148

149
    im_show = draw_ocr_box_txt(
U
user1018 已提交
150
        img_layout, boxes, txts, scores, font_path=font_path, drop_score=0)
151
    return im_show