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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <code id="customToast"class="toast">
 It's like Animoji in the Browser with face-api.js and  Avataaars <small>(inspired by <a href="https://pragli.com/blog/live-avatars-with-faceapi-js/">pragli</a>)</small>
</code>

<!--GITHUB ICON-->
    <a
      href="https://github.com/SimHub/avatar-face-expression"
      class="github-corner"
      aria-label="View source on Github"
    >
      <svg
        width="80"
        height="80"
        viewBox="0 0 250 250"
        style="fill:#11acf4; color:#fff; position: absolute; top: 0; border: 0; right: 0;"
        aria-hidden="true"
      >
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path
          d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
          fill="currentColor"
          style="transform-origin: 130px 106px;"
          class="octo-arm"
        ></path>
        <path
          d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
          fill="currentColor"
          class="octo-body"
        ></path>
      </svg>
    </a>

<div id="wrapper">  
  <video id="video" width="720" height="560"></video>

      <div class="panel">
        <i id="stopStreaming" class="icon icon-cross"></i>
        <code id="date"></code>
        <div id="statusBox">
          <div id="loader">
            <span class="avatar-lamp"></span>
            <span class="loading"></span>
          </div>
          <div id="status"><code id="statusCode" class="label"></code></div>
        </div>
        <div id="imgbox">
          <img
            class="img-avatar-start neutral-male avatar-blur"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/neutral_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar neutral-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/neutral_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar neutral-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/neutral_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar angry-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/angry_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar angry-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/angry_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar disgusted-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/disgusted_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar disgusted-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/disgusted_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar fearful-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/fearful_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar fearful-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/fearful_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar happy-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/happy_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar happy-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/happy_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar sad-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/sad_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar sad-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/sad_female.svg"
            alt="avatar"
          />
          <img
            class="img-avatar surprised-male"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/surprised_male.svg"
            alt="avatar"
          />
          <img
            class="img-avatar surprised-female"
            src="https://simhub.github.io/avatar-face-expression/img/avatar/svg/surprised_female.svg"
            alt="avatar"
          />
        </div>
        <div id="panelBox">
          <div class="panel-subtitle" id="expression-txt"></div>
        </div>
      </div>
    </div>

              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Sacramento', cursive;
}
video {
  width: 0;
  height: 0;
}
canvas {
  position: absolute;
}

#wrapper {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
#customToast{
  position:fixed;
  text-align:center;
  font-size:1.2em;
  top:0;
}
#expression-pending {
  font-size: 2em;
  text-align: center;
}
#expression-txt {
 font-family: 'Sacramento', cursive;
  font-size: 4em;
}
#chipContainer {
  transform: rotateZ(90deg);
  position: absolute;
  margin-left: -100px;
  margin-top: 55px;
  display: flex;
}
#statusBox {
   background-image: linear-gradient(
    108.7deg,
    rgba(34, 219, 231, 1) -0.9%,
    rgba(52, 118, 246, 1) 88.7%
  );
  width: 0;
  border-radius: 10px;
  height: 30px;
  transition: all 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
#imgBox {
  height: 100%;
}

#loader {
  position: absolute;
  margin-left: 4px;
  margin-top: 3px;
}
#status {
  background: #11acf4;
  width: 100%;
  text-align: -webkit-right;
    text-align:  right;



}
#statusCode {
  position: absolute;
  left: 30px;
  color: #fff;
}
#avatarMale,
#avatarFemale {
  display: none;
}
#avatarFaces.female {
  position: absolute;
  top: 125px;
  left: 136px;
}
#avatarFaces.male {
  position: absolute;
  top: 116px;
  left: 60px;
}
#panelBox {
  position: absolute;
  width: 100%;
  text-align: center;
  bottom: 0;
}

#date {
  width: 210px;
  color: #03a9f4;
  background: none;
  border-radius: 10px;
  transform: rotateZ(90deg);
  position: absolute;
  left: -123px;
  top: 100px;
}
#stopStreaming {
  display: none;
  position: absolute;
  right: -30px;
  top: 10px;
  color: #bbbbbb;
  cursor: pointer;
  transition: all 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
code.label {
  background: none;
  border-radius: 0.1rem;
  color: #ffffff;
}

.img-avatar,
.img-avatar-start {
  /* opacity: 0; */
  display: none;
  position: absolute;
  left: 20px;
  transition: all 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.avatar-blur {
  animation: image_blur 3s ease 0s infinite;
  -webkit-animation: image_blur 3s ease 0s infinite;
  -moz-animation: image_blur 3s ease 0s infinite;
}

.panel {
  position: absolute;
  box-sizing: border-box;
  height: 35px;
  width: 300px;
  border-radius: 10px;
  background: aliceblue;
  border: none;
  padding: 5px;
  box-shadow: 1px 5px 20px -8px;
  transition: all 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.chip {
  font-size: 1.5em;
  border-radius: 0;
  height: 30px;
}
.gender-chip,
.age-chip {
  flex: 1;
  font-size: 3.2em;
  height: 30px;
}
.age-chip {
}
.gender-chip {
}
.loading {
  position: absolute;
  margin-left: 10px;
  margin-top: 1px;
}
.loading:after {
  animation: loading 0.5s infinite linear;
  border: 0.1rem solid rgba(0, 0, 0, 0.95);
  border-radius: 50%;
  border-right-color: transparent;
  border-top-color: transparent;
  content: '';
  display: block;
  height: 0.8rem;
  left: 50%;
  margin-left: -0.4rem;
  margin-top: -0.4rem;
  position: absolute;
  top: 50%;
  width: 0.8rem;
  z-index: 1;
}
.avatar-lamp {
  position: absolute;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 90px;
  filter: blur(1px);
}

/**** ANIMATIONS ****/
.progress-animation {
  animation-name: colorchanger;
  animation-duration: 5s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-play-state: running;
}

@keyframes image_blur {
  0%,
  90% {
    -webkit-filter: blur(2px);
    -moz-filter: blur(2px);
    -o-filter: blur(2px);
    -ms-filter: blur(2px);
  }
  50% {
    -webkit-filter: blur(15px);
    -moz-filter: blur(15px);
    -o-filter: blur(15px);
    -ms-filter: blur(15px);
  }
}

@keyframes colorchanger {
  0% {
    background-image: linear-gradient(
      72.5deg,
      rgba(0, 175, 255, 1) 27.9%,
      rgba(0, 224, 254, 1) 84.2%
    );
  }
  50% {
    background-image: radial-gradient(
      circle farthest-corner at 10% 20%,
      rgba(220, 217, 249, 1) 0%,
      rgba(172, 224, 217, 1) 89.8%
    );
  }
  100% {
    background-image: linear-gradient(
      108.7deg,
      rgba(34, 219, 231, 1) -0.9%,
      rgba(52, 118, 246, 1) 88.7%
    );
  }
}
              
            
!

JS

              
                const video = document.getElementById("video");
const stopStreaming = document.querySelector("#stopStreaming");
const avatarImgs = document.querySelectorAll(".img-avatar");
const avatarImgStart = document.querySelector(".img-avatar-start");
const avatarLamp = document.querySelector(".avatar-lamp");
const panelBox = document.querySelector("#panelBox");
const expressionTxt = document.querySelector("#expression-txt");
const expressionTitle = document.querySelector("#expression-title");
const loader = document.querySelector(".loading");
const status = document.querySelector("#status");
const statusCode = document.querySelector("#statusCode");
const statusBox = document.querySelector("#statusBox");
const panel = document.querySelector(".panel");
const dateTime = document.querySelector("#date");
const models = "https://simhub.github.io/avatar-face-expression/models";
let ua = navigator.userAgent.toLowerCase();
let is_safari = ua.indexOf("safari/") > -1 && ua.indexOf("chrome") < 0;
let gender = "";
let age = "";
let exp = "";
let angry = "";
let neutral = "";
let happy = "";
let surprised = "";
let disgusted = "";
let fearful = "";
let sad = "";
let d = new Date();
let nD = d.toString();
let sD = nD.split(" ").splice(0, 5);

dateTime.innerText = `${sD[0]} ${sD[1]} ${sD[2]} ${sD[3]} ${sD[4]}`;
statusCode.innerHTML = "loading module...";
statusBox.classList.add("progress-animation");

let getAvatar = (mood, gender) => {
  if (mood[0] > 0.6 || mood[0] >= 1) {
    if (gender === "male") {
      avatarImgs.forEach(i => {
        if (i.classList[1] === `${mood[1]}-male`) {
          i.style.display = "block";
        } else {
          i.style.display = "none";
        }
      });
    }
    if (gender === "female") {
      avatarImgs.forEach(i => {
        if (i.classList[1] === `${mood[1]}-female`) {
          i.style.display = "block";
        } else {
          i.style.display = "none";
        }
      });
    }
    expressionTxt.innerText = mood[1];
  }
};

Promise.all([
  faceapi.nets.tinyFaceDetector.loadFromUri(models),
  faceapi.nets.faceLandmark68Net.loadFromUri(models),
  faceapi.nets.faceRecognitionNet.loadFromUri(models),
  faceapi.nets.faceExpressionNet.loadFromUri(models),
  faceapi.nets.ageGenderNet.loadFromUri(models)
]).then(startVideo);

function startVideo() {
  video.setAttribute("autoplay", "");
  video.setAttribute("muted", "");
  video.setAttribute("playsinline", "");

  navigator.mediaDevices
    .getUserMedia({ video: { facingMode: "user" } })
    .then(function(stream) {
    /* use the stream */
    video.srcObject = stream;
  })
    .catch(function(err) {
    /* handle the error */
    console.error(err);
  });
  statusCode.innerHTML = "start video session...";
  statusBox.style.width = "34%";
  if (is_safari) {
    setTimeout(function() {
      video.play();
    }, 50);
  }
}
function stopStreamedVideo(videoElem) {
  let stream = videoElem.srcObject;
  let tracks = stream.getTracks();
  // console.log(stream,tracks)
  tracks.forEach(function(track) {
    track.stop();
  });
  videoElem.srcObject = null;
}
video.addEventListener("play", () => {
  panel.style.height = "400px";
  expressionTxt.style.fontSize = "3.2em";
  setTimeout(() => {
    statusCode.innerHTML = "just a moment please..";
    avatarImgStart.style.display = "block";
    expressionTxt.innerText = "i am still focusing 🧐";
    statusBox.style.width = "80%";
  }, 800);

  setInterval(async () => {
    const detections = await faceapi
    .detectAllFaces(video, new faceapi.TinyFaceDetectorOptions())
    .withFaceLandmarks()
    .withFaceExpressions()
    .withAgeAndGender();
    if (detections[0]) {
      gender = detections[0].gender;
      // status.innerHTML= "";
      // status.style.visibility = 'hidden';
      // status.style.dislay = 'none';
      // statusBox.style.visibility = 'hidden';
      statusBox.style.width = "100%";
      statusBox.classList.remove("progress-animation");
      statusCode.innerHTML = "ready!";
      loader.style.display = "none"; // hide preloader
      avatarImgStart.style.display = "none";
      avatarLamp.style.backgroundColor = "lightgreen";
      exp = detections[0].expressions;
      angry = [exp.angry.toFixed(2), "angry"];
      neutral = [exp.neutral.toFixed(2), "neutral"];
      happy = [exp.happy.toFixed(2), "happy"];
      surprised = [exp.surprised.toFixed(2), "surprised"];
      disgusted = [exp.disgusted.toFixed(2), "disgusted"];
      fearful = [exp.fearful.toFixed(2), "fearful"];
      sad = [exp.sad.toFixed(2), "sad"];

      getAvatar(angry, gender);
      getAvatar(neutral, gender);
      getAvatar(happy, gender);
      getAvatar(surprised, gender);
      getAvatar(disgusted, gender);
      getAvatar(sad, gender);
    } else {
      avatarLamp.style.backgroundColor = "#ccc";
      panel.style.backgroundColor = "#d3d3d345";
      statusCode.innerHTML = "can't see you!..";
    }
  }, 800);

  stopStreaming.style.display = "block";

  stopStreaming.addEventListener("click", e => {
    let el = e.target;
    let elClass = e.target.classList[1];
    // console.log(elClass);
    if (elClass === "icon-cross") {
      el.classList.remove("icon-cross");
      el.classList.add("icon-refresh");
      stopStreamedVideo(video);
    }
    if (elClass === "icon-refresh") {
      el.classList.remove("icon-refresh");
      el.classList.add("icon-cross");
      avatarLamp.style.backgroundColor = "#ccc";
    }
  });
});

              
            
!
999px

Console