File size: 2,963 Bytes
e176226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Web Worker: owns the model. Single in-flight inference (main thread enforces it, worker double-checks).
import { pipeline, env, RawImage } from '@huggingface/transformers';
import { MODEL_ID, MODEL_REVISION, MODEL_DTYPE } from './config.js';

env.allowLocalModels = false;
env.allowRemoteModels = true;
env.useBrowserCache = true; // model cached by the browser Cache API after first download

let detector = null;
let device = null;
let busy = false;

function configureWasm(wasmBase, wantWebGPU) {
  // Self-hosted ONNX Runtime Web binaries (copied from the pinned npm package) instead of a third-party CDN.
  // Plain build for WASM/CPU; asyncify (JSEP) build only when trying WebGPU.
  const suffix = wantWebGPU ? '.asyncify' : '';
  env.backends.onnx.wasm.wasmPaths = {
    mjs: `${wasmBase}ort-wasm-simd-threaded${suffix}.mjs`,
    wasm: `${wasmBase}ort-wasm-simd-threaded${suffix}.wasm`,
  };
  const cores = self.navigator?.hardwareConcurrency || 2;
  env.backends.onnx.wasm.numThreads = self.crossOriginIsolated ? Math.min(4, cores) : 1;
}

async function load({ wantWebGPU, wasmBase, fallbackReason = null }) {
  configureWasm(wasmBase, wantWebGPU);
  const progress_callback = (p) => self.postMessage({ type: 'progress', data: { status: p.status, file: p.file, loaded: p.loaded, total: p.total } });
  const tryDevice = async (dev) =>
    pipeline('zero-shot-object-detection', MODEL_ID, { revision: MODEL_REVISION, dtype: MODEL_DTYPE, device: dev, progress_callback });
  if (wantWebGPU) {
    try {
      detector = await tryDevice('webgpu');
      device = 'webgpu';
    } catch (e) {
      // ORT's wasm binary cannot be swapped inside this worker; ask the main thread for a fresh WASM worker.
      self.postMessage({ type: 'webgpu-failed', reason: String(e?.message || e).slice(0, 200) });
      return;
    }
  } else {
    detector = await tryDevice('wasm');
    device = 'wasm';
  }
  self.postMessage({ type: 'ready', device, fallbackReason, threads: env.backends.onnx.wasm.numThreads });
}

async function detect({ id, width, height, buffer, labels, threshold }) {
  if (!detector) throw new Error('El modelo no está cargado.');
  if (busy) throw new Error('Ya hay una inferencia en curso.');
  busy = true;
  try {
    const rgba = new Uint8ClampedArray(buffer);
    const image = new RawImage(rgba, width, height, 4).rgb();
    const t0 = performance.now();
    const out = await detector(image, labels, { threshold, percentage: false });
    const latencyMs = performance.now() - t0;
    self.postMessage({ type: 'result', id, detections: out, latencyMs, device });
  } finally {
    busy = false;
  }
}

self.addEventListener('message', async (ev) => {
  const msg = ev.data || {};
  try {
    if (msg.type === 'load') await load(msg);
    else if (msg.type === 'detect') await detect(msg);
  } catch (e) {
    self.postMessage({ type: 'error', id: msg.id, stage: msg.type, message: String(e?.message || e).slice(0, 400) });
  }
});