mirror of
https://github.com/huggingface/candle.git
synced 2025-06-16 18:48:51 +00:00
[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:
49
candle-wasm-examples/yolo/yoloWorker.js
Normal file
49
candle-wasm-examples/yolo/yoloWorker.js
Normal file
@ -0,0 +1,49 @@
|
||||
//load the candle yolo wasm module
|
||||
import init, { Model } from "./build/m.js";
|
||||
|
||||
class Yolo {
|
||||
static instance = {};
|
||||
// Retrieve the YOLO model. When called for the first time,
|
||||
// this will load the model and save it for future use.
|
||||
static async getInstance(modelID, modelURL, modelSize) {
|
||||
// load individual modelID only once
|
||||
if (!this.instance[modelID]) {
|
||||
await init();
|
||||
|
||||
self.postMessage({ status: `loading model ${modelID}:${modelSize}` });
|
||||
const modelRes = await fetch(modelURL);
|
||||
const yoloArrayBuffer = await modelRes.arrayBuffer();
|
||||
const weightsArrayU8 = new Uint8Array(yoloArrayBuffer);
|
||||
this.instance[modelID] = new Model(weightsArrayU8, modelSize);
|
||||
} else {
|
||||
self.postMessage({ status: "model already loaded" });
|
||||
}
|
||||
return this.instance[modelID];
|
||||
}
|
||||
}
|
||||
|
||||
self.addEventListener("message", async (event) => {
|
||||
const { imageURL, modelID, modelURL, modelSize, confidence, iou_threshold } =
|
||||
event.data;
|
||||
try {
|
||||
self.postMessage({ status: "detecting" });
|
||||
|
||||
const yolo = await Yolo.getInstance(modelID, modelURL, modelSize);
|
||||
|
||||
self.postMessage({ status: "loading image" });
|
||||
const imgRes = await fetch(imageURL);
|
||||
const imgData = await imgRes.arrayBuffer();
|
||||
const imageArrayU8 = new Uint8Array(imgData);
|
||||
|
||||
self.postMessage({ status: `running inference ${modelID}:${modelSize}` });
|
||||
const bboxes = yolo.run(imageArrayU8, confidence, iou_threshold);
|
||||
|
||||
// Send the output back to the main thread as JSON
|
||||
self.postMessage({
|
||||
status: "complete",
|
||||
output: JSON.parse(bboxes),
|
||||
});
|
||||
} catch (e) {
|
||||
self.postMessage({ error: e });
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user