示例

import $ from 'jquery';

const tf = require('@tensorflow/tfjs');

const speechCommands = require('@tensorflow-models/speech-commands');

const MODEL_PATH='http://127.0.0.1:8080/speech';

$(async ()=>{

//创建语音识别器

const recognizer=speechCommands.create(

'BROWSER_FFT',

null,

MODEL_PATH+'/model.json',

MODEL_PATH+'/metadata.json'

);

//加载模型

await recognizer.ensureModelLoaded();

//查看可识别单词

const labels=recognizer.wordLabels()

console.log(labels);

//创建标签

const element=document.querySelector('#result');

element.innerHTML=labels.map(item=>`

${item}
`).join('');

//设置语音监听

recognizer.listen(res=>{

//找出识别最接近的值

const {scores}=res;

const maxValue=Math.max(...scores);

const index=scores.indexOf(maxValue);

console.log(labels[index]);

element.innerHTML=labels.map((l,i)=>{

return `

${l}
`

}).join('');

},{

//识别频率

overlapFactor:0.3,

//有75%以上接近,打印结果

probabilityThreshold:0.75

});

});

html部分

Document

语音识别

执行结果

 

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。