Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Tensorflow.js Examples for Node.js
* Script adatapted from
* https://github.com/tensorflow/tfjs-examples
* https://groups.google.com/a/tensorflow.org/forum/#!forum/tfjs
* @author Loreto Parisi (loretoparisi@gmail.com)
* @copyright 2020 Loreto Parisi (loretoparisi@gmail.com)
*/
const tf = require('@tensorflow/tfjs-node');
var model, image;
const model_path = './model/new_object_detection_1';
const labels = require('./model/new_object_detection_1/assets/labels.json');
const { createCanvas, Image } = require('canvas');
tf.node.loadSavedModel(model_path, ['serve'], 'serving_default')
.then(res => {
model = res; // LP: loaded TFSavedModel
console.log(model);
return tf.node.getMetaGraphsFromSavedModel(model_path);
})
.then(modelInfo => {
console.log(modelInfo);
image = require('fs').readFileSync('./image.jpeg');
const uint8array = new Uint8Array(image);
// Decode the image into a tensor.
return tf.node.decodeImage(uint8array);
})
.then(imageTensor => {
const input = imageTensor.expandDims(0);
// Feed the image tensor into the model for inference.
const startTime = tf.util.now();
.then(modelInfo => {
console.log(modelInfo);
image = require('fs').readFileSync('./image.jpeg');
const uint8array = new Uint8Array(image);
// Decode the image into a tensor.
return tf.node.decodeImage(uint8array);
})
.then(imageTensor => {
async function loadModel() {
// Warm up the model
if (!objectDetectionModel) {
objectDetectionModel = await tf.node.loadSavedModel(
'./model/new_object_detection_1', ['serve'], 'serving_default');
}
const tempTensor = tf.zeros([1, 2, 2, 3]).toInt();
objectDetectionModel.predict(tempTensor);
}
getTensor3dObject(numOfChannels) {
const imageData = this.inputImage.replace('data:image/jpeg;base64','')
.replace('data:image/png;base64','');
const imageArray = toUint8Array(imageData);
const tensor3d = tf.node.decodeJpeg( imageArray, numOfChannels );
return tensor3d;
}
busboy.on('finish', async () => {
const buf = req.files.file[0].buffer;
const uint8array = new Uint8Array(buf);
if (!objectDetectionModel) {
objectDetectionModel = await tf.node.loadSavedModel(
'./model/new_object_detection_1', ['serve'], 'serving_default');
}
const imageTensor = await tf.node.decodeImage(uint8array);
const input = imageTensor.expandDims(0);
let outputTensor = objectDetectionModel.predict({'x': input});
const scores = await outputTensor['detection_scores'].arraySync();
const boxes = await outputTensor['detection_boxes'].arraySync();
const names = await outputTensor['detection_classes'].arraySync();
outputTensor['detection_scores'].dispose();
outputTensor['detection_boxes'].dispose();
outputTensor['detection_classes'].dispose();
outputTensor['num_detections'].dispose();
const detectedBoxes = [];
const detectedNames = [];
for (let i = 0; i < scores[0].length; i++) {
if (scores[0][i] > 0.3) {
detectedBoxes.push(boxes[0][i]);