@smcgann/node-red-cosine-similarity-plus v0.0.2
@smcgann/node-red-cosine-similarity-plus
A Node-RED node that calculates the cosine similarity, between a set of stored vectors and provided vector values.
This node is a significantly modified version of š @good-i-deer/node-red-contrib-cosine-similarity and doesn't maintain compatibility with that node.
Key Changes
ā input data - accepts payload direct from š @smcgann/node-red-face-vectorization-plus.
ā Supports multiple stored vector locations: file, flow context, or global context.
ā Stored vectors now also include names and image file data, for meaningful results.
ā Returns sorted results, filtered by a configurable similarity threshold.
ā Generates an error message if no results meet threshold.
ā More robust error handling with validation for missing and invalid data structures.
ā Supports runtime configuration through msg.cosineOptions
for dynamic settings.
ā Includes metadata in msg.cosineConfig
(threshold used, file type, file path).
ā Easier integration into Node-RED flows.
Description
This node calculates cosine similarity between two sets of vectors. One passed in by msg.payload
and one stored in a file or context variable.
It returns the results in msg.payload
as an array of objects, where the similarity is above set threshold.
input = array[2] // (2 faces)
0: array[0.12, 0.34, 0.56, "..."] // 512 vectors for each face.
1: array[0.11, 0.22, 0.33, "..."]
// Stored as a nested object, with each key representing an individual person.
storedVectors =
{
"Adam": {
"/full-path/Adam/Adam-01.jpg": [0.12, 0.34, 0.56, "..."], // 512 vectors for each face.
"/full-path/Adam/Adam-03.jpg": [0.23, 0.45, 0.67, "..."],
"/full-path/Adam/Adam-02.jpg": [0.34, 0.56, 0.78, "..."]
},
"Alison": {
"/full-path/Alison/Alison-01.jpg": [0.11, 0.22, 0.33, "..."],
"/full-path/Alison/Alison-02.jpg": [0.44, 0.55, 0.66, "..."]
}
// repeated for x number of people
}
output = array[2] // (2 matches above threshold)
[
{ "Alison": { "/full-path/Alison/8.jpg": 0.7265643591861766 } },
{ "Peter": { "/full-path/people/Peter/9.jpg": 0.6443388973714721 } }
]
Installation
Either use the Edit Menu - Manage Palette option to install, or run the following command in your Node-RED user directory - typically ~/.node-red
cd ~/.node-red
npm install @smcgann/node-red-cosine-similarity-plus
Restart your Node-RED instance
Input Properties
š msg.payload ā Array
The input is an array of vector arrays. (created by the vectorize node)
āļø msg.cosineOptions ā Object
(Optional)
- Allows overriding node config settings dynamically.
- Example:
{
"threshold": 0.4,
"fileType":"path",
"file":"/home/pi/vectortest.txt"
}
Node Properties
š·ļø Name
- The name of the node displayed on the screen.
šļø Threshold
- Results with a value equal to or above the specified threshold will be returned. Set the threshold to 0 to include all matches.
š Vectors
- File or context path of file or variable, that will be compared with the input vector array. Cannot be empty.
Output
š msg.payload ā Array
The output is an array of name objects. Each containing the orginal source image file and percentage similarity to the input vectors.
āļø msg.cosineConfig ā Object
- The configuration used for the calculations.
āļø Authors
S.McGann ā Modified Version.
GOOD-I-DEER in SSAFY(Samsung Software Academy for Youth) 9th
š Copyright and license
S.McGann ā Modified Version
Copyright Samsung Automation Studio Team under the Apache 2.0 license