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.


                <!DOCTYPE html>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Link to our CSS stylesheet -->
  <link rel="stylesheet" type="text/css" href="./styles.css">
  <!-- CDN Link to the ml5 library -->
  <script type="text/javascript" src=""></script>
  <title>Image Classification</title>

  <!-- container -->
  <div class="container">
    <p style="position:absolute; top: 0; left: 0; color: #000; margin: 20px"><b>Important Message: </b> If you are seeing this on codepen, then try to <b>upload a new image</b> and then try to PREDICT it. The default image doesn't work perfectly on Codepen.</p>

    <!-- image-box -->
    <div class="image-box">
      <img src="" class="image">
      <!-- label-text -->
      <p class="label-text">Unidentified</p>
      <!-- accuracy-text -->
      <p class="accuracy-text">Accuracy: 0%</p>

    <!-- upload-btn will go here -->
    <input type="file" class="upload-btn" />
    <!-- predict-btn (disabled) -->
    <button class="predict-btn" disabled>PREDICT</button>

  <!-- Link to our Javascript file  -->
  <script src="./script.js"></script>


                /* common styles */
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;

/* body */
body {
  width: 100vw; /* 100% viewport width */
  height: 100vh; /* 100% viewport height */
  overflow-x: hidden;
  display: flex;
  justify-content: center; /* horizontally center */
  align-items: center; /* vertically center */
  background: linear-gradient(to right, #0072ff, #00c6ff); /* background color */
  font-size: 1.2rem; /* default font-size */
  font-family: sans-serif;

/* .container */
.container {
  display: flex;
  flex-direction: column;
  justify-content: center; /* now this is for vertically center. Because of flex-direction: column; */
  align-items: center; /* horizontally center */

/* .image-box */
.image-box {
  background-color: #fff;
  padding: 15px; /* inner spacing */
  margin: 30px 0px 10px 0px; /* outer spacing. Order -> top right bottom left*/
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;

/* .image */
.image {
  width: 230px; /* the more you increase width, the more precise the prediction will be! */
  margin-bottom: 15px;

/* .label-text */
.label-text {
  font-size: 1.5rem;
  font-family: cursive;
  max-width: 200px;
  margin-bottom: 7px;

/* .accuracy-text */
.accuracy-text {
  margin-bottom: 2px;

/* .predict-btn */
.predict-btn {
  background-color: #00ff00;
  font-size: 1.4rem;
  padding: 5px;
  margin: 20px 0px; /* top-bottom left-right */
  outline: none;
  cursor: pointer;
  border-radius: 3px;
  box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5); /* x-offset y-offset blur-radius color  */

/* .upload-btn styles will go here */
/* hide the default upload button */
.upload-btn::-webkit-file-upload-button {
  display: none;

/* create our own upload button */
.upload-btn::before {
  content: 'Upload Image';
  display: block;
  background-color: yellow;
  font-size: 1.3rem;
  text-align: center;
  padding: 5px 8px;
  margin: 15px 0px 0px 0px;
  border-radius: 3px; /* for curved edges */
  border: none;
  outline: none;
  cursor: pointer;
  box-shadow: 2px 3px 9px rgba(0, 0, 0, 0.5);

/* remove the "outline" and "border" */
input[type="file" i] {
  outline: none;
  border: none;



                // grab reference for needed elements
// .image
const image = document.querySelector('.image');
// .label-text
const labelText = document.querySelector('.label-text');
// .accuracy-text
const accuracyText = document.querySelector('.accuracy-text');
// .predict-btn
const predictBtn = document.querySelector('.predict-btn');
// .upload-btn will go here
const uploadBtn = document.querySelector('.upload-btn');

// Loading our MobileNet model
const model = ml5.imageClassifier("MobileNet", modelLoaded);

// modelLoaded() callback function definition
function modelLoaded() {
  // first just console.log() 'Model Loaded'
  console.log('Model Loaded!');
  // then enable the .predict-btn
  predictBtn.disabled = false;

  /* then attach an 'click' eventListener to .predict-btn */
  predictBtn.addEventListener('click', predict);

// predict() callback function definition
function predict() {
  /* take the .image and classify() it */
  model.classify(image, function (error, results) {
    // check if error happened
    if (error) {
      // just console.log() the error
      console.log('Error: ', error);
    } else {
      /* from "results" array, grab first element's "label" and "confidence" level */
      const label = results[0].label.toUpperCase();
      const accuracy = (results[0].confidence * 100).toFixed(2);

      /* update .label-text and .accuracy-text */
      labelText.innerText = label;
      /* Careful Here: Use back-ticks ``, Not single quote '' or double quote "" */
      accuracyText.innerText = `Accuracy: ${accuracy}%`;

/* attach a 'change' eventListener to .upload-btn */
uploadBtn.addEventListener('change', function (event) {
  // check if user had selected any file
  if ([0]) {
    /* create an ObjectURL for the selected/uploaded file */
    const objectURL = URL.createObjectURL([0]);

    /* then replace previous .image on DOM with the new file the user had uploaded */
    image.src = objectURL;