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

ModuleFactory overwrites global "Module" variable thereby interfering with other libraries #5135

Open awmartin opened 9 months ago

awmartin commented 9 months ago

Found in code here: https://github.com/google/mediapipe/blob/3a93a5d5d3f90ba3863f14ef4b0a4fc01505d035/mediapipe/web/graph_runner/graph_runner.ts#L929

I'm not sure how the ModuleFactory is defined, but when the build generates the vision_wasm_internal.js file for hand landmark tracking, that code will overwrite the Module variable in the global scope that may be consumed by other libraries. Seen here:

Module overwritten in vision_wasm_interna

One such conflicting library is the WASM version of whisper.cpp, linked below, which also uses a global "Module" variable both in this index file and other included JavaScript files: https://github.com/ggerganov/whisper.cpp/blob/02b4c52c1289e05c8c04ff8370a4835b8ee99c86/examples/whisper.wasm/index-tmpl.html#L247

I'm also not quite sure of the resolution, but global namespace collisions are certainly possible with names as simple as "Module", but I'm also not sure whether this "Module" is a standard and thus needs to be accounted for with any WASM module (particularly when using emscripten), or if this is truly a coincidence. Or is there a way to pass Module variable into the ModuleFactory, say from createFromOptions and the like?

Regardless, it means in this case that whisper.cpp and MediaPipe can't be used together, but again, I'm unclear whether this will be a common collision or just a peculiar one-off.

awmartin commented 9 months ago

Just adding a note that I found that the Module variable is an emscripten construct. Learning as I go. But it still stands that the MediaPipe implementation somehow sets this to "undefined" in the process of starting the vision task. Still looking.

kuaashish commented 9 months ago

Hi @schmidt-sebastian,

Could you please have look into this issue? Thank you!!