Recognize.js
Node.js image detection and recognition framework
Installation
First download and install GraphicsMagick. In Mac OS X, you can simply use Homebrew and do:
brew install graphicsmagick
Then download the Recognizejs
using npm:
npm i recognizejs
Getting started
Import Recognizejs
into your project:
const Recognizejs = ;
Try Recognizejs
- Create a model with
Recognizejs
and initialize it:
const myModel = ; // initialize it// The init function returns a Promise objectawait myModel;
PS: Model initialization may take up to 1-2 minutes (depending on the performance of your device), so please be patient. 😉
- Read your image file
const fs = ; const myImgBuffer = fs;
- Call the model's
recognize
function and pass the image buffer as a parameter:
// The recognize function will return a Promise object, we recommend that you use await statement to get the return value.const results = await myModel; /* [ { className: ['className1', 'className2', 'className...'], probability: 0.9 }, { className: ['className1', 'className2', 'className...'], probability: 0.599 } ]*/console;
The code for this example can be found in the examples
folder.
API
Create a Recognizejs model object
new Recognizejs(config?);
Args: config is an optional parameter and has the following attributes:
cocoSsd
and mobileNet
are different neural networks. cocoSsd
is used to identify and classify multiple objects in an image, while mobileNet
is used to accurately identify an object.
Initialize the training model
model.init(modelType?);
The init
function returns a Promise
object, you can use await
statement to handle it.
Args: modelType can be a string or an array. You can set the model to be loaded here to avoid loading the model that is not needed. [If you don't set modelType, it will load both cocoSsd and mobileNet models]
Example:
model; // or model; // or model; // or model;
If you don't use the init
function to load the model, the model will load automatically when you need to use them, but it may take a long time to load the model, so please choose the loading method as appropriate.
Identify objects in image
model.recognize(buf);
The recognize
function returns a Promise
object, you can use await
statement to get its return value.
Args: The buf parameter requires you to pass a buffer type of image data. You can read the image through the fs module.
Return value:
className: 'giant panda' 'panda' 'panda bear' 'coon bear' 'Ailuropoda melanoleuca' probability: 09819085597991943 className: 'Chihuahua' probability: 0006128392647951841 className: 'French bulldog' probability: 00026271280366927385
Example:
const myImgBuf = ; model;
Detect all objects in the image
model.detect(buf)
The detect
function returns a Promise
object, you can use await
statement to get its return value.
Args: The buf parameter requires you to pass a buffer type of image data. You can read the image through the fs module.
Return value:
bbox: x: 6692952662706375 y: 15830181241035461 width: 15767111629247665 height: 16500252485275269 class: 'bear' score: 09642460346221924 bbox: x: 18056899309158325 y: -032786130905151367 width: 2466680407524109 height: 3083251893520355 class: 'bear' score: 09133073091506958
Example:
const myImgBuf = ; model;
Detect all objects in the image and identify them
model.detectAndRecognize(buf);
The detectAndRecognize
function returns a Promise
object, you can use await
statement to get its return value.
Args: The buf parameter requires you to pass a buffer type of image data. You can read the image through the fs module.
Return value:
recognizeObject recognizeObject recognizeObject
Example:
const myImgBuf = ; model;
License
Copyright ©️ 2020, Yingxuan (Bill) Dong