Files
douyin_guaji-main/tests/test_ocr_pipeline.py
T
CodexandClaude Opus 4.7 8457219241 refactor(underlying): 抽 analyse_pic_word OCR 管道到 ocr_pipeline (1939 → 1560)
underlying_operations.analyse_pic_word L723-1102 是 380 行 OCR 主管道:
路径解析 + 字节缓存 + PIL 加载 + change_color 预处理 + 调试图像保存
+ EasyOCR/PaddleOCR 调用 + 置信度提取 + 缓存写回 + 成功识别落盘。

抽到 underlying_operations_pkg/ocr_pipeline.py:
- analyse_pic_word(self, picname, change_color, pic_path, return_confidence)
- _save_debug_preprocessed: 预处理后的调试图保存(仅 SAVE_OCR_DEBUG_IMAGES=1)
- _save_debug_after_ocr: 含 OCR 文本预览 + 置信度的 after_ocr 调试图
- _save_success_pic: 成功识别落盘到 ocr/<category>/<picname>_success.png

主类方法替换为 4 行委托。原行为完全保留:缓存 / 二值化 / EasyOCR vs Paddle
predict/ocr 兜底 / [None] 短路 / 异常返回空 / picname 失败诊断日志。

新增 16 个单元测试覆盖:
- 早出 4 种(pic_path 空/不存在/带 confidence/OCR 未初始化)
- OCR 调用路径(EasyOCR / Paddle predict / Paddle ocr 兜底 / [None] 短路 / 异常)
- 缓存命中跳过 OCR / 缓存未命中写回
- change_color 0/1/2 三种模式
- return_confidence True/False 返回类型
- autouse fixture 清 OCR 模块级缓存防止测试污染

1361 passed (+16)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 02:36:47 +08:00

270 lines
10 KiB
Python

"""douyin/core/underlying_operations_pkg/ocr_pipeline 单元测试。
analyse_pic_word 主流程的早出和分支测试。OCR 实际调用通过 mock 完成。
"""
from __future__ import annotations
import os
from unittest.mock import MagicMock, patch
import pytest
try:
from PIL import Image
except ImportError: # pragma: no cover
pytest.skip("PIL unavailable", allow_module_level=True)
from douyin.core.underlying_operations_pkg.ocr_pipeline import analyse_pic_word
@pytest.fixture(autouse=True)
def _clear_ocr_cache():
"""每个测试前清 operations.ocr_operations 模块级 OCR 缓存,避免互相污染。"""
try:
from operations import ocr_operations
with ocr_operations._ocr_cache_lock:
ocr_operations._ocr_cache.clear()
except Exception:
pass
yield
try:
from operations import ocr_operations
with ocr_operations._ocr_cache_lock:
ocr_operations._ocr_cache.clear()
except Exception:
pass
def _mk_self(*, ocr=None, ocr_type=None, save_debug=False):
s = MagicMock()
s.ocr = ocr
s.ocr_type = ocr_type
s.save_ocr_debug_images = save_debug
s._find_pic_path = MagicMock(return_value="")
s._ocr_coords_lock = MagicMock()
s._ocr_coords_lock.__enter__ = MagicMock(return_value=None)
s._ocr_coords_lock.__exit__ = MagicMock(return_value=False)
s._ocr_coords_cache = {}
# extract_ocr_content 默认返回 ("", None)
s.extract_ocr_content = MagicMock(return_value=("", None))
return s
def _make_real_pic(tmp_path):
"""生成一张真实 PNG 文件作为 OCR 输入,避免 mock PIL.Image 引入复杂度。"""
p = tmp_path / "tmp.png"
Image.new("RGB", (50, 50), (200, 200, 200)).save(str(p))
return str(p)
# ============================================================
# 早出条件
# ============================================================
class TestEarlyReturns:
def test_pic_path_empty_returns_empty(self):
s = _mk_self()
s._find_pic_path = MagicMock(return_value="")
result = analyse_pic_word(s, "X", change_color=0)
assert result == ""
def test_pic_path_nonexistent_returns_empty(self):
s = _mk_self()
s._find_pic_path = MagicMock(return_value="/nonexistent/path.png")
result = analyse_pic_word(s, "X", change_color=0)
assert result == ""
def test_pic_path_nonexistent_with_confidence(self):
s = _mk_self()
s._find_pic_path = MagicMock(return_value="/nonexistent/path.png")
text, conf = analyse_pic_word(
s, "X", change_color=0, return_confidence=True
)
assert text == ""
assert conf is None
def test_ocr_not_initialized_returns_empty(self, tmp_path):
s = _mk_self(ocr=None)
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == ""
# ============================================================
# OCR 调用路径
# ============================================================
class TestOcrCallPaths:
def test_easyocr_path(self, tmp_path):
ocr = MagicMock()
ocr.readtext = MagicMock(return_value=[("bbox", "识别出的文本", 0.95)])
s = _mk_self(ocr=ocr, ocr_type="easyocr")
s.extract_ocr_content = MagicMock(
return_value=("识别出的文本", {"avg_confidence": 0.95, "item_count": 1})
)
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == "识别出的文本"
ocr.readtext.assert_called_once()
def test_paddleocr_predict_path(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"some": "result"}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("paddle text", None))
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == "paddle text"
ocr.predict.assert_called_once()
def test_paddleocr_ocr_fallback(self, tmp_path):
"""没有 predict 方法时回退到 ocr() 调用。"""
ocr = MagicMock(spec=["ocr"])
ocr.ocr = MagicMock(return_value=[{"some": "result"}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("ocr text", None))
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == "ocr text"
ocr.ocr.assert_called_once()
def test_paddleocr_returns_none_short_circuits(self, tmp_path):
"""PaddleOCR 返回 [None] 时直接返回空(不调用 extract_ocr_content)。"""
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[None])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("不应被调用", None))
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == ""
s.extract_ocr_content.assert_not_called()
def test_ocr_throws_returns_empty(self, tmp_path):
"""OCR 调用抛异常时安全返回空。"""
ocr = MagicMock()
ocr.predict = MagicMock(side_effect=RuntimeError("OCR fail"))
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == ""
# ============================================================
# 缓存路径
# ============================================================
class TestCachePath:
def test_cache_hit_skips_ocr(self, tmp_path):
"""缓存命中时不应调用 OCR。"""
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
pic_path = _make_real_pic(tmp_path)
with patch(
"operations.ocr_operations._img_fingerprint",
return_value="cache_key_X",
), patch(
"operations.ocr_operations.cache_get",
return_value=("缓存的文本", {"avg_confidence": 0.99}),
):
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == "缓存的文本"
# OCR 不应被调用
ocr.predict.assert_not_called()
def test_cache_miss_runs_ocr_and_writes_back(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("新识别的文本", None))
pic_path = _make_real_pic(tmp_path)
with patch(
"operations.ocr_operations._img_fingerprint", return_value="key1"
), patch(
"operations.ocr_operations.cache_get", return_value=None
), patch(
"operations.ocr_operations.cache_put"
) as mock_put:
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert result == "新识别的文本"
# OCR 被调用 + 结果写回缓存
ocr.predict.assert_called_once()
mock_put.assert_called_once_with("key1", ("新识别的文本", None))
# ============================================================
# 颜色预处理分支
# ============================================================
class TestChangeColor:
def test_mode_0_keeps_color(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("text", None))
pic_path = _make_real_pic(tmp_path)
# 不抛即可
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
assert isinstance(result, str)
def test_mode_1_binarizes(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("binarized text", None))
pic_path = _make_real_pic(tmp_path)
# change_color=1 走二值化分支
result = analyse_pic_word(s, "X", change_color=1, pic_path=pic_path)
assert isinstance(result, str)
def test_mode_2_binarizes_high_threshold(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("text", None))
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=2, pic_path=pic_path)
assert isinstance(result, str)
# ============================================================
# return_confidence
# ============================================================
class TestReturnConfidence:
def test_returns_text_only_by_default(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("text", {"item_count": 1}))
pic_path = _make_real_pic(tmp_path)
result = analyse_pic_word(s, "X", change_color=0, pic_path=pic_path)
# 默认只返回字符串
assert isinstance(result, str)
assert result == "text"
def test_returns_tuple_with_flag(self, tmp_path):
ocr = MagicMock()
ocr.predict = MagicMock(return_value=[{"X": 1}])
conf = {"avg_confidence": 0.9, "item_count": 1}
s = _mk_self(ocr=ocr, ocr_type="paddleocr")
s.extract_ocr_content = MagicMock(return_value=("text", conf))
pic_path = _make_real_pic(tmp_path)
text, ret_conf = analyse_pic_word(
s, "X", change_color=0, pic_path=pic_path, return_confidence=True
)
assert text == "text"
assert ret_conf == conf