#computer-vision #vision #wasi #wasm-interface #opencv

wasmcv

Rust bindings for wasmCV computer vision interfaces based on WebAssembly

7 releases (breaking)

0.6.0 Oct 16, 2024
0.5.0 Oct 9, 2024
0.4.0 Oct 4, 2024
0.3.1 Sep 27, 2024
0.1.0 Sep 16, 2024

#641 in WebAssembly

Download history 205/week @ 2024-09-13 61/week @ 2024-09-20 253/week @ 2024-09-27 279/week @ 2024-10-04 146/week @ 2024-10-11 36/week @ 2024-10-18 1/week @ 2024-10-25

68 downloads per month

Apache-2.0

170KB
3.5K SLoC

wasmCV

Rust bindings for wasmCV WebAssembly interfaces to computer vision systems.

See https://github.com/wasmvision/wasmcv for information about wasmCV.

Package

How to use

This example Rust module exports a process() function to the WASM host application. When the host calls the processor, it passes in the wasmCV image Mat to be processed. The wasmCV module then calls functions on that Mat which are handled by the host application, by calling OpenCV to actually perform the operations.

#![no_std]

extern crate core;
extern crate wee_alloc;
extern crate alloc;
extern crate wasmcv;

use alloc::string::String;
use alloc::string::ToString;
use wasmcv::wasm::cv;

#[no_mangle]
pub extern fn process(mat: cv::mat::Mat) -> cv::mat::Mat {
    println(&["Cols: ", &mat.cols().to_string(), " Rows: ", &mat.rows().to_string()].concat());

    return mat;
}

/// Print a message to the host [`_println`].
fn println(message: &String) {
    unsafe {
        let (ptr, len) = string_to_ptr(message);
        _println(ptr, len);
    }
}

#[link(wasm_import_module = "hosted")]
extern "C" {
    #[link_name = "println"]
    fn _println(ptr: u32, size: u32);
}

unsafe fn string_to_ptr(s: &String) -> (u32, u32) {
    return (s.as_ptr() as u32, s.len() as u32);
}

// Use `wee_alloc` as the global allocator...for now.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

You can then compile this module using the Rust compiler.

cargo build --target wasm32-unknown-unknown --release

The wasm32-unknown-unknown target can be used with wasmCV to produce very lightweight guest modules when combined with no_std. The example above compiles to around 14k, including debug information.

-rwxrwxr-x 1 ron ron 14488 sep 12 14:23 processrs.wasm

Dependencies

~160KB