Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @mugifly mugifly revised this gist May 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ const IMAGE_FILE_PATH = `${__dirname}/example.jpg`;
    let img_tensor = tf.tensor3d(values, outShape, 'float32');
    img_tensor = img_tensor.expandDims(0);

    let predictions = await model.predict(img_tensor).dataSync();
    const predictions = await model.predict(img_tensor).dataSync();

    for (let i = 0; i < predictions.length; i++) {
    const label = labels[i];
  2. @mugifly mugifly revised this gist May 29, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    const tf = require('@tensorflow/tfjs-node');
    const Jimp = require('jimp');

    // Directory path for model file (model.json)
    // Directory path for model files (model.json, metadata.json, weights.bin)
    // NOTE: It can be obtained from [Export Model] -> [Tensorflow.js] -> [Download my model]
    // on https://teachablemachine.withgoogle.com/train/image
    const MODEL_DIR_PATH = `${__dirname}`;
  3. @mugifly mugifly revised this gist May 29, 2020. 1 changed file with 15 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,22 @@
    const tf = require('@tensorflow/tfjs-node');
    const Jimp = require('jimp');

    // Path for model file
    // Directory path for model file (model.json)
    // NOTE: It can be obtained from [Export Model] -> [Tensorflow.js] -> [Download my model]
    // on https://teachablemachine.withgoogle.com/train/image
    const MODEL_FILE = `${__dirname}/model.json`;
    const MODEL_DIR_PATH = `${__dirname}`;

    // Path for image file to predict class
    const IMAGE_FILE = `${__dirname}/example.jpg`;
    const IMAGE_FILE_PATH = `${__dirname}/example.jpg`;

    (async () => {

    const model = await tf.loadLayersModel(`file://${MODEL_FILE}`);
    const labels = require(`${MODEL_DIR_PATH}/metadata.json`).labels;

    const model = await tf.loadLayersModel(`file://${MODEL_DIR_PATH}/model.json`);
    model.summary();

    const image = await Jimp.read(`${IMAGE_FILE}`);
    const image = await Jimp.read(IMAGE_FILE_PATH);
    image.cover(224, 224, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);

    const NUM_OF_CHANNELS = 3;
    @@ -37,7 +39,12 @@ const IMAGE_FILE = `${__dirname}/example.jpg`;
    let img_tensor = tf.tensor3d(values, outShape, 'float32');
    img_tensor = img_tensor.expandDims(0);

    const predictions = await model.predict(img_tensor);
    predictions.print();
    let predictions = await model.predict(img_tensor).dataSync();

    for (let i = 0; i < predictions.length; i++) {
    const label = labels[i];
    const probability = predictions[i];
    console.log(`${label}: ${probability}`);
    }

    })();
    })();
  4. @mugifly mugifly revised this gist May 28, 2020. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -17,18 +17,24 @@ const IMAGE_FILE = `${__dirname}/example.jpg`;
    const image = await Jimp.read(`${IMAGE_FILE}`);
    image.cover(224, 224, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);

    const NUM_OF_CHANNELS = 3;
    let values = new Float32Array(224 * 224 * NUM_OF_CHANNELS);

    let i = 0;
    image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
    const pixel = Jimp.intToRGBA(image.getPixelColor(x, y));
    pixel.r = Math.max(0.0, pixel.r / 127.0 - 1);
    pixel.g = Math.max(0.0, pixel.g / 127.0 - 1);
    pixel.b = Math.max(0.0, pixel.b / 127.0 - 1);
    pixel.a = Math.max(0.0, pixel.a / 127.0 - 1);
    image.setPixelColor(Jimp.rgbaToInt(pixel.r, pixel.g, pixel.b, pixel.a), x, y);
    pixel.r = pixel.r / 127.0 - 1;
    pixel.g = pixel.g / 127.0 - 1;
    pixel.b = pixel.b / 127.0 - 1;
    pixel.a = pixel.a / 127.0 - 1;
    values[i * NUM_OF_CHANNELS + 0] = pixel.r;
    values[i * NUM_OF_CHANNELS + 1] = pixel.g;
    values[i * NUM_OF_CHANNELS + 2] = pixel.b;
    i++;
    });

    const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);

    let img_tensor = tf.node.decodeImage(new Uint8Array(buffer), 3);
    const outShape = [224, 224, NUM_OF_CHANNELS];
    let img_tensor = tf.tensor3d(values, outShape, 'float32');
    img_tensor = img_tensor.expandDims(0);

    const predictions = await model.predict(img_tensor);
  5. @mugifly mugifly revised this gist May 28, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,12 @@
    const tf = require('@tensorflow/tfjs-node');
    const Jimp = require('jimp');

    // Path for model file
    // NOTE: It can be obtained from [Export Model] -> [Tensorflow.js] -> [Download my model]
    // on https://teachablemachine.withgoogle.com/train/image
    const MODEL_FILE = `${__dirname}/model.json`;

    // Path for image file to predict class
    const IMAGE_FILE = `${__dirname}/example.jpg`;

    (async () => {
  6. @mugifly mugifly created this gist May 28, 2020.
    32 changes: 32 additions & 0 deletions image-predict-on-tfjs-node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const tf = require('@tensorflow/tfjs-node');
    const Jimp = require('jimp');

    const MODEL_FILE = `${__dirname}/model.json`;
    const IMAGE_FILE = `${__dirname}/example.jpg`;

    (async () => {

    const model = await tf.loadLayersModel(`file://${MODEL_FILE}`);
    model.summary();

    const image = await Jimp.read(`${IMAGE_FILE}`);
    image.cover(224, 224, Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE);

    image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
    const pixel = Jimp.intToRGBA(image.getPixelColor(x, y));
    pixel.r = Math.max(0.0, pixel.r / 127.0 - 1);
    pixel.g = Math.max(0.0, pixel.g / 127.0 - 1);
    pixel.b = Math.max(0.0, pixel.b / 127.0 - 1);
    pixel.a = Math.max(0.0, pixel.a / 127.0 - 1);
    image.setPixelColor(Jimp.rgbaToInt(pixel.r, pixel.g, pixel.b, pixel.a), x, y);
    });

    const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);

    let img_tensor = tf.node.decodeImage(new Uint8Array(buffer), 3);
    img_tensor = img_tensor.expandDims(0);

    const predictions = await model.predict(img_tensor);
    predictions.print();

    })();
    17 changes: 17 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    {
    "name": "image-predict-on-tfjs-node",
    "description": "Prediction using tfjs-node (with model made by Teachable Machine Image)",
    "scripts": {
    "start": "node image-predict-on-tfjs-node.js"
    },
    "engines": {
    "node": "14"
    },
    "dependencies": {
    "jimp": "^0.12.1",
    "@tensorflow/tfjs-node": "^1.3.1",
    },
    "devDependencies": {
    },
    "private": true
    }