JSFiddle - React, Tailwind, and code Playground

by morphcast

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/smoothie/1.34.0/smoothie.min.js"></script>
<script src="https://ai-sdk.morphcast.com/dev/ai-sdk.js"></script>
<body>
  <div class="container-fluid">
    <div class="row">
      <canvas id="canvas" class="col-md-8"></canvas>
      <br />
      Stefano attention
      <br />
      <canvas id="chart_att" width="400" height="250"></canvas>
      <br />
      Yaw pose raw (in red):
      <br />
      <canvas id="chart_yaw" width="400" height="250"></canvas>
      <br />
      Raw arousal (in white):
      <br />
      <canvas id="chart_arousal" width="400" height="250"></canvas>
      <div class="col-md-4">
        <strong>MorphCast JS SDK - Attention module (dev environment) with fullFrameDetection at most every 10 frames</strong>
        <div id="results" style="word-wrap:break-word;font-size:40px"></div>
        <div id="detector"></div>
      </div>
    </div>
    <div>
      <button id="start" onclick="onStart()" disabled>Start</button>
      <button id="stop" onclick="onStop()">Stop</button>
      <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 />";
}

let timeout;

function print(node, msg) {
  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");
  print("results", "Attention: --");
}

//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) => {
  const att = evt.detail.output.attention || '--';
  print("results", "Attention: " + att.toFixed(2));

  /*ts_att.append(new Date().getTime(), evt.detail.output.attention);
  ts_att_raw.append(new Date().getTime(), evt.detail.output.rawAttention);*/


  let pose_attention = 1 - Math.abs(raw_yaw);
  let arousal_attention = raw_arousal;

  let attention = pose_attention * arousal_attention;

  attention = Math.min(1, Math.max(0, attention));

  ts_att_raw.append(new Date().getTime(), attention);

});




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);

});


window.addEventListener(CY.modules().FACE_DETECTOR.eventName, (evt) => {
  console.log(CY.modules().FACE_DETECTOR.eventName, evt.detail);
  document.getElementById('detector').innerHTML = 'Tracker status: ' + evt.detail.status;
});

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

let ts_att = new...