- hr_sdk.wage_import:DrissionPage 注入 cookie 打开 HR 门户,页面内 WS 订阅 + fetch 上传,解析 over: 结果;build_xlsx 第 9 列卡号强制数值(修复「卡号必须是数字」) - 纯 HTTP 导入不可用:服务端异步任务与浏览器会话强绑定,且前端无轮询接口(已核实),结果通道只能用浏览器上下文 - desktopauto 0.5.0 新增 cardocr:身份证/银行卡 OCR、号码纠错、Luhn 与模 11-2 校验、交叉核对 - 新增插件 XQ0084 银行卡更新EHR系统:AI 表格待处理行 → 影像识别 → 一致性核对/异常名单 - .gitignore 忽略 tests/aitable_probe/_out/(含员工证件照,禁止入库)
93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""XQ0084 识别层:调用 desktopauto 的银行卡/身份证识别能力。"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
|
||
|
||
def _digit_runs(results):
|
||
runs = []
|
||
for res in results:
|
||
for raw in (res.get("raw") or []):
|
||
runs.extend(raw.get("digit_runs") or [])
|
||
return runs
|
||
|
||
|
||
def _best(results, kind, prefer):
|
||
"""在同一条记录的多张照片里取最优识别结果(优先校验通过,其次位数更长)。"""
|
||
cands = [r.get(kind) or {} for r in results if (r.get(kind) or {})]
|
||
if not cands:
|
||
return {}
|
||
return max(cands, key=lambda c: (1 if c.get(prefer) else 0,
|
||
len(str(c.get("number") or ""))))
|
||
|
||
|
||
def _enhance(paths, bank_hint):
|
||
"""常规识别读不出卡号时,用浮雕增强接口兜底(只采信 Luhn 自身合法的高置信结果)。"""
|
||
try:
|
||
from desktopauto import read_bank_card_number
|
||
except Exception: # noqa: BLE001 老版本 SDK 没有该接口
|
||
return None
|
||
best = None
|
||
for path in paths:
|
||
try:
|
||
r = read_bank_card_number(path, face_text=bank_hint)
|
||
except Exception: # noqa: BLE001
|
||
continue
|
||
if r.status == "ok":
|
||
return r
|
||
if best is None or _erank(r) > _erank(best):
|
||
best = r
|
||
return best
|
||
|
||
|
||
def _erank(r):
|
||
level = {"high": 3, "medium": 2, "low": 1}.get(r.confidence, 0)
|
||
return (1 if r.luhn_ok else 0, level, len(r.candidates))
|
||
|
||
|
||
def recognize(paths, expected_name=""):
|
||
"""识别一批照片,合并出身份证/银行卡最佳结果。
|
||
|
||
Returns:
|
||
{"id": {...}, "bank": {...}, "runs": [...], "errors": [...], "files": [...]}
|
||
"""
|
||
from desktopauto.cardocr.pipeline import analyze_image
|
||
|
||
results, errors = [], []
|
||
for path in paths:
|
||
try:
|
||
results.append(analyze_image(path, expected_name=expected_name))
|
||
except Exception as e:
|
||
errors.append(f"{os.path.basename(path)}:{e}")
|
||
|
||
idc = _best(results, "id_card", "checksum_ok")
|
||
bk = _best(results, "bank_card", "luhn_ok")
|
||
bank = {
|
||
"number": str(bk.get("number") or ""),
|
||
"bank": str(bk.get("bank") or ""),
|
||
"luhn_ok": bool(bk.get("luhn_ok")),
|
||
}
|
||
# 常规通道没读到可信卡号:浮雕/反光增强兜底;仍不合法则只留候选,交人工
|
||
if not bank["luhn_ok"]:
|
||
enh = _enhance(paths, bank["bank"])
|
||
if enh is not None and enh.status == "ok":
|
||
bank.update(number=enh.number, bank=enh.bank or bank["bank"],
|
||
luhn_ok=True, confidence=enh.confidence, source="enhance")
|
||
elif enh is not None:
|
||
bank["confidence"] = enh.confidence
|
||
# 只有「差 1~2 位就能通过校验」的候选才值得给人看;差太多的是噪声
|
||
if enh.candidates and enh.candidates[0].get("cost", 9) <= 2:
|
||
bank["candidates"] = [c["number"] for c in enh.candidates]
|
||
return {
|
||
"id": {
|
||
"number": str(idc.get("number") or ""),
|
||
"name": str(idc.get("name") or ""),
|
||
"checksum_ok": bool(idc.get("checksum_ok")),
|
||
},
|
||
"bank": bank,
|
||
"runs": _digit_runs(results),
|
||
"errors": errors,
|
||
"files": list(paths),
|
||
}
|