From 7f53b10dc7677420b83a3d5a9f85c4d4820d3d77 Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Fri, 25 Sep 2020 17:39:43 +0300 Subject: [PATCH] [patch] - issue #3 --- index.js | 13 ++++++++++--- package.json | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7d0f975..c0bea64 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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) { diff --git a/package.json b/package.json index 646f4e0..eac285f 100644 --- a/package.json +++ b/package.json @@ -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",