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="pic-container">
  <img src="https://storage.googleapis.com/tfweb/demos/io2021/cat.jpg"
     crossOrigin="anonymous"/>
  <a class="credit" href="https://unsplash.com/photos/FilM6ng7VGQ" target="_blank">Credit</a>
</div>

<div class="result-container">
  <div class="trigger">Loading...</div> 
  <canvas width="224px" height="224px" class="hide"></canvas>
</div>

              
            
!

CSS

              
                body {
  display: flex;
  justify-content: center;
  padding: 20px;
  font-family: Arial, Helvetica, sans-serif;
}

img {
  display: block;
  width: 224px;
  height: 224px;
  margin-right: 20px;
  border-radius: 8px;
}

.credit {
  margin-top: 6px;
  display: inline-block;
  font-size: 12px;
  color: #999;
}

canvas {
  border-radius: 8px;
  position: absolute;
}

.result-container {
  position: relative;
  width: 224px;
  height: 224px;
}

.trigger {
  border: 1px dashed #ccc;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #e28843;
}

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

.hide {
  display: none;
}
              
            
!

JS

              
                async function start() {
  // Load the model.
  const tfliteModel = await tflite.loadTFLiteModel(
    "https://storage.googleapis.com/tfweb/models/cartoongan_fp16.tflite",
  );

  // Setup the trigger button.
  setupTrigger(tfliteModel);
}

async function cartoonize(tfliteModel) {
  // Prepare the input tensors from the image.
  const inputTensor = tf.image
    // Resize.
    .resizeBilinear(tf.browser.fromPixels(document.querySelector("img")), [
      224,
      224
    ])
    // Normalize.
    .expandDims()
    .div(127.5)
    .sub(1);
  
  // Run the inference and get the output tensors.
  const outputTensor = tfliteModel.predict(inputTensor);
  
  // Process and draw the result on the canvas.
  //
  // De-normalize.
  const data = outputTensor.add(1).mul(127.5);
  // Convert from RGB to RGBA, and create and return ImageData.
  const rgb = Array.from(data.dataSync());
  const rgba = [];
  for (let i = 0; i < rgb.length / 3; i++) {
    for (let c = 0; c < 3; c++) {
      rgba.push(rgb[i * 3 + c]);
    }
    rgba.push(255);
  }
  // Draw on canvas.
  const imageData = new ImageData(Uint8ClampedArray.from(rgba), 224, 224);
  const canvas = document.querySelector("canvas");
  const ctx = canvas.getContext("2d");
  ctx.putImageData(imageData, 0, 0);
  canvas.classList.remove("hide");
}

function setupTrigger(tfliteModel) {
  const trigger = document.querySelector(".trigger");
  trigger.textContent = "Cartoonize!";
  document.querySelector(".trigger").addEventListener("click", (e) => {
    trigger.textContent = "Processing...";
    setTimeout(() => {
      cartoonize(tfliteModel);
      trigger.classList.add("hide");
    });
  });
}

start();

              
            
!
999px

Console