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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
  <div class="img-container">
    <img src="https://storage.googleapis.com/tfweb/demos/static/airplane.jpeg" crossorigin="anonymous">
    <canvas></canvas>
  </div>
  <div class="buttons">
    <div class="model tfjs-webgl">
      <div class="btn disabled">TFJS (webgl)</div>
      <div class="result"></div>
    </div>
    <div class="model tflite">
      <div class="btn disabled">TFLite</div>
      <div class="result"></div>
    </div>
    <div class="model tflite-custom">
      <div class="btn disabled">TFLite (custom)</div>
      <div class="result"></div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  font-family: 'Google Sans';
}

.container {
  display: flex;
}

.img-container {
  position: relative;
}

img {
  height: 250px;
  border-radius: 8px;
}

canvas {
  border-radius: 8px;
  left: 0;
  position: absolute;
  opacity: .65;
  top: 0;
  visibility: visible;
}

.buttons {
  margin-left: 20px;
}

.model {
  display: flex;
  align-items: center;
}

.result {
  margin-left: 20px;
}

.btn {
  border: 1px solid #888;
  cursor: pointer;
  width: 150px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
}

.btn.disabled {
  color: #bbb;
  border-color: #bbb;
  pointer-events: none;
}

.btn:hover {
  background-color: #eee;
}

.box-container {
  position: absolute;
}

              
            
!

JS

              
                const img = document.querySelector("img");
const canvas = document.querySelector("canvas");
let lastModel;
let lastClassName;
let warmedUp = false;

setupButton(
  "tfjs-webgl",
  async () =>
    await tfTask.ImageSegmentation.Deeplab.TFJS.load({
      backend: "webgl"
    }),
  true
);

setupButton(
  "tflite",
  async () => await tfTask.ImageSegmentation.Deeplab.TFLite.load()
);

setupButton(
  "tflite-custom",
  async () =>
    await tfTask.ImageSegmentation.CustomModel.TFLite.load({
      // mobilenetv2-coco
      model:
        "https://tfhub.dev/sayakpaul/lite-model/mobilenetv2-coco/fp16/1?lite-format=tflite"
    })
);

async function setupButton(className, modelCreateFn, needWarmup) {
  document
    .querySelector(`.model.${className} .btn`)
    .classList.remove("disabled");
  const resultEle = document.querySelector(`.model.${className} .result`);
  document
    .querySelector(`.model.${className} .btn`)
    .addEventListener("click", async () => {
      let model;
      // Create the model when user clicks on a button.
      if (lastClassName !== className) {
        // Clean up the previous model if existed.
        if (lastModel) {
          lastModel.cleanUp();
        }
        // Create the new model and save it.
        resultEle.textContent = "Loading...";
        model = await modelCreateFn();
        lastModel = model;
        lastClassName = className;
      }
      // Reuse the model if user clicks on the same button.
      else {
        model = lastModel;
      }

      // Warm up if needed.
      if (needWarmup && !warmedUp) {
        await model.predict(img);
        warmedUp = true;
      }

      // Run inference and update result.
      const start = Date.now();
      const result = await model.predict(img);
      const latency = Date.now() - start;
      renderCanvas(result);
      resultEle.textContent = `Latency: ${latency}ms`;
    });
}

function renderCanvas(result) {
  const ctx = canvas.getContext("2d");
  canvas.width = result.width;
  canvas.height = result.height;
  ctx.clearRect(0, 0, result.width, result.height);
  ctx.putImageData(
    new ImageData(result.segmentationMap, result.width, result.height),
    0,
    0
  );
  document.querySelector(".img-container").appendChild(canvas);

  // Stretch the canvas to the same size as the image.
  canvas.style.width = `${img.width}px`;
  canvas.style.height = `${img.height}px`;
}

              
            
!
999px

Console