google-ai-edge / mediapipe

Cross-platform, customizable ML solutions for live and streaming media.
https://mediapipe.dev
Apache License 2.0
26.78k stars 5.09k forks source link

CanvasRenderingContext2D is not defined in DrawingUtils constructor #5284

Open ddgg-el opened 5 months ago

ddgg-el commented 5 months ago

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

Yes

OS Platform and Distribution

macOS 12.6

MediaPipe Tasks SDK version

npm package version 0.10.12

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

Pose Landmarker

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

Typescript

Describe the actual behavior

Instanciating a DrawingUtils with an OffscreenCanvasRenderingContext2D as cpuContext in Worker returns an Uncaught ReferenceError: CanvasRenderingContext2D is not defined

Describe the expected behaviour

a DrawingUtils is properly Instantiated

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

//index.ts
const canvasCamera = document.getElementById("output_canvas") as HTMLCanvasElement
const offscreenVideoCanvas:OffscreenCanvas = canvasCamera.transferControlToOffscreen()
poseWorker.postMessage({canvas: offscreenVideoCanvas}, [offscreenVideoCanvas])

// pose pose_worker.ts
self.onmessage = (e) => {
    if ("canvas" in e.data ) {
        canvasCamera = e.data.canvas as OffscreenCanvas
    canvasCtxVideo = canvasCamera.getContext("2d") as OffscreenCanvasRenderingContext2D
    painter = new DrawingUtils(canvasCtxVideo)
    }
}

Other info / Complete Logs

Uncaught ReferenceError: CanvasRenderingContext2D is not defined
    at new Ra (vision_bundle.mjs:1:58732)
    at self.onmessage (pose_worker.ts:29:13)
JDBar commented 3 months ago

I've got a goofy workaround, which is to define CanvasRenderingContext2D in the web worker scope.

declare let self: WorkerGlobalScope &
    typeof globalThis & {
        CanvasRenderingContext2D: typeof OffscreenCanvasRenderingContext2D;
    };
self.CanvasRenderingContext2D = OffscreenCanvasRenderingContext2D;

DrawingUtils seems perfectly happy with this.