Skip to content

Commit

Permalink
Merge pull request #225 from Honry/fix-1411
Browse files Browse the repository at this point in the history
LeNet: do not predict if the pen is cleared
  • Loading branch information
huningxin authored Apr 28, 2024
2 parents 82021b5 + fbbda29 commit e5bdbab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lenet/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions lenet/pen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit e5bdbab

Please sign in to comment.