fix: keep word boxes when filtering by confidence

i2t 每次请求都会调用 filter_document,之前它会丢弃 words,导致 return_word_box 的结果无法返回。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
zimoyin
2026-10-13 14:40:21 +08:00
parent 52696aca90
commit 6821d0d137
2 changed files with 43 additions and 5 deletions
+10 -5
View File
@@ -240,8 +240,13 @@ class OCRTaskQueue:
@classmethod @classmethod
def filter_document(cls, document: OCRDocument, min_confidence: float) -> OCRDocument: def filter_document(cls, document: OCRDocument, min_confidence: float) -> OCRDocument:
# The compact response is intentionally rebuilt from its own polygon map. # 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, "text": item.text,
"confidence": item.confidence, "confidence": item.confidence,
"polygon": [ "polygon": [
@@ -251,9 +256,9 @@ class OCRTaskQueue:
[item.Location.xtop, item.Location.ybottom], [item.Location.xtop, item.Location.ybottom],
], ],
} }
for item in document.polygon.values() if item.words:
if item.confidence >= min_confidence segment["words"] = item.words
] selected.append(segment)
return cls.build_document(selected) return cls.build_document(selected)
@classmethod @classmethod
+33
View File
@@ -118,6 +118,39 @@ def test_build_document_preserves_word_boxes() -> None:
assert document.polygon["text1"].words == recognized[0]["words"] 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: def test_document_rows_groups_by_coordinates() -> None:
"""行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。""" """行文本按坐标(bbox 纵向重叠)归并,供字段抽取的 row 选项使用。"""
recognized = [ recognized = [