Machine Learning Image Classifier for NodeJS
npm install image-classifier
// CommonJS
const ImageClassifier = require('image-classifier');
// Or ES Modules
import ImageClassifier from "image-classifier/lib/ImageClassifier";
Example:
const classifier = await ImageClassifier.create()
Parameter | Description | Type | Memory |
---|---|---|---|
datasetPath | Path to load the dataset | String | True |
Example:
const classifier = await ImageClassifier.load('./dataset.json');
Parameter | Description | Type | Memory |
---|---|---|---|
datasetPath | Path to save the dataset | String | True |
Example:
await classifier.save('./carset.json');
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 */
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');
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>"
}
}