feat(cardreader): 新增自包含卡证识别 SDK,XQ0084 改用它读卡号
cardocr(身份证/银行卡分析)与 YOLO 卡面检测模型整体迁入 cardreader,插件不再依赖 desktopauto;常规识别读不出合法卡号时按 balanced→thorough 补读。
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
流程编排:
|
||||
1) 读取 AI 表格「银行卡-变更收集表」,只取「是否更新HR系统」为空或「否」的记录;
|
||||
2) 下载银行卡卡号面照片、持卡人身份证照片;
|
||||
3) desktopauto.cardocr 识别卡号/身份证号/姓名/开户银行,与表格登记值比对;
|
||||
3) cardreader 识别卡号/身份证号/姓名/开户银行,与表格登记值比对;
|
||||
4) 关闭试算模式时,核对通过的记录导入 HR「工资管理-工资卡管理」,成功后在 AI 表格回写
|
||||
「是否更新HR系统=是」;开启试算模式 = 只核对出表(不写 HR、不回写台账);
|
||||
5) 输出核对工作簿(核对结果 + 异常名单),识别不出或不一致的推送钉钉群提醒人事手动处理。
|
||||
@@ -31,7 +31,7 @@ class BankCardEhrUpdatePlugin(ScenePlugin):
|
||||
|
||||
id = "xq0084"
|
||||
name = "银行卡更新EHR系统"
|
||||
version = "0.1.8"
|
||||
version = "0.2.0"
|
||||
description = ("读取人资 AI 表格「银行卡变更收集表」中「是否更新HR系统」为空或「否」的记录,"
|
||||
"下载银行卡与身份证照片识别卡号、身份证号、姓名、开户银行,"
|
||||
"与表格登记值比对;核对通过自动导入 HR 工资卡并回写「是否更新HR系统=是」,"
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
"""XQ0084 银行卡更新EHR系统 - 配置常量与全局钩子。
|
||||
|
||||
插件自包含:只依赖 Python 标准库 / 第三方库 / 执行机挂载的 SDK
|
||||
(desktopauto、dingtalk_sdk),不 import 任何插件包之外的业务代码。
|
||||
(cardreader、dingtalk_sdk、hr_sdk),不 import 任何插件包之外的业务代码。
|
||||
|
||||
当前实现范围(第一阶段):
|
||||
1) 从 AI 表格「37°人资领域相关工作台账-银行卡变更收集表」拉取待处理行;
|
||||
2) 下载员工上传的银行卡卡号面照片、持卡人身份证照片;
|
||||
3) 用 desktopauto.cardocr 识别卡号/身份证号/姓名/开户银行,与表格登记值比对;
|
||||
3) 用 cardreader 识别卡号/身份证号/姓名/开户银行,与表格登记值比对;
|
||||
4) 输出核对工作簿;识别不出或不一致的进异常名单,推送钉钉群提醒人事手动处理。
|
||||
HR 系统「薪资管理-工资数据-工资卡管理」的写入留到后续阶段。
|
||||
"""
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0084 识别层:调用 desktopauto 的银行卡/身份证识别能力。"""
|
||||
"""XQ0084 识别层:cardreader 常规识别 + 卡面检测补读银行卡号。
|
||||
|
||||
常规通道(cardreader 的 analyze_image)负责身份证与银行卡的整体识别;
|
||||
银行卡号读不出 Luhn 合法值时,再用 cardreader 的 read_card(YOLO 找卡 +
|
||||
多视角投票)兜底,仍不行才走浮雕增强候选,最终交人工核对。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from .. import config
|
||||
|
||||
_CONF_RANK = {"high": 3, "medium": 2, "low": 1}
|
||||
|
||||
#: 卡面检测模块探测结果:None=未探测,False=不可用(依赖缺失,后续不再重试)
|
||||
_reader = None
|
||||
_reader_failed = False
|
||||
|
||||
|
||||
def _digit_runs(results):
|
||||
runs = []
|
||||
@@ -22,11 +35,57 @@ def _best(results, kind, prefer):
|
||||
len(str(c.get("number") or ""))))
|
||||
|
||||
|
||||
def _read_card():
|
||||
"""懒加载卡面识别 SDK;未挂载或依赖缺失时降级为只用常规通道。"""
|
||||
global _reader, _reader_failed
|
||||
if _reader is None and not _reader_failed:
|
||||
try:
|
||||
from cardreader import read_card
|
||||
_reader = read_card
|
||||
except Exception as exc: # noqa: BLE001
|
||||
_reader_failed = True
|
||||
config.logger.warning("卡面识别 SDK(cardreader)不可用,卡号只走常规通道:%s", exc)
|
||||
return _reader
|
||||
|
||||
|
||||
def _rank(result):
|
||||
return (1 if result.luhn_ok else 0, _CONF_RANK.get(result.confidence, 0),
|
||||
int(result.votes or 0))
|
||||
|
||||
|
||||
def _cardreader(paths, prefer=()):
|
||||
"""卡面检测补读卡号,返回最优 Result;模块不可用或全部失败返回 None。
|
||||
|
||||
先 balanced;若检测到银行卡但仍未读出合法卡号,再用 thorough
|
||||
(号带掩码密集搜索,专治压印银字/反光)。优先读常规通道已有银行卡痕迹的照片。
|
||||
"""
|
||||
read_card = _read_card()
|
||||
if read_card is None:
|
||||
return None
|
||||
order = [p for p in paths if p in prefer] + [p for p in paths if p not in prefer]
|
||||
best = None
|
||||
for path in order:
|
||||
try:
|
||||
cur = read_card(path, mode="balanced")
|
||||
if not cur.luhn_ok and "bank_card" in (cur.kinds or []):
|
||||
deep = read_card(path, mode="thorough")
|
||||
if _rank(deep) > _rank(cur):
|
||||
cur = deep
|
||||
except Exception as exc: # noqa: BLE001
|
||||
config.logger.warning("卡面检测失败(%s):%s", os.path.basename(str(path)), exc)
|
||||
continue
|
||||
if best is None or _rank(cur) > _rank(best):
|
||||
best = cur
|
||||
if cur.luhn_ok and cur.confidence == "high":
|
||||
break
|
||||
return best
|
||||
|
||||
|
||||
def _enhance(paths, bank_hint):
|
||||
"""常规识别读不出卡号时,用浮雕增强接口兜底(只采信 Luhn 自身合法的高置信结果)。"""
|
||||
try:
|
||||
from desktopauto import read_bank_card_number
|
||||
except Exception: # noqa: BLE001 老版本 SDK 没有该接口
|
||||
from cardreader import read_bank_card_number
|
||||
except Exception: # noqa: BLE001 依赖缺失时只用常规通道
|
||||
return None
|
||||
best = None
|
||||
for path in paths:
|
||||
@@ -52,7 +111,7 @@ def recognize(paths, expected_name=""):
|
||||
Returns:
|
||||
{"id": {...}, "bank": {...}, "runs": [...], "errors": [...], "files": [...]}
|
||||
"""
|
||||
from desktopauto.cardocr.pipeline import analyze_image
|
||||
from cardreader import analyze_image
|
||||
|
||||
results, errors = [], []
|
||||
for path in paths:
|
||||
@@ -68,17 +127,31 @@ def recognize(paths, expected_name=""):
|
||||
"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]
|
||||
prefer = [p for p, r in zip(paths, results)
|
||||
if (r.get("bank_card") or {}).get("number")
|
||||
or (r.get("bank_card") or {}).get("bank")]
|
||||
extra = _cardreader(paths, prefer)
|
||||
if extra is not None and extra.luhn_ok:
|
||||
bank.update(number=extra.number, luhn_ok=True,
|
||||
confidence=extra.confidence,
|
||||
source=f"卡面检测({extra.source or 'detect'},票数 {extra.votes})")
|
||||
config.logger.info("卡面检测补读卡号成功:%s(%s,票数 %d)",
|
||||
extra.number, extra.confidence, extra.votes)
|
||||
else:
|
||||
if extra is not None and "bank_card" in (extra.kinds or []):
|
||||
bank["confidence"] = "low" # 检测到卡但没读准,交人工对照影像
|
||||
# 卡面检测也没读准:再走浮雕增强兜底,仍不合法则只留候选
|
||||
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 ""),
|
||||
|
||||
@@ -10,8 +10,8 @@ from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from desktopauto.cardocr.match import EXACT, PARTIAL, STRONG, WEAK, coverage, digits
|
||||
from desktopauto.cardocr.validators import bin_bank, id_checksum_ok, luhn_ok
|
||||
from cardreader.cardocr.match import EXACT, PARTIAL, STRONG, WEAK, coverage, digits
|
||||
from cardreader.cardocr.validators import bin_bank, id_checksum_ok, luhn_ok
|
||||
|
||||
from .. import config
|
||||
|
||||
@@ -157,7 +157,7 @@ def verify(item, ocr):
|
||||
base["name_status"] = "一致" if ocr_name.replace(" ", "") == holder.replace(" ", "") else "不一致"
|
||||
bank_status = compare_bank(table_bank, ocr_bank)
|
||||
bank_note = ""
|
||||
# 影像没读出银行名时,用卡号 BIN 兜底判定(BIN 表见 desktopauto validators.BIN_BANK)
|
||||
# 影像没读出银行名时,用卡号 BIN 兜底判定(BIN 表见 cardreader validators.BIN_BANK)
|
||||
bin_guess = bin_bank(ocr_card) or bin_bank(table_card)
|
||||
if bank_status == "无法识别" and bin_guess and config.text(table_bank):
|
||||
if compare_bank(table_bank, bin_guess) == "一致":
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"schema_version": 1,
|
||||
"id": "xq0084",
|
||||
"name": "银行卡更新EHR系统",
|
||||
"version": "0.1.8",
|
||||
"version": "0.2.0",
|
||||
"description": "读取人资 AI 表格「银行卡变更收集表」中「是否更新HR系统」为空或「否」的记录,下载银行卡与身份证照片识别卡号、身份证号、姓名、开户银行并比对;支持「试算模式」(默认开启):只核对并输出核对表(结论 + 说明),不写 HR、不回写台账;关闭后核对通过自动导入 HR 工资卡管理并回写「是否更新HR系统=是」,不一致或识别不出进异常名单提醒人事手动处理。",
|
||||
"category": "人事薪酬",
|
||||
"icon": "",
|
||||
@@ -15,8 +15,8 @@
|
||||
},
|
||||
"requires": {
|
||||
"sdks": {
|
||||
"desktopauto": {
|
||||
"compat": "^0.5"
|
||||
"cardreader": {
|
||||
"compat": "^0.1"
|
||||
},
|
||||
"dingtalk_sdk": {
|
||||
"compat": "^0.1.3"
|
||||
|
||||
Reference in New Issue
Block a user