-2

I'm using a webcam to detect changes in an environment. When a person walks into view of the webcam the Status displays "INTRUDER ALERT". I want an alarm to sound when this is displayed.

Code:

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();
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Jxck
  • 51
  • 1
  • 6
  • Possible duplicate of [Playing audio with Javascript?](https://stackoverflow.com/questions/9419263/playing-audio-with-javascript) – Thomas Dondorf Aug 27 '19 at 19:09

2 Answers2

0

You will want something like:


let alarm = new Audio();

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

The Audio API is fully supported in most modern browsers, so everything but IE :)

You can verify this works just by opening up your browser's developer console and trying this example:

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

myAlarm.play();

Hopefully this is helpful.

username-yj
  • 105
  • 1
  • 4
0

Found out that result.classIndex is what changed when the status did. SECURED is 0 and INTRUDER ALERT is 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();
  }
}
Jxck
  • 51
  • 1
  • 6