Last active
September 19, 2022 21:57
-
-
Save apocas/8c54d3fc7cac1eb70490f3ef589cc94d to your computer and use it in GitHub Desktop.
Tensorflow Nodejs example. Read an image from file, detect objects and save a new image with bounding boxes drawn. 100% server side no browser code.
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 characters
const tf = require('@tensorflow/tfjs-node') | |
//const tf = require('@tensorflow/tfjs-node-gpu') | |
const cocossd = require('@tensorflow-models/coco-ssd'); | |
const fs = require('fs'); | |
const gm = require('gm'); | |
var path = 'image.jpg'; | |
//https://js.tensorflow.org/api_node/latest/ | |
const buf = fs.readFileSync(path); | |
const input = tf.node.decodeJpeg(buf); | |
load(input); | |
async function load(img){ | |
const model = await cocossd.load({ 'base': 'mobilenet_v2' }); | |
const predictions = await model.detect(img); | |
console.log('Predictions: '); | |
console.log(predictions); | |
drawPredictions(predictions); | |
} | |
async function drawPredictions(predictions) { | |
var pic = gm(path); | |
for(let i = 0; i < predictions.length; i++) { | |
var p = predictions[i].bbox; | |
pic.fill('transparent'); | |
pic.stroke('#ff0000', 5); | |
pic.drawRectangle(p[0], p[1], p[0]+p[2], p[1]+p[3]); | |
pic.fill('red'); | |
pic.stroke('#ff0000', 1); | |
pic.fontSize(25); | |
pic.drawText(p[0]+10, p[1]+25, predictions[i].class); | |
} | |
pic.write('predicted-' + path, function (err) { | |
if (!err) console.log('Done'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment