# -*- coding: utf-8 -*- """诊断:对识别失败的卡面照片做多方向 / 多增强扫描,看卡号像素里到底有没有信息。 用法(仓库根): .venv\\Scripts\\python.exe tests\\aitable_probe\\diag_hard.py """ from __future__ import annotations import sys import time from pathlib import Path HERE = Path(__file__).resolve().parent REPO_ROOT = HERE.parents[1] sys.path.insert(0, str(REPO_ROOT / "libs" / "desktopauto")) import numpy as np # noqa: E402 from desktopauto.cardocr.engine import lines as ocr # noqa: E402 from desktopauto.cardocr.img import gray_variants, imread, resize_side, rotate90, to_gray # noqa: E402 from desktopauto.cardocr.validators import _CONFUSABLE, normalize_text # noqa: E402 OUT = HERE / "_out" # 识别失败的 4 个卡号样本(真值来自人工核对表) CASES = [ ("丁丽", OUT / "丁丽" / "银行卡-丁丽.jpg", "6214832377534461", (0, 3)), ("李瑞雪", OUT / "李瑞雪" / "银行卡-李瑞雪银行卡.JPG", "6214830893671585", (0,)), ("李诗灿", OUT / "李诗灿" / "银行卡-e2b49e6270df400c7b027ac7ab760d54db92a644be3243391401f9e8045ed24b.png", "6214832377534412", (0,)), ("陈宇欣", OUT / "陈宇欣" / "银行卡-488291ab4cab19369f7102b6b2e9280b3e35ca042e5692c90d3ee220c4dd37f6.png", "6214832377534396", (0,)), ] def digit_runs(text: str, min_len: int = 6) -> list[str]: s = normalize_text(text) norm = "".join(ch if ch.isdigit() else _CONFUSABLE.get(ch, (" ",))[0] for ch in s) import re # noqa: PLC0415 return re.findall(r"[0-9]{%d,}" % min_len, norm) def main() -> int: try: sys.stdout.reconfigure(encoding="utf-8") except Exception: # noqa: BLE001 pass for name, path, truth, rots in CASES: print(f"\n===== {name} 真值 {truth} =====") img = imread(str(path)) print(f" 原图 {img.shape[1]}x{img.shape[0]}") for k in rots: rot = rotate90(img, k) small = resize_side(rot, max_side=1800, min_side=900) for vname, var in gray_variants(to_gray(small), upscale=1.0): if vname not in ("gray", "clahe", "blackhat", "otsu_inv", "sharp"): continue t0 = time.time() ls = ocr(var) hits = [] for ln in ls: d = len(digit_runs(ln.text)) if d or "银行" in ln.text or "Union" in ln.text: hits.append(ln.text[:40]) runs = digit_runs(" ".join(ln.text for ln in ls), 8) print(f" k={k} {vname:9s} {time.time() - t0:4.1f}s 段={len(ls):2d} " f"数字runs={runs[:3]} 文本={hits[:4]}") return 0 if __name__ == "__main__": raise SystemExit(main())