0.0.2 • Published 6 months ago

@smcgann/node-red-cosine-similarity-plus v0.0.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

platform npm version Min Node Version GitHub license

@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.payloadas 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


Reference


0.0.2

6 months ago

0.0.1

6 months ago