示例

const $ = require('jquery');
const tf = require('@tensorflow/tfjs');
const tfvis = require('@tensorflow/tfjs-vis');
const speechCommands = require('@tensorflow-models/speech-commands');
const MODEL_PATH = 'http://127.0.0.1:8080/speech';

let transferRecognizer;

$(async () => {
  //创建语音识别器
  const recognizer = speechCommands.create(
    'BROWSER_FFT',
    null,
    MODEL_PATH + '/model.json',
    MODEL_PATH + '/metadata.json'
  );

  //加载模型
  await recognizer.ensureModelLoaded();

  //创建迁移学习器
  transferRecognizer = recognizer.createTransfer('轮播图');
});

window.collect = async (btn) => {
  btn.disabled = true;
  const label = btn.innerText;
  console.log(label);

  //收集声音
  await transferRecognizer.collectExample(
    label === '背景噪音' ? '_background_noise_' : label
  );
  btn.disabled = false;
  $('#count').html(JSON.stringify(transferRecognizer.countExamples()));
  console.log(transferRecognizer.countExamples());
}

//训练模型
window.train = async () => {
  await transferRecognizer.train({
    epochs: 30,
    callback: tfvis.show.fitCallbacks(
      { name: "训练过程" },
      ['loss', 'acc'],
      { callbacks: ['onEpochEnd'] }
    )
  });
}

//设置监听开关,用于测试训练结果
window.toggle = async (checked) => {
  console.log(checked);
  if (checked) {
    await transferRecognizer.listen(result => {
      const { scores } = result;
      const labels = transferRecognizer.wordLabels();
      const index = scores.indexOf(Math.max(...scores));
      console.log(labels[index]);
    }, {
      //识别频率,越大越频繁
      overlapFactor: 0,
      probabilityThreshold: 0.75
    });
  } else {
    transferRecognizer.stopListening();
  }
}

//保存模型
window.save = () => {
  const arrayBuffer = transferRecognizer.serializeExamples();
  const blob = new Blob([arrayBuffer]);
  const link = document.createElement('a');
  link.href = window.URL.createObjectURL(blob);
  link.download = 'data.bin';
  link.click();
}

html部分

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="collect(this)">上一张</button>
  <button onclick="collect(this)">下一张</button>
  <button onclick="collect(this)">背景噪音</button>
  <button onclick="save()">保存</button>
  <pre id="count"></pre>
  <button onclick="train()">训练</button>
  <br><br>
  监听开关:<input type="checkbox" onchange="toggle(this.checked)">
</body>
<script src="./t9.js"></script>
</html>

执行结果

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐