当前位置: 首页 > 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.rkmt.cn/news/182927.html

相关文章:

  • 软银54亿美元收购ABB机器人部门 押注“物理AI”
  • PyTorch自动微分机制详解:在Miniconda中实践
  • 2025继续教育降AI率工具TOP10测评榜单
  • Markdown emoji表情增强技术文档可读性
  • 【遗传算法(GA)和模拟退火(SA)对翼型升阻比进行优化】基于神经网络和无导数算法的翼型优化附Matlab代码
  • 遭了!看到这5款优质汽车模型,再也不纠结汽车模型哪个好了!
  • 读书笔记4-11.6
  • PyTorch模型保存与加载:在Miniconda中进行持久化操作
  • Miniconda vs Anaconda:为什么选择轻量级Python镜像更高效
  • AI赋能软件测试:如何在质量保障领域真正降低人力成本
  • [线上问题排查参考 | 01]
  • 制剂处方数据库【制剂组分研究】
  • numa balancing缺陷分析
  • yolov5识别demo
  • vector模拟实现与核心机制
  • Python安装后无法导入模块?Miniconda-Python3.10修复sys.path
  • 免费开源!Stable Diffusion AI 生图天花板,下载安装一款能免费部署的 AI 绘图神器
  • HTML前端展示:将训练结果嵌入网页中的Jupyter输出
  • 2025年东莞线对板连接器厂家口碑洞察报告:从技术到服务的专业选型指南 - 品致汇
  • 使用SSH连接远程Miniconda容器进行长时间模型推理任务
  • 《程序员修炼之道:从小工到专家》观后感第八篇
  • Mac M1芯片适配:Miniconda安装PyTorch ARM64版本指南
  • HTML音频播放功能:Miniconda-Python3.10处理语音大模型输出
  • 数据人狂喜!这款Oracle导出工具,效率直接拉满!
  • 零门槛大模型知识库搭建指南:5分钟用Dify实现RAG应用,小白必学,建议收藏!
  • 清华镜像HTTPS配置:Miniconda-Python3.10避免证书验证错误
  • 经营帮集中采购:中小企业采购降本的实用选择
  • 项目管理中最大的浪费是什么?
  • 《程序员修炼之道:从小工到专家》观后感第七篇
  • CUDA安装后无法识别?教你正确配置Miniconda中的PyTorch环境