Applied the javascript refactoring to applied classify solution

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

@ -47,73 +47,51 @@
<!-- import ONNXRuntime Web from CDN --> <!-- import ONNXRuntime Web from CDN -->
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.9.0/dist/ort.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.9.0/dist/ort.min.js"></script>
<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')); const checks = [...document.querySelectorAll('.checkbox')];
// use an async context to call onnxruntime functions. checks.forEach(check => {
function init() { check.addEventListener('change', function() {
// toggle the state of the ingredient
checks.forEach(function (checkbox, index) { // based on the checkbox's value (1 or 0)
checkbox.onchange = function () { ingredients[check.value] = check.checked ? 1 : 0;
if (this.checked) { });
var index = checkbox.value; });
if (index !== -1) { function testCheckboxes() {
ingredients[index] = 1; // validate if at least one checkbox is checked
} return checks.some(check => check.checked);
console.log(ingredients) }
}
else { async function startInference() {
var index = checkbox.value;
let atLeastOneChecked = testCheckboxes()
if (index !== -1) {
ingredients[index] = 0; if (!atLeastOneChecked) {
} alert('Please select at least one ingredient.');
console.log(ingredients) return;
}
}
})
}
function testCheckboxes() {
for (var i = 0; i < checks.length; i++)
if (checks[i].type == "checkbox")
if (checks[i].checked)
return true;
return false;
} }
try {
// create a new session and load the model.
const session = await ort.InferenceSession.create('./model.onnx');
async function startInference() { const input = new ort.Tensor(new Float32Array(ingredients), [1, 380]);
const feeds = { float_input: input };
let checked = testCheckboxes()
if (checked) {
try {
// create a new session and load the model.
const session = await ort.InferenceSession.create('./model.onnx');
const input = new ort.Tensor(new Float32Array(ingredients), [1, 380]);
const feeds = { float_input: input };
// feed inputs and run
const results = await session.run(feeds); // feed inputs and run
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(); }
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save