import numpy as np
from itertools import chain
from mdutils.mdutils import MdUtils
from YQ_OCR.utils.datasets import Dataset
from YQ_OCR.utils.text2md import TableMD
from YQ_OCR.utils.utils import *

# 1. xlsx -> 正确json文件(写入厂家信息)
# 2. 发送图片(带正确json文件)
# 3. 把返回的json 和正确的json 进行对比(有key--用返回结果与正确结果比对,无key--用正确结果与返回结果比对)


if __name__ == '__main__':
    img_paths = chain(*[Path(imgs_path).rglob(f'*.{ext}') for ext in ['jpg', 'png', 'jpeg']])
    all_rate = []
    table_mean_acc = []
    for img_path in img_paths:
        print(img_path)
        true_d, true_json = open_true_json(img_path.with_suffix('.json'))
        result = send_request(img_path, true_json)
        res_d = parse_result(result)

        markdown = TableMD(img_path.name)
        markdown.write_header(title='推理结果', level=2)
        # json result
        rate, statistics = markdown.evaluate_one(true_d, res_d)
        all_rate.append(rate)
        print(f'文字识别正确率：{rate:.2f}%')

        # table gt result
        dataset = Dataset(gt_file=img_path.with_suffix('.txt'), img_name=img_path.name, results=res_d)
        markdown.write_table_accuracy(ds=dataset, key='new')
        table_acc = markdown.get_table_accuracy()
        table_mean_acc.append(table_acc)
        print(f'表格识别正确率：{table_acc:.2f}%')
        markdown.f.create_md_file()

    print('----------------------------------------')
    all_rate = "{:.2f}%".format(np.mean(all_rate))
    all_table_rate = "{:.2f}%".format(np.mean(table_mean_acc))
    print(f'文字识别总体正确率：{all_rate}')
    print(f'表格识别总体正确率：{all_table_rate}')
