您好,欢迎访问一九零五行业门户网

详解JS应用程序中如何执行语音识别

本篇文章给大家介绍一下在javascript应用程序中执行语音识别的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
语音识别是计算机科学和计算语言学的一个跨学科子领域。它可以识别口语并将其翻译成文本,它也被称为自动语音识别(asr),计算机语音识别或语音转文本(stt)。
机器学习(ml)是人工智能(ai)的一种应用,它使系统能够自动学习并从经验中进行改进,而无需进行明确的编程。机器学习在本世纪提供了大多数语音识别方面的突破。如今,语音识别技术无处不在,例如apple siri,amazon echo和google nest。
语音识别以及语音响应(也称为语音合成或文本到语音(tts))由web speech api提供支持。
在本文中,我们重点介绍javascript应用程序中的语音识别。另一篇文章介绍了语音合成。
语音识别接口speechrecognition 是识别服务的控制器接口,在chrome中称为 webkitspeechrecognition。speechrecognition 处理从识别服务发送的 speechrecognitionevent。speechrecognitionevent.results 返回一个speechrecognitionresultlist 对象,该对象表示当前会话的所有语音识别结果。
可以使用以下几行代码来初始化 speechrecognition:
// 创建一个speechrecognition对象const recognition = new webkitspeechrecognition();// 配置设置以使每次识别都返回连续结果recognition.continuous = true;// 配置应返回临时结果的设置recognition.interimresults = true;// 正确识别单词或短语时的事件处理程序recognition.onresult = function (event) { console.log(event.results);};
ognition.start() 开始语音识别,而 ognition.stop() 停止语音识别,它也可以中止( recognition.abort)。
当页面正在访问您的麦克风时,地址栏中将显示一个麦克风图标,以显示该麦克风已打开并且正在运行。
我们用句子对页面说。“hello comma i'm talking period.” onresult 在我们说话时显示所有临时结果。
这是此示例的html代码:
<!doctype html><html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>speech recognition</title> <script> window.onload = () => { const button = document.getelementbyid('button'); button.addeventlistener('click', () => { if (button.style['animation-name'] === 'flash') { recognition.stop(); button.style['animation-name'] = 'none'; button.innertext = 'press to start'; content.innertext = ''; } else { button.style['animation-name'] = 'flash'; button.innertext = 'press to stop'; recognition.start(); } }); const content = document.getelementbyid('content'); const recognition = new webkitspeechrecognition(); recognition.continuous = true; recognition.interimresults = true; recognition.onresult = function (event) { let result = ''; for (let i = event.resultindex; i < event.results.length; i++) { result += event.results[i][0].transcript; } content.innertext = result; }; }; </script> <style> button { background: yellow; animation-name: none; animation-duration: 3s; animation-iteration-count: infinite; } @keyframes flash { 0% { background: red; } 50% { background: green; } } </style> </head> <body> <button id="button">press to start</button> <div id="content"></div> </body></html>
第25行创建了 speechrecognition 对象,第26和27行配置了 speechrecognition 对象。
当一个单词或短语被正确识别时,第28-34行设置一个事件处理程序。
第19行开始语音识别,第12行停止语音识别。
在第12行,单击该按钮后,它可能仍会打印出一些消息。这是因为 recognition.stop() 尝试返回到目前为止捕获的speechrecognitionresult。如果您希望它完全停止,请改用 ognition.abort()。
您会看到动画按钮的代码(第38-51行)比语音识别代码长。这是该示例的视频剪辑:https://youtu.be/5v3bb5yonj0
以下是浏览器兼容性表:
网络语音识别依赖于浏览器自己的语音识别引擎。在chrome中,此引擎在云中执行识别。因此,它仅可在线运行。
语音识别库有一些开源语音识别库,以下是基于npm趋势的这些库的列表:
1. annyangannyang是一个javascript语音识别库,用于通过语音命令控制网站。它建立在speechrecognition web api之上。在下一节中,我们将举例说明annyang的工作原理。
2. artyom.jsartyom.js是一个javascript语音识别和语音合成库。它建立在web语音api的基础上,除语音命令外,它还提供语音响应。
3. mumblemumble是一个javascript语音识别库,用于通过语音命令控制网站。它建立在speechrecognition web api之上,这类似于annyang的工作方式。
4. julius.jsjulius是面向语音相关研究人员和开发人员的高性能,占用空间小的大词汇量连续语音识别(lvcsr)解码器软件。它可以在从微型计算机到云服务器的各种计算机和设备上执行实时解码。julis是使用c语言构建的,而julius.js是julius自以为是javascript的移植版。
5.voice-commands.jsvoice-commands.js是一个javascript语音识别库,用于通过语音命令控制网站。它建立在speechrecognition web api之上,这类似于annyang的工作方式。
annyangannyang初始化一个 speechrecognition 对象,该对象定义如下:
var speechrecognition = root.speechrecognition || root.webkitspeechrecognition || root.mozspeechrecognition || root.msspeechrecognition || root.ospeechrecognition;
有一些api可以启动或停止annyang:
annyang.start:使用选项(自动重启,连续或暂停)开始监听,例如 annyang.start({autorestart:true,continuous:false})。annyang.abort:停止收听(停止speechrecognition引擎或关闭麦克风)。annyang.pause:停止收听(无需停止speechrecognition引擎或关闭麦克风)。annyang.resume:开始收听时不带任何选项。这是此示例的html代码:
<!doctype html><html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>annyang</title> <script src="//cdnjs.cloudflare.com/ajax/libs/annyang/2.6.1/annyang.min.js"></script> <script> window.onload = () => { const button = document.getelementbyid('button'); button.addeventlistener('click', () => { if (button.style['animation-name'] === 'flash') { annyang.pause(); button.style['animation-name'] = 'none'; button.innertext = 'press to start'; content.innertext = ''; } else { button.style['animation-name'] = 'flash'; button.innertext = 'press to stop'; annyang.start(); } }); const content = document.getelementbyid('content'); const commands = { hello: () => { content.innertext = 'you said hello.'; }, 'hi *splats': (name) => { content.innertext = `you greeted to ${name}.`; }, 'today is :day': (day) => { content.innertext = `you said ${day}.`; }, '(red) (green) (blue)': () => { content.innertext = 'you said a primary color name.'; }, }; annyang.addcommands(commands); }; </script> <style> button { background: yellow; animation-name: none; animation-duration: 3s; animation-iteration-count: infinite; } @keyframes flash { 0% { background: red; } 50% { background: green; } } </style> </head> <body> <button id="button">press to start</button> <div id="content"></div> </body></html>
第7行添加了annyang源代码。
第20行启动annyang,第13行暂停annyang。
annyang提供语音命令来控制网页(第26-42行)。
第27行是一个简单的命令。如果用户打招呼,页面将回复“您说‘你好’。”
第30行是带有 splats 的命令,该命令会贪婪地捕获命令末尾的多词文本。如果您说“hi,爱丽丝e”,它的回答是“您向爱丽丝致意。”如果您说“嗨,爱丽丝和约翰”,它的回答是“您向爱丽丝和约翰打招呼。”
第33行是一个带有命名变量的命令。一周的日期被捕获为 day,在响应中被呼出。
第36行是带有可选单词的命令。如果您说“黄色”,则将其忽略。如果您提到任何一种原色,则会以“您说的是原色名称”作为响应。
从第26行到第39行定义的所有命令都在第41行添加到annyang中。
... ...
结束我们已经了解了javascript应用程序中的语音识别,chrome对web语音api提供了最好的支持。我们所有的示例都是在chrome浏览器上实现和测试的。
在探索web语音api时,这里有一些提示:如果您不想在日常生活中倾听,请记住关闭语音识别应用程序。
更多编程相关知识,请访问:编程视频!!
以上就是详解js应用程序中如何执行语音识别的详细内容。
其它类似信息

推荐信息