该库用于计算Precision
、Recall
和H-mean
三个指标,用来快速评测文本检测算法效果,与text_det_test_dataset配套使用。
flowchart LR
A([Text Detect Algorithm]) --get_pred_txt.py--> B([pred_txt])
B --compute_metric.py--> C([TextDetMetric]) --> D([Precision])
C --> E([Recall])
C --> F([H-mean])
如果想要评测其他文本检测算法,需要将预测结果写入pred.txt
中,格式为预测框坐标\t真实框坐标\t耗时
,详细可参考link。示例如下:
[[[85.0, 43.0], [164.0, 44.0], [164.0, 72.0], [85.0, 70.0]]] [[[473.36082474226805, 271.2938144329896], [520.7835051546391, 290.8814432989691]]] 0.14536070823669434
-
安装运行环境
pip install rapidocr_onnxruntime==1.3.16 pip install datasets pip install text_det_metric
-
获得
pred.txt
文本文件from pathlib import Path import cv2 import numpy as np from datasets import load_dataset from rapidocr_onnxruntime import RapidOCR from tqdm import tqdm engine = RapidOCR() dataset = load_dataset("SWHL/text_det_test_dataset") test_data = dataset["test"] content = [] for i, one_data in enumerate(tqdm(test_data)): img = np.array(one_data.get("image")) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) dt_boxes, elapse = engine(img, use_det=True, use_cls=False, use_rec=False) dt_boxes = [] if dt_boxes is None else dt_boxes elapse = 0 if elapse is None else elapse[0] gt_boxes = [v["points"] for v in one_data["shapes"]] content.append(f"{dt_boxes}\t{gt_boxes}\t{elapse}") with open("pred.txt", "w", encoding="utf-8") as f: for v in content: f.write(f"{v}\n")
-
计算指标
from text_det_metric import TextDetMetric metric = TextDetMetric() pred_path = "pred.txt" metric = metric(pred_path) print(metric)
-
得到结果
{'precision': 0.8301, 'recall': 0.8659, 'hmean': 0.8476, 'avg_elapse': 0.2246}