Files
ai-passport/tools/build_wake_model.py
T
hex2077 6a7f4e010b chore: 新增大量静态资源、测试桩文件与配置文件,清理旧无用资源
本次提交新增了多种字体、UI图片、音频剪辑等静态资源,添加了测试桩文件与项目配置文件,同时清理了部分已废弃的旧图片与测试桩代码文件。
2026-09-22 00:42:26 +08:00

36 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Package ONE compatible, already trained WakeNet9s model; never train text.
Use --source DIRECTORY to package a custom model supplied for ESP-SR 2.1.3/C3.
The displayed phrase comes from the model metadata, not a user-editable label.
"""
import argparse,hashlib,json,re,struct
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
def pack(source,destination):
name=source.name
if not re.fullmatch(r'wn9s_[a-z0-9_]{1,26}',name):raise ValueError('Require a WakeNet9s model directory name')
info=(source/'_MODEL_INFO_').read_text(encoding='utf-8').strip()
parts=info.split('_')
if len(parts)!=6 or parts[0]!='wakenet9s':raise ValueError('Require a single-phrase WakeNet9s metadata format')
phrase=parts[2]
if not phrase or len(phrase)>16 or any(ord(c)<32 for c in phrase):raise ValueError('Invalid wake phrase')
files=['_MODEL_INFO_','wn9_data','wn9_index'];offset=40+40*len(files)
header=bytearray(struct.pack('<I32sI',1,name.encode(),len(files)));payload=bytearray()
for filename in files:
raw=(source/filename).read_bytes()
if not raw or len(raw)>256*1024:raise ValueError('Model file size out of range')
header.extend(struct.pack('<32sII',filename.encode(),offset,len(raw)));payload.extend(raw);offset+=len(raw)
destination.mkdir(parents=True,exist_ok=True)
(destination/'wake_model.bin').write_bytes(header+payload)
# Bench-calibrated only for the bundled coefficients. Custom models retain
# their own normal-mode threshold until separately validated on the device.
known=hashlib.sha256((source/'wn9_data').read_bytes()).hexdigest()=='f93799743dad310e2933446c302088d32605d3275206f53d3f3c184220caecf6'
threshold='0.67f' if known else '0.0f'
(destination/'wake_model_config.h').write_text('#pragma once\n#define BADGE_WAKE_PHRASE '+json.dumps(phrase,ensure_ascii=False)+'\n#define BADGE_WAKE_THRESHOLD '+threshold+'\n',encoding='utf-8')
return len(header+payload)
if __name__=='__main__':
p=argparse.ArgumentParser(description=__doc__);p.add_argument('destination',type=Path);p.add_argument('--source',type=Path,default=ROOT/'assets/xiaozhi/wake/wn9s_nihaoxiaozhi')
a=p.parse_args();print('Packaged WakeNet9s model bytes:',pack(a.source,a.destination))