Files
ocr-server/scripts/benchmark_ocr.py
T
zimoyin cf7088a13d revert: remove enable_detection and return_word_box options
按需求移除两个开关:
- enable_detection:跳过检测直接识别,实测只对单行图有效,多行文档会返回空结果
- return_word_box:字/词级坐标,默认即关闭且开销在噪声内,无实际收益
恢复正常的两段式链路:文本检测 → 文本识别。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-13 19:16:21 +08:00

47 lines
1.7 KiB
Python

"""Benchmark the configured OCR pipeline without modifying model files."""
from pathlib import Path
from time import perf_counter
from ocr_server.config import Settings
from ocr_server.ocr import OCRService
if __name__ == "__main__":
image_path = Path("http/fixtures/general_ocr_002.png")
settings = Settings()
service = OCRService(
lang=settings.lang,
device=settings.device,
model_dir=settings.model_dir,
det_model_name=settings.det_model_name,
rec_model_name=settings.rec_model_name,
text_det_limit_side_len=settings.text_det_limit_side_len,
text_det_limit_type=settings.text_det_limit_type,
text_det_thresh=settings.text_det_thresh,
text_det_box_thresh=settings.text_det_box_thresh,
text_det_unclip_ratio=settings.text_det_unclip_ratio,
text_rec_score_thresh=settings.text_rec_score_thresh,
text_recognition_batch_size=settings.text_recognition_batch_size,
cpu_threads=settings.cpu_threads_resolved,
enable_mkldnn=settings.enable_mkldnn,
)
service.prepare()
stages: list[str] = []
def on_progress(percent: int, message: str) -> None:
stages.append(f"{percent}% {message}")
started = perf_counter()
lines = service.recognize(image_path, on_progress=on_progress)
elapsed_ms = (perf_counter() - started) * 1000
print(f"image={image_path}")
print(f"cpu_threads={settings.cpu_threads_resolved} (config={settings.cpu_threads})")
print(f"enable_mkldnn={settings.enable_mkldnn}")
print(f"det={settings.det_model_name} rec={settings.rec_model_name}")
print(f"elapsed_ms={elapsed_ms:.2f}")
print(f"segments={len(lines)}")
for stage in stages:
print(f"stage={stage}")