-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting C++ code with MediaPipe library into Web Assembly #877
Comments
I tried to it by getting the .bazelrc and WORKSPACE from this blog: https://hackernoon.com/c-to-webassembly-using-bazel-and-emscripten-4him3ymc. I can produce Web Assembly code for simple cc code with no dependencies. However, I am stuck with 2 types of errors based on what MediaPipe dependency I include in the BUILD file. Here is my .bazelrc:
I turned off the optimization flags Here is the WORKSPACE:
In particular, I added the lines in the end:
The contents of //javascript/toolchain:cc_toolchain_config.bzl:
And the dummy cc file:
And the BUILD file used for
The The command I used for
Now, if I do not include the calculators dependency then I get this main error related to
If I include the calculators dependency then I get the following error:
I do not have any idea about how to solve the errors |
I was experimenting this again and stumbled onto the following error:
and found a prospective solution which requires using a specific commit of GLog. I was thinking how to add the commit update locally. |
@prantoran Were you able to fix this? Also the web version seems to be loading a |
@droidlabour I have not made any new attempts. Last time, I was stuck with GLog version in build files which was later updated in MediaPipe. Currently, I am learning about wasm and pthreads, after which I will attempt again. Not sure about |
@prantoran , I am also stuck at the same, I guess you should try again as MediaPipe launched a release and you already explored a lot. best of luck in advance. |
@vKrypto It is harder than it seems to be, currently I am doing the desktop version but trying to port to web-assembly. I faced the pthread issue, as far as I know the current web-assembly version do not support multithreading natively and I am not sure whether XNNPACK web-assembly version or TensorflowJS was used for web version. There is a MediaPipe pre-trained handpose model available as npm package that uses TensorflowJS so I guess TensorflowJS is compatible. |
Hi all-- this is quite a tricky task, so expect to run into a lot of strange build errors along the way. However, a few tips that might be helpful: Also, yes, the .data file is for packaging up assets to load at runtime, and is tied to the Emscripten virtual FileSystem. So don't worry too much about that, since it won't really be relevant until you have things building. |
Hi, has anyone made progress building Mediapipe for Javascript? |
Hi @prantoran, Did you get a chance to go through this comment |
@sgowroji I saw the comment, I am working on it. I did make some progress by gradually adding dependencies and fixing bugs. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you. |
Closing as stale. Please reopen if you'd like to work on this further. |
I merged all the changes that seemed essential to me in a single commit on the latest version of MediaPipe: https://github.com/prantoran/mediapipe/tree/latest_wasm. The |
@prantoran @tyrmullen could you tell :
MY question:
Thanks a lot! |
@LebronJames0423 In the latest branch of the project, all the changes in the MediaPipe's internal files are shown in the commits. One caveat is that I developed using Ubuntu so there might be additional requirements for Windows. |
@LebronJames0423 For any of our APIs running on GPU, you can instead force CPU ML inference by setting the |
|
@prantoran By refering to your repo, I was able to compile the hello world desktop into webassembly. I was able to load the wasm file in chrome and the cout commands in the cpp file were appearing in the browser console as well. However, the wasm hangs at poller.next(&packet), as is evident from observing the debug flags I had put in the cpp code from the console. I did some digging in the internet and found out that it is hanging because poller.next() waits until the next packet is available. The same code is working properly when run from the terminal as a cpp file but upon converting to wasm something is not working. It would be great if someone could provide more information on why the code behaves as such upon being converted to wasm. build file for cpp helloworld
cpp helloworld code that runs properly
output as expected in the terminal build file for the wasm helloworld:
cpp code used to compile the wasm file:
the output from chrome console: The chrome tab hangs and will need multiple clicks to close upon loading the wasm file. I guess it is not throwing any errors since packet.next() function is behaving as it is intended to behave, however, I would like to understand why the packets aren't in the queue like it is in the cpp example when run in the terminal. |
WebAssembly by default is single-threaded, while in your terminal cpp example, it is almost certainly multi-threaded. Therefore, any statements which "wait" for something to happen from another thread will not work. Instead of using OutputStreamPoller, try using ObserveOutputStream instead. |
@tyrmullen Hi, thank you for the suggestion. Following your advice I tried to modify the code and compiled it to wasm. This time it is not hanging when it is loaded in chrome. However I'm not getting the "hello world" strings either. I might be doing something silly here.
|
Try moving ObserveOutputStream to before GraphStart to make sure it's connecting, and add a WaitUntilIdle where you used to have WaitUntilDone. WaitUntilIdle will not actually wait by default on web (despite the name), but rather will force all processing to occur inline; otherwise, you're just queueing up input packets without actually running them through the graph. You may also want to move the |
@tyrmullen That seems to be what was causing the problems. It is showing the outputs correctly in chrome console now. Thanks again. |
Thanks for your valuable comments @tyrmullen and your work @prantoran |
@Hrituraj202 I was saying that non-web C++ CPU should be faster than WebAssembly CPU. If you're looking just at web apps, then GPU will usually be faster than CPU (although CPU lends itself better to multithreading, so it's not as straightforward a comparison), so most users will not want to use the |
@JonathanGoorBlink I don't know about all the modules that use OpenCV, but I was able to compile the box tracking cpu module which uses OpenCV to WebAssembly following the information in this thread. My cloned repo is heavily modified but I think it might be useful. |
Can u do a bootstrap code to use box tracking cpu js ? |
This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you. |
This issue was closed due to lack of activity after being marked stale for past 7 days. |
I organized my project in the same workspace as MediaPipe and build my project with Bazel. Now I am looking into converting my project into a wasm Web Assembly file so that I can import it on a static website.
I thought about directly converting using Ecmscript but not sure how to efficiently include all the dependencies of MediaPipe present in the MediaPipe's WORKSPACE file.
I am also wondering how to use Ecmscript when building the MediaPipe project using Bazel.
Also, is it better to include MediaPipe as a separate dependency and organize my project from scratch with a separate WORKSPACE file? If so then how to include MediaPipe as a dependency?
Is there any code demo where MediaPipe example is converted to Web Assembly?
I am new to Bazel and Ecmscript so the questions might seem obvious ^_^
The text was updated successfully, but these errors were encountered: