-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Emscripten Port #264
Comments
Well, what's the point of that? It easier to write code like this on MatLab, but it must be in C++ because C and C++ compile to very efficient code. Javascript is the opposite, it is very slow. ORB-SLAM2 on javascript could process 30 frames per year :) |
I suppose javascript does not necessarily have to be the compile target: http://webassembly.org/ |
@Introvertuous , thank you, now I see what you meant. It would be great to create a repo devoted to this goal. Right now I prefer to know if that compiler can compile C++ with STL and opencv. I believe emscripten can't use dependencies, you have to add all dependencies in one big project (including opencv, for example). I don't know if that is even possible. |
@Introvertuous, opencv now has opencv.js https://docs.opencv.org/master/d5/d10/tutorial_js_root.html One step closer to port ORB-SLAM2 to webassembly :) |
@Introvertuous Any update / progress on this? |
As far as I know, noone is working on this. |
Experimenting with getting ORB_SLAM3 working on emscripten. See https://github.com/nickw1/ORB_SLAM3, this compiles on emscripten (the library, but not the examples which depend on opencv features not available in opencv.js) Have started experimenting with creating an application using it, see https://github.com/nickw1/orb-slam-expts/ ... it compiles, but loading the vocabulary is very slow, as in, more than 10 minutes (at which point I gave up), Maybe the file I/O in emscripten is too slow? Would be interested if anyone has any hints for getting this working. |
@nickw1, that's great! There are many binary implementation for vocabulary: voc file is smaller, and loads 100x faster. I don't know any standalone project on binary voc, but many orb-slam2 forks implemented their own very similar binary voc. My own is here, look for loadFromBinaryFile method to see how it is implemented on orb-slam2, and the binary voc file: |
@AlejandroSilvestri thanks! As it happens that is precisely what I have been working on this morning, having discovered this late yesterday :-) I found this one: https://github.com/surfii3z/ORB_SLAM3. Looks like this was originally done for ORB_SLAM2 and was ported to ORB_SLAM3. Thanks for letting me know of your own implementation. |
@AlejandroSilvestri to follow up... with the binary file the vocabulary now loads successfully in 2 seconds on emscripten. Haven't tested whether tracking works yet. Thanks again! |
I am very interested in following up on your project. Please continue posting here or tell me where. |
@AlejandroSilvestri thanks. and https://github.com/nickw1/ORB_SLAM3 (my fork, the For both repos, I will keep a log of changes made, and experiments done, in the README. |
Sorry, typo in first repo, note it's https://github.com/nickw1/orb-slam-expts |
Making some progress on this. However, as soon as reconstruction with two views occurs, I get an unaligned memory access error. Emscripten can't handle unaligned memory access: is this something that ORB-SLAM3 is doing? @AlejandroSilvestri any ideas? Thanks. Unfortunately the debugging from Emscripten can't pinpoint the actual line, so this will require more digging... but just wondered whether this was a known issue in ORB-SLAM3. |
Output from log:
|
@nickw1 WebAssembly does not allow misaligned memory read/writes -- Well, thanks, duh. Here's the problem: If you have a type bigger than one byte (e.g. Most memory allocated with struct MisalignedA {
char something;
short notAlignedProperly;
char something;
int alignedProperly; // it's address will be the address of the struct instance + 4 bytes, which will again be a multiple of 4 bytes
};
struct MisalignedB {
short alignedProperly;
char something;
};
MisalignedB array[2];
array[1].alignedProperly = 0u; // Error! This is misaligned in element 0, since the address will be array + 3 byes, which is a non-even address! You can make use of Good luck with the rest of your porting efforts, it would be absolutely amazing to have SLAM accessible in the browser! |
@Squareys thanks for your explanation... as it happened I had read up on this some months ago so was aware of the nature of misalignment, just wondering if any of the developers could give some hints on where it would be happening. Moving on to a more general question, not sure if anyone can give any input on this. I am now able to test this on a real mobile device; before some recent updates, something was causing Chrome to crash (for all Emscripten/WASM code) when remote debugging was attempted. Now this has been fixed. My main problem though is trying to initialise the tracking, it seems to be proving very difficult to find two key frames with enough corresponding points. I have tried translating and rotating the mobile device in a room with plenty of recognisable 'features', and good lighting but little success. I've used the camera parameters from this ORB-SLAM2 on Android project. Any tips from anyone? Also (regarding the feasibility of mobile tracking) does anyone have any comment on ORB-SLAM3 vs ORB-SLAM2 for this? Wondering whether to try ORB-SLAM2, particularly given that @Martin20150405 has got a native Android app working with ORB-SLAM2. Sorry if that's a lot of questions but it would be good to hear any tips from people who might have tried running ORB-SLAM on mobile devices with the live camera feed already. Thanks! |
Hi @nickw1 Glad to hear you are making progress! Camera calibrationYou can't initialize the map because you need your own camera parameters, both intrinsics matrix and distortion coefficients. OpenCV tutorial has many camera calibration codes, in C++ and Python, sadly they don't have one in JS. https://docs.opencv.org/4.5.5/d4/d94/tutorial_camera_calibration.html One of my students did an online interactive camera calibration with opencv.js, sadly it's not online, you must install it on your own server to use it: https://github.com/frank-117/camera-calibration Aligned memoryC++ aligns memory by default. It would be great to pinpoint the exact line of code that is causing that missalignment error when tracking starts. It would be greater if you post the steps to get your orb-slam3 compiled to wasm with emscripten. For example, it helps to track if your compiler is using at least c++11 and automatically aligning objects. (I believe it is.) |
Hi @AlejandroSilvestri thanks for your hints - the links you posted with calibration will help a great deal. Quite happy with converting C++ or Python code to JS but your student's work looks particularly interesting - will take a look. Will try and put some more comprehensive instructions on how I'm compiling orb-slam3 but basically it's with an emscripten from last year so it should be using recent C++ standards. |
To follow up on this, my modified orb-slam3, which builds with emscripten, is available at https://github.com/nickw1/ORB_SLAM3/tree/binvoc. Specifically it's the |
Bit of an update on this. Unfortunately I have hit a bit of a brick wall with tracking, have tried both the calibrator suggested above by @AlejandroSilvestri and also the official Android OpenCV calibrator sample to try and get my device's parameters, but still no joy initialising tracking. It would be great if anyone had any hints on this. In the meantime though I have tried to get ORB_SLAM3 working on Android, see UZ-SLAMLab/ORB_SLAM3#77. |
Hi @nickw1,
|
@carlodek sounds good! Unfortunately I don't have any suggestions to speed-up re-recognition: I am not one of the authors of ORB-SLAM, it would be better to ask one of them (e.g. @AlejandroSilvestri). I am just attempting to get it working in WASM/Emscripten and native Android environments. My current problem is still to achieve tracking on a mobile device, I have tried different calibrators to get camera parameters but I still have no luck. Which calibrator did you use to get the parameters? Did you have any problems here? (I also have the memory alignment issue to solve - see above - did you have this problem? But that will probably be easier to solve once I get tracking working). |
Hi @nickw1 ,
@AlejandroSilvestri Hi, |
Hi @carlodek thanks. Thanks for your build options, will try out those as mine were slightly different. What about your camera parameters (camera intrinsics, as defined in the .yaml files)? I am also experimenting with ORB-SLAM3 on Android, see https://github.com/nickw1/orb-slam3-android-expts. My approach has been slightly different: I have used the OpenCV Android SDK to create a Kotlin front end, loaded each camera frame into a Mat, and sent the address of that to the native back-end. I have got it building and running but getting a strange crash on a method call, perhaps due to memory corruption of some kind causing the stack to be corrupted? (I am not sure, I have done extensive Android development but I am new to the NDK). Do you have any ideas on that? (This isn't really the place to discuss this though, it's best to move to UZ-SLAMLab/ORB_SLAM3#77). |
Hi @nickw1 , #-------------------------------------------------------------------------------------------- Camera Parameters. Adjust them!#-------------------------------------------------------------------------------------------- 220610 CURRENT PARAMETERS ARE FROM THE OFFICIAL OPENCV ANDROID CALIBRATOR SAMPLE APPCamera.type: "PinHole" Parameters from ptam, possibly from ThorstenmgvvCameraParams[0] = 1.59328; - for focal lengthmgvvCameraParams[1] = 2.11149;mgvvCameraParams[2] = 0.512158; - for centermgvvCameraParams[3] = 0.436717;mgvvCameraParams[4] = 0.961982;Calculations from ptam// First: Focal length and image center in pixel coordinatesmvFocal[0] = mvImageSize[0] * mgvvCameraParams[0];mvFocal[1] = mvImageSize[1] * mgvvCameraParams[1];mvCenter[0] = mvImageSize[0] * mgvvCameraParams[2] - 0.5;mvCenter[1] = mvImageSize[1] * mgvvCameraParams[3] - 0.5;Camera calibration and distortion parameters (OpenCV)Used calculations above with ptam parameters to work them outCamera.fx: 458.654Camera.fx: 1029.69920Camera.fx: 1034.0 Camera.fy: 457.296Camera.fy: 1013.51520Camera.fy: 1034.0 Camera.cx: 367.215Camera.cx: 327.281120Camera.cx : 640.0 Camera.cy: 248.375Camera.cy: 209.124160Camera.cy : 384.0 Distortion parameters. Not sure about these, are not specified in ptam in the same way, leave for nowCamera.k1: -0.28340811Camera.k2: 0.07395907Camera.p1: 0.00019359Camera.p2: 1.76187114e-05Camera.k1: 0.3164 Camera.width: 640Camera.height: 480Camera.width: 640 Camera frames per secondCamera.fps: 20.0Camera.fps: 10.0 Color order of the images (0: BGR, 1: RGB. It is ignored if images are grayscale)Camera.RGB: 1 #-------------------------------------------------------------------------------------------- ORB Parameters#-------------------------------------------------------------------------------------------- ORB Extractor: Number of features per imageORBextractor.nFeatures: 1000 ORB Extractor: Scale factor between levels in the scale pyramidORBextractor.scaleFactor: 1.2 ORB Extractor: Number of levels in the scale pyramidORBextractor.nLevels: 8 ORB Extractor: Fast thresholdImage is divided in a grid. At each cell FAST are extracted imposing a minimum response.Firstly we impose iniThFAST. If no corners are detected we impose a lower value minThFASTYou can lower these values if your images have low contrastORBextractor.iniThFAST: 20 #-------------------------------------------------------------------------------------------- Viewer Parameters#--------------------------------------------------------------------------------------------- About android porting problem, which method cause the crash? And secondly, do you got the same crash on a real android device? |
Hi @carlodek ok thanks, yes these look like mine unchanged. The crash is on a real device. The stack trace is here: https://github.com/nickw1/orb-slam3-android-expts/blob/master/crashes.txt It occurs when the However I cannot step into the |
Hi @nickw1 , |
@carlodek ok thanks for that, you could be right about doing the whole lot in native Android. |
No, I'm not! Glad to answer if I can, but I didn't work with Raul Mur, the main author of ORB-SLAM and ORB-SLAM2. |
All ORB-SLAM (the three of them) aim to speed. It's impossible to speed them up by tweaking. There were many visual slam alternatives based on orb-slam, with variations. None of them really improved speed. |
Did you succeeded at camera calibration? Here are some tips for a cellphone camera:
|
For performance, keep image resolution at hd, don't use 50 Mpixels images. I believe you already know this. Native can be a lot better than wasm, and easier to migrate. nickw1, what lag do you have by sending images to server? Some students and me are extracting keypoints and descriptors at the cellphone, and sending them to the server to reduce lag and bandwidth, and it works very well. But we aren't working with orb-slam3 on that one. |
Hi @AlejandroSilvestri , thanks a lot for tricks about camera, wasm is really faster on my mac. Now I will try tricks for android. |
OK sorry! I assumed you were, many apologies. |
@AlejandroSilvestri not yet, unfortunately. I tried both the calibrator you linked to above, and the "official" OpenCV Android calibrator sample using print-outs of the "official" OpenCV calibrator patterns. Thank you for the tips though! :-) |
Thanks for the tips. As I mentioned above, I am also experimenting with native. I believe that there were some possible memory alignment issues in Eigen, could this be a potential problem on a different platform? |
Don't worry, on the contrary, I feel honored! |
Hi @nickw1, |
@carlodek thanks! |
I have now got some more detailed debug information on the memory alignment issue, this is on a desktop (still struggling to get any initialisation occurring on a mobile device). It's a threading/synchronisation issue by the looks of things. The line throwing the alignment error appears to be a line in
Stack trace is:
I'm a bit puzzled as to why creating a lock would cause an unaligned access issue. Anyone got any ideas? Thanks. @carlodek is your code available (as you are not getting this error, just wondering if I could try yours and see if I get the same result). If it's closed-source / otherwise not available though, no worries! |
Hi @nickw1, //Initialize the Tracking thread |
@carlodek OK thanks, will give that a try. |
@carlodek have just tried that flag. Seems to have fixed it! Many thanks! :-) Puzzled as to why I got an "unaligned access" error, though; from my understanding this flag just makes the browser more responsive by running |
Hi @nickw1 , |
How many people are aware of this? https://github.com/alanross/AlvaAR Looks like it is a working project - it has included ORB-SLAM plus other projects to produce working web AR. |
Thank you for the link. AlvaAR uses OV2SLAM, I'll read it. |
I was wondering if there is any interest in a javascript port of this library through emscripten, or if that is something you guys have possibly looked into.
The text was updated successfully, but these errors were encountered: