JSFiddle - React, Tailwind, and code Playground

by morphcast

JavaScript 1.7

// Singleton
let aiSdkInstance;

async function downloadAiSdk() {
  if (globalThis.CY) {
    throw new Error("AI-SDK has already been downloaded.");
  }
  await loadScript("https://sdk.morphcast.com/mphtools/v1.1/mphtools.js", "cameraPrivacyPopup, compatibilityUI, compatibilityAutoCheck");
  await loadScript("https://ai-sdk.morphcast.com/v1.16/ai-sdk.js"); // CY is a global var now
}


async function loadScript(url, config = null) {
  return new Promise(resolve => {
    const script = document.createElement("script");
    script.type = "text/javascript";
    if (script.readyState) {  //IE
      script.onreadystatechange = function () {
        if (script.readyState === "loaded" || script.readyState === "complete") {
          script.onreadystatechange = null;
          resolve();
        }
      };
    } else {  //Others
      script.onload = function () {
        resolve();
      };
    }
    if (config) {
      script.setAttribute('data-config', config);
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
  });
}

async function initAiSdk() {
  if (aiSdkInstance) {
    throw new Error("An instance of the AI-SDK is already running.");
  }

  aiSdkInstance = await globalThis.CY.loader()
    // TODO INSERT YOUR LICENSE KEY HERE
    .licenseKey("9087cfd2037a28a79769fdcaa4962252379cd2e2fd0b") // <--- ##############
    .maxInputFrameSize(640)
    .addModule(CY.modules().FACE_DETECTOR.name, {
      maxInputFrameSize: 640,
      multiFace: true,
    })
    .addModule(globalThis.CY.modules().FACE_EMOTION.name, {
      smoothness: 0.5, // custom setting
    })
    .addModule(globalThis.CY.modules().FACE_GENDER.name, {})
    .addModule(globalThis.CY.modules().FACE_AGE.name, {
      windowSizeMs: 4000, // custom setting
      maxVarianceCutoff: Math.pow(7, 2),
      numericalStability: 1,
    })
    .addModule(globalThis.CY.modules().FACE_AROUSAL_VALENCE.name, {
      smoothness: 0.9, // custom setting
    })
   ...