news 2026/5/1 5:51:04

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 11:46:27

远程批量执行命令:Ansible管理多台Miniconda主机

远程批量执行命令&#xff1a;Ansible管理多台Miniconda主机 在AI实验室或工程团队中&#xff0c;一个常见的场景是&#xff1a;新成员刚入职&#xff0c;急需搭建Python环境跑通模型训练脚本。传统做法是手动登录每台服务器&#xff0c;逐个安装依赖——这个过程不仅耗时数小…

作者头像 李华
网站建设 2026/4/18 9:47:04

在Windows WSL中运行Miniconda-Python3.10镜像进行AI开发

在 Windows WSL 中运行 Miniconda-Python3.10 镜像进行 AI 开发 在当今 AI 技术快速演进的背景下&#xff0c;越来越多开发者面临一个看似简单却棘手的问题&#xff1a;如何在自己的电脑上快速、干净地搭建一个能“跑通代码”的环境&#xff1f;尤其当你从 GitHub 下载了一个热…

作者头像 李华
网站建设 2026/4/23 13:12:53

Miniconda环境合并:将多个env整合为统一平台

Miniconda环境合并&#xff1a;将多个env整合为统一平台 在现代AI研发与数据科学实践中&#xff0c;一个常见的痛点是&#xff1a;随着项目增多&#xff0c;开发者的机器上逐渐堆积起十几个甚至几十个Conda环境——有的用于NLP实验&#xff0c;有的跑CV模型&#xff0c;还有的只…

作者头像 李华
网站建设 2026/4/18 12:38:40

大模型学习全攻略:零基础入门到项目实战,附免费资源下载,程序员必收藏!_大模型入门指南(非常详细)

文章系统介绍AI大模型学习路径与资源&#xff0c;涵盖自然语言处理、神经网络、注意力机制和Transformer等核心技术&#xff0c;提供思维导图、视频教程、应用报告、案例PPT、电子书及面试题等学习资料。强调大模型学习的重要性&#xff0c;指出最先掌握AI者将获得竞争优势&…

作者头像 李华
网站建设 2026/4/25 23:57:38

程序员必看!收藏这篇:知识图谱如何解决大模型的幻觉问题

文章介绍了知识图谱如何解决大模型幻觉问题。大模型虽强但易产生"一本正经地胡说八道"&#xff0c;现有三种工程解决方法。知识图谱通过"实体-关系-实体"三元组构建结构化知识网络&#xff0c;可作为大模型的"权威知识库"提供可验证事实&#xf…

作者头像 李华
网站建设 2026/5/1 4:53:44

基于SpringBoot的在线网络学习平台的设计与实现毕设源码+文档+讲解视频

前言 随着数字化教育的蓬勃发展&#xff0c;在线学习凭借灵活便捷的优势成为主流学习方式之一&#xff0c;传统线下教育模式存在时空限制、资源分配不均等问题。本课题旨在设计并实现一款基于SpringBoot框架的在线网络学习平台&#xff0c;构建一体化学习服务载体。系统采用Spr…

作者头像 李华