Files
雨天 a3166c725e feat: 登录页可改服务器地址,支持无硬件回放测试
- 登录页页脚新增「修改服务器地址」,保存后立即生效并写回
  config.json(server.base_url / history),不用重启程序
- camera.replay_dir:把真实商品图片回放当摄像头画面,识别仍走
  真实模型,没摄像头也能验证识别准确率
- scale.sim_loop_kg 支持列表,模拟秤每轮换一个重量,便于对上待办
- 识别请求不再继承系统代理(与 ApiClient 一致),修复现场
  ProxyError 导致 AI 一直认不出的问题
- 模拟/回放画面不再写入 data/learn;新增 tools/learn_clean_sim.py
  清理已被污染(空白图当教材)的样本库
- 重量容差、严格写入开关这类服务端拒绝改成能照做的中文提示
- 版本号统一到 1.0.66,使用说明补对应章节
2026-11-10 20:55:54 +08:00

121 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""清掉样本库里的「模拟画面」脏样本(不是车间实拍的占位图)
背景:v1.0.65 之前,模拟摄像头(camera.simulate=true)拍的画面也会在
「人点头记账」那一刻存进 data/learn/<品名>/,等于拿一张写着
「SIM CAMERA seq=20」的白底图当教材。现场实测 38 张里 37 张都是这种,
识别被 few-shot 带偏,对真实商品图也一直回 NOT_IN_LIST。
做法:把近白色的图(白占比 > 0.85)挪到 data/_diag/learn_sim_quarantine/,
并在 index.json 里删掉对应条目(含指向已删文件的残留条目)。
**只挪不删**,看错了随时能挪回去。
用法(项目根目录):
python tools\\learn_clean_sim.py 只看,不移动
python tools\\learn_clean_sim.py --apply 真的挪走
"""
import os
import sys
import io
import shutil
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import cv2
import numpy as np
from core.recognition.dataset import SampleStore
ROOT = os.path.join('data', 'learn')
INDEX = os.path.join(ROOT, 'index.json')
QUAR = os.path.join('data', '_diag', 'learn_sim_quarantine')
WHITE_RATIO = 0.85 # 近白像素占比超过这个值 = 模拟画面
EXTS = ('.jpg', '.jpeg', '.png', '.bmp', '.webp')
def is_sim_frame(path):
try:
im = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_COLOR)
except Exception:
return False
if im is None:
return False
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
return float((gray > 235).mean()) > WHITE_RATIO
def scan_disk():
"""data/learn 下所有图片(含不在索引里的孤儿文件)"""
out = []
for base, _dirs, files in os.walk(ROOT):
for fn in sorted(files):
if fn.lower().endswith(EXTS):
out.append(os.path.join(base, fn))
return out
def move_out(path):
"""挪进隔离目录(按品名分子目录),返回是否成功"""
rel = os.path.relpath(path, ROOT)
dst = os.path.join(QUAR, rel)
os.makedirs(os.path.dirname(dst), exist_ok=True)
try:
if os.path.abspath(path) == os.path.abspath(dst):
return False
shutil.move(path, dst)
return True
except Exception as e: # noqa: BLE001
print(' 挪不动 %s:%s' % (path, e))
return False
def main():
apply = '--apply' in sys.argv
store = SampleStore(root=ROOT, path=INDEX)
# 1) 索引里指向已不存在文件的残留条目
stale = []
for name, rows in list(store.items.items()):
for r in list(rows or []):
rel = str((r or {}).get('file') or '')
if rel and not os.path.isfile(store._abs(rel)):
stale.append((name, r))
# 2) 磁盘上的模拟画面(不管在不在索引里)
sims = [p for p in scan_disk() if is_sim_frame(p)]
if not sims and not stale:
print('样本库很干净:没有模拟画面样本,索引也没有残留条目。')
print('当前样本:%s' % (store.stats() or '空'))
return 0
if stale:
print('索引残留条目 %d 条(文件已不在):' % len(stale))
for name, r in stale:
print(' %-16s %s' % (name, r.get('file')))
if sims:
print('模拟画面样本 %d 张:' % len(sims))
for p in sims:
print(' %s' % os.path.relpath(p, ROOT))
if not apply:
print('\n(只看了,没动。加 --apply 才会真的挪走 / 清索引)')
return 0
moved = 0
for p in sims:
if move_out(p):
moved += 1
for name, r in stale:
store._remove_entry(name, r)
if sims or stale:
store._save()
print('\n已挪走 %d 张 → %s' % (moved, QUAR))
print('剩余样本:%s' % (store.stats() or '空'))
return 0
if __name__ == '__main__':
raise SystemExit(main())