Skip to content

Commit

Permalink
Merge pull request #4 from pivanov/master
Browse files Browse the repository at this point in the history
[patch] - issue #3
  • Loading branch information
pivanov authored Sep 25, 2020
2 parents a7ba30a + 7f53b10 commit 6b69cce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const retryOperation = (operation, delay, times) => new Promise((resolve, reject
});
});

const getHTMLCanvasElement = (imageUrl) => {
return new Promise((resolve, reject) => {
const image = new canvas.Image();
image.onload = () => resolve(image);
image.onerror = (e) => reject(e);
image.src = imageUrl;
});
}

const byProbabilty = (predictionA, predictionB) => {
if (predictionA.probability > predictionB.probability) return -1;
if (predictionA.probability < predictionB.probability) return 1;
Expand Down Expand Up @@ -83,9 +92,7 @@ class SashiDoTeachableMachine {

async inference({ imageUrl }) {
try {
const image = new canvas.Image();
image.src = imageUrl;

const image = await getHTMLCanvasElement(imageUrl);
const predictions = await this.model.predict(image);
return predictions.sort(byProbabilty);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sashido/teachablemachine-node",
"version": "1.1.0",
"version": "1.1.1",
"description": "Simplifies integration with Teachable Machine models from Google",
"author": {
"name": "Team SashiDo",
Expand Down

0 comments on commit 6b69cce

Please sign in to comment.