imageClassifier()

You can use neural networks to recognize the content of images.

You can use neural networks to recognize the content of images. ml5.imageClassifier() is a method to create an object that classifies an image using a pre-trained model.

It should be noted that the pre-trained model provided by the example below was trained on a database of approximately 15 million images (ImageNet). The ml5 library accesses this model from the cloud. What the algorithm labels an image is entirely dependent on that training data -- what is included, excluded, and how those images are labeled (or mislabeled).

Example

// Initialize the Image Classifier method with MobileNet
const classifier = ml5.imageClassifier('MobileNet', modelLoaded);

// When the model is loaded
function modelLoaded() {
  console.log('Model Loaded!');
}

// Make a prediction with a selected image
classifier.predict(document.getElementById('image'), function(err, results) {
  console.log(results);
});

Here is a complete example using the p5.js library for loading and displaying the image.

Syntax

ml5.imageClassifier(model)
ml5.imageClassifier(model, ?callback)
ml5.imageClassifier(model, ?options, ?callback)
ml5.imageClassifier(model, ?video, ?options, ?callback)

Parameters

  • model - A String value of a valid model OR a url to a model.json that contains a pre-trained model. Case insensitive. Models available are: 'MobileNet', 'Darknet' and 'Darknet-tiny', or any image classifiation model trained in Teachable Machine
  • callback - Optional. A function to run once the model has been loaded. If no callback is provided, it will return a promise that will be resolved once the model has loaded.
  • options - Optional. An object describing a model accuracy and performance. For MobileNet the defaults are: { version: 1, alpha: 1.0, topk: 3, }
  • video - Optional. A HTML video element or a p5 video element.

Properties

.modelName

The name of the model used.

.modelLoaded

Boolean to check if the model has loaded

.model

The model architecture

Methods

.classify(input, ?callback)
.classify(input, ?numberOfClasses ,?callback)
.classify(?callback)
.classify(?numberOfClasses ,?callback)

Given an image or video, returns an array of objects containing class names and probabilities.

input - A HTML video or image element or a p5 image or video element. If no input is provided, the default is to use the video given in the constructor.

numberOfClasses - The number of results to return.

callback - Optional. A function to run once the model has made the prediction. If no callback is provided, it will return a promise that will be resolved once the model has made a prediction.

Source

/src/ImageClassifier