eval_table.py 3.4 KB
Newer Older
文幕地方's avatar
文幕地方 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
W
WenmuZhou 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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

W
WenmuZhou 已提交
15 16
import os
import sys
文幕地方's avatar
文幕地方 已提交
17

W
WenmuZhou 已提交
18 19
__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
文幕地方's avatar
文幕地方 已提交
20
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, '../..')))
W
WenmuZhou 已提交
21 22

import cv2
文幕地方's avatar
文幕地方 已提交
23 24
import pickle
import paddle
W
WenmuZhou 已提交
25
from tqdm import tqdm
W
WenmuZhou 已提交
26 27 28
from ppstructure.table.table_metric import TEDS
from ppstructure.table.predict_table import TableSystem
from ppstructure.utility import init_args
W
WenmuZhou 已提交
29 30 31 32 33 34 35 36 37 38 39
from ppocr.utils.logging import get_logger

logger = get_logger()


def parse_args():
    parser = init_args()
    parser.add_argument("--gt_path", type=str)
    return parser.parse_args()


文幕地方's avatar
文幕地方 已提交
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
def load_txt(txt_path):
    pred_html_dict = {}
    if not os.path.exists(txt_path):
        return pred_html_dict
    with open(txt_path, encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip().split('\t')
            img_name, pred_html = line
            pred_html_dict[img_name] = pred_html
    return pred_html_dict


def load_result(path):
    data = {}
    if os.path.exists(path):
        data = pickle.load(open(path, 'rb'))
    return data


def save_result(path, data):
    old_data = load_result(path)
    old_data.update(data)
    with open(path, 'wb') as f:
        pickle.dump(old_data, f)


def main(gt_path, img_root, args):
    os.makedirs(args.output, exist_ok=True)
    # init TableSystem
W
WenmuZhou 已提交
70
    text_sys = TableSystem(args)
文幕地方's avatar
文幕地方 已提交
71 72 73 74 75 76 77
    # load gt and preds html result
    gt_html_dict = load_txt(gt_path)

    ocr_result = load_result(os.path.join(args.output, 'ocr.pickle'))
    structure_result = load_result(
        os.path.join(args.output, 'structure.pickle'))

W
WenmuZhou 已提交
78 79
    pred_htmls = []
    gt_htmls = []
文幕地方's avatar
文幕地方 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    for img_name, gt_html in tqdm(gt_html_dict.items()):
        img = cv2.imread(os.path.join(img_root, img_name))
        # run ocr and save result
        if img_name not in ocr_result:
            dt_boxes, rec_res, _, _ = text_sys._ocr(img)
            ocr_result[img_name] = [dt_boxes, rec_res]
            save_result(os.path.join(args.output, 'ocr.pickle'), ocr_result)
        # run structure and save result
        if img_name not in structure_result:
            structure_res, _ = text_sys._structure(img)
            structure_result[img_name] = structure_res
            save_result(
                os.path.join(args.output, 'structure.pickle'), structure_result)
        dt_boxes, rec_res = ocr_result[img_name]
        structure_res = structure_result[img_name]
        # match ocr and structure
        pred_html = text_sys.match(structure_res, dt_boxes, rec_res)
W
WenmuZhou 已提交
97

文幕地方's avatar
文幕地方 已提交
98
        pred_htmls.append(pred_html)
W
WenmuZhou 已提交
99 100
        gt_htmls.append(gt_html)

文幕地方's avatar
文幕地方 已提交
101 102 103 104
    # compute teds
    teds = TEDS(n_jobs=16)
    scores = teds.batch_evaluate_html(gt_htmls, pred_htmls)
    logger.info('teds: {}'.format(sum(scores) / len(scores)))
W
WenmuZhou 已提交
105 106 107 108


if __name__ == '__main__':
    args = parse_args()
文幕地方's avatar
文幕地方 已提交
109
    main(args.gt_path, args.image_dir, args)