Image Classifier NodeJS Package
Machine Learning Image Classifier for NodeJS
Installation
npm install image-classifier
Getting Started
// CommonJS
const ImageClassifier = require('image-classifier');
// Or ES Modules
import ImageClassifier from "image-classifier/lib/ImageClassifier";
Creating an ImageClassifier
ImageClassifier.create()
Create a new instance of ImageClassifier
from scratch
Example:
const classifier = await ImageClassifier.create()
ImageClassifier.load(datasetPath: string)
Create a new instance of ImageClassifier
from a dataset
Parameter |
Description |
Type |
Memory |
datasetPath |
Path to load the dataset |
String |
True |
Example:
const classifier = await ImageClassifier.load('./dataset.json');
ImageClassifier
ImageClassifier.prototype.save(datasetDestination: string)
Save the ImageClassifier's dataset to a json file
Parameter |
Description |
Type |
Memory |
datasetPath |
Path to save the dataset |
String |
True |
Example:
await classifier.save('./carset.json');
ImageClassifier.prototype.addExample(label: string, image: string | Buffer)
Add an example image and label it to train the ImageClassifier
Parameter |
Description |
Type |
Memory |
label |
Category label for what the image is |
String |
True |
image |
Path to image or raw image data to add as an example for the label |
String |
True |
Example:
// Add Toyota Examples from path
await classifier.addExample('Toyota', './toyota0.png');
await classifier.addExample('Toyota', './toyota1.png');
await classifier.addExample('Toyota', './toyota2.png');
// Add Toyota Example from raw image
const toyotaRawImage = fs.readFileSync('./toyota3.png');
await classifier.addExample('Toyota', toyotaRawImage);
/* ...Add more examples */
// Add Honda Examples
await classifier.addExample('Honda', './honda0.png');
await classifier.addExample('Honda', './honda1.png');
await classifier.addExample('Honda', './honda2.png');
// Add Honda Example from raw image
const hondaRawImage = await fs.promises.readFile('./honda3.png');
await classifier.addExample('Honda', hondaRawImage);
/* ...Add more examples */
ImageClassifier.prototype.dropClassifier(label: string)
Drop all classification for the specified label
Parameter |
Description |
Type |
Memory |
label |
Label for classifier you would like to remove from ImageClassifier.
Drops all examples of specified label.
|
String |
True |
Example:
classifier.dropClassifier('Honda');
ImageClassifier.prototype.predict(image: string | Buffer)
Predict the label for an image
Parameter |
Description |
Type |
Memory |
image |
Path to image or raw image data for evaluating a prediction with your ImageClassifier |
String |
True |
Example:
// Predict by image path
const prediction1 = await classifier.predict('./toyotaTest0.png');
// Predict by raw image
const toyotaTestRawImage = fs.readFileSync('./toyotaTest1.png');
const prediction2 = await classifier.predict('./toyotaTest1.png');
Prediction:
{
"classIndex": "<Index of Label>",
"label": "<Label>",
"confidences": {
"Toyota": "<Percentile>",
"Honda": "<Percentile>"
}
}