Files
kefu/deploy/recognition-audit-20260916/fix_visual_cache.py
T
2026-09-21 10:34:06 +08:00

52 lines
2.4 KiB
Python

"""Bind upgraded OCR results to both avatar and visible name crop."""
from pathlib import Path
import shutil
import hashlib
import json
root = Path(__file__).resolve().parent
records = []
for project in (Path('C:/kefu/wechat_rpa'), Path('C:/wechat_rpa')):
path = project / 'wechat_bot.py'
original = path.read_text(encoding='utf-8')
start = original.index(' """升级识别的缓存键:头像归一指纹。')
end = original.index(' def _row_name_deep_cached(', start)
replacement = ''' """升级识别缓存必须同时匹配头像和当前昵称裁片。
默认头像会被许多客户共用,头像本身不能证明客户身份。名字裁片改变
时重新 OCR;宁可额外识别一次,也不能把前一个客户的名字用于新客户。
不把行号加入缓存键,因此列表重排后相同的裁片仍可命中。
"""
try:
panel = self._row_name_panel(img, rel_y, row_center=row_center)
if panel is None or getattr(panel, "ndim", 0) != 3 or not panel.size:
return ""
name_pixels = np.ascontiguousarray(panel[:, :, :3])
name_digest = hashlib.blake2b(
str(name_pixels.shape).encode("ascii") + name_pixels.tobytes(), digest_size=16
).hexdigest()
raw = self._raw_session_fingerprint(img, rel_y, row_center=row_center)
try:
avatar = self._canonical_fp(raw)
except Exception:
avatar = raw
return avatar.hex() + ":" + name_digest
except Exception:
return ""
'''
changed = original[:start] + replacement + original[end:]
compile(changed, str(path), 'exec')
backup = project / 'backups' / 'recognition-audit-20260916' / 'visual' / path.name
assert not backup.exists()
shutil.copy2(path, backup)
path.write_text(changed, encoding='utf-8', newline='\n')
test = project / 'test_visual_cache_audit.py'
assert not test.exists()
shutil.copy2(root / test.name, test)
records.append({'project':str(project), 'file':str(path), 'backup':str(backup),
'before_sha256':hashlib.sha256(original.encode()).hexdigest(),
'after_sha256':hashlib.sha256(changed.encode()).hexdigest()})
(root / 'visual-cache-changes.json').write_text(json.dumps(records, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(records, ensure_ascii=False))