Skip to content

Commit

Permalink
Merge pull request #8 from eternaleclipse/data-url-support
Browse files Browse the repository at this point in the history
Added data url support
  • Loading branch information
pivanov authored May 31, 2021
2 parents 3d276a8 + a4c571a commit 30d0edb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 69 deletions.
33 changes: 23 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
global.fetch = require("node-fetch");

const { Readable } = require('stream');
const tf = require('@tensorflow/tfjs')
const PImage = require('pureimage')
const tf = require('@tensorflow/tfjs');
const PImage = require('pureimage');
const isImageUrl = require('is-image-url');
const parseDataUrl = require('parse-data-url');

const wait = ms => new Promise(r => setTimeout(r, ms));

Expand All @@ -27,7 +28,7 @@ const retryOperation = (operation, delay, times) => new Promise((resolve, reject
const bufferToStream = (binary) => {
const readableInstanceStream = new Readable({
read() {
this.push(binary)
this.push(binary);
this.push(null);
}
});
Expand Down Expand Up @@ -76,7 +77,7 @@ const getTopKClasses = async (logits, classes) => {
topkIndices[i] = valuesAndIndices[i].index;
}

const topClassesAndProbs = []
const topClassesAndProbs = [];
for (let i = 0; i < topkIndices.length; i++) {
topClassesAndProbs.push({
class: classes[topkIndices[i]],
Expand All @@ -100,7 +101,7 @@ class SashiDoTeachableMachine {

try {
const modelURL = `${modelUrl}model.json`;
const response = await fetch(`${modelUrl}metadata.json`)
const response = await fetch(`${modelUrl}metadata.json`);
const body = await response.text();
this.model = await tf.loadLayersModel(modelURL);
this.model.classes = JSON.parse(body).labels;
Expand All @@ -125,7 +126,7 @@ class SashiDoTeachableMachine {
async classify(params) {
const { imageUrl } = params;

if (!isImageUrl(imageUrl)) {
if ((!imageUrl.startsWith('data:image/')) && (!isImageUrl(imageUrl))) {
return Promise.reject({ error: "Image URL is not valid!" });
}

Expand All @@ -138,10 +139,22 @@ class SashiDoTeachableMachine {

async inference({ imageUrl }) {
try {
const data = await fetch(imageUrl);

const contentType = data.headers.get("Content-Type");
const buffer = await data.buffer();
let data;
let buffer;
let contentType;

if (imageUrl.startsWith('data:image/')) {
data = parseDataUrl(imageUrl);

contentType = data.contentType;
buffer = data.toBuffer();
} else {
data = await fetch(imageUrl);

contentType = data.headers.get("Content-Type");
buffer = await data.buffer();
}

const stream = bufferToStream(buffer);
let imageBitmap;

Expand Down
129 changes: 71 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 30d0edb

Please sign in to comment.