Pen Settings

HTML

CSS

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

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                <body>
  <h1>Image classification using MobileNet and p5.js</h1>
  <p><font color='red'>Current ML5 distribution does not work with p5.Image object.<br> 
   So here I used the latest nightly build.</font></p>

  <p id="status">Loading Model...</p>
  <p>The MobileNet model labeled this as <span id="result">...</span>, with a confidence of <span id="probability">...</span>.</p>
 
  
</body>
              
            
!

CSS

              
                body {
  font-family: Helvetica, san-serif;
}

#status {
  font-size:20px;
}

#result {
  font-size:20px;
  background-color: yellow;
}

#probability {
  font-size:20px;
  background-color: yellow;
}
              
            
!

JS

              
                // Initialize the Image Classifier method with MobileNet. A callback needs to be passed.
const classifier = ml5.imageClassifier('MobileNet', modelReady);

// A variable to hold the image we want to classify
let img;

function setup() {  
  noLoop();
  c = createCanvas(400, 400).drop(gotFile).dragOver(highlight).dragLeave(redraw);

  bg = color(20);
  hover_bg = color('red');
  textAlign(CENTER);
}

// Change the status when the model loads.
function modelReady(){
  select('#status').html('Model Loaded');
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

// A function to run when we get any errors and the results
function gotResult(err, results) {
  if (err) {
    console.error(err);
  } else {
    console.log(results);

    // The results are in an array ordered by probability.
    select('#result').html(results[0].className);
    select('#probability').html(nf(results[0].probability, 0, 2));
  }
}

function draw() {
  background(bg);

  if (img)  {
    console.log("image draw");
    image(img, 5, 5, width-10, height-10);

    classifier.predict(img, gotResult);
    console.log("predict");
  }
  else {
    fill(255); 
    textSize(20);
    text('Drag an image file onto the canvas.', width/2, height/2);
  }
}


function gotFile(f) {
  if (f.type === 'image') {
    console.log(f);
    img = loadImage(f.data, redraw);
    img.resize(400, 400);
    
    
  }
}

function highlight(evt) {
  background(hover_bg);
  evt.preventDefault();
}
              
            
!
999px

Console