Skip to content

Instantly share code, notes, and snippets.

@Daniel15
Last active October 13, 2023 18:56
Show Gist options
  • Save Daniel15/acd0a1a71d09c23f708e74b60ca67a8f to your computer and use it in GitHub Desktop.
Save Daniel15/acd0a1a71d09c23f708e74b60ca67a8f to your computer and use it in GitHub Desktop.
Blue Iris AI alerts in Node-RED
const {img, ...otherPayloadFields} = msg.payload;
const objectsToFilter = new Set([
'ball',
'sports ball',
'horse',
'book',
'suitcase',
]);
let formattedObjectNames = 'Nothing';
const objectsData = (otherPayloadFields.json || [])
.find(x => x.api === 'objects');
// Would normally use `objectData?.found?.success` but Node-RED doesn't support it
const hasObjects = objectsData && objectsData.found && objectsData.found.success;
if (hasObjects) {
// Using Array.from(new Set(...)) to dedupe object names
const objectNames = Array.from(new Set(
(objectsData.found.predictions || [])
.sort(x => -x.confidence)
.map(x => x.label)
.filter(x => !objectsToFilter.has(x))
));
if (objectNames.length > 0) {
// Capitalise first letter of first object
objectNames[0] = objectNames[0][0].toUpperCase() + objectNames[0].substring(1)
formattedObjectNames = objectNames.join(' and ');
}
}
const message = formattedObjectNames + ' detected at ' + otherPayloadFields.name;
return {
image_filename: Date.now() + '-' + otherPayloadFields.id + '.jpg',
message,
object_names: formattedObjectNames,
payload: Buffer.from(img, 'base64'),
original_payload: otherPayloadFields,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment