JSFiddle - React, Tailwind, and code Playground

by Laxmikant Dange

HTML

<script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script>

JavaScript

const knnClassifier = ml5.KNNClassifier();
const featureExtractor = ml5.featureExtractor('MobileNet', () => console.log('model loaded'));



for (let i = 0; i < 100; i++) {

  const num = Math.random();

  // Get the features of an image
  const features = featureExtractor.infer(num);

  // Add an example with a label to the KNN Classifier
  knnClassifier.addExample(features, num > 0.5 ? 'More' : 'Less');
}

// Use KNN Classifier to classify these features
knnClassifier.classify(features, function(err, result) {
  console.log(result); // result.label is the predicted label
});