Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active January 7, 2024 16:11
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mugifly/9abfed649259107f138121bc283a9fbb to your computer and use it in GitHub Desktop.
Save mugifly/9abfed649259107f138121bc283a9fbb to your computer and use it in GitHub Desktop.
Image Prediction on tfjs-node (with model made by Teachable Machine Image)
const tf = require('@tensorflow/tfjs-node');
const Jimp = require('jimp');
// 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}`;
// Path for image file to predict class
const IMAGE_FILE_PATH = `${__dirname}/example.jpg`;
(async () => {
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_PATH);
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 = 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 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).dataSync();
for (let i = 0; i < predictions.length; i++) {
const label = labels[i];
const probability = predictions[i];
console.log(`${label}: ${probability}`);
}
})();
{
"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
}
@mugifly
Copy link
Author

mugifly commented May 28, 2020

@salnikov777
Copy link

Thanks a million! I've been searching for this solution for a week! Hurrah!! It is the best solution that I have found

@Cyxo
Copy link

Cyxo commented Mar 14, 2021

Great code sample, thank you!

@KaKi87
Copy link

KaKi87 commented Sep 24, 2023

Hello,
Could you please explain what are lines 19 to 40 for, and if there's any simpler way to get the result of an image ?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment