Forked from mugifly/image-predict-on-tfjs-node.js
Created
July 16, 2022 01:07
Revisions
-
mugifly revised this gist
May 29, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); const predictions = await model.predict(img_tensor).dataSync(); for (let i = 0; i < predictions.length; i++) { const label = labels[i]; -
mugifly revised this gist
May 29, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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}`; -
mugifly revised this gist
May 29, 2020 . 1 changed file with 15 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,22 @@ const tf = require('@tensorflow/tfjs-node'); const Jimp = require('jimp'); // 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_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; @@ -37,7 +39,12 @@ const IMAGE_FILE = `${__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(); for (let i = 0; i < predictions.length; i++) { const label = labels[i]; const probability = predictions[i]; console.log(`${label}: ${probability}`); } })(); -
mugifly revised this gist
May 28, 2020 . 1 changed file with 14 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = 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); -
mugifly revised this gist
May 28, 2020 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 () => { -
mugifly created this gist
May 28, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); })(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }