fix(contract): 纯图片扫描 PDF 坐标高亮与 OCR 复核

This commit is contained in:
sophie committed 2026-07-29 08:56:13 +08:00
1 parent ca312ea751
commit ce1fd59f61
21 files changed
+1665 -54

No files matched your search

@@ -1,9 +1,9 @@
"""
backend/tests/services/procurement_contract/test_preprocessing.py
用途:验证采购合同调用 MinerU 的请求协议、Markdown 读取与故障回退前提。
用途:验证采购合同调用 MinerU 的请求协议、Markdown 读取、OCR 坐标产物与故障回退前提。
依赖:procurement_contract.preprocessing、Flask 最小应用上下文。
逻辑:模拟测试环境 MinerU 的 `/file_parse` 返回,确保 PDF 解析产物可审计且不依赖服务端文件名。
逻辑:模拟测试环境 MinerU 和 edkb 产物,确保 PDF 解析结果与纯图片行坐标可审计、可归档。
"""
from io import BytesIO
@@ -144,3 +144,37 @@ def test_extract_page_texts_uses_mineru_logical_page_when_pdf_has_no_text_layer(
# The logical page is only an anchor fallback; physical attachment page counts remain unavailable.
assert pages == ["兹因甲方向乙方购买设备\n第一条 价格不变。\n本合同一式两份。\n甲方:和信公司"]
assert warning == "PDF native page text unavailable; used MinerU logical page for body boundaries"
def test_ocr_layout_from_edkb_keeps_pdf_page_line_polygons(tmp_path):
"""edkb 已识别的 PDF 整页行框应直接复用,避免合同预处理重复 OCR。"""
raw_layout = tmp_path / "01_raw_layout.json"
raw_layout.write_text(
"""
[
{
"id": "ocr-pdf-page-2",
"page": 2,
"type": "ocr_text",
"text": "OCR_TEXT: 设备使用地:一厂",
"bbox": [0, 0, 1000, 1000],
"meta": {
"page_image": true,
"ocr_lines": [
{
"text": "设备使用地:一厂",
"confidence": 0.98,
"polygon": [[0.1, 0.2], [0.4, 0.2], [0.4, 0.24], [0.1, 0.24]]
}
]
}
}
]
""",
encoding="utf-8",
)
pages = preprocessing._ocr_layout_from_edkb(raw_layout)
assert pages[0]["page_no"] == 2
assert pages[0]["lines"][0]["text"] == "设备使用地:一厂"
assert pages[0]["lines"][0]["polygon"][0] == [0.1, 0.2]