From 6821d0d137aa76c6c71e51be203d67890c08a807 Mon Sep 17 00:00:00 2001 From: zimoyin <2556608754@qq.com> Date: Tue, 13 Oct 2026 14:40:21 +0800 Subject: [PATCH] fix: keep word boxes when filtering by confidence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit i2t 每次请求都会调用 filter_document,之前它会丢弃 words,导致 return_word_box 的结果无法返回。 Co-Authored-By: Claude Code --- src/ocr_server/task_queue.py | 15 ++++++++++----- tests/test_queue.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/ocr_server/task_queue.py b/src/ocr_server/task_queue.py index 8527ba8..b088fe0 100644 --- a/src/ocr_server/task_queue.py +++ b/src/ocr_server/task_queue.py @@ -240,8 +240,13 @@ class OCRTaskQueue: @classmethod def filter_document(cls, document: OCRDocument, min_confidence: float) -> OCRDocument: # The compact response is intentionally rebuilt from its own polygon map. - selected = [ - { + # words 必须一并带上:这个函数在每次 i2t 请求里都会被调用(即使 + # min_confidence 为 0),漏掉就会让 return_word_box 的结果全部丢失。 + selected: list[dict[str, Any]] = [] + for item in document.polygon.values(): + if item.confidence < min_confidence: + continue + segment: dict[str, Any] = { "text": item.text, "confidence": item.confidence, "polygon": [ @@ -251,9 +256,9 @@ class OCRTaskQueue: [item.Location.xtop, item.Location.ybottom], ], } - for item in document.polygon.values() - if item.confidence >= min_confidence - ] + if item.words: + segment["words"] = item.words + selected.append(segment) return cls.build_document(selected) @classmethod diff --git a/tests/test_queue.py b/tests/test_queue.py index b6b527e..544eb77 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -118,6 +118,39 @@ def test_build_document_preserves_word_boxes() -> None: 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]]]}, + ] + 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: """行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。""" recognized = [