Refactored the classify-4 README to use modern ES6 javascript syntax

pull/498/head
Avarayr 4 years ago
parent d33b80da64
commit 4ccdc4a666
No known key found for this signature in database
GPG Key ID: B88572B8EC34F22A

@ -226,50 +226,33 @@ You can use your model directly in a web app. This architecture also allows you
1. Once the Runtime is in place, you can call it: 1. Once the Runtime is in place, you can call it:
```javascript ```html
<script> <script>
const ingredients = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] const ingredients = Array(380).fill(0);
const checks = [].slice.call(document.querySelectorAll('.checkbox'));
// use an async context to call onnxruntime functions. const checks = document.querySelectorAll('.checkbox');
function init() {
checks.forEach(function (checkbox, index) { checks.forEach(check => {
checkbox.onchange = function () { check.addEventListener('change', function() {
if (this.checked) { // toggle the state of the ingredient
var index = checkbox.value; // based on the checkbox's value (1 or 0)
ingredients[check.value] = check.checked ? 1 : 0;
if (index !== -1) { });
ingredients[index] = 1; });
}
console.log(ingredients)
}
else {
var index = checkbox.value;
if (index !== -1) {
ingredients[index] = 0;
}
console.log(ingredients)
}
}
})
}
function testCheckboxes() { function testCheckboxes() {
for (var i = 0; i < checks.length; i++) // validate if at least one checkbox is checked
if (checks[i].type == "checkbox") return checks.some(check => check.checked);
if (checks[i].checked)
return true;
return false;
} }
async function startInference() { async function startInference() {
let checked = testCheckboxes() let atLeastOneChecked = testCheckboxes()
if (checked) { if (!atLeastOneChecked) {
alert('Please select at least one ingredient.');
return;
}
try { try {
// create a new session and load the model. // create a new session and load the model.
@ -279,17 +262,15 @@ You can use your model directly in a web app. This architecture also allows you
const feeds = { float_input: input }; const feeds = { float_input: input };
// feed inputs and run // feed inputs and run
const results = await session.run(feeds); const results = await session.run(feeds);
// read from results // read from results
alert('You can enjoy ' + results.label.data[0] + ' cuisine today!') alert('You can enjoy ' + results.label.data[0] + ' cuisine today!')
} catch (e) { } catch (e) {
console.log(`failed to inference ONNX model: ${e}.`); console.log(`failed to inference ONNX model`);
} console.error(e);
} }
else alert("Please check an ingredient")
} }
init(); init();

Loading…
Cancel
Save