google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://ai.google.dev/edge/mediapipe
Apache License 2.0
27.71k stars 5.18k forks source link

Facelandmarker failed to load inside a module web worker #5527

Open Hamed-Hassanzadeh opened 4 months ago

Hamed-Hassanzadeh commented 4 months ago

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

Yes

OS Platform and Distribution

Windows 10, Chrome 126

MediaPipe Tasks SDK version

0.10.14

Task name (e.g. Image classification, Gesture recognition etc.)

Face Landmarker

Programming Language and version (e.g. C++, Python, Java)

JavaScript

Describe the actual behavior

Module scripts don't support importScripts() so when you try to run face landmarker in a module web worker it throws an error

Describe the expected behaviour

Should be able to run face landmarker in a module web worker

Standalone code/steps you may have used to try to get what you need

From the main thread:
const myWorker = new Worker("worker.js", { type: "module" });

--------------
worker.js:

import vision from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision/vision_bundle.js";
const { FaceLandmarker, FilesetResolver, DrawingUtils } = vision;

async function createFaceLandmarker() {
  const filesetResolver = await FilesetResolver.forVisionTasks(
    "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.14/wasm"
  );
  faceLandmarker = await FaceLandmarker.createFromOptions(filesetResolver, {
    baseOptions: {
      modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task`,
      delegate: "GPU"
    },
    outputFaceBlendshapes: true,
    runningMode: "IMAGE",
    numFaces: 1
  });
}
createFaceLandmarker();

------

Error message:

vision_bundle.js:1 Uncaught (in promise) TypeError: Failed to execute 'importScripts' on 'WorkerGlobalScope': Module scripts don't support importScripts().
    at o (vision_bundle.js:1:452684)
    at Array.map (<anonymous>)
    at i (vision_bundle.js:1:452810)
    at createTaskRunner (vision_bundle.js:1:454548)
    at TaskRunner.createInstance (vision_bundle.js:1:454769)
    at VisionTaskRunner.createVisionInstance (vision_bundle.js:1:460257)
    at M.createFromOptions (vision_bundle.js:1:596091)
    at createFaceLandmarker (worker.js:10:47)

Other info / Complete Logs

The "runScript" helper function (https://github.com/google-ai-edge/mediapipe/blob/master/mediapipe/web/graph_runner/run_script_helper.ts.template) needs to get updated to support both the classic web workers and module web workers.
kuaashish commented 3 months ago

Hi @Hamed-Hassanzadeh,

Currently, the web worker is not very familiar with our task API. However, we are marking this as a feature request and will share it with the team to ensure it is on our radar. We will consider making it more user-friendly if there is more demand in the near future.

Thank you!!