kalwalt / wasm-akaze-example

A simple test to build a Wasm example with akaze and Rust
https://kalwalt.github.io/wasm-akaze-example/www/
Apache License 2.0
0 stars 0 forks source link

[bug] panic error running in the browser #1

Closed kalwalt closed 3 years ago

kalwalt commented 3 years ago

The code compile fine with wasm-pack build, i don't get any errors, but when i start the browser server i receive this panic error:

Module
wasm_akaze_example_bg.js:197 Hello using web-sys
client:52 [WDS] Live Reloading enabled.
index.js:21 ImageData
wasm_akaze_example_bg.js:197 Init Akaze...
wasm_akaze_example_bg.js:197 DynamicImage...
wasm_akaze_example_bg.js:197 Creating descriptors with akaze...
wasm_akaze_example_bg.js:190 panicked at 'not implemented', /home/walter/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.23.14/./src/dynimage.rs:1097:9

Stack:

Error
    at Module.__wbg_new_59cb74e423758ede (webpack:///../pkg/wasm_akaze_example_bg.js?:176:15)
    at __wbg_new_59cb74e423758ede (http://localhost:8080/bootstrap.js:65:112)
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[62]:0x11a54
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[509]:0x202c2
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[173]:0x1a398
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[225]:0x1c57a
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[377]:0x1f772
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[351]:0x1f279
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[379]:0x1f7d6
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[336]:0x1eeb6

__wbg_error_4bb6c2a97407129a @ wasm_akaze_example_bg.js:190
2e4d40f70078903b6e58.module.wasm:0x1a3cb Uncaught RuntimeError: unreachable
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[173]:0x1a3cb
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[225]:0x1c57a
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[377]:0x1f772
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[351]:0x1f279
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[379]:0x1f7d6
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[336]:0x1eeb6
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[102]:0x15ab2
    at http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[46]:0xf7ed
    at open_from_buffer (http://localhost:8080/2e4d40f70078903b6e58.module.wasm:wasm-function[251]:0x1d0ad)
    at Module.open_from_buffer (webpack:///../pkg/wasm_akaze_example_bg.js?:79:81)
:8080/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)
DevTools failed to load SourceMap: Could not load content for webpack:///node_modules/sockjs-client/dist/sockjs.js.map: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME

Look at this line: panicked at 'not implemented', /home/walter/.cargo/registry/src/github.com-1ecc6299db9ec823/image-0.23.14/./src/dynimage.rs:1097:9 What does it means?

kalwalt commented 3 years ago

that error refer to this line of code in dynimage.rs:

// DEPRECATED: Do not use is function: It is unimplemented!
    fn get_pixel_mut(&mut self, _: u32, _: u32) -> &mut color::Rgba<u8> {
        unimplemented!()
    }

can not understand why this happens, since in my code i'm not using this function.

kalwalt commented 3 years ago

if i remove this line drawing::draw_cross_mut(&mut image, Rgba([0, 255, 255, 128]), x as i32, y as i32); from the compilation i don't get that panic error.

kalwalt commented 3 years ago

@vadixidav i'm trying to convert the final image to raw data (Vec):

#[wasm_bindgen]
pub fn open_from_buffer(buf: Vec<u8>, width: u32, height: u32) -> Vec<u8> {
    utils::set_panic_hook();
    let src_image = ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(width, height, buf).unwrap();
    let threshold = 0.001f64;
    let akaze = Akaze::new(threshold);
    console::log_1(&"Init Akaze...".into());
    let dyn_image = DynamicImage::ImageRgba8(src_image.clone());
    console::log_1(&"DynamicImage...".into());
    let (key_points, _descriptor) = akaze.extract(&dyn_image);
    console::log_1(&"Creating descriptors with akaze...".into());
    let mut image = drawing::Blend(src_image);
    for KeyPoint { point: (x, y), .. } in key_points {
        drawing::draw_cross_mut(&mut image, Rgba([0, 255, 255, 128]), x as i32, y as i32);
    }

    let out_image = DynamicImage::ImageRgba8(&mut image);
    out_image.into_vec()
}

but it is not correct because:

wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling wasm-akaze-example v0.1.0 (/home/walter/kalwalt-github/wasm-akaze-example)
warning: unused import: `image::io::Reader as ImageReader`
 --> src/lib.rs:6:5
  |
6 | use image::io::Reader as ImageReader;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0308]: mismatched types
  --> src/lib.rs:34:46
   |
34 |     let out_image = DynamicImage::ImageRgba8(&mut image);
   |                                              ^^^^^^^^^^ expected struct `ImageBuffer`, found mutable reference
   |
   = note:         expected struct `ImageBuffer<Rgba<u8>, Vec<u8>>`
           found mutable reference `&mut Blend<ImageBuffer<Rgba<u8>, Vec<u8>>>`

error[E0599]: no method named `into_vec` found for enum `DynamicImage` in the current scope
  --> src/lib.rs:35:15
   |
35 |     out_image.into_vec()
   |               ^^^^^^^^ method not found in `DynamicImage`

error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `wasm-akaze-example`

To learn more, run the command again with --verbose.
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown
vadixidav commented 3 years ago

@vadixidav i'm trying to convert the final image to raw data (Vec):

#[wasm_bindgen]
pub fn open_from_buffer(buf: Vec<u8>, width: u32, height: u32) -> Vec<u8> {
    utils::set_panic_hook();
    let src_image = ImageBuffer::<Rgba<u8>, Vec<u8>>::from_vec(width, height, buf).unwrap();
    let threshold = 0.001f64;
    let akaze = Akaze::new(threshold);
    console::log_1(&"Init Akaze...".into());
    let dyn_image = DynamicImage::ImageRgba8(src_image.clone());
    console::log_1(&"DynamicImage...".into());
    let (key_points, _descriptor) = akaze.extract(&dyn_image);
    console::log_1(&"Creating descriptors with akaze...".into());
    let mut image = drawing::Blend(src_image);
    for KeyPoint { point: (x, y), .. } in key_points {
        drawing::draw_cross_mut(&mut image, Rgba([0, 255, 255, 128]), x as i32, y as i32);
    }

    let out_image = DynamicImage::ImageRgba8(&mut image);
    out_image.into_vec()
}

but it is not correct because:

wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling wasm-akaze-example v0.1.0 (/home/walter/kalwalt-github/wasm-akaze-example)
warning: unused import: `image::io::Reader as ImageReader`
 --> src/lib.rs:6:5
  |
6 | use image::io::Reader as ImageReader;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0308]: mismatched types
  --> src/lib.rs:34:46
   |
34 |     let out_image = DynamicImage::ImageRgba8(&mut image);
   |                                              ^^^^^^^^^^ expected struct `ImageBuffer`, found mutable reference
   |
   = note:         expected struct `ImageBuffer<Rgba<u8>, Vec<u8>>`
           found mutable reference `&mut Blend<ImageBuffer<Rgba<u8>, Vec<u8>>>`

error[E0599]: no method named `into_vec` found for enum `DynamicImage` in the current scope
  --> src/lib.rs:35:15
   |
35 |     out_image.into_vec()
   |               ^^^^^^^^ method not found in `DynamicImage`

error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `wasm-akaze-example`

To learn more, run the command again with --verbose.
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown

I believe you need to use image.into_raw(). You don't need to create a DynamicImage to get the raw data.

kalwalt commented 3 years ago

@vadixidav simply fixed with: image.0.into_raw() note the 0 to access the ImageBuffer i think instead of the Blend. in the console now i get the raw data:

DevTools failed to load SourceMap: Could not load content for webpack:///node_modules/sockjs-client/dist/sockjs.js.map: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME
index.js:5 Module {…}
wasm_akaze_example_bg.js:212 Hello using web-sys
client:52 [WDS] Live Reloading enabled.
index.js:21 ImageData {data: Uint8ClampedArray(13410304), width: 1637, height: 2048}
wasm_akaze_example_bg.js:212 Init Akaze...
wasm_akaze_example_bg.js:212 DynamicImage...
wasm_akaze_example_bg.js:212 Creating descriptors with akaze...
index.js:23 Uint8Array(13410304) [37, 29, 27, 255, 37, 29, 27, 255, 36, 28, 26, 255, 36, 28, 26, 255, 36, 28, 26, 255, 37, 29, 27, 255, 38, 30, 28, 255, 39, 31, 29, 255, 41, 33, 31, 255, 40, 32, 30, 255, 38, 30, 28, 255, 36, 28, 26, 255, 35, 27, 25, 255, 35, 27, 25, 255, 36, 28, 26, 255, 37, 29, 27, 255, 38, 34, 31, 255, 36, 32, 29, 255, 33, 29, 26, 255, 32, 28, 25, 255, 33, 29, 26, 255, 35, 31, 28, 255, 36, 32, 29, 255, 37, 33, 30, 255, 32, 28, 25, 255, …][0 … 999999][0 … 9999][0 … 99]0: 371: 292: 273: 2554: 375: 296: 277: 2558: 369: 2810: 2611: 25512: 3613: 2814: 2615: 25516: 3617: 2818: 2619: 25520: 3721: 2922: 2723: 25524: 3825: 3026: 2827: 25528: 3929: 3130: 2931: 25532: 4133: 3334: 3135: 25536: 4037: 3238: 3039: 25540: 3841: 3042: 2843: 25544: 3645: 2846: 2647: 25548: 3549: 2750: 2551: 25552: 3553: 2754: 2555: 25556: 3657: 2858: 2659: 25560: 3761: 2962: 2763: 25564: 3865: 3466: 3167: 25568: 3669: 3270: 2971: 25572: 3373: 2974: 2675: 25576: 3277: 2878: 2579: 25580: 3381: 2982: 2683: 25584: 3585: 3186: 2887: 25588: 3689: 3290: 2991: 25592: 3793: 3394: 3095: 25596: 3297: 2898: 2599: 255[100 … 199][200 … 299][300 … 399][400 … 499][500 … 599][600 … 699][700 … 799][800 … 899][900 … 999][1000 … 1099][1100 … 1199][1200 … 1299][1300 … 1399][1400 … 1499][1500 … 1599][1600 … 1699][1700 … 1799][1800 … 1899][1900 … 1999][2000 … 2099][2100 … 2199][2200 … 2299][2300 … 2399][2400 … 2499][2500 … 2599][2600 … 2699][2700 … 2799][2800 … 2899][2900 … 2999][3000 … 3099][3100 … 3199][3200 … 3299][3300 … 3399][3400 … 3499][3500 … 3599][3600 … 3699][3700 … 3799][3800 … 3899][3900 … 3999][4000 … 4099][4100 … 4199][4200 … 4299][4300 … 4399][4400 … 4499][4500 … 4599][4600 … 4699][4700 … 4799][4800 … 4899][4900 … 4999][5000 … 5099][5100 … 5199][5200 … 5299][5300 … 5399][5400 … 5499][5500 … 5599][5600 … 5699][5700 … 5799][5800 … 5899][5900 … 5999][6000 … 6099][6100 … 6199][6200 … 6299][6300 … 6399][6400 … 6499][6500 … 6599][6600 … 6699][6700 … 6799][6800 … 6899][6900 … 6999][7000 … 7099][7100 … 7199][7200 … 7299][7300 … 7399][7400 … 7499][7500 … 7599][7600 … 7699][7700 … 7799][7800 … 7899][7900 … 7999][8000 … 8099][8100 … 8199][8200 … 8299][8300 … 8399][8400 … 8499][8500 … 8599][8600 … 8699][8700 … 8799][8800 … 8899][8900 … 8999][9000 … 9099][9100 … 9199][9200 … 9299][9300 … 9399][9400 … 9499][9500 … 9599][9600 … 9699][9700 … 9799][9800 … 9899][9900 … 9999][10000 … 19999][20000 … 29999][30000 … 39999][40000 … 49999][50000 … 59999][60000 … 69999][70000 … 79999][80000 … 89999][90000 … 99999][100000 … 109999][110000 … 119999][120000 … 129999][130000 … 139999][140000 … 149999][150000 … 159999][160000 … 169999][170000 … 179999][180000 … 189999][190000 … 199999][200000 … 209999][210000 … 219999][220000 … 229999][230000 … 239999][240000 … 249999][250000 … 259999][260000 … 269999][270000 … 279999][280000 … 289999][290000 … 299999][300000 … 309999][310000 … 319999][320000 … 329999][330000 … 339999][340000 … 349999][350000 … 359999][360000 … 369999][370000 … 379999][380000 … 389999][390000 … 399999][400000 … 409999][410000 … 419999][420000 … 429999][430000 … 439999][440000 … 449999][450000 … 459999][460000 … 469999][470000 … 479999][480000 … 489999][490000 … 499999][500000 … 509999][510000 … 519999][520000 … 529999][530000 … 539999][540000 … 549999][550000 … 559999][560000 … 569999][570000 … 579999][580000 … 589999][590000 … 599999][600000 … 609999][610000 … 619999][620000 … 629999][630000 … 639999][640000 … 649999][650000 … 659999][660000 … 669999][670000 … 679999][680000 … 689999][690000 … 699999][700000 … 709999][710000 … 719999][720000 … 729999][730000 … 739999][740000 … 749999][750000 … 759999][760000 … 769999][770000 … 779999][780000 … 789999][790000 … 799999][800000 … 809999][810000 … 819999][820000 … 829999][830000 … 839999][840000 … 849999][850000 … 859999][860000 … 869999][870000 … 879999][880000 … 889999][890000 … 899999][900000 … 909999][910000 … 919999][920000 … 929999][930000 … 939999][940000 … 949999][950000 … 959999][960000 … 969999][970000 … 979999][980000 … 989999][990000 … 999999][1000000 … 1999999][2000000 … 2999999][3000000 … 3999999][4000000 … 4999999][5000000 … 5999999][6000000 … 6999999][7000000 … 7999999][8000000 … 8999999][9000000 … 9999999][10000000 … 10999999][11000000 … 11999999][12000000 … 12999999][13000000 … 13410303]__proto__: TypedArray

Thank you very much!

kalwalt commented 3 years ago

Screenshot_wasm_akaze_example_20210317_165120 @vadixidav on the left the original pinball image, on the right the same image from the wasm file with the descriptors, with thiny blu points. It works perfectly!

vadixidav commented 3 years ago

@kalwalt Excellent! Let me know if you run into any more issues. I may use some of the source here (with attribution, of course) in a tutorial for Rust CV to do WASM interaction.

kalwalt commented 3 years ago

That is fantastic! This was only a test, starting point for a more complex idea: to develop a library for WebAR for image tracking detection. We, (me, @ThorstenBux and other guys) have founded a little github org here WebARKit. I think that developing WASM libraries with Rust :crab: is much simpler than transpling code from C/C++ with Emscripten. Of course i'm still learning Rust but i think it worth the time spent to this new language. I think i will make other questions related to rust-cv in the near future... :smile:

kalwalt commented 3 years ago

Related to this example: i would host a Live example with gh-pages, but i think it won't work with the webpack server...

kalwalt commented 3 years ago

I changed the code, now webpack is not required and the example can be displayed on gh-pages: https://kalwalt.github.io/wasm-akaze-example/www/

kalwalt commented 3 years ago

Closing because the bug is solved.