-
Notifications
You must be signed in to change notification settings - Fork 11
/
deepstack-custom-model.js
82 lines (69 loc) · 2.51 KB
/
deepstack-custom-model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const clonedeep = require('lodash.clonedeep')
const deepstack = require('./deepstack-integration');
const pu = require('./prediction-utils');
/*
* Custom Model node
*
* Status:
* - grey dot "Idling, waiting for images to process"
* - green dot "Successfully processed an image and result delivered"
* - yellow ring "Processing image"
* - red ring "Error processing image"
*/
module.exports = function(RED) {
function CustomModel(config) {
RED.nodes.createNode(this, config);
let node = this;
node.server = RED.nodes.getNode(config.server);
node.status({fill:"grey",shape:"dot",text:"idling"});
node.on('input', function(msg, send, done) {
node.status({fill:"yellow",shape:"ring",text:"Processing..."});
customModel(msg, config, node.server).then(output =>{
node.status({fill: "green", shape: "dot", text: "success"});
setTimeout(function () {
node.status({fill: "grey", shape: "dot", text: "idling"});
}, 2000);
node.send(output);
}).catch(reason => {
node.status({fill:"red",shape:"ring",text:"error detecting objects"});
node.error(reason);
console.log(reason);
});
});
}
RED.nodes.registerType("deepstack-custom-model", CustomModel, {});
};
/**
* Construct the object detection outputs.
*
* @param msg the incomming msg object.
* @param config the node configuration.
* @param server the server configuration node.
* @returns {Promise<unknown>}
*/
function customModel(msg, config, server) {
return new Promise((resolve, reject) => {
let original = msg.payload;
if (!original) {
reject("No image provided. Please set msg.payload.")
}
if (original.type === 'Buffer') {
original = Buffer.from(original.data)
}
deepstack.customModel(
original,
server,
config.confidence/100,
config.customModel,
).then(async result => {
msg.payload = result.predictions;
msg.success = result.success;
msg.duration = result.duration || 0;
msg.originalImage = original;
if (config.drawPredictions) {
msg.outlinedImage = await pu.drawPredictions(original, result.predictions, config.outlineColor, config.printLabel);
}
resolve(msg);
}).catch(reject);
});
}