@viun/node-red-contrib-overview
Version:
Collection of nodes to control the Overview's smart AI cameras
69 lines (58 loc) • 2.95 kB
JavaScript
module.exports = function(RED) {
function CheckConfidenceClassificationNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.passThreshold = config.passThreshold;
node.failThreshold = config.failThreshold;
node.on('input', function(msg, send, done) {
try {
if (!msg.payload?.classification?.predictions || !Array.isArray(msg.payload.classification.predictions)) {
throw new Error('Input must contain payload.classification.predictions array');
}
const predictions = msg.payload.classification.predictions;
let result = true;
let reason = '';
// Check pass conditions
const passClasses = predictions.filter(p => p.predicted_class.toLowerCase().startsWith('pass'));
const failClasses = predictions.filter(p => p.predicted_class.toLowerCase().startsWith('fail'));
// Check if any pass class is below threshold
for (const prediction of passClasses) {
if (prediction.confidence < node.passThreshold) {
result = false;
reason = `Pass: ${prediction.predicted_class} confidence (${prediction.confidence}) was lower than ${node.passThreshold}`;
break;
}
}
// Check if any fail class is above threshold
if (result) {
for (const prediction of failClasses) {
if (prediction.confidence > node.failThreshold) {
result = false;
reason = `Fail: ${prediction.predicted_class} confidence (${prediction.confidence}) was higher than ${node.failThreshold}`;
break;
}
}
}
// If result is still true and we found pass classes, set success reason
if (result && passClasses.length > 0) {
reason = 'All pass conditions met required confidence thresholds';
}
// Update status with reason
if (result) {
node.status({fill: "green", shape: "dot", text: reason});
} else {
node.status({fill: "red", shape: "ring", text: reason});
}
// Set output message
msg.payload = result;
msg.reason = reason;
send(msg);
done();
} catch (error) {
node.status({fill: "red", shape: "ring", text: error.message});
done(error);
}
});
}
RED.nodes.registerType('check confidence classification', CheckConfidenceClassificationNode);
}