fix: give whole-image polygon in skip-detection mode
无检测模式下没有检测框,原先 polygon 为空,build_document 会丢弃无坐标的片段, 导致识别其实成功但响应 segments=0。改为用整张图的矩形作为兜底坐标。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+22
-4
@@ -324,7 +324,16 @@ class OCRService:
|
|||||||
outputs = list(
|
outputs = list(
|
||||||
inner.text_rec_model([image_array], return_word_box=self._return_word_box)
|
inner.text_rec_model([image_array], return_word_box=self._return_word_box)
|
||||||
)
|
)
|
||||||
lines = self._parse_recognition_output(outputs)
|
# 没有检测框,但必须给出坐标:文本区域就是整张图。
|
||||||
|
# 否则 build_document 会丢弃无坐标的片段,导致结果里有字却返回空。
|
||||||
|
height, width = int(image_array.shape[0]), int(image_array.shape[1])
|
||||||
|
whole_image = [
|
||||||
|
[0.0, 0.0],
|
||||||
|
[float(width), 0.0],
|
||||||
|
[float(width), float(height)],
|
||||||
|
[0.0, float(height)],
|
||||||
|
]
|
||||||
|
lines = self._parse_recognition_output(outputs, polygon=whole_image)
|
||||||
if not lines:
|
if not lines:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"跳过检测后没有识别出任何文字。该模式要求图片本身是单行文本;"
|
"跳过检测后没有识别出任何文字。该模式要求图片本身是单行文本;"
|
||||||
@@ -333,8 +342,15 @@ class OCRService:
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse_recognition_output(outputs: Any) -> list[dict[str, Any]]:
|
def _parse_recognition_output(
|
||||||
"""解析识别模型(无检测)的输出。单次输入只会返回一个结果。"""
|
outputs: Any, polygon: list[list[float]] | None = None
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
"""解析识别模型(无检测)的输出。单次输入只会返回一个结果。
|
||||||
|
|
||||||
|
``polygon`` 是无检测模式下的兜底坐标(整张图),缺了它后续 build_document
|
||||||
|
会因为「没有坐标」而丢弃该片段。
|
||||||
|
"""
|
||||||
|
fallback = polygon or []
|
||||||
lines: list[dict[str, Any]] = []
|
lines: list[dict[str, Any]] = []
|
||||||
for output in outputs:
|
for output in outputs:
|
||||||
if not isinstance(output, dict):
|
if not isinstance(output, dict):
|
||||||
@@ -351,7 +367,9 @@ class OCRService:
|
|||||||
if not value:
|
if not value:
|
||||||
continue
|
continue
|
||||||
score = float(scores[index]) if index < len(scores) else 0.0
|
score = float(scores[index]) if index < len(scores) else 0.0
|
||||||
lines.append({"text": str(value), "confidence": score, "polygon": []})
|
lines.append(
|
||||||
|
{"text": str(value), "confidence": score, "polygon": list(fallback)}
|
||||||
|
)
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def _parse_results(self, results: Any) -> list[dict[str, Any]]:
|
def _parse_results(self, results: Any) -> list[dict[str, Any]]:
|
||||||
|
|||||||
@@ -186,6 +186,25 @@ def test_parse_recognition_output_handles_list_and_scalar() -> None:
|
|||||||
assert scalar == [{"text": "MU2379", "confidence": 0.97, "polygon": []}]
|
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:
|
def test_parse_recognition_output_with_word_box_tuple() -> None:
|
||||||
from ocr_server.ocr import OCRService
|
from ocr_server.ocr import OCRService
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user