Files
Ubuntu edaa057d68 feat(1.0.72): 本地模型自动更新 + 入库照片送审(数据飞轮)
基于 1.0.71 合入:model_sync/local_cls/assist 本地模型自动更新(版本比对/下载/sha+加载校验/替换失败回滚);review_queue 入库照先落队列、成功才送审、回执 image_sha256 核对;build_exe/installer 有 models/cls 就打包;版本 1.0.72;补工具脚本、文档与 config.sanitized.json。
2026-12-02 15:58:27 +08:00

99 lines
3.6 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 -*-
"""手工同步本地模型(现场验收/排障用;正常情况下秤端登录后自己会做)。
用法(在 windows-fenjian 目录下):
python tools\\sync_cls_model.py # 查一次,服务器有新版就下载替换
python tools\\sync_cls_model.py --status # 只看本地版本,不联网
python tools\\sync_cls_model.py --config D:\\入库分拣工作台\\config.json
它和秤端开机做的是同一件事(`core/recognition/model_sync.py`),
区别只是把结果打印到控制台,方便现场一眼看清「现在跑的是哪一版、服务器是哪一版」。
"""
import argparse
import io
import json
import os
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, HERE)
from core.api.client import ApiClient, ApiError # noqa: E402
from core.recognition.model_sync import ModelSync # noqa: E402
def load_config(path):
for cand in (path, os.path.join(HERE, 'config.json'),
os.path.join(HERE, 'config.sanitized.json')):
if cand and os.path.isfile(cand):
with open(cand, 'r', encoding='utf-8') as f:
return cand, json.load(f)
raise SystemExit('[x] 找不到 config.json(用 --config 指定)')
def print_local(ms):
m = ms.local_manifest()
if not m:
print('本地模型:未安装(%s 下没有 model.json)' % ms.dir)
return
print('本地模型:%s(%s 类,装于 %s)'
% (m.get('version') or '?', m.get('class_count') or '?',
m.get('installed_at') or m.get('created_at') or '?'))
def main(argv=None):
ap = argparse.ArgumentParser(description='手工同步秤端本地识别模型')
ap.add_argument('--config', default='', help='config.json 路径')
ap.add_argument('--status', action='store_true', help='只看本地版本,不联网')
a = ap.parse_args(argv)
path, cfg = load_config(a.config)
print('配置:%s' % path)
ms = ModelSync(cfg)
print_local(ms)
if a.status:
return 0
api = ApiClient(cfg)
if getattr(api, 'mock', False):
print('[x] 这份配置是 mock 模式(server.mock=true),不会联网')
return 2
try:
api.login()
except ApiError as e:
print('[x] 登录失败(%s):%s' % (e.code, e.message))
return 2
try:
remote = api.cls_model_manifest()
except ApiError as e:
print('[x] 取服务器版本失败(%s):%s' % (e.code, e.message))
return 2
if not remote:
print('服务器:还没有发布模型(404)—— 本地保持现状')
return 0
print('服务器:%s(%s 类,发布于 %s%s)'
% (remote.get('version') or '?', remote.get('class_count') or '?',
remote.get('created_at') or '?',
',' + remote['notes'] if remote.get('notes') else ''))
res = ms.run(api, force=True)
status = res.get('status')
if status == 'updated':
print('[ok] 已更新到 %s —— 重启秤端后生效(正在运行的界面仍用旧模型)'
% res.get('version'))
return 0
if status == 'current':
print('[ok] 已是最新,无需下载')
return 0
if status == 'skipped':
print('[!] 跳过:%s' % res.get('reason'))
return 0
print('[x] 更新失败:%s(本地仍是原版本)' % res.get('reason'))
return 1
if __name__ == '__main__':
sys.exit(main())