当前位置: 首页 > news >正文

Unity使用sherpa-onnx实现关键词检测

使用模型 sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01

效果图

具体代码

using uMicrophoneWebGL; using UnityEngine; [RequireComponent(typeof(MicrophoneWebGL))] public class KeywordSpottingSample : MonoBehaviour { MicrophoneWebGL microphone; public KeywordSpotting keywordSpotting; // Start is called before the first frame update void Start() { keywordSpotting.Init(); microphone = GetComponent<MicrophoneWebGL>(); microphone.dataEvent.AddListener(OnAudioData); } public void OnAudioData(float[] data) { if (keywordSpotting != null) { keywordSpotting.AcceptData(data); } } float timer = 0f; float interval = 0.2f; string keyword; private void Update() { if (keywordSpotting != null && keywordSpotting.initDone) { timer += Time.deltaTime; if (timer >= interval) { keyword = keywordSpotting.Recognize(); if (!string.IsNullOrEmpty(keyword)) { Debug.Log("keyword:" + keyword); } timer = 0f; } } } }
using System.IO; using SherpaOnnx; using UnityEngine; /// <summary> /// 关键字识别 /// </summary> public class KeywordSpotting : MonoBehaviour { KeywordSpotter keywordSpotter; string pathRoot; string modelPath; OnlineStream onlineStream; int sampleRate = 16000; public bool initDone = false; public void Init() { pathRoot = Util.GetPath() + "/models"; //需要将此文件夹拷贝到models modelPath = pathRoot + "/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01"; KeywordSpotterConfig config = new KeywordSpotterConfig(); config.FeatConfig.SampleRate = 16000; config.FeatConfig.FeatureDim = 80; config.ModelConfig.Transducer.Encoder = Path.Combine(modelPath, "encoder-epoch-12-avg-2-chunk-16-left-64.onnx"); config.ModelConfig.Transducer.Decoder = Path.Combine(modelPath, "decoder-epoch-12-avg-2-chunk-16-left-64.onnx"); config.ModelConfig.Transducer.Joiner = Path.Combine(modelPath, "joiner-epoch-12-avg-2-chunk-16-left-64.onnx"); config.ModelConfig.Tokens = Path.Combine(modelPath, "tokens.txt"); config.ModelConfig.Provider = "cpu"; config.ModelConfig.NumThreads = 1; config.ModelConfig.Debug = 0; config.KeywordsFile = Path.Combine(modelPath, "keywords.txt"); keywordSpotter = new KeywordSpotter(config); onlineStream = keywordSpotter.CreateStream(); initDone = true; } public void AcceptData(float[] data) { onlineStream.AcceptWaveform(sampleRate, data); } KeywordResult result; public string Recognize() { while (keywordSpotter.IsReady(onlineStream)) { keywordSpotter.Decode(onlineStream); result = keywordSpotter.GetResult(onlineStream); if (result.Keyword != string.Empty) { Debug.Log("关键字: " + result.Keyword); // Remember to call Reset() right after detecting a keyword keywordSpotter.Reset(onlineStream); return result.Keyword; } } return string.Empty; } }

最后是工程地址

https://github.com/xue-fei/sherpa-onnx-unity

http://www.jsqmd.com/news/167300/

相关文章:

  • Docker build cache提高Miniconda镜像构建效率
  • 基于SpringBoot的知识产权代管理系统设计与实现毕设源码+文档+讲解视频
  • 软银54亿美元收购ABB机器人部门 押注“物理AI”
  • 2025继续教育降AI率工具TOP10测评榜单
  • Markdown语法进阶:制作美观的技术文档记录环境搭建过程
  • PyTorch自动微分机制详解:在Miniconda中实践
  • Markdown emoji表情增强技术文档可读性
  • Python虚拟环境最佳实践:Miniconda取代传统venv方案
  • AI应用架构师重塑智能家居生态系统AI应用格局
  • AUTO TECH China 2026 广州国际汽车底盘系统技术展览会
  • 【遗传算法(GA)和模拟退火(SA)对翼型升阻比进行优化】基于神经网络和无导数算法的翼型优化附Matlab代码
  • 学长亲荐10个AI论文软件,继续教育论文写作必备!
  • 【遗传算法(GA)和模拟退火(SA)对翼型升阻比进行优化】基于神经网络和无导数算法的翼型优化附Matlab代码
  • 拆分的第一性原理——按业务域、一致性与团队边界来切,避免“为拆而拆”
  • 遭了!看到这5款优质汽车模型,再也不纠结汽车模型哪个好了!
  • 光谷聚势:OVC 2026为何成为中西部电子产业“引力场”
  • 读书笔记5-11.13
  • 【语音处理】基于自适应差分脉冲编码调制(ADPCM)的实现研究附Matlab代码
  • 用mRMR算法给特征排个座次,手把手教你数据瘦身
  • 程序员必看!收藏这篇:知识图谱如何解决大模型的幻觉问题
  • AI大模型时代程序员生存指南:从职业转型到高薪岗位的完整路径_大龄程序员想转行大模型,应该往哪个方向转?
  • 读书笔记4-11.6
  • Markdown表格对比:Miniconda与Anaconda功能差异一览
  • PyTorch模型保存与加载:在Miniconda中进行持久化操作
  • 四轴桥板卧加编程:AB轴坐标转换宏程序与VT送出
  • 2025.10.9
  • Miniconda环境合并:将多个env整合为统一平台
  • 远程服务器资源监控:查看Miniconda容器GPU占用情况
  • Java学习。
  • Miniconda vs Anaconda:为什么选择轻量级Python镜像更高效