ImageClassifier

image classification of bird

Description

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).

Train your own image classification model with Teachable Machine: If you’d like to train your own custom image classification model, try Google’s Teachable Machine.

Quickstart

  1. // Initialize the Image Classifier method with MobileNet
  2. const classifier = ml5.imageClassifier('MobileNet', modelLoaded);
  3. // When the model is loaded
  4. function modelLoaded() {
  5. console.log('Model Loaded!');
  6. }
  7. // Make a prediction with a selected image
  8. classifier.classify(document.getElementById('image'), (err, results) => {
  9. console.log(results);
  10. });

Usage

Initialize

  1. const classifier = ml5.imageClassifier(model, ?video, ?options, ?callback);

Parameters

  • model: REQUIRED. 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’,’DoodleNet’, or any image classification model trained in Teachable Machine. Below are some examples of creating a new image classifier:

    • mobilenet:

      1. const classifier = ml5.imageClassifier('MobileNet', modelReady);
    • Darknet:

      1. const classifier = ml5.imageClassifier('Darknet', modelReady);
    • DoodleNet:

      1. const classifier = ml5.imageClassifier('DoodleNet', modelReady);
    • Custom Model from Teachable Machine:

      1. const classifier = ml5.imageClassifier('path/to/custom/model.json', modelReady);
  • video: OPTIONAL. An HTMLVideoElement

  • 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 to change the defaults (shown below). The available options are:

    1. {
    2. version: 1,
    3. alpha: 1.0,
    4. topk: 3,
    5. };

Properties


.video

Object. HTMLVideoElement if given in the constructor. Otherwise it is null.



.model

Object. The image classifier model specified in the constructor.



.modelName

String. The name of the image classifier model specified in the constructor



.modelUrl

String. The absolute or relative URL path to the input model.


Methods


.classify()

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

If you DID NOT specify an image or video in the constructor…

  1. classifier.classify(input, ?numberOfClasses, ?callback);

If you DID specify an image or video in the constructor…

  1. classifier.classify(?numberOfClasses , ?callback);

📥 Inputs

  • input: HTMLImageElement | ImageData | HTMLCanvasElement | HTMLVideoElement. NOTE: Videos can also be added in the constructor and then do not need to be specified again as an input.
  • numberOfClasses: Number. The number of classes you want to return.
  • callback: Function. A function to handle the results of .segment(). Likely a function to do something with the segmented image.

📤 Outputs

  • Object: Returns an array of objects. Each object contains {label, confidence}.

Examples

p5.js

p5.js Web Editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

Webcam Image Classification via CodingTrain

Image Classification with MobileNet via CodingTrain

Acknowledgements

Contributors:

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code