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/v1.6.0/ai-sdk.js"></script>
<body>
<div class="container-fluid">
<div class="row">
<canvas id="canvas" class="col-md-8"></canvas>
<br />
<canvas id="chart_age" width="400" height="250"></canvas>
<div class="col-md-4">
<strong>MorphCast JS SDK - Age module (v1.6.0 environment)</strong>
<div id="age_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("age_results", "Age: --");
}
//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_AGE.eventName, (evt) => {
const age = evt.detail.output.numericAge || '--';
print("age_results", "Age: " + age);
ts_age.append(new Date().getTime(), evt.detail.output.numericAge);
ts_rawAge.append(new Date().getTime(), evt.detail.output.rawAge);
});
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_age = new TimeSeries();
let ts_rawAge = new TimeSeries();
function createTimeline() {
let chart_a = new SmoothieChart({interpolation:'step',maxValue:100,minValue:0,grid:{sharpLines:true,verticalSections:9}});
chart_a.addTimeSeries(ts_rawAge, { lineWidth:0.9,strokeStyle:'#ff0000' });
chart_a.addTimeSeries(ts_age, { strokeStyle: 'rgba(0, 255, 0, 1)', lineWidth: 2 });
chart_a.streamTo(document.getElementById("chart_age"), 50);
}
const config = {};
log('Current config is : ' + JSON.stringify(config));
CY.loader()
...