diff --git a/examples/wasm-emscripten/.gitignore b/examples/wasm-emscripten/.gitignore index 6aa14c7..47380b0 100644 --- a/examples/wasm-emscripten/.gitignore +++ b/examples/wasm-emscripten/.gitignore @@ -1,3 +1,3 @@ libonnxruntime.a -models +yolov8m.onnx emsdk \ No newline at end of file diff --git a/examples/wasm-emscripten/Cargo.toml b/examples/wasm-emscripten/Cargo.toml index afa9d2e..ba8f06c 100644 --- a/examples/wasm-emscripten/Cargo.toml +++ b/examples/wasm-emscripten/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] ort = { path = "../../" } ndarray = "0.16" -rust-embed = { version = "8", features = ["debug-embed"] } # Embed in debug to debug WASM. image = "0.25" [build-dependencies] diff --git a/examples/wasm-emscripten/build.rs b/examples/wasm-emscripten/build.rs index 886a8b2..a28c1a6 100644 --- a/examples/wasm-emscripten/build.rs +++ b/examples/wasm-emscripten/build.rs @@ -14,7 +14,7 @@ fn copy_dir_all(src: impl AsRef, dst: impl AsRef::new(); request.read_to_end(&mut buf).expect("Cannot read model."); - create_dir_all("models").expect("Cannot create models directory."); - let mut file = File::create("models/yolov8m.onnx").expect("Cannot create model file."); + let mut file = File::create("./yolov8m.onnx").expect("Cannot create model file."); file.write_all(&buf).expect("Cannot store model."); } diff --git a/examples/wasm-emscripten/rust-toolchain.toml b/examples/wasm-emscripten/rust-toolchain.toml index 000bf64..bf4cc09 100644 --- a/examples/wasm-emscripten/rust-toolchain.toml +++ b/examples/wasm-emscripten/rust-toolchain.toml @@ -1,4 +1,4 @@ -# Threading requires to compile the standard library with "atomics" and "bulk-memory" features. +# Threading requires to compile the standard library with "+atomics,+bulk-memory,+mutable-globals". # See: https://rustwasm.github.io/wasm-bindgen/examples/raytrace.html#building-the-demo [toolchain] channel = "nightly" diff --git a/examples/wasm-emscripten/src/main.rs b/examples/wasm-emscripten/src/main.rs index cb334da..916613d 100644 --- a/examples/wasm-emscripten/src/main.rs +++ b/examples/wasm-emscripten/src/main.rs @@ -1,10 +1,5 @@ #![no_main] -// Embed models into the .wasm file. -#[derive(rust_embed::RustEmbed)] -#[folder = "models/"] -pub struct Models; - // Below YoloV8 implementation partly copied from the "yolov8" example. #[derive(Debug, Clone, Copy)] struct BoundingBox { @@ -27,17 +22,18 @@ static YOLOV8_CLASS_LABELS: [&str; 80] = [ ]; #[no_mangle] -pub extern "C" fn alloc(capacity: usize) -> *mut std::os::raw::c_void { - let mut buf = Vec::with_capacity(capacity); - let ptr = buf.as_mut_ptr(); - std::mem::forget(buf); - return ptr as *mut std::os::raw::c_void; +pub extern "C" fn alloc(size: usize) -> *mut std::os::raw::c_void { + unsafe { + let layout = std::alloc::Layout::from_size_align(size, std::mem::align_of::()).expect("Cannot create memory layout."); + return std::alloc::alloc(layout) as *mut std::os::raw::c_void; + } } #[no_mangle] -pub extern "C" fn dealloc(ptr: *mut std::os::raw::c_void, capacity: usize) { +pub extern "C" fn dealloc(ptr: *mut std::os::raw::c_void, size: usize) { unsafe { - let _buf = Vec::from_raw_parts(ptr, 0, capacity); + let layout = std::alloc::Layout::from_size_align(size, std::mem::align_of::()).expect("Cannot create memory layout."); + std::alloc::dealloc(ptr as *mut u8, layout); } } @@ -49,8 +45,6 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) { .commit() .expect("Cannot initialize ort."); - let model = Models::get("yolov8m.onnx").expect("Cannot find model.").data.to_vec(); - let session = ort::session::Session::builder() .expect("Cannot create Session builder.") .with_optimization_level(ort::session::builder::GraphOptimizationLevel::Level3) @@ -61,7 +55,7 @@ pub extern "C" fn detect_objects(ptr: *const u8, width: u32, height: u32) { .expect("Cannot set intra thread count.") .with_inter_threads(1) .expect("Cannot set inter thread count.") - .commit_from_memory(&model) + .commit_from_memory(include_bytes!("../yolov8m.onnx")) .expect("Cannot commit model."); let image_data = unsafe { std::slice::from_raw_parts(ptr, (width * height * 4) as usize).to_vec() }; // Copy via .to_vec might be not necessary as memory lives long enough.