Goal:
- Return best match prediction to client-side
- Render result to HTML page
const mostLikelyPrediction = results.predictions.sort((a, b) => {
return (a.probability > b.probability) ? -1 :
(a.probability === b.probability ? 0 : 1)
;
})[0].tagName;
response.setHeader('Content-Type', 'text/json');
response.end(`{ "prediction": "${mostLikelyPrediction}" }`);
<div class="appPanel">
<div class="appResults" id="appResults">
<div class="answer"><span>User: </span><span class="appUserAnswer value">-</span></div>
</div>
<div class="appRestart"><button id="appRestartButton" class="appRestartButton hide">NEW GAME</button></div>
</div>
/** Results panel **/
.appContainer .appPanel {
position: relative;
height: 20%;
width: 100%;
font-size: 1.3em;
box-sizing: border-box;
background: #3c3c3c;
color: #cccccc;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
}
.appPanel .appResults {
flex: 1 0 0;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
}
.appResults .answer {
flex: 0;
padding: 0.5em;
text-transform: uppercase;
}
.appResults .answer .value {
font-weight: bold;
}
.appPanel .appRestart {
flex: 1 0 0;
align-self: center;
}
.appPanel .appRestartButton {
padding: 0.25em 0.5em;
font-size: 1em;
border-radius: 1em;
}
.hide {
display: none;
}
change height: 80%;
// Global variables
let webcamStream;
const appResults = document.getElementById('appResults');
const videoElement = document.querySelector('video');
const appRestartButton = document.getElementById('appRestartButton');
let counter = 0;
const counterStart = 0;
const counterStop = 4;
const counterStep = 1;
const timerTick = 1000;
...
// Success!
const prediction = JSON.parse(request.responseText).prediction;
processPrediction(prediction);
...
const processPrediction = (prediciton) => {
appResults.querySelector(".appUserAnswer").innerHTML = prediciton;
appRestartButton.classList.remove('hide');
};
...
canvasElement.classList.add('hide');
appRestartButton.classList.add('hide');
...
canvasElement.classList.remove('hide');
...
bindCamera(videoElement);
appRestartButton.addEventListener("click", function(){
startCounter();
});