node-red-contrib-tfjs-pj1
Version:
custom Node-RED for classification
35 lines (28 loc) • 1.03 kB
JavaScript
const RED = require('node-red'); // This assumes Node-RED's runtime is available.
const tfutil = require('./tfjs-pj-util.js');
// Load the model
async function loadModel (config, node) {
node.model = await tfutil.loadModel(config.modelUrl);
}
// Define the node's behavior
function MyTfjsNode(config) {
RED.nodes.createNode(this, config);
const node = this;
loadModel(config, node);
// Register a listener to get called whenever a message arrives at the node
node.on('input', function (msg) {
// Preprocess the incoming image
const inputTensor = tfutil.processInput(msg.payload);
// Get the prediction
node.model
.predict(inputTensor)
.array()
.then(predictionArray => {
msg.payload = tfutil.processOutput(predictionArray);
// Send the prediction out
node.send(msg);
});
});
}
// Register the node with the runtime
RED.nodes.registerType('my-tfjs-node', MyTfjsNode);