[WIP] Improve Yolo WASM UI example (#591)

* return detections with classes names

* ignore .DS_Store

* example how to load wasm module

* add param to set model size

* add param for model size

* accept iou and confidence threshold on run

* conf and iou thresholds

* clamp only

* remove images from branch

* a couple of renamings, add readme with instructions

* final design

* minor font + border update
This commit is contained in:
Radamés Ajna
2023-08-26 03:40:41 -07:00
committed by GitHub
parent b23b347b35
commit 864227edbf
10 changed files with 604 additions and 23 deletions

View File

@ -1,3 +1,5 @@
use candle_wasm_example_yolo::coco_classes;
use candle_wasm_example_yolo::model::Bbox;
use candle_wasm_example_yolo::worker::Model as M;
use wasm_bindgen::prelude::*;
@ -9,15 +11,36 @@ pub struct Model {
#[wasm_bindgen]
impl Model {
#[wasm_bindgen(constructor)]
pub fn new(data: Vec<u8>) -> Result<Model, JsError> {
let inner = M::load_(&data)?;
pub fn new(data: Vec<u8>, model_size: &str) -> Result<Model, JsError> {
let inner = M::load_(&data, model_size)?;
Ok(Self { inner })
}
#[wasm_bindgen]
pub fn run(&self, image: Vec<u8>) -> Result<String, JsError> {
let boxes = self.inner.run(image)?;
let json = serde_json::to_string(&boxes)?;
pub fn run(
&self,
image: Vec<u8>,
conf_threshold: f32,
iou_threshold: f32,
) -> Result<String, JsError> {
let bboxes = self.inner.run(image, conf_threshold, iou_threshold)?;
let mut detections: Vec<(String, Bbox)> = vec![];
for (class_index, bboxes_for_class) in bboxes.iter().enumerate() {
for b in bboxes_for_class.iter() {
detections.push((
coco_classes::NAMES[class_index].to_string(),
Bbox {
xmin: b.xmin,
ymin: b.ymin,
xmax: b.xmax,
ymax: b.ymax,
confidence: b.confidence,
},
));
}
}
let json = serde_json::to_string(&detections)?;
Ok(json)
}
}