Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.



  <meta charset="UTF-8">
  <title>Image Classification using Feature Extraction with MobileNet</title>

  <script src="" type="text/javascript"></script>


  <h2>Image Classification using Feature Extraction with MobileNet</h2>
  <h3>(Train a Neural Network to distinguish between your Cat or Dog poses)</h3>
  <video id="video" width="640" height="480" autoplay></video>

  <h6><span id="loading">Loading base model...</span> | <span id="videoStatus">Loading video...</span></h6>
    <button id="catButton">Add Cat Image</button>
    </p><p><span id="amountOfCatImages">0</span> Cat Images</p>
    <br /><button id="dogButton">Add Dog Image</button>
    <p><span id="amountOfDogImages">0</span> Dog Images</p>
  <br />
  <p><button id="train">Train</button><span id="loss"></span></p>
  <br />
    <button id="predict">Start guessing!</button><br />
    My guess is: <span id="result">...</span>
    , with a confidence of <span id="confidence">...</span>.






                // Copyright (c) 2018 ml5
// This software is released under the MIT License.

/* ===
ml5 Example
Image Classification using Feature Extractor with MobileNet
=== */

// Grab all the DOM elements
var video = document.getElementById('video');
var videoStatus = document.getElementById('videoStatus');

var loading = document.getElementById('loading');

var catButton = document.getElementById('catButton');
var dogButton = document.getElementById('dogButton');

var amountOfCatImages = document.getElementById('amountOfCatImages');
var amountOfDogImages = document.getElementById('amountOfDogImages');

var train = document.getElementById('train');

var loss = document.getElementById('loss');
var result = document.getElementById('result');
var confidence = document.getElementById('confidence');
var predict = document.getElementById('predict');

// A variable to store the total loss
let totalLoss = 0;

// Create a webcam capture
navigator.mediaDevices.getUserMedia({ video: true })
  .then((stream) => {
    video.srcObject = stream;;

// A function to be called when the model has been loaded
function modelLoaded() {
  loading.innerText = 'Model loaded!';

// Extract the already learned features from MobileNet
const featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded);
// Create a new classifier using those features
const classifier = featureExtractor.classification(video, videoReady);

// Predict the current frame.
function predict() {

// A function to be called when the video is finished loading
function videoReady() {
  videoStatus.innerText = 'Video ready!';

// When the Cat button is pressed, add the current frame
// from the video with a label of cat to the classifier
catButton.onclick = function () {
  amountOfCatImages.innerText = Number(amountOfCatImages.innerText) + 1;

// When the Cat button is pressed, add the current frame
// from the video with a label of cat to the classifier
dogButton.onclick = function () {
  amountOfDogImages.innerText = Number(amountOfDogImages.innerText) + 1;

// When the train button is pressed, train the classifier
// With all the given cat and dog images
train.onclick = function () {
  classifier.train(function(lossValue) {
    if (lossValue) {
      totalLoss = lossValue;
      loss.innerHTML = 'Loss: ' + totalLoss;
    } else {
      loss.innerHTML = 'Done Training! Final Loss: ' + totalLoss;

// Show the results
function gotResults(err, results) {
  // Display any error
  if (err) {
  if (results && results[0]) {
    result.innerText = results[0].label;
    confidence.innerText = results[0].confidence;

// Start predicting when the predict button is clicked
predict.onclick = function () {