From 8457219241a4be78e4a4b715b316c1b642ed4c01 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 23 May 2026 02:36:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor(underlying):=20=E6=8A=BD=20analyse=5Fp?= =?UTF-8?q?ic=5Fword=20OCR=20=E7=AE=A1=E9=81=93=E5=88=B0=20ocr=5Fpipeline?= =?UTF-8?q?=20(1939=20=E2=86=92=201560)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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//_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) --- douyin/core/underlying_operations.py | 373 +----------------- .../underlying_operations_pkg/ocr_pipeline.py | 367 +++++++++++++++++ tests/test_ocr_pipeline.py | 269 +++++++++++++ 3 files changed, 640 insertions(+), 369 deletions(-) create mode 100644 douyin/core/underlying_operations_pkg/ocr_pipeline.py create mode 100644 tests/test_ocr_pipeline.py diff --git a/douyin/core/underlying_operations.py b/douyin/core/underlying_operations.py index f4588f5..546d4b5 100644 --- a/douyin/core/underlying_operations.py +++ b/douyin/core/underlying_operations.py @@ -730,376 +730,11 @@ class underlying_operations: change_color: 颜色处理模式,0=保留彩色(推荐),1/2=二值化等 pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找 return_confidence: 是否返回置信度信息(默认False,保持向后兼容) - 返回: - 如果return_confidence=False: 识别到的文本(字符串),识别失败返回空字符串 - 如果return_confidence=True: (text, confidence_info) 元组 """ - if not pic_path: - try: - pic_path = self._find_pic_path(picname) - except Exception: - pic_path = None - - if not pic_path or not os.path.exists(pic_path): - return ("", None) if return_confidence else "" - - # ===== 缓存查询:基于图片字节哈希 ===== - # 同一张图重复 OCR 直接命中(业务里 cut_pic 可能短时间多次重读相同区域) - cache_key = None - try: - from operations.ocr_operations import ( - _img_fingerprint, cache_get, cache_put, - ) - with open(pic_path, "rb") as _f: - _bytes = _f.read() - cache_key = _img_fingerprint(_bytes, mode=int(change_color or 0)) - cached = cache_get(cache_key) - if cached is not None: - return cached if return_confidence else cached[0] - except Exception: - cache_key = None - - # 优化:减少图像验证步骤,直接加载(节省50-100ms) - img = None - try: - img = Image.open(pic_path) - img.load() - except (IOError, OSError, UnidentifiedImageError): - return ("", None) if return_confidence else "" - - if img is None: - return ("", None) if return_confidence else "" - - # 预处理:实测 PaddleOCR v4-mobile 内部已自带 resize/normalize, - # 外层"放大+锐化+对比度增强"反而会引入伪影,让识别变差(tools/test_preprocess_impact.py 实测)。 - # 因此默认走原图。仅在 change_color=1/2 业务显式要求二值化时才做。 - if change_color in (1, 2): - img = img.convert("L") - thresh = 128 if change_color == 1 else 180 - img = img.point(lambda x, t=thresh: 0 if int(x) < t else 255) - img = img.convert("RGB") - else: - img = img.convert("RGB") - - w, h = img.size - img1 = img # 不再做放大/锐化/对比度,PaddleOCR 自己处理 - - # 调试图像保存(默认关闭,业务调试时通过 SAVE_OCR_DEBUG_IMAGES=1 启用) - try: - save_debug = os.environ.get("SAVE_OCR_DEBUG_IMAGES", "0") == "1" - if not save_debug: - try: - save_debug = bool(getattr(self, "save_ocr_debug_images", False)) - except Exception: - pass - - if save_debug: - from douyin.handlers import douyin_screenshot_manager - from douyin.core import path_config - - ocr_coords = None - try: - with self._ocr_coords_lock: - ocr_coords = self._ocr_coords_cache.get(picname) - except Exception: - pass - - full_screenshot = None - try: - screenshot_path = path_config.get_screenshot_path() - if os.path.exists(screenshot_path): - full_screenshot = Image.open(screenshot_path) - full_screenshot.load() - full_screenshot = full_screenshot.convert("RGB") - except Exception: - pass - - debug_image = full_screenshot if full_screenshot is not None else img1 - debug_path = douyin_screenshot_manager.save_debug_image( - debug_image, - operation_type="ocr", - picname=picname, - step="preprocessed", - coords=ocr_coords, - extra_info={ - "mode": change_color, - "size": f"{w}x{h}", - "source": pic_path if pic_path else picname, - "has_full_screenshot": full_screenshot is not None, - "has_coords": ocr_coords is not None, - }, - ) - if debug_path: - try: - logging.info("[调试图像] OCR预处理保存: %s", debug_path) - except Exception: - pass - except Exception: - pass - - img_np = np.array(img1) - - # 用于收集置信度信息 - ocr_confidence_info = None - - # 如果 OCR 未初始化,安全降级 - try: - ocr_obj = getattr(self, "ocr", None) - ocr_type = getattr(self, "ocr_type", None) - except Exception: - ocr_obj = None - ocr_type = None - - if ocr_obj is None: - try: - logging.warning("OCR 未初始化,跳过识别:picname=%s", picname) - except Exception: - pass - return ("", None) if return_confidence else "" - - try: - if ocr_type == "easyocr": - result = ocr_obj.readtext(img_np) - extract_result = self.extract_ocr_content( - result, return_confidence=True - ) - if isinstance(extract_result, tuple): - text, ocr_confidence_info = extract_result - else: - text = extract_result - else: - # PaddleOCR 3.x:predict() 是新推荐 API;ocr() 仍兼容 - _predict = getattr(ocr_obj, "predict", None) or ocr_obj.ocr - result = _predict(img_np) - if result == [None]: - if cache_key: - try: - cache_put(cache_key, ("", None)) - except Exception: - pass - return ("", None) if return_confidence else "" - extract_result = self.extract_ocr_content( - result, return_confidence=True - ) - if isinstance(extract_result, tuple): - text, ocr_confidence_info = extract_result - else: - text = extract_result - except Exception as e: - try: - logging.warning("OCR 调用失败,返回空结果:%s", e) - except Exception: - pass - return ("", None) if return_confidence else "" - - # 写入缓存 - if cache_key: - try: - cache_put(cache_key, (text, ocr_confidence_info)) - except Exception: - pass - - # Initialize variables to avoid unbound variable issues - img1 = img # img1 is used later, initialize from img - img2 = None - text2 = None - # 禁用二次OCR识别(用户要求:没有GPU,不需要二次OCR) - # 如果第一次识别失败,直接返回空结果,避免耗时3-4秒的二次识别 - # if (not text) and max(w, h) <= 1200: - # try: - # img2 = img.resize((w * 2, h * 2), _RESAMPLE.LANCZOS) - # try: - # from PIL import ImageEnhance - # - # img2_sharpened = img2.filter( - # ImageFilter.UnsharpMask(radius=1.5, percent=180, threshold=3) - # ) - # enhancer = ImageEnhance.Contrast(img2_sharpened) - # img2 = enhancer.enhance(1.2) - # except Exception: - # img2 = img2.filter(ImageFilter.SHARPEN) - # - # if getattr(self, "ocr_type", None) == "easyocr": - # result2 = self.ocr.readtext(np.array(img2)) - # extract_result2 = self.extract_ocr_content(result2, return_confidence=True) - # if isinstance(extract_result2, tuple): - # text2, ocr_confidence_info = extract_result2 - # else: - # text2 = extract_result2 - # if text2: - # text = text2 - # result = result2 - # else: - # result2 = self.ocr.ocr(np.array(img2)) - # if result2 != [None]: - # extract_result2 = self.extract_ocr_content(result2, return_confidence=True) - # if isinstance(extract_result2, tuple): - # text2, ocr_confidence_info = extract_result2 - # else: - # text2 = extract_result2 - # if text2: - # text = text2 - # result = result2 - # except Exception: - # pass - - if picname in ("福袋内容", "福袋倒计时") and not text: - try: - if isinstance(result, list) and result and isinstance(result[0], dict): - _uo_log( - f"OCR调试({picname}): result[0] keys={list(result[0].keys())}" - ) - else: - _uo_log(f"OCR调试({picname}): result type={type(result)}") - except Exception: - pass - - try: - from douyin.handlers import douyin_screenshot_manager - - # Ensure img1 is defined before use - if "img1" not in locals(): - img1 = img - img_for_attempt = ( - img2 if (img2 is not None and text2 and text2.strip()) else img1 - ) - douyin_screenshot_manager.save_latest_attempt( - img_for_attempt, operation_type="ocr" - ) - - # 保存OCR结果后的调试图像(带OCR文本结果和置信度)- 使用完整截图 - ocr_result_preview = ( - (text or text2 or "").strip()[:50] if (text or text2) else "" - ) - extra_info_dict = { - "mode": change_color, - "text_len": len(text or text2 or ""), - "text_preview": ocr_result_preview, - "used_img2": bool(img2 is not None and text2 and text2.strip()), - } - # 添加置信度信息 - if ocr_confidence_info: - extra_info_dict["avg_confidence"] = round( - ocr_confidence_info.get("avg_confidence", 0.0), 4 - ) - extra_info_dict["min_confidence"] = round( - ocr_confidence_info.get("min_confidence", 0.0), 4 - ) - extra_info_dict["max_confidence"] = round( - ocr_confidence_info.get("max_confidence", 0.0), 4 - ) - extra_info_dict["confidence_items"] = ocr_confidence_info.get( - "item_count", 0 - ) - - # 尝试获取OCR区域坐标 - ocr_coords = None - try: - ocr_coords = self._ocr_coords_cache.get(picname) - except Exception: - pass - - # 加载完整截图 - full_screenshot = None - try: - from douyin.core import path_config - - screenshot_path = path_config.get_screenshot_path() - if os.path.exists(screenshot_path): - full_screenshot = Image.open(screenshot_path) - full_screenshot.load() - full_screenshot = full_screenshot.convert("RGB") - except Exception: - pass - - # 如果成功加载完整截图,使用完整截图;否则使用预处理图像 - debug_image = ( - full_screenshot if full_screenshot is not None else img_for_attempt - ) - extra_info_dict["has_full_screenshot"] = full_screenshot is not None - extra_info_dict["has_coords"] = ocr_coords is not None - - debug_path = douyin_screenshot_manager.save_debug_image( - debug_image, - operation_type="ocr", - picname=picname, - step="after_ocr", - coords=ocr_coords, - extra_info=extra_info_dict, - ) - - # 记录OCR置信度到日志 - if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0: - try: - logging.info( - "[OCR置信度] %s: 平均=%.4f, 最小=%.4f, 最大=%.4f, 项目数=%d", - picname, - ocr_confidence_info.get("avg_confidence", 0.0), - ocr_confidence_info.get("min_confidence", 0.0), - ocr_confidence_info.get("max_confidence", 0.0), - int(ocr_confidence_info.get("item_count", 0) or 0), - ) - except Exception: - pass - if debug_path: - try: - logging.info( - "[调试图像] OCR结果保存: %s, 文本: %s", - debug_path, - ocr_result_preview, - ) - except Exception: - pass - except Exception: - pass - - if text and text.strip() and picname: - try: - try: - from douyin.handlers import douyin_screenshot_manager - - category = douyin_screenshot_manager.get_category_for_picname( - picname - ) - except Exception: - category = "others" - - from douyin.core import path_config - pic_base = path_config.get_pic_dir() - category_dir = os.path.join(pic_base, "ocr", category) - os.makedirs(category_dir, exist_ok=True) - - success_path = os.path.join(category_dir, f"{picname}_success.png") - try: - # Ensure img1 is defined before use - if "img1" not in locals(): - img1 = img - if img2 is not None and text2 and text2.strip(): - img_to_save = img2 - else: - img_to_save = img1 - try: - from douyin.handlers import douyin_screenshot_manager - - douyin_screenshot_manager.save_last_success( - img_to_save, operation_type="ocr" - ) - except Exception: - pass - img_to_save.save(success_path) - except Exception: - try: - if img is not None: - img.save(success_path) - except Exception: - pass - except Exception: - pass - - if return_confidence: - return text, ocr_confidence_info - else: - return text + from douyin.core.underlying_operations_pkg.ocr_pipeline import ( + analyse_pic_word as _impl, + ) + return _impl(self, picname, change_color, pic_path, return_confidence) def analyse_pic_digits(self, picname, change_color=1, pic_path=None): return _impl_analyse_pic_digits(self, picname, change_color, pic_path) diff --git a/douyin/core/underlying_operations_pkg/ocr_pipeline.py b/douyin/core/underlying_operations_pkg/ocr_pipeline.py new file mode 100644 index 0000000..ddd0293 --- /dev/null +++ b/douyin/core/underlying_operations_pkg/ocr_pipeline.py @@ -0,0 +1,367 @@ +"""douyin/core/underlying_operations_pkg/ocr_pipeline - analyse_pic_word OCR 管道 + +把 underlying_operations.analyse_pic_word 整段抽出来。流程: +1. 路径解析 + 缓存查询(基于图片字节哈希) +2. 加载 + 验证图片完整性 +3. change_color 预处理(0=保留彩色,1/2=二值化) +4. 调试图像保存(preprocessed 步骤,仅当 SAVE_OCR_DEBUG_IMAGES=1) +5. 调用 OCR(EasyOCR / PaddleOCR predict/ocr) +6. 提取文本 + 置信度 +7. 写缓存 +8. 调试图像保存(after_ocr 步骤) +9. 成功识别保存到 ocr//_success.png + +入口:analyse_pic_word(self, picname, change_color, pic_path, return_confidence) +self 必须提供:_find_pic_path / extract_ocr_content / ocr / ocr_type / + _ocr_coords_lock / _ocr_coords_cache / save_ocr_debug_images +""" +from __future__ import annotations + +import logging +import os +from typing import Any, Optional + +from PIL import Image, UnidentifiedImageError +import numpy as np + + +def analyse_pic_word( + self: Any, + picname: str, + change_color: int = 0, + pic_path: Optional[str] = None, + return_confidence: bool = False, +): + """对指定图片运行 OCR 并返回拼接后的文本结果。 + + Args: + self: underlying_operations 实例 + picname: 图片名(如"福袋内容") + change_color: 0=保留彩色,1/2=二值化 + pic_path: 可选;未提供时通过 self._find_pic_path 查找 + return_confidence: True 时返回 (text, confidence_info);否则只返回 text + + Returns: + return_confidence=False: text(str) + return_confidence=True: (text, confidence_info) + """ + from douyin.core.underlying_operations import _uo_log + + if not pic_path: + try: + pic_path = self._find_pic_path(picname) + except Exception: + pic_path = None + + if not pic_path or not os.path.exists(pic_path): + return ("", None) if return_confidence else "" + + # ===== 缓存查询:基于图片字节哈希 ===== + cache_key = None + try: + from operations.ocr_operations import ( + _img_fingerprint, cache_get, cache_put, + ) + with open(pic_path, "rb") as _f: + _bytes = _f.read() + cache_key = _img_fingerprint(_bytes, mode=int(change_color or 0)) + cached = cache_get(cache_key) + if cached is not None: + return cached if return_confidence else cached[0] + except Exception: + cache_key = None + + # 加载图片 + img = None + try: + img = Image.open(pic_path) + img.load() + except (IOError, OSError, UnidentifiedImageError): + return ("", None) if return_confidence else "" + + if img is None: + return ("", None) if return_confidence else "" + + # 预处理:实测 PaddleOCR v4-mobile 内部已自带 resize/normalize, + # 外层"放大+锐化+对比度增强"反而会引入伪影,让识别变差。 + # 因此默认走原图。仅在 change_color=1/2 业务显式要求二值化时才做。 + if change_color in (1, 2): + img = img.convert("L") + thresh = 128 if change_color == 1 else 180 + img = img.point(lambda x, t=thresh: 0 if int(x) < t else 255) + img = img.convert("RGB") + else: + img = img.convert("RGB") + + w, h = img.size + img1 = img # 不再做放大/锐化/对比度 + + # 调试图像保存(preprocessed 步骤) + _save_debug_preprocessed(self, img1, picname, pic_path, change_color, w, h) + + img_np = np.array(img1) + ocr_confidence_info = None + + try: + ocr_obj = getattr(self, "ocr", None) + ocr_type = getattr(self, "ocr_type", None) + except Exception: + ocr_obj = None + ocr_type = None + + if ocr_obj is None: + try: + logging.warning("OCR 未初始化,跳过识别:picname=%s", picname) + except Exception: + pass + return ("", None) if return_confidence else "" + + # 执行 OCR + try: + if ocr_type == "easyocr": + result = ocr_obj.readtext(img_np) + extract_result = self.extract_ocr_content(result, return_confidence=True) + if isinstance(extract_result, tuple): + text, ocr_confidence_info = extract_result + else: + text = extract_result + else: + # PaddleOCR 3.x:predict() 是新推荐 API;ocr() 仍兼容 + _predict = getattr(ocr_obj, "predict", None) or ocr_obj.ocr + result = _predict(img_np) + if result == [None]: + if cache_key: + try: + cache_put(cache_key, ("", None)) + except Exception: + pass + return ("", None) if return_confidence else "" + extract_result = self.extract_ocr_content(result, return_confidence=True) + if isinstance(extract_result, tuple): + text, ocr_confidence_info = extract_result + else: + text = extract_result + except Exception as e: + try: + logging.warning("OCR 调用失败,返回空结果:%s", e) + except Exception: + pass + return ("", None) if return_confidence else "" + + # 写缓存 + if cache_key: + try: + cache_put(cache_key, (text, ocr_confidence_info)) + except Exception: + pass + + # 调试日志:福袋内容 / 福袋倒计时 OCR 失败时打印 result 结构 + if picname in ("福袋内容", "福袋倒计时") and not text: + try: + if isinstance(result, list) and result and isinstance(result[0], dict): + _uo_log(f"OCR调试({picname}): result[0] keys={list(result[0].keys())}") + else: + _uo_log(f"OCR调试({picname}): result type={type(result)}") + except Exception: + pass + + # 调试图像保存(after_ocr 步骤)+ 置信度日志 + _save_debug_after_ocr( + self, img1, picname, change_color, text, + ocr_confidence_info, + ) + + # 成功识别 → 保存到 ocr//_success.png + if text and text.strip() and picname: + _save_success_pic(img, img1, picname, text) + + if return_confidence: + return text, ocr_confidence_info + else: + return text + + +def _save_debug_preprocessed( + self: Any, img1, picname: str, pic_path: str, + change_color: int, w: int, h: int, +) -> None: + """保存预处理后的调试图像(OCR 输入),仅当 SAVE_OCR_DEBUG_IMAGES=1 时执行。""" + try: + save_debug = os.environ.get("SAVE_OCR_DEBUG_IMAGES", "0") == "1" + if not save_debug: + try: + save_debug = bool(getattr(self, "save_ocr_debug_images", False)) + except Exception: + pass + + if not save_debug: + return + + from douyin.handlers import douyin_screenshot_manager + from douyin.core import path_config + + ocr_coords = None + try: + with self._ocr_coords_lock: + ocr_coords = self._ocr_coords_cache.get(picname) + except Exception: + pass + + full_screenshot = None + try: + screenshot_path = path_config.get_screenshot_path() + if os.path.exists(screenshot_path): + full_screenshot = Image.open(screenshot_path) + full_screenshot.load() + full_screenshot = full_screenshot.convert("RGB") + except Exception: + pass + + debug_image = full_screenshot if full_screenshot is not None else img1 + debug_path = douyin_screenshot_manager.save_debug_image( + debug_image, + operation_type="ocr", + picname=picname, + step="preprocessed", + coords=ocr_coords, + extra_info={ + "mode": change_color, + "size": f"{w}x{h}", + "source": pic_path if pic_path else picname, + "has_full_screenshot": full_screenshot is not None, + "has_coords": ocr_coords is not None, + }, + ) + if debug_path: + try: + logging.info("[调试图像] OCR预处理保存: %s", debug_path) + except Exception: + pass + except Exception: + pass + + +def _save_debug_after_ocr( + self: Any, img1, picname: str, change_color: int, + text: Optional[str], ocr_confidence_info: Optional[dict], +) -> None: + """保存 OCR 完成后的调试图像(含识别文本预览 + 置信度)。""" + try: + from douyin.handlers import douyin_screenshot_manager + + douyin_screenshot_manager.save_latest_attempt(img1, operation_type="ocr") + + ocr_result_preview = (text or "").strip()[:50] if text else "" + extra_info_dict = { + "mode": change_color, + "text_len": len(text or ""), + "text_preview": ocr_result_preview, + "used_img2": False, # 二次 OCR 已禁用 + } + if ocr_confidence_info: + extra_info_dict["avg_confidence"] = round( + ocr_confidence_info.get("avg_confidence", 0.0), 4 + ) + extra_info_dict["min_confidence"] = round( + ocr_confidence_info.get("min_confidence", 0.0), 4 + ) + extra_info_dict["max_confidence"] = round( + ocr_confidence_info.get("max_confidence", 0.0), 4 + ) + extra_info_dict["confidence_items"] = ocr_confidence_info.get( + "item_count", 0 + ) + + ocr_coords = None + try: + ocr_coords = self._ocr_coords_cache.get(picname) + except Exception: + pass + + full_screenshot = None + try: + from douyin.core import path_config + + screenshot_path = path_config.get_screenshot_path() + if os.path.exists(screenshot_path): + full_screenshot = Image.open(screenshot_path) + full_screenshot.load() + full_screenshot = full_screenshot.convert("RGB") + except Exception: + pass + + debug_image = full_screenshot if full_screenshot is not None else img1 + extra_info_dict["has_full_screenshot"] = full_screenshot is not None + extra_info_dict["has_coords"] = ocr_coords is not None + + debug_path = douyin_screenshot_manager.save_debug_image( + debug_image, + operation_type="ocr", + picname=picname, + step="after_ocr", + coords=ocr_coords, + extra_info=extra_info_dict, + ) + + # 记录 OCR 置信度到日志 + if ocr_confidence_info and ocr_confidence_info.get("item_count", 0) > 0: + try: + logging.info( + "[OCR置信度] %s: 平均=%.4f, 最小=%.4f, 最大=%.4f, 项目数=%d", + picname, + ocr_confidence_info.get("avg_confidence", 0.0), + ocr_confidence_info.get("min_confidence", 0.0), + ocr_confidence_info.get("max_confidence", 0.0), + int(ocr_confidence_info.get("item_count", 0) or 0), + ) + except Exception: + pass + if debug_path: + try: + logging.info( + "[调试图像] OCR结果保存: %s, 文本: %s", + debug_path, + ocr_result_preview, + ) + except Exception: + pass + except Exception: + pass + + +def _save_success_pic(img, img1, picname: str, text: str) -> None: + """成功识别后保存到 ocr//_success.png。""" + try: + try: + from douyin.handlers import douyin_screenshot_manager + + category = douyin_screenshot_manager.get_category_for_picname(picname) + except Exception: + category = "others" + + from douyin.core import path_config + + pic_base = path_config.get_pic_dir() + category_dir = os.path.join(pic_base, "ocr", category) + os.makedirs(category_dir, exist_ok=True) + + success_path = os.path.join(category_dir, f"{picname}_success.png") + try: + img_to_save = img1 + try: + from douyin.handlers import douyin_screenshot_manager + + douyin_screenshot_manager.save_last_success( + img_to_save, operation_type="ocr" + ) + except Exception: + pass + img_to_save.save(success_path) + except Exception: + try: + if img is not None: + img.save(success_path) + except Exception: + pass + except Exception: + pass diff --git a/tests/test_ocr_pipeline.py b/tests/test_ocr_pipeline.py new file mode 100644 index 0000000..7207860 --- /dev/null +++ b/tests/test_ocr_pipeline.py @@ -0,0 +1,269 @@ +"""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