From fbbda29db0bfe7f3a636667e61f3897078c8e2f0 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Sun, 28 Apr 2024 14:33:49 +0800 Subject: [PATCH] LeNet: do not predict if the pen is cleared --- lenet/main.js | 5 +++++ lenet/pen.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lenet/main.js b/lenet/main.js index cc064527..39deac1a 100644 --- a/lenet/main.js +++ b/lenet/main.js @@ -36,6 +36,7 @@ $('#backendBtns .btn').on('change', async () => { }); function drawNextDigitFromMnist() { + pen.setIsCleared(false); const n = Math.floor(Math.random() * 10); const digit = mnist[n].get(); mnist.draw(digit, digitContext); @@ -105,6 +106,10 @@ async function main() { predictButton.addEventListener('click', async function(e) { clearInferenceResult(); + if (pen.isCleared) { + addAlert('Please draw a digit first.'); + return; + } predictButton.setAttribute('disabled', true); try { let start; diff --git a/lenet/pen.js b/lenet/pen.js index 5d48f85e..1575c530 100644 --- a/lenet/pen.js +++ b/lenet/pen.js @@ -7,6 +7,7 @@ export class Pen { this.canvas.style.cursor = 'crosshair'; this.context = cavans.getContext('2d'); this.down = false; + this.isCleared = false; this.start = {}; const self = this; this.canvas.addEventListener('mousedown', (e) => { @@ -33,6 +34,7 @@ export class Pen { } draw(start, end) { + this.isCleared = false; this.context.strokeStyle = 'white'; this.context.lineJoin = 'round'; this.context.lineWidth = 20; @@ -44,7 +46,12 @@ export class Pen { this.context.stroke(); } + setIsCleared(isCleared) { + this.isCleared = isCleared; + } + clear() { + this.isCleared = true; this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); } }