JSFiddle - React, Tailwind, and code Playground

by morphcast

HTML

<script src="https://ai-sdk.morphcast.com/v1.16/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 - Startup time estimation (v1.16 environment)</strong>
        <div id="time_results" style="word-wrap:break-word;font-size:40px"></div>
        <div id="emo_results" style="word-wrap:break-word;font-size:40px"></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("emo_results", "Emotion: --");
}

//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_EMOTION.eventName, (evt) => {
	const emotion = JSON.stringify(evt.detail.output.emotion) || '--';
  print("emo_results", "Emotion: " + emotion);
});


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()
	.licenseKey("9087cfd2037a28a79769fdcaa4962252379cd2e2fd0b")
  .addModule(CY.modules().FACE_EMOTION.name)
  .load().then(({ start, stop, terminate }) => {
  	detector.start = start;
    detector.stop = stop;
    document.getElementById("start").disabled = false;
  }).catch((err) => {
    console.error(err);
  });