PP-OCRv6_Demo/tests/test_result_adapter.py

37 lines
1.3 KiB
Python

from pathlib import Path
from PIL import Image
from ocr_app.result_adapter import adapt_ocr_result, result_markdown, result_plain_text
def test_adapt_ppocr_result(tmp_path):
image = tmp_path / "sample.png"
Image.new("RGB", (100, 50), "white").save(image)
result = {
"page_index": 0,
"rec_texts": ["第一行", "第二行"],
"rec_scores": [0.98, 0.75],
"rec_polys": [
[[1, 2], [20, 2], [20, 10], [1, 10]],
[[2, 20], [30, 20], [30, 30], [2, 30]],
],
"rec_boxes": [[1, 2, 20, 10], [2, 20, 30, 30]],
}
payload = adapt_ocr_result(result, input_path=image, source_type="image_ocr", language="ch")
assert payload["schema_version"] == 1
assert payload["image"] == {"width": 100, "height": 50}
assert payload["summary"]["detected_lines"] == 2
assert payload["summary"]["mean_score"] == 0.865
assert payload["lines"][0]["text"] == "第一行"
assert result_plain_text(payload) == "第一行\n第二行"
assert result_markdown(payload, title="sample.png").startswith("# sample.png\n\n第一行")
def test_empty_result_is_supported(tmp_path):
payload = adapt_ocr_result({}, input_path=Path(tmp_path / "missing.png"), source_type="image_ocr", language="ch")
assert payload["lines"] == []
assert payload["summary"]["mean_score"] is None