PaddleOCR/ppstructure/utility.py

151 lines
5.0 KiB
Python
Raw Normal View History

2021-06-10 14:24:59 +08:00
# 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.
2022-03-11 08:09:27 +08:00
import ast
2021-06-10 14:24:59 +08:00
from PIL import Image
import numpy as np
from tools.infer.utility import draw_ocr_box_txt, str2bool, init_args as infer_args
2021-06-10 14:24:59 +08:00
def init_args():
parser = infer_args()
# params for output
parser.add_argument("--output", type=str, default='./output')
2021-06-10 14:24:59 +08:00
# params for table structure
2021-06-23 12:28:32 +08:00
parser.add_argument("--table_max_len", type=int, default=488)
2022-06-16 21:24:38 +08:00
parser.add_argument("--table_algorithm", type=str, default='TableAttn')
2021-06-23 12:28:32 +08:00
parser.add_argument("--table_model_dir", type=str)
2022-08-10 18:52:34 +08:00
parser.add_argument(
2022-08-16 18:46:09 +08:00
"--merge_no_span_structure", type=str2bool, default=True)
parser.add_argument(
"--table_char_dict_path",
type=str,
default="../ppocr/utils/dict/table_structure_dict_ch.txt")
# params for layout
2022-08-10 12:54:25 +08:00
parser.add_argument("--layout_model_dir", type=str)
parser.add_argument(
2022-08-10 12:54:25 +08:00
"--layout_dict_path",
type=str,
2022-08-22 14:58:09 +08:00
default="../ppocr/utils/dict/layout_dict/layout_publaynet_dict.txt")
2022-03-11 08:09:27 +08:00
parser.add_argument(
2022-08-10 13:53:43 +08:00
"--layout_score_threshold",
2022-08-10 12:54:25 +08:00
type=float,
default=0.5,
help="Threshold of score.")
parser.add_argument(
2022-08-10 22:15:52 +08:00
"--layout_nms_threshold",
type=float,
default=0.5,
help="Threshold of nms.")
# params for kie
parser.add_argument("--kie_algorithm", type=str, default='LayoutXLM')
parser.add_argument("--ser_model_dir", type=str)
2022-09-20 22:13:27 +08:00
parser.add_argument("--re_model_dir", type=str)
parser.add_argument("--use_visual_backbone", type=str2bool, default=True)
parser.add_argument(
"--ser_dict_path",
type=str,
default="../train_data/XFUND/class_list_xfun.txt")
# need to be None or tb-yx
parser.add_argument("--ocr_order_method", type=str, default=None)
# params for inference
parser.add_argument(
"--mode",
type=str,
default='structure',
help='structure and kie is supported')
parser.add_argument(
"--image_orientation",
type=bool,
default=False,
help='Whether to enable image orientation recognition')
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')
2022-08-22 11:48:18 +08:00
# param for recovery
2022-05-07 16:55:20 +08:00
parser.add_argument(
"--recovery",
2022-08-22 11:48:18 +08:00
type=str2bool,
2022-05-07 16:55:20 +08:00
default=False,
2022-06-16 21:24:38 +08:00
help='Whether to enable layout of recovery')
2022-08-22 11:48:18 +08:00
2021-06-10 14:24:59 +08:00
return parser
def parse_args():
parser = init_args()
return parser.parse_args()
def draw_structure_result(image, result, font_path):
2021-06-10 14:24:59 +08:00
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
boxes, txts, scores = [], [], []
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")
2021-06-10 14:24:59 +08:00
for region in result:
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)
2022-08-08 14:50:27 +08:00
if region['type'] == 'table':
2021-06-10 14:24:59 +08:00
pass
else:
2022-03-30 22:15:12 +08:00
for text_result in region['res']:
boxes.append(np.array(text_result['text_region']))
txts.append(text_result['text'])
scores.append(text_result['confidence'])
im_show = draw_ocr_box_txt(
img_layout, boxes, txts, scores, font_path=font_path, drop_score=0)
return im_show