【问题标题】:How do I add an alarm sound when the status result is changed状态结果更改时如何添加警报声音
【发布时间】:2019-08-27 18:24:48
【问题描述】:

我正在使用网络摄像头来检测环境中的变化。当有人走进网络摄像头的视野时,状态会显示“入侵者警报”。我希望在显示时发出警报。

代码:

while (true) {

if (classifier.getNumClasses() > 0) {

  // Get the activation from mobilenet from the webcam.

  const activation = net.infer(webcamElement, 'conv_preds');

  // Get the most likely class and confidences from the classifier module.

  const result = await classifier.predictClass(activation);
  const classes = ['SECURED', 'INTRUDER ALERT'];
  document.getElementById('console').innerText = `
    Status: ${classes[result.classIndex]}\n
    Accuracy: ${result.confidences[result.classIndex]}
  `;
}

await tf.nextFrame();

【问题讨论】:

标签: javascript tensorflow.js


【解决方案1】:

你会想要这样的:


let alarm = new Audio();

alarm.src = './myAlarm.mp3'; // path to your alarm audio file
alarm.play();

大多数现代浏览器都完全支持Audio API,所以除了 IE 之外的所有浏览器 :)

您可以通过打开浏览器的开发者控制台并尝试以下示例来验证它是否有效:

var myAlarm = new Audio("https://freesound.org/data/previews/470/470504_2694940-lq.mp3");

myAlarm.play();

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    发现 result.classIndex 是状态发生变化时发生的变化。 安全为 0 并且 入侵者警报为 1

     while (true) {
    if (classifier.getNumClasses() > 0) {
      // Get the activation from mobilenet from the webcam.
      const activation = net.infer(webcamElement, 'conv_preds');
      // Get the most likely class and confidences from the classifier module.
      const result = await classifier.predictClass(activation);
    
      const classes = ['SECURED', 'INTRUDER ALERT'];
      document.getElementById('console').innerText = `
        Status: ${classes[result.classIndex]}\n
        Accuracy: ${result.confidences[result.classIndex]}
      `;
      console.log(result);
    
      if (result.classIndex === 1) {
        let alarm = new Audio();
        alarm.src = 'alarm.mp3';
        alarm.play();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2014-07-07
      相关资源
      最近更新 更多