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 />
<canvas id="chart_att" width="400" height="250"></canvas>
<div class="col-md-4">
<strong>MorphCast JS SDK - Attention module (dev environment)</strong>
<div id="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("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);
});
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;
let ts_att = new TimeSeries();
function createTimeline() {
let chart_a = new SmoothieChart({interpolation:'step',maxValue:1,minValue:0,grid:{sharpLines:true,verticalSections:9}});
chart_a.addTimeSeries(ts_att, { strokeStyle: 'rgba(0, 255, 0, 1)', lineWidth: 2 });
chart_a.streamTo(document.getElementById("chart_att"), 50);
}
const config = { };
log('Current config is : ' + JSON.stringify(config));
CY.loader()
.addModule(CY.modules().FACE_ATTENTION.name, config)
.load().then(({ start, stop, terminate }) => {
createTimeline();
detector.start = start;
detector.stop =...