#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试数字OCR模块 使用debug_capture中的图片测试analyse_pic_digits函数 """ import os import sys from pathlib import Path # 添加项目根目录到路径 project_root = Path(__file__).parent sys.path.insert(0, str(project_root)) from Underlying_Operations import underlying_operations from douyin_utils import extract_prize_value_yuan def test_digit_ocr(): """测试数字OCR功能""" print("=" * 60) print("数字OCR模块测试") print("=" * 60) # 初始化OCR操作类 print("\n正在初始化OCR引擎...") try: ops = underlying_operations() print(f"OCR引擎类型: {ops.ocr_type}") print("OCR初始化成功!\n") except Exception as e: print(f"OCR初始化失败: {e}") return False # 测试图片路径 - 优先使用pic/ocr/fudai目录下的图片(这些是实际使用的图片) fudai_dir = project_root / "pic" / "ocr" / "fudai" debug_dir = project_root / "pic" / "debug_captures" / "2025-12-26" # 查找测试图片 test_images = { "福袋价值": None, "福袋倒计时_分钟": None, "福袋倒计时_秒": None, } print("正在查找测试图片...") # 第一轮:优先使用pic/ocr/fudai目录下的图片(这些是实际使用的图片) if fudai_dir.exists(): for file in fudai_dir.glob("*.png"): filename = file.name # 排除success图片,使用原始图片 if "success" not in filename: if "福袋价值" in filename and "倒计时" not in filename and test_images["福袋价值"] is None: test_images["福袋价值"] = file elif "福袋倒计时_分钟" in filename and test_images["福袋倒计时_分钟"] is None: test_images["福袋倒计时_分钟"] = file elif "福袋倒计时_秒" in filename and test_images["福袋倒计时_秒"] is None: test_images["福袋倒计时_秒"] = file # 第二轮:如果没找到,使用debug_capture中的cut_pic图片 if debug_dir.exists(): for name in test_images.keys(): if test_images[name] is None: for file in debug_dir.glob("*.png"): filename = file.name if "cut_pic" in filename and "before_ocr" in filename: if name in filename: test_images[name] = file break # 显示找到的图片 print("\n找到的测试图片:") for name, path in test_images.items(): if path: print(f" [OK] {name}: {path.name}") else: print(f" [X] {name}: 未找到") # 测试数字OCR print("\n" + "=" * 60) print("开始测试数字OCR识别") print("=" * 60) results = {} # 测试福袋价值 if test_images["福袋价值"]: print(f"\n【测试1】福袋价值识别") print(f"图片: {test_images['福袋价值'].name}") try: # 使用analyse_pic_digits识别 result = ops.analyse_pic_digits( "test_福袋价值", change_color=1, pic_path=str(test_images["福袋价值"]) ) print(f"识别结果(数字OCR): '{result}'") # 提取价值 value = extract_prize_value_yuan(result) print(f"提取的价值: {value} 元") results["福袋价值"] = { "result": result, "value": value, "success": bool(result) } except Exception as e: print(f"识别失败: {e}") import traceback traceback.print_exc() results["福袋价值"] = {"error": str(e)} # 测试倒计时分钟 if test_images["福袋倒计时_分钟"]: print(f"\n【测试2】福袋倒计时分钟识别") print(f"图片: {test_images['福袋倒计时_分钟'].name}") try: result = ops.analyse_pic_digits( "test_福袋倒计时_分钟", change_color=1, pic_path=str(test_images["福袋倒计时_分钟"]) ) print(f"识别结果(数字OCR): '{result}'") results["福袋倒计时_分钟"] = { "result": result, "success": bool(result) } except Exception as e: print(f"识别失败: {e}") import traceback traceback.print_exc() results["福袋倒计时_分钟"] = {"error": str(e)} # 测试倒计时秒 if test_images["福袋倒计时_秒"]: print(f"\n【测试3】福袋倒计时秒识别") print(f"图片: {test_images['福袋倒计时_秒'].name}") try: result = ops.analyse_pic_digits( "test_福袋倒计时_秒", change_color=1, pic_path=str(test_images["福袋倒计时_秒"]) ) print(f"识别结果(数字OCR): '{result}'") results["福袋倒计时_秒"] = { "result": result, "success": bool(result) } except Exception as e: print(f"识别失败: {e}") import traceback traceback.print_exc() results["福袋倒计时_秒"] = {"error": str(e)} # 对比测试:使用原来的analyse_pic_word print("\n" + "=" * 60) print("对比测试:使用原来的analyse_pic_word") print("=" * 60) comparison_results = {} if test_images["福袋价值"]: print(f"\n【对比测试1】福袋价值(使用analyse_pic_word)") try: result = ops.analyse_pic_word( "test_福袋价值", change_color=0, pic_path=str(test_images["福袋价值"]) ) print(f"识别结果(完整OCR): '{result}'") value = extract_prize_value_yuan(result) print(f"提取的价值: {value} 元") comparison_results["福袋价值"] = {"result": result, "value": value} except Exception as e: print(f"识别失败: {e}") comparison_results["福袋价值"] = {"error": str(e)} if test_images["福袋倒计时_分钟"]: print(f"\n【对比测试2】福袋倒计时分钟(使用analyse_pic_word)") try: result = ops.analyse_pic_word( "test_福袋倒计时_分钟", change_color=0, pic_path=str(test_images["福袋倒计时_分钟"]) ) print(f"识别结果(完整OCR): '{result}'") comparison_results["福袋倒计时_分钟"] = {"result": result} except Exception as e: print(f"识别失败: {e}") comparison_results["福袋倒计时_分钟"] = {"error": str(e)} if test_images["福袋倒计时_秒"]: print(f"\n【对比测试3】福袋倒计时秒(使用analyse_pic_word)") try: result = ops.analyse_pic_word( "test_福袋倒计时_秒", change_color=0, pic_path=str(test_images["福袋倒计时_秒"]) ) print(f"识别结果(完整OCR): '{result}'") comparison_results["福袋倒计时_秒"] = {"result": result} except Exception as e: print(f"识别失败: {e}") comparison_results["福袋倒计时_秒"] = {"error": str(e)} # 总结 print("\n" + "=" * 60) print("测试总结") print("=" * 60) print("\n数字OCR结果:") for name, result in results.items(): if "error" in result: print(f" {name}: [ERROR] 错误 - {result['error']}") elif result.get("success"): print(f" {name}: [OK] 成功 - '{result.get('result', '')}'") else: print(f" {name}: [WARN] 未识别到数字") print("\n完整OCR对比结果:") for name, result in comparison_results.items(): if "error" in result: print(f" {name}: [ERROR] 错误 - {result['error']}") else: print(f" {name}: '{result.get('result', '')}'") # 检查是否有问题 print("\n" + "=" * 60) print("问题检查") print("=" * 60) issues = [] # 检查数字OCR是否正常工作 for name, result in results.items(): if "error" in result: issues.append(f"{name}: 数字OCR函数报错 - {result['error']}") elif not result.get("success"): issues.append(f"{name}: 数字OCR未识别到任何数字") # 检查对比结果 for name in results.keys(): if name in comparison_results: digit_result = results[name].get("result", "") full_result = comparison_results[name].get("result", "") if digit_result and full_result: # 提取数字进行比较 import re digit_digits = re.findall(r'\d+', digit_result) full_digits = re.findall(r'\d+', full_result) # 对于价值,比较提取的数值(忽略前导零) if name == "福袋价值": digit_value = results[name].get("value", 0.0) full_value = comparison_results[name].get("value", 0.0) if abs(digit_value - full_value) > 0.01: issues.append(f"{name}: 数字OCR提取的价值({digit_value})和完整OCR提取的价值({full_value})不一致") else: # 对于倒计时,比较数字(忽略前导零) digit_nums = [int(d) for d in digit_digits] full_nums = [int(d) for d in full_digits] if digit_nums != full_nums: issues.append(f"{name}: 数字OCR({digit_result})和完整OCR({full_result})结果不一致") if issues: print("\n[WARN] 发现以下问题:") for issue in issues: print(f" - {issue}") return False else: print("\n[OK] 所有测试通过,未发现问题!") return True if __name__ == "__main__": success = test_digit_ocr() sys.exit(0 if success else 1)