Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!-- Copyright 2023 The MediaPipe Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>

<h1>Measure image similarity using the MediaPipe Image Embedder task</h1>
<section id="demos" class="invisible">
  <h2>Demo: Embedding Images</h2>
  <p><b>Click on an image below</b> to calculate the similarity between the two images.</p>
  <div style="display: block;">
    <div class="embedOnClick">
      <img src=" https://assets.codepen.io/9177687/dog_flickr_publicdomain.jpeg" width="100%" crossorigin="anonymous" title="Click to get embeddings and cosine similarity" />
      <p id="embed1" class="embedding removed">
    </div>
    <div class="embedOnClick">
      <img src="https://assets.codepen.io/9177687/cat_flickr_publicdomain.jpeg" width="100%" crossorigin="anonymous" title="Click to get embeddings and cosine similarity" />
      <p id="embed2" class="embedding removed">
    </div>
  </div>

  <p id="im_result" class="removed"></p>

  <h2>Demo: Webcam continuous cosine similarity</h2>
  <p>Upload an image as the reference image and continuously calculate its similarity to video frames captured by your webcam! Upload an image by clicking <b>Upload Image</b>, and then click <b>Enable Webcam</b> and accept access to the webcam.</p>
  <p>Tip: if you want to see two similar images, take a photo with your webcam, and then compare it to streaming webcam video</p>

  <div class="uploadImage">
    <button id="uploadImageButton" class="mdc-button mdc-button--raised" onclick="document.getElementById('getFile').click()">
      <span class="mdc-button__ripple"></span>
      <span class="mdc-button__label">UPLOAD IMAGE</span>
    </button>
    <input id="getFile" type="file" accept="image/*" style="display:none">
    <img id="uploadImage" width=100%></img>
  </div>
  <div class="videoView">
    <button id="webcamButton" class="mdc-button mdc-button--raised">
      <span class="mdc-button__ripple"></span>
      <span class="mdc-button__label">ENABLE WEBCAM</span>
    </button>
    <video id="webcam" autoplay playsinline></video>
  </div>
  <p id="video_result" class="removed"></p>
</section>
              
            
!

CSS

              
                /* Copyright 2023 The MediaPipe Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
@use "@material";
body {
  font-family: roboto;
  margin: 2em;
  color: #3d3d3d;
  --mdc-theme-primary: #007f8b;
  --mdc-theme-on-primary: #f1f3f4;
}

h1 {
  color: #007f8b;
}

h2 {
  clear: both;
}

#im_result,
#video_result {
  clear: both;
  margin: auto;
  text-align: center;
}

video {
  clear: both;
  display: block;
}

section {
  opacity: 1;
  transition: opacity 500ms ease-in-out;
}

.mdc-button.mdc-button--raised.removed {
  display: none;
}

.removed {
  display: none;
}

.invisible {
  opacity: 0.2;
}

.videoView,
.embedOnClick,
.uploadImage {
  position: relative;
  float: left;
  width: 48%;
  margin: 2% 1%;
  cursor: pointer;
}

.videoView p,
.embedOnClick p {
  padding: 5px;
  background-color: #007f8b;
  color: #fff;
  z-index: 2;
  font-size: 12px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.highlighter {
  background: rgba(0, 255, 0, 0.25);
  border: 1px dashed #fff;
  z-index: 1;
  position: absolute;
}

.embedOnClick {
  z-index: 0;
}

.embedOnClick img,
uploadImage img {
  width: 100%;
}

#video_result,
#im_result {
  padding-top: 5px;
  padding-bottom: 5px;
  background-color: #007f8b;
  color: #fff;
  z-index: 2;
  margin: 0;
  width: 100%;
  font-size: calc(8px + 1.2vw);
}

              
            
!

JS

              
                // Copyright 2023 The MediaPipe Authors.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//      http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {
  ImageEmbedder,
  FilesetResolver
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0";

const demosSection = document.getElementById("demos");

let imageEmbedder: ImageEmbedder;
let runningMode = "IMAGE";

// Before we can use ImageEmbedder class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
const createImageEmbedder = async () => {
  const vision = await FilesetResolver.forVisionTasks(
    "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm"
  );

  imageEmbedder = await ImageEmbedder.createFromOptions(vision, {
    baseOptions: {
      modelAssetPath: `https://storage.googleapis.com/mediapipe-models/image_embedder/mobilenet_v3_small/float32/1/mobilenet_v3_small.tflite`
    },
    runningMode: runningMode
    // quantize: true
  });

  demosSection.classList.remove("invisible");
};
createImageEmbedder();

/********************************************************************
 // Demo 1: Grab a bunch of images from the page and Embedder them
 // upon click.
 ********************************************************************/

// In this demo, we have put all our clickable images in divs with the
// CSS class 'detectionOnClick'. Lets get all the elements that have
// this class.
const imageContainers = document.getElementsByClassName("embedOnClick");

// Now let's go through all of these and add a click event listener.
for (let imageContainer of imageContainers) {
  // Add event listener to the child element whichis the img element.
  imageContainer.children[0].addEventListener("click", handleClick);
}

/**
 * Get embeddings of images on click
 */
const imageResult = document.getElementById("im_result");
const videoResult = document.getElementById("video_result");
const embedding1: HTMLElement = document.getElementById("embed1");
const embedding2: HTMLElement = document.getElementById("embed2");

async function handleClick(event) {
  if (!imageEmbedder) {
    console.log("Wait for objectDetector to load before clicking");
    return;
  }
  // if video mode is initialized, set runningMode to image
  if (runningMode === "VIDEO") {
    runningMode = "IMAGE";
    await imageEmbedder.setOptions({ runningMode: runningMode });
  }
  if (imageContainers.length == 2) {
    const imageEmbedderResult0 = imageEmbedder.embed(
      imageContainers[0].children[0]
    );
    const imageEmbedderResult1 = imageEmbedder.embed(
      imageContainers[1].children[0]
    );
    embedding1.className = "embedding";
    embedding2.className = "embedding";
    const truncatedEmbedding0 =
      imageEmbedderResult0.embeddings[0].floatEmbedding;
    truncatedEmbedding0.length = 4;
    const truncatedEmbedding1 =
      imageEmbedderResult1.embeddings[0].floatEmbedding;
    truncatedEmbedding1.length = 4;
    embedding1.innerText = `Float Embedding: ${truncatedEmbedding0}...`;
    embedding2.innerText = `Float Embedding: ${truncatedEmbedding1}...`;
    // Compute cosine similarity.
    const similarity = ImageEmbedder.cosineSimilarity(
      imageEmbedderResult0.embeddings[0],
      imageEmbedderResult1.embeddings[0]
    );
    console.log(similarity);
    imageResult.className = "";
    imageResult.innerText = "Image similarity: " + similarity.toFixed(2);
  }
  // Write predictions to a new paragraph element and add to the DOM.
}

/********************************************************************
 // Demo 2: Continuously grab image from webcam stream and Embedder it.
 ********************************************************************/

const video = document.getElementById("webcam");
let enableWebcamButton: HTMLButtonElement;
const getFile = document.getElementById("getFile");
let uploadImageEmbedderResult;

getFile.addEventListener("change", preview_image, false);

function preview_image(event) {
  let reader = new FileReader();
  const output = document.getElementById("uploadImage");
  reader.onload = function () {
    output.src = reader.result;
    setTimeout(async function () {
      if (runningMode === "VIDEO") {
        runningMode = "IMAGE";
        await imageEmbedder.setOptions({ runningMode: runningMode });
      }
      uploadImageEmbedderResult = await imageEmbedder.embed(output);
      document.getElementById("uploadImageButton").classList.add("removed");
    }, 100);
  };
  reader.readAsDataURL(event.target.files[0]);
}

// Check if webcam access is supported.
function hasGetUserMedia() {
  return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}

// If webcam supported, add event listener to button for when user
// wants to activate it.
if (hasGetUserMedia()) {
  enableWebcamButton = document.getElementById("webcamButton");
  enableWebcamButton.addEventListener("click", enableCam);
} else {
  console.warn("getUserMedia() is not supported by your browser");
}

// Enable the live webcam view and start detection.
async function enableCam(event) {
  if (!imageEmbedder) {
    alert("ImageEmbedder still loading");
    return;
  }

  // Hide the button.
  enableWebcamButton.classList.add("removed");

  // getUsermedia parameters
  const constraints = {
    video: true
  };

  // Activate the webcam stream.
  navigator.mediaDevices
    .getUserMedia(constraints)
    .then(function (stream) {
      video.srcObject = stream;
      video.addEventListener("loadeddata", predictWebcam);
    })
    .catch((err) => {
      console.error(err);
      /* handle the error */
    });
}

async function predictWebcam() {
  // if image mode is initialized, create a new embedder with video runningMode
  if (runningMode === "IMAGE") {
    runningMode = "VIDEO";
    await imageEmbedder.setOptions({ runningMode: runningMode });
  }

  // Embed image using imageEmbedder.embedForVideo().
  const startTimeMs = performance.now();
  const embedderResult = await imageEmbedder.embedForVideo(video, startTimeMs);

  if (uploadImageEmbedderResult != null) {
    const similarity = ImageEmbedder.cosineSimilarity(
      uploadImageEmbedderResult.embeddings[0],
      embedderResult.embeddings[0]
    );
    videoResult.className = "";
    videoResult.innerText = "Image similarity: " + similarity.toFixed(2);
  }

  // Call this function again to keep predicting when the browser is ready
  window.requestAnimationFrame(predictWebcam);
}

              
            
!
999px

Console