JSFiddle - React, Tailwind, and code Playground

by morphcast

HTML

<script src="https://ai-sdk.morphcast.com/v1.7.0/ai-sdk.js"></script>
<body>
  <div class="container-fluid">
    <div class="row">
      <canvas id="canvas" class="col-md-8"></canvas>
      <div class="col-md-4">
        <strong>MorphCast JS SDK (v1.7.0) - Attention module</strong>
        <div id="attention_results" style="word-wrap:break-word;font-size:40px"></div>
        <div>with Exponential decay (when face is not detected) and audio feedback</div>
      </div>
    </div>
    <div>
      <button id="start" onclick="onStart()" disabled>Start</button>
      <button id="stop" onclick="onStop()">Stop</button>
      <audio id="audio" src="http://www.soundjay.com/button/beep-07.wav" autoplay="false" ></audio>

      <p>
        <strong>Instructions</strong>
        Press the start button to start the detector.
        <br/> Press the stop button to end the detector.
      </p>
      <div>
        <strong>LOG:</strong>
      </div>
      <div id="logs"></div>
    </div>
  </div>
</body>

JavaScript

function log(msg) {
	var element = document.getElementById("logs");
  element.innerHTML += "<span>" + msg + "</span><br />";
}

const sound = document.getElementById("audio");

let timeout;
let attention = 0;

function print(node, msg) {
  if (attention < 40 && attention > 0.1) {
    sound.play();
    document.body.style.backgroundColor = "red";
  }
	clearTimeout(timeout);
  timeout = setTimeout(()=>{
  	if(attention > 0.1) {
    	attention /= 3;
			print("attention_results", "Attention: " + attention.toFixed(0));
    } else {
			print("attention_results", "Attention: --");
    }
  }, 1000);
  document.getElementById(node).innerHTML = msg;
}

//function executes when Start button is pushed.
function onStart() {
  if (detector && !detector.isRunning) {
    detector.isRunning = true;
    detector.start();
  }
  log("Clicked the start button");
}

//function executes when the Stop button is pushed.
function onStop() {
  log("Clicked the stop button");
  if (detector && detector.isRunning) {
    detector.stop();
    detector.isRunning = false;
  }
}

window.addEventListener(CY.modules().FACE_ATTENTION.eventName, (evt) => {
	attention = evt.detail.output.attention * 100;
  document.body.style.backgroundColor = "white";
  print("attention_results", "Attention: " + (attention).toFixed(0));
});


const ctx = document.getElementById('canvas').getContext('2d');
let dimLogged = false;
window.addEventListener(CY.modules().CAMERA.eventName, (evt) => {

  const imageData = evt.detail;
  if (!dimLogged) {
    log(`Camera dimensions: w: ${imageData.width}, h: ${imageData.height}`);
    dimLogged = true;
  }
  ctx.canvas.width = imageData.width;
  ctx.canvas.height = imageData.height;
  ctx.putImageData(imageData, 0, 0);
  
});

var detector = {};
detector.isRunning = false;

CY.loader()
  .addModule(CY.modules().FACE_ATTENTION.name, {})
  .load().then(({ start, stop, terminate }) => {
  	detector.start = start;
    detector.stop = stop;
   ...