revert: remove enable_detection and return_word_box options

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

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
zimoyin
2026-10-13 19:16:21 +08:00
parent 932f1a8b13
commit cf7088a13d
10 changed files with 31 additions and 366 deletions
+3 -101
View File
@@ -101,54 +101,15 @@ def test_ocr_service_resolves_threads_and_stores_mkldnn() -> None:
assert plain._enable_mkldnn is False
def test_build_document_preserves_word_boxes() -> None:
def test_filter_document_drops_low_confidence() -> None:
recognized = [
{
"text": "姓名:万镭",
"confidence": 0.99,
"polygon": [[0, 0], [100, 0], [100, 20], [0, 20]],
"words": [
[[0, 0], [50, 0], [50, 20], [0, 20]],
[[50, 0], [100, 0], [100, 20], [50, 20]],
],
}
]
document = OCRTaskQueue.build_document(recognized)
assert document.polygon["text1"].words == recognized[0]["words"]
def test_filter_document_keeps_word_boxes() -> None:
"""i2t 每次都走 filter_document,words 不能被过滤流程丢掉。"""
recognized = [
{
"text": "姓名:万镭",
"confidence": 0.9,
"polygon": [[0, 0], [100, 0], [100, 20], [0, 20]],
"words": [
[[0, 0], [50, 0], [50, 20], [0, 20]],
[[50, 0], [100, 0], [100, 20], [50, 20]],
],
}
]
document = OCRTaskQueue.build_document(recognized)
filtered = OCRTaskQueue.filter_document(document, 0.0)
assert filtered.polygon["text1"].words == recognized[0]["words"]
def test_filter_document_drops_low_confidence_with_words() -> None:
recognized = [
{"text": "保留", "confidence": 0.9, "polygon": [[0, 0], [10, 0], [10, 10], [0, 10]],
"words": [[[0, 0], [10, 0], [10, 10], [0, 10]]]},
{"text": "丢弃", "confidence": 0.2, "polygon": [[0, 20], [10, 20], [10, 30], [0, 30]],
"words": [[[0, 20], [10, 20], [10, 30], [0, 30]]]},
{"text": "保留", "confidence": 0.9, "polygon": [[0, 0], [10, 0], [10, 10], [0, 10]]},
{"text": "丢弃", "confidence": 0.2, "polygon": [[0, 20], [10, 20], [10, 30], [0, 30]]},
]
document = OCRTaskQueue.build_document(recognized)
filtered = OCRTaskQueue.filter_document(document, 0.5)
assert [item.text for item in filtered.polygon.values()] == ["保留"]
assert filtered.polygon["text1"].words is not None
def test_document_rows_groups_by_coordinates() -> None:
@@ -171,65 +132,6 @@ def test_document_rows_empty_document() -> None:
assert OCRTaskQueue.document_rows(document) == []
def test_parse_recognition_output_handles_list_and_scalar() -> None:
from ocr_server.ocr import OCRService
listed = OCRService._parse_recognition_output(
[{"rec_text": ["MU2379", "03DEC"], "rec_score": [0.97, 0.95]}]
)
assert [item["text"] for item in listed] == ["MU2379", "03DEC"]
assert listed[0]["confidence"] == 0.97
scalar = OCRService._parse_recognition_output(
[{"rec_text": "MU2379", "rec_score": 0.97}]
)
assert scalar == [{"text": "MU2379", "confidence": 0.97, "polygon": []}]
def test_parse_recognition_output_applies_fallback_polygon() -> None:
"""无检测模式必须带上兜底坐标,否则 build_document 会把片段丢掉。"""
from ocr_server.ocr import OCRService
whole = [[0, 0], [640, 0], [640, 42], [0, 42]]
parsed = OCRService._parse_recognition_output(
[{"rec_text": "MU2379", "rec_score": 0.97}], polygon=whole
)
assert parsed[0]["polygon"] == whole
from ocr_server.task_queue import OCRTaskQueue
document = OCRTaskQueue.build_document(parsed)
assert document.text == "MU2379"
assert len(document.polygon) == 1
assert document.polygon["text1"].text == "MU2379"
def test_parse_recognition_output_with_word_box_tuple() -> None:
from ocr_server.ocr import OCRService
parsed = OCRService._parse_recognition_output(
[{"rec_text": [("MU2379", [[1, 2, 3]])], "rec_score": [0.97]}]
)
assert parsed == [{"text": "MU2379", "confidence": 0.97, "polygon": []}]
def test_parse_recognition_output_skips_empty() -> None:
from ocr_server.ocr import OCRService
assert OCRService._parse_recognition_output([{"rec_text": [""], "rec_score": [0.0]}]) == []
assert OCRService._parse_recognition_output([{"rec_text": [], "rec_score": []}]) == []
assert OCRService._parse_recognition_output([None, "junk"]) == []
def test_enable_detection_defaults_to_true() -> None:
from ocr_server.config import Settings
assert Settings().enable_detection is True
assert Settings(enable_detection=False).enable_detection is False
def test_progress_is_monotonic_and_includes_checkpoints() -> None:
from ocr_server.ocr import OCRService