tempest-react-sdk 0.19.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/vision.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Xt=require("onnxruntime-web");function Kt(o){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const e in o)if(e!=="default"){const s=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(t,e,s.get?s:{enumerable:!0,get:()=>o[e]})}}return t.default=o,Object.freeze(t)}const Q=Kt(Xt);class B extends Error{constructor(t,e){super(t,e),this.name=new.target.name}}class At extends B{}class rt extends B{}class Gt extends B{}class C extends B{}class P extends B{}class N{constructor(t,e,s){if(this.data=t,this.width=e,this.height=s,t.length!==e*s*3)throw new C(`RGBImage data length ${t.length} does not match width * height * 3 = ${e*s*3}.`)}data;width;height}class ht{constructor(t,e,s,n){this.x1=t,this.y1=e,this.x2=s,this.y2=n}x1;y1;x2;y2;get width(){return Math.max(0,this.x2-this.x1)}get height(){return Math.max(0,this.y2-this.y1)}get area(){return this.width*this.height}get xyxy(){return[this.x1,this.y1,this.x2,this.y2]}get xywh(){return[(this.x1+this.x2)/2,(this.y1+this.y2)/2,this.width,this.height]}xyxyn(t){const[e,s]=t;return s<=0||e<=0?[0,0,0,0]:[this.x1/s,this.y1/e,this.x2/s,this.y2/e]}xywhn(t){const[e,s]=t;if(s<=0||e<=0)return[0,0,0,0];const[n,r,a,i]=this.xywh;return[n/s,r/e,a/s,i/e]}asXyxy(){return[this.x1,this.y1,this.x2,this.y2]}asXywh(){return[this.x1,this.y1,this.width,this.height]}asIntXyxy(){return[Math.trunc(this.x1),Math.trunc(this.y1),Math.trunc(this.x2),Math.trunc(this.y2)]}}class R{constructor(t,e,s){if(this.data=t,this.width=e,this.height=s,t.length!==e*s)throw new C(`Mask data length ${t.length} does not match width * height = ${e*s}.`)}data;width;height}class lt{constructor(t,e,s,n){this.xyxy=t,this.cls=e,this.conf=s,this.origShape=n}xyxy;cls;conf;origShape;get length(){return this.cls.length}get shape(){return[this.length,4]}get xywh(){const t=new Float32Array(this.xyxy.length);for(let e=0;e<this.length;e++){const s=this.xyxy[e*4],n=this.xyxy[e*4+1],r=this.xyxy[e*4+2],a=this.xyxy[e*4+3];t[e*4]=(s+r)/2,t[e*4+1]=(n+a)/2,t[e*4+2]=r-s,t[e*4+3]=a-n}return t}get xyxyn(){const[t,e]=this.origShape,s=new Float32Array(this.xyxy.length);if(this.length===0||e<=0||t<=0)return s;for(let n=0;n<this.length;n++)s[n*4]=this.xyxy[n*4]/e,s[n*4+1]=this.xyxy[n*4+1]/t,s[n*4+2]=this.xyxy[n*4+2]/e,s[n*4+3]=this.xyxy[n*4+3]/t;return s}get xywhn(){const t=this.xywh,[e,s]=this.origShape;if(this.length===0||s<=0||e<=0)return t;for(let n=0;n<this.length;n++)t[n*4]=t[n*4]/s,t[n*4+1]=t[n*4+1]/e,t[n*4+2]=t[n*4+2]/s,t[n*4+3]=t[n*4+3]/e;return t}get data(){const t=new Float32Array(this.length*6);for(let e=0;e<this.length;e++)t[e*6]=this.xyxy[e*4],t[e*6+1]=this.xyxy[e*4+1],t[e*6+2]=this.xyxy[e*4+2],t[e*6+3]=this.xyxy[e*4+3],t[e*6+4]=this.conf[e],t[e*6+5]=this.cls[e];return t}}class Tt{constructor(t){this.data=t}data;get length(){return this.data.length}get shape(){return[this.length]}get top1(){if(this.data.length===0)return 0;let t=0,e=this.data[0];for(let s=1;s<this.data.length;s++){const n=this.data[s];n>e&&(t=s,e=n)}return t}get top1conf(){return this.data.length===0?0:this.data[this.top1]}get top5(){return this._topK(5).indices}get top5conf(){return this._topK(5).values}_topK(t){const e=Math.min(t,this.data.length),s=[];for(let a=0;a<this.data.length;a++)s.push(a);s.sort((a,i)=>this.data[i]-this.data[a]);const n=new Int32Array(e),r=new Float32Array(e);for(let a=0;a<e;a++)n[a]=s[a],r[a]=this.data[s[a]];return{indices:n,values:r}}}class Ct{constructor(t,e,s){this.data=t,this.xyxy=e,this.origShape=s}data;xyxy;origShape;get length(){return this.data.length}get shape(){return[this.length]}[Symbol.iterator](){return this.data[Symbol.iterator]()}}class Et{constructor(t,e,s,n,r,a=null,i={}){this.boxes=t,this.detections=e,this.names=s,this.origImg=n,this.origShape=r,this.path=a,this.speed=i}boxes;detections;names;origImg;origShape;path;speed;get length(){return this.detections.length}get(t){return this.detections[t]}[Symbol.iterator](){return this.detections[Symbol.iterator]()}}class $t{constructor(t,e,s,n,r,a=null,i={}){this.probs=t,this.result=e,this.names=s,this.origImg=n,this.origShape=r,this.path=a,this.speed=i}probs;result;names;origImg;origShape;path;speed;get cls(){return this.probs.top1}get conf(){return this.probs.top1conf}get name(){return this.names[this.cls]??`class_${this.cls}`}get probabilities(){return this.result.probabilities}}class kt{constructor(t,e,s,n,r,a,i=null,c={}){this.boxes=t,this.masks=e,this.detections=s,this.names=n,this.origImg=r,this.origShape=a,this.path=i,this.speed=c}boxes;masks;detections;names;origImg;origShape;path;speed;get length(){return this.detections.length}get(t){return this.detections[t]}[Symbol.iterator](){return this.detections[Symbol.iterator]()}}const Ot=Object.freeze(["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"]),_t={coco:Ot};function Z(o,t={}){const e=Ht(o,t.numClasses);if(t.numClasses!==void 0&&e.length!==t.numClasses)throw new P(`Resolved ${e.length} labels but the model has ${t.numClasses} classes.`);return e}function Ht(o,t){if(o==null){if(t===void 0)throw new P("Cannot auto-generate labels without numClasses. Pass an explicit labels spec or numClasses.");return Array.from({length:t},(e,s)=>`class_${s}`)}if(Array.isArray(o))return[...o];if(typeof o=="string"){const e=_t[o];if(e!==void 0)return e;throw new P(`Unknown labels preset: ${JSON.stringify(o)}. Known presets: ${Object.keys(_t).join(", ")}.`)}if(typeof o=="object"){const e=o,s=Object.keys(e).map(r=>Number(r));if(s.length===0)return[];const n=Math.max(...s);return Array.from({length:n+1},(r,a)=>e[a]??`class_${a}`)}throw new P(`Unsupported labels spec type: ${typeof o}.`)}const at=["webgpu","wasm"];function Ft(o){return o===void 0?[...at]:o.length===0?[...at]:[...o]}class U{constructor(t,e){this._session=t,this.providers=e}_session;providers;static async create(t,e={}){const s=Ft(e.providers),n={...e.sessionOptions??{},executionProviders:s};let r;try{typeof t=="string"?r=await Q.InferenceSession.create(t,n):t instanceof Uint8Array?r=await Q.InferenceSession.create(t,n):r=await Q.InferenceSession.create(t,n)}catch(a){throw new At(`Failed to load ONNX model: ${a.message}`,{cause:a})}return new U(r,s)}get inputNames(){return this._session.inputNames}get inputName(){const t=this._session.inputNames[0];if(t===void 0)throw new rt("Model has no inputs.");return t}get outputNames(){return this._session.outputNames}get raw(){return this._session}async run(t){try{return await this._session.run(t)}catch(e){throw new rt(`Inference failed: ${e.message}`,{cause:e})}}}function it(o,t){if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(o,t);if(typeof document<"u"){const e=document.createElement("canvas");return e.width=o,e.height=t,e}throw new C("No canvas implementation available in this environment.")}function ct(o){const t=o.getContext("2d");if(t===null)throw new C("Failed to obtain 2D rendering context.");return t}function dt(o){const{data:t,width:e,height:s}=o;if(t.length!==e*s*4)throw new C(`Unexpected ImageData length ${t.length} for ${e}x${s} (expected ${e*s*4}).`);const n=new Uint8Array(e*s*3);for(let r=0,a=0;r<t.length;r+=4,a+=3)n[a]=t[r],n[a+1]=t[r+1],n[a+2]=t[r+2];return new N(n,e,s)}function Jt(o){const t=new Uint8ClampedArray(o.width*o.height*4);for(let e=0,s=0;e<o.data.length;e+=3,s+=4)t[s]=o.data[e],t[s+1]=o.data[e+1],t[s+2]=o.data[e+2],t[s+3]=255;return new ImageData(t,o.width,o.height)}async function q(o){if(o instanceof N)return o;if(typeof ImageData<"u"&&o instanceof ImageData)return dt(o);if(typeof o=="string")return Qt(o);if(typeof Blob<"u"&&o instanceof Blob)return Nt(o);if(typeof HTMLImageElement<"u"&&o instanceof HTMLImageElement)return await Zt(o),V(o,o.naturalWidth,o.naturalHeight);if(typeof HTMLCanvasElement<"u"&&o instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&o instanceof OffscreenCanvas||typeof ImageBitmap<"u"&&o instanceof ImageBitmap)return V(o,o.width,o.height);throw new C(`Unsupported image source type: ${Object.prototype.toString.call(o)}`)}async function Qt(o){let t;try{t=await fetch(o)}catch(s){throw new C(`Failed to fetch image from ${o}: ${s.message}`,{cause:s})}if(!t.ok)throw new C(`Failed to fetch image from ${o}: HTTP ${t.status} ${t.statusText}`);const e=await t.blob();return Nt(e)}async function Nt(o){let t;try{t=await createImageBitmap(o)}catch(e){throw new C(`Failed to decode image blob: ${e.message}`,{cause:e})}try{return V(t,t.width,t.height)}finally{t.close()}}function Zt(o){return o.complete&&o.naturalWidth>0?Promise.resolve():new Promise((t,e)=>{const s=()=>{r(),t()},n=()=>{r(),e(new C("Failed to load HTMLImageElement (load event errored)"))},r=()=>{o.removeEventListener("load",s),o.removeEventListener("error",n)};o.addEventListener("load",s,{once:!0}),o.addEventListener("error",n,{once:!0})})}function V(o,t,e){if(t===0||e===0)throw new C(`Cannot load image with zero dimension (${t}x${e}).`);const s=it(t,e),n=ct(s);n.drawImage(o,0,0);const r=n.getImageData(0,0,t,e);return dt(r)}function ut(o,t,e){if(t<=0||e<=0)throw new Error(`Invalid resize target ${t}x${e}.`);if(t===o.width&&e===o.height)return o;const s=it(o.width,o.height);ct(s).putImageData(Jt(o),0,0);const r=it(t,e),a=ct(r);a.imageSmoothingEnabled=!0,a.imageSmoothingQuality="high",a.drawImage(s,0,0,t,e);const i=a.getImageData(0,0,t,e);return dt(i)}function Lt(o,t,e,s=1/255){const n=new Float32Array(o.data.length),r=o.data,a=t[0],i=t[1],c=t[2],f=e[0],m=e[1],h=e[2];for(let l=0;l<r.length;l+=3)n[l]=(r[l]*s-a)/f,n[l+1]=(r[l+1]*s-i)/m,n[l+2]=(r[l+2]*s-c)/h;return n}function W(o,t=1/255){const e=new Float32Array(o.data.length),s=o.data;for(let n=0;n<s.length;n++)e[n]=s[n]*t;return e}function j(o,t,e,s=3){const n=t*e*s;if(o.length!==n)throw new Error(`toCHW: expected length ${n} for ${t}x${e}x${s}, got ${o.length}.`);const r=new Float32Array(n),a=t*e;for(let i=0;i<e;i++)for(let c=0;c<t;c++){const f=(i*t+c)*s,m=i*t+c;for(let h=0;h<s;h++)r[h*a+m]=o[f+h]}return r}function tt(o,t){return new Q.Tensor("float32",o,t)}function qt(o){const t=W(o);return j(t,o.width,o.height,3)}function Wt(o,t,e){if(o.length!==t*e*3)throw new Error(`fromCv2: data length ${o.length} does not match width * height * 3 = ${t*e*3}.`);const s=new Uint8Array(o.length);for(let n=0;n<o.length;n+=3)s[n]=o[n+2],s[n+1]=o[n+1],s[n+2]=o[n];return new N(s,t,e)}function te(o){const t=o.data,e=new Uint8Array(t.length);for(let s=0;s<t.length;s+=3)e[s]=t[s+2],e[s+1]=t[s+1],e[s+2]=t[s];return e}function ft(o,t,e,s=[114,114,114]){const n=Math.min(t/o.width,e/o.height),r=Math.round(o.width*n),a=Math.round(o.height*n),i=ut(o,r,a),c=new Uint8Array(t*e*3),f=s[0],m=s[1],h=s[2];for(let d=0;d<c.length;d+=3)c[d]=f,c[d+1]=m,c[d+2]=h;const l=Math.floor((t-r)/2),w=Math.floor((e-a)/2),x=r*3;for(let d=0;d<a;d++){const u=d*x,p=((w+d)*t+l)*3;c.set(i.data.subarray(u,u+x),p)}return{image:new N(c,t,e),scale:n,padLeft:l,padTop:w}}function zt(o){const t=o.length,e=new Float32Array(t);let s=-1/0;for(let r=0;r<t;r++){const a=o[r];a>s&&(s=a)}let n=0;for(let r=0;r<t;r++){const a=Math.exp(o[r]-s);e[r]=a,n+=a}for(let r=0;r<t;r++)e[r]=e[r]/n;return e}function Dt(o,t){const e=o.length,s=t===null?e:Math.min(Math.max(0,t),e),n=new Array(e);for(let i=0;i<e;i++)n[i]=i;n.sort((i,c)=>o[c]-o[i]);const r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=n[i];r[i]=c,a[i]=o[c]}return{indices:r,values:a}}function Yt(o,t,e){const s=t.length;if(s===0)return new Int32Array(0);const n=new Float32Array(s);for(let c=0;c<s;c++){const f=o[c*4],m=o[c*4+1],h=o[c*4+2],l=o[c*4+3];n[c]=Math.max(0,h-f)*Math.max(0,l-m)}const r=new Array(s);for(let c=0;c<s;c++)r[c]=c;r.sort((c,f)=>t[f]-t[c]);const a=new Uint8Array(s),i=[];for(let c=0;c<r.length;c++){const f=r[c];if(a[f])continue;i.push(f);const m=o[f*4],h=o[f*4+1],l=o[f*4+2],w=o[f*4+3],x=n[f];for(let d=c+1;d<r.length;d++){const u=r[d];if(a[u])continue;const p=o[u*4],_=o[u*4+1],g=o[u*4+2],A=o[u*4+3],S=Math.max(m,p),M=Math.max(h,_),v=Math.min(l,g),E=Math.min(w,A),$=Math.max(0,v-S),y=Math.max(0,E-M),b=$*y,I=x+n[u]-b;(I>0?b/I:0)>e&&(a[u]=1)}}return Int32Array.from(i)}function Rt(o,t,e,s){if(t.length===0)return new Int32Array(0);const n=new Map;for(let a=0;a<e.length;a++){const i=e[a],c=n.get(i);c===void 0?n.set(i,[a]):c.push(a)}const r=[];for(const a of n.values()){const i=a.length,c=new Float32Array(i*4),f=new Float32Array(i);for(let h=0;h<i;h++){const l=a[h];c[h*4]=o[l*4],c[h*4+1]=o[l*4+1],c[h*4+2]=o[l*4+2],c[h*4+3]=o[l*4+3],f[h]=t[l]}const m=Yt(c,f,s);for(let h=0;h<m.length;h++)r.push(a[m[h]])}return r.sort((a,i)=>t[i]-t[a]),Int32Array.from(r)}function et(o,t,e){let s=t;if(s.length===3){if(s[0]!==1)throw new Error(`decodeYoloAnchors: expected batch size 1, got ${s[0]}.`);s=[s[1],s[2]]}if(s.length!==2)throw new Error(`decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(t)}.`);const n=s[0],r=s[1],{numClasses:a,originalWidth:i,originalHeight:c,padLeft:f,padTop:m,scale:h,confThreshold:l,iouThreshold:w,maxDetections:x}=e;if(a<1||a+4>n)throw new Error(`decodeYoloAnchors: invalid numClasses=${a} for channels=${n}.`);if(o.length!==n*r)throw new Error(`decodeYoloAnchors: data length ${o.length} does not match channels*numAnchors=${n*r}.`);const d=[];for(let y=0;y<r;y++){let b=0,I=-1/0;for(let Y=0;Y<a;Y++){const z=o[(4+Y)*r+y];z!==void 0&&z>I&&(I=z,b=Y)}if(I<l)continue;const T=o[y],X=o[r+y],K=o[2*r+y],G=o[3*r+y];let D=T-K/2,k=X-G/2,O=T+K/2,L=X+G/2;D=(D-f)/h,k=(k-m)/h,O=(O-f)/h,L=(L-m)/h,D=Math.max(0,Math.min(i,D)),k=Math.max(0,Math.min(c,k)),O=Math.max(0,Math.min(i,O)),L=Math.max(0,Math.min(c,L)),d.push({anchorIdx:y,x1:D,y1:k,x2:O,y2:L,classId:b,confidence:I})}if(d.length===0)return vt();const u=new Float32Array(d.length*4),p=new Float32Array(d.length),_=new Int32Array(d.length);for(let y=0;y<d.length;y++){const b=d[y];u[y*4]=b.x1,u[y*4+1]=b.y1,u[y*4+2]=b.x2,u[y*4+3]=b.y2,p[y]=b.confidence,_[y]=b.classId}const g=Rt(u,p,_,w);if(g.length===0)return vt();const A=Array.from(g).slice(0,x),S=A.length,M=new Int32Array(S),v=new Float32Array(S*4),E=new Int32Array(S),$=new Float32Array(S);for(let y=0;y<S;y++){const b=d[A[y]];M[y]=b.anchorIdx,v[y*4]=b.x1,v[y*4+1]=b.y1,v[y*4+2]=b.x2,v[y*4+3]=b.y2,E[y]=b.classId,$[y]=b.confidence}return{anchorIndices:M,boxesXyxy:v,classIds:E,confidences:$}}function vt(){return{anchorIndices:new Int32Array(0),boxesXyxy:new Float32Array(0),classIds:new Int32Array(0),confidences:new Float32Array(0)}}function mt(o,t,e){const s=t.length===3?t[1]:t[0];if(s===void 0||s<5)throw new Error(`decodeYolo: invalid output channel count ${s} (expected >= 5).`);const n=s-4,r=et(o,t,{numClasses:n,...e}),a=[];for(let i=0;i<r.classIds.length;i++)a.push({bbox:new ht(r.boxesXyxy[i*4],r.boxesXyxy[i*4+1],r.boxesXyxy[i*4+2],r.boxesXyxy[i*4+3]),classId:r.classIds[i],confidence:r.confidences[i]});return a}let Mt=!1,St=!1;function ee(o,t,e){return Mt||(Mt=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. The alias will be removed in 0.3.0.")),mt(o,t,e)}function ne(o,t,e){return St||(St=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. The alias will be removed in 0.3.0.")),et(o,t,e)}function yt(o,t,e,s,n){let r=s;if(r.length===4){if(r[0]!==1)throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${r[0]}.`);r=[r[1],r[2],r[3]]}if(r.length!==3)throw new Error(`decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(s)}.`);const a=r[0],i=r[1],c=r[2],f=t.length===3?t[1]:t[0],m=t.length===3?t[2]:t[1];if(f===void 0||m===void 0)throw new Error(`decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(t)}.`);const h=4+n.numClasses+a;if(f!==h)throw new Error(`decodeYoloSeg: channels=${f} does not match 4 + numClasses(${n.numClasses}) + numMaskCoefs(${a}) = ${h}.`);if(e.length!==a*i*c)throw new Error(`decodeYoloSeg: prototype length ${e.length} does not match dims=${JSON.stringify(s)}.`);const l=et(o,t,{numClasses:n.numClasses,originalWidth:n.originalWidth,originalHeight:n.originalHeight,padLeft:n.padLeft,padTop:n.padTop,scale:n.scale,confThreshold:n.confThreshold,iouThreshold:n.iouThreshold,maxDetections:n.maxDetections});if(l.anchorIndices.length===0)return[];const w=n.maskThreshold??.5,x=c/n.inputWidth,d=i/n.inputHeight,u=i*c,p=(4+n.numClasses)*m,_=[];for(let g=0;g<l.anchorIndices.length;g++){const A=l.anchorIndices[g],S=l.boxesXyxy[g*4],M=l.boxesXyxy[g*4+1],v=l.boxesXyxy[g*4+2],E=l.boxesXyxy[g*4+3],$=new ht(S,M,v,E),y=l.classIds[g],b=l.confidences[g],I=Math.max(0,Math.trunc(v)-Math.trunc(S)),T=Math.max(0,Math.trunc(E)-Math.trunc(M));if(I===0||T===0){_.push({bbox:$,classId:y,confidence:b,mask:new R(new Uint8Array(0),0,0)});continue}const X=S*n.scale+n.padLeft,K=M*n.scale+n.padTop,G=v*n.scale+n.padLeft,D=E*n.scale+n.padTop,k=Math.max(0,Math.floor(X*x)),O=Math.max(0,Math.floor(K*d)),L=Math.min(c,Math.ceil(G*x)),Y=Math.min(i,Math.ceil(D*d));if(L<=k||Y<=O){_.push({bbox:$,classId:y,confidence:b,mask:new R(new Uint8Array(I*T),I,T)});continue}const z=L-k,st=Y-O,pt=new Float32Array(z*st);for(let F=0;F<st;F++){const Ut=O+F;for(let H=0;H<z;H++){const Vt=k+H;let bt=0;for(let J=0;J<a;J++){const Pt=o[p+J*m+A],jt=e[J*u+Ut*c+Vt];bt+=Pt*jt}pt[F*z+H]=se(bt)}}const Bt=re(pt,z,st,I,T),ot=new Uint8Array(I*T);for(let F=0;F<ot.length;F++)ot[F]=Bt[F]>=w?255:0;_.push({bbox:$,classId:y,confidence:b,mask:new R(ot,I,T)})}return _}function se(o){if(o>=0)return 1/(1+Math.exp(-o));const t=Math.exp(o);return t/(1+t)}let It=!1;function oe(o,t,e,s,n){return It||(It=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. The alias will be removed in 0.3.0.")),yt(o,t,e,s,n)}function re(o,t,e,s,n){const r=new Float32Array(s*n);if(s===0||n===0||t===0||e===0)return r;if(s===t&&n===e)return r.set(o),r;const a=t/s,i=e/n;for(let c=0;c<n;c++){const f=(c+.5)*i-.5,m=Math.max(0,Math.floor(f)),h=Math.min(e-1,m+1),l=Math.max(0,Math.min(1,f-m));for(let w=0;w<s;w++){const x=(w+.5)*a-.5,d=Math.max(0,Math.floor(x)),u=Math.min(t-1,d+1),p=Math.max(0,Math.min(1,x-d)),_=o[m*t+d],g=o[m*t+u],A=o[h*t+d],S=o[h*t+u],M=_*(1-p)+g*p,v=A*(1-p)+S*p;r[c*s+w]=M*(1-l)+v*l}}return r}class nt{constructor(t){this._session=t}_session;get session(){return this._session}}const ae=[.485,.456,.406],ie=[.229,.224,.225];class wt extends nt{constructor(t,e,s,n,r,a,i){super(t),this._labels=e,this._names=s,this._inputSize=n,this._mean=r,this._std=a,this._applySoftmax=i}_labels;_names;_inputSize;_mean;_std;_applySoftmax;static async create(t,e){const s=await U.create(t,e),n=Z(e.labels,{numClasses:e.numClasses}),r={};for(let a=0;a<n.length;a++)r[a]=n[a];return new wt(s,n,r,e.inputSize??[224,224],e.mean??ae,e.std??ie,e.applySoftmax??!0)}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await q(t),r=this._preprocess(n),a=await this._session.run({[this._session.inputName]:r}),i=this._session.outputNames[0];if(i===void 0)throw new Error("Classifier model has no outputs.");const c=a[i];if(c===void 0)throw new Error(`Classifier model output ${i} missing from run() result.`);const f=this._postprocess(c.data),{indices:m,values:h}=Dt(f,e.topK??null),l=[];for(let u=0;u<m.length;u++){const p=m[u],_=this._labels[p]??`class_${p}`;l.push({classId:p,className:_,probability:h[u],cls:p,name:_,conf:h[u]})}if(l.length===0)throw new Error("Classifier produced no probabilities (empty output).");const w=l[0],x={classId:w.classId,className:w.className,confidence:w.probability,cls:w.classId,name:w.className,conf:w.probability,image:n,probabilities:l},d=[n.height,n.width];return[new $t(new Tt(f),x,this._names,n,d,s)]}_preprocess(t){const[e,s]=this._inputSize,n=ut(t,e,s),r=Lt(n,this._mean,this._std),a=j(r,n.width,n.height,3);return tt(a,[1,3,n.height,n.width])}_postprocess(t){return this._applySoftmax?zt(t):new Float32Array(t)}}class gt extends nt{constructor(t,e,s,n,r,a,i,c){super(t),this._head=e,this._labels=s,this._names=n,this._inputSize=r,this._confThreshold=a,this._iouThreshold=i,this._maxDetections=c}_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;static async create(t,e={}){const s=e.head??"yolo";if(s!=="yolo")throw new Error(`Unsupported detector head '${s}'. Supported: 'yolo'.`);const n=await U.create(t,e),r=Z(e.labels??"coco",{numClasses:e.numClasses}),a={};for(let i=0;i<r.length;i++)a[i]=r[i];return new gt(n,s,r,a,e.inputSize??[640,640],e.confThreshold??.25,e.iouThreshold??.45,e.maxDetections??300)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await q(t),{tensor:r,scale:a,padLeft:i,padTop:c}=this._preprocess(n),f=await this._session.run({[this._session.inputName]:r}),m=this._session.outputNames[0];if(m===void 0)throw new Error("Detector model has no outputs.");const h=f[m];if(h===void 0)throw new Error(`Detector model output ${m} missing from run() result.`);const l=mt(h.data,h.dims,{originalWidth:n.width,originalHeight:n.height,padLeft:i,padTop:c,scale:a,confThreshold:e.confThreshold??this._confThreshold,iouThreshold:e.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections}),x=(e.classes!==void 0?(()=>{const u=new Set(e.classes);return l.filter(p=>u.has(p.classId))})():l).map(u=>this._buildResult(n,u.bbox,u.classId,u.confidence)),d=[n.height,n.width];return[new Et(this._buildBoxes(x,d),x,this._names,n,d,s)]}_preprocess(t){const[e,s]=this._inputSize,n=ft(t,e,s),r=W(n.image),a=j(r,n.image.width,n.image.height,3);return{tensor:tt(a,[1,3,n.image.height,n.image.width]),scale:n.scale,padLeft:n.padLeft,padTop:n.padTop}}_buildResult(t,e,s,n){const[r,a,i,c]=e.asIntXyxy(),f=Math.max(0,r),m=Math.max(0,a),h=Math.min(t.width,i),l=Math.min(t.height,c);let w;if(h>f&&l>m){const d=h-f,u=l-m,p=new Uint8Array(d*u*3);for(let _=0;_<u;_++){const g=((m+_)*t.width+f)*3;p.set(t.data.subarray(g,g+d*3),_*d*3)}w=new N(p,d,u)}else w=new N(new Uint8Array(0),0,0);const x=this._names[s]??`class_${s}`;return{classId:s,className:x,confidence:n,bbox:e,cls:s,name:x,conf:n,box:e,croppedImage:w}}_buildBoxes(t,e){const s=t.length,n=new Float32Array(s*4),r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=t[i];n[i*4]=c.bbox.x1,n[i*4+1]=c.bbox.y1,n[i*4+2]=c.bbox.x2,n[i*4+3]=c.bbox.y2,r[i]=c.classId,a[i]=c.confidence}return new lt(n,r,a,e)}}class xt extends nt{constructor(t,e,s,n,r,a,i,c,f){super(t),this._head=e,this._labels=s,this._names=n,this._inputSize=r,this._confThreshold=a,this._iouThreshold=i,this._maxDetections=c,this._maskThreshold=f}_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;_maskThreshold;static async create(t,e={}){const s=e.head??"yolo-seg";if(s!=="yolo-seg")throw new Error(`Unsupported segmenter head '${s}'. Supported: 'yolo-seg'.`);const n=await U.create(t,e),r=Z(e.labels??"coco",{numClasses:e.numClasses}),a={};for(let i=0;i<r.length;i++)a[i]=r[i];return new xt(n,s,r,a,e.inputSize??[640,640],e.confThreshold??.25,e.iouThreshold??.45,e.maxDetections??300,e.maskThreshold??.5)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await q(t),{tensor:r,scale:a,padLeft:i,padTop:c}=this._preprocess(n),f=await this._session.run({[this._session.inputName]:r}),{perAnchor:m,prototypes:h}=this._splitOutputs(f),l=yt(m.data,m.dims,h.data,h.dims,{numClasses:this._labels.length,inputWidth:this._inputSize[0],inputHeight:this._inputSize[1],originalWidth:n.width,originalHeight:n.height,padLeft:i,padTop:c,scale:a,confThreshold:e.confThreshold??this._confThreshold,iouThreshold:e.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections,maskThreshold:this._maskThreshold}),x=(e.classes!==void 0?(()=>{const u=new Set(e.classes);return l.filter(p=>u.has(p.classId))})():l).map(u=>this._buildResult(n,u.bbox,u.classId,u.confidence,u.mask)),d=[n.height,n.width];return[new kt(this._buildBoxes(x,d),this._buildMasks(x,d),x,this._names,n,d,s)]}_preprocess(t){const[e,s]=this._inputSize,n=ft(t,e,s),r=W(n.image),a=j(r,n.image.width,n.image.height,3);return{tensor:tt(a,[1,3,n.image.height,n.image.width]),scale:n.scale,padLeft:n.padLeft,padTop:n.padTop}}_splitOutputs(t){let e,s;for(const n of this._session.outputNames){const r=t[n];r!==void 0&&(r.dims.length===3&&e===void 0?e=r:r.dims.length===4&&s===void 0&&(s=r))}if(e===void 0||s===void 0){const n=this._session.outputNames.map(r=>`${r}: ${JSON.stringify(t[r]?.dims??[])}`);throw new Error(`Segmenter expected one 3-D and one 4-D output, got [${n.join(", ")}].`)}return{perAnchor:e,prototypes:s}}_buildResult(t,e,s,n,r){const[a,i,c,f]=e.asIntXyxy(),m=Math.max(0,a),h=Math.max(0,i),l=Math.min(t.width,c),w=Math.min(t.height,f);let x,d=r;if(l>m&&w>h&&r.data.length>0){const p=l-m,_=w-h,g=Math.min(r.width,p),A=Math.min(r.height,_),S=new Uint8Array(g*A*3);for(let M=0;M<A;M++){const v=((h+M)*t.width+m)*3,E=M*g*3,$=M*r.width;for(let y=0;y<g;y++)if(r.data[$+y]!==0){const I=v+y*3,T=E+y*3;S[T]=t.data[I],S[T+1]=t.data[I+1],S[T+2]=t.data[I+2]}}if(x=new N(S,g,A),g!==r.width||A!==r.height){const M=new Uint8Array(g*A);for(let v=0;v<A;v++)M.set(r.data.subarray(v*r.width,v*r.width+g),v*g);d=new R(M,g,A)}}else d=new R(new Uint8Array(0),0,0),x=new N(new Uint8Array(0),0,0);const u=this._names[s]??`class_${s}`;return{classId:s,className:u,confidence:n,bbox:e,cls:s,name:u,conf:n,box:e,mask:d,segmentedImage:x}}_buildBoxes(t,e){const s=t.length,n=new Float32Array(s*4),r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=t[i];n[i*4]=c.bbox.x1,n[i*4+1]=c.bbox.y1,n[i*4+2]=c.bbox.x2,n[i*4+3]=c.bbox.y2,r[i]=c.classId,a[i]=c.confidence}return new lt(n,r,a,e)}_buildMasks(t,e){const s=new Float32Array(t.length*4);for(let n=0;n<t.length;n++){const r=t[n];s[n*4]=r.bbox.x1,s[n*4+1]=r.bbox.y1,s[n*4+2]=r.bbox.x2,s[n*4+3]=r.bbox.y2}return new Ct(t.map(n=>n.mask),s,e)}}const ce="0.2.1";exports.BoundingBox=ht;exports.Boxes=lt;exports.COCO_CLASSES=Ot;exports.ClassificationResults=$t;exports.Classifier=wt;exports.DEFAULT_PROVIDERS=at;exports.DetectionResults=Et;exports.Detector=gt;exports.ImageLoadError=C;exports.InferenceError=rt;exports.LabelMapError=P;exports.Mask=R;exports.Masks=Ct;exports.ModelLoadError=At;exports.OrtSession=U;exports.OrtVisionError=B;exports.Probs=Tt;exports.ProviderNotAvailableError=Gt;exports.RGBImage=N;exports.SegmentationResults=kt;exports.Segmenter=xt;exports.VERSION=ce;exports.VisionTask=nt;exports.batchedNms=Rt;exports.decodeYolo=mt;exports.decodeYoloAnchors=et;exports.decodeYoloSeg=yt;exports.decodeYoloV8=ee;exports.decodeYoloV8Anchors=ne;exports.decodeYoloV8Seg=oe;exports.fromCv2=Wt;exports.letterbox=ft;exports.loadImage=q;exports.nms=Yt;exports.normalize=Lt;exports.resize=ut;exports.resolveLabels=Z;exports.resolveProviders=Ft;exports.softmax=zt;exports.toCHW=j;exports.toCv2=te;exports.toFloat32=W;exports.toFloat32Tensor=tt;exports.toTensor=qt;exports.topK=Dt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("react"),qt=require("onnxruntime-web");function Qt(o){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const e in o)if(e!=="default"){const s=Object.getOwnPropertyDescriptor(o,e);Object.defineProperty(t,e,s.get?s:{enumerable:!0,get:()=>o[e]})}}return t.default=o,Object.freeze(t)}const Q=Qt(qt);class B extends Error{constructor(t,e){super(t,e),this.name=new.target.name}}class Ct extends B{}class at extends B{}class Wt extends B{}class T extends B{}class j extends B{}class F{constructor(t,e,s){if(this.data=t,this.width=e,this.height=s,t.length!==e*s*3)throw new T(`RGBImage data length ${t.length} does not match width * height * 3 = ${e*s*3}.`)}data;width;height}class ht{constructor(t,e,s,n){this.x1=t,this.y1=e,this.x2=s,this.y2=n}x1;y1;x2;y2;get width(){return Math.max(0,this.x2-this.x1)}get height(){return Math.max(0,this.y2-this.y1)}get area(){return this.width*this.height}get xyxy(){return[this.x1,this.y1,this.x2,this.y2]}get xywh(){return[(this.x1+this.x2)/2,(this.y1+this.y2)/2,this.width,this.height]}xyxyn(t){const[e,s]=t;return s<=0||e<=0?[0,0,0,0]:[this.x1/s,this.y1/e,this.x2/s,this.y2/e]}xywhn(t){const[e,s]=t;if(s<=0||e<=0)return[0,0,0,0];const[n,r,a,i]=this.xywh;return[n/s,r/e,a/s,i/e]}asXyxy(){return[this.x1,this.y1,this.x2,this.y2]}asXywh(){return[this.x1,this.y1,this.width,this.height]}asIntXyxy(){return[Math.trunc(this.x1),Math.trunc(this.y1),Math.trunc(this.x2),Math.trunc(this.y2)]}}class U{constructor(t,e,s){if(this.data=t,this.width=e,this.height=s,t.length!==e*s)throw new T(`Mask data length ${t.length} does not match width * height = ${e*s}.`)}data;width;height}class dt{constructor(t,e,s,n){this.xyxy=t,this.cls=e,this.conf=s,this.origShape=n}xyxy;cls;conf;origShape;get length(){return this.cls.length}get shape(){return[this.length,4]}get xywh(){const t=new Float32Array(this.xyxy.length);for(let e=0;e<this.length;e++){const s=this.xyxy[e*4],n=this.xyxy[e*4+1],r=this.xyxy[e*4+2],a=this.xyxy[e*4+3];t[e*4]=(s+r)/2,t[e*4+1]=(n+a)/2,t[e*4+2]=r-s,t[e*4+3]=a-n}return t}get xyxyn(){const[t,e]=this.origShape,s=new Float32Array(this.xyxy.length);if(this.length===0||e<=0||t<=0)return s;for(let n=0;n<this.length;n++)s[n*4]=this.xyxy[n*4]/e,s[n*4+1]=this.xyxy[n*4+1]/t,s[n*4+2]=this.xyxy[n*4+2]/e,s[n*4+3]=this.xyxy[n*4+3]/t;return s}get xywhn(){const t=this.xywh,[e,s]=this.origShape;if(this.length===0||s<=0||e<=0)return t;for(let n=0;n<this.length;n++)t[n*4]=t[n*4]/s,t[n*4+1]=t[n*4+1]/e,t[n*4+2]=t[n*4+2]/s,t[n*4+3]=t[n*4+3]/e;return t}get data(){const t=new Float32Array(this.length*6);for(let e=0;e<this.length;e++)t[e*6]=this.xyxy[e*4],t[e*6+1]=this.xyxy[e*4+1],t[e*6+2]=this.xyxy[e*4+2],t[e*6+3]=this.xyxy[e*4+3],t[e*6+4]=this.conf[e],t[e*6+5]=this.cls[e];return t}}class kt{constructor(t){this.data=t}data;get length(){return this.data.length}get shape(){return[this.length]}get top1(){if(this.data.length===0)return 0;let t=0,e=this.data[0];for(let s=1;s<this.data.length;s++){const n=this.data[s];n>e&&(t=s,e=n)}return t}get top1conf(){return this.data.length===0?0:this.data[this.top1]}get top5(){return this._topK(5).indices}get top5conf(){return this._topK(5).values}_topK(t){const e=Math.min(t,this.data.length),s=[];for(let a=0;a<this.data.length;a++)s.push(a);s.sort((a,i)=>this.data[i]-this.data[a]);const n=new Int32Array(e),r=new Float32Array(e);for(let a=0;a<e;a++)n[a]=s[a],r[a]=this.data[s[a]];return{indices:n,values:r}}}class $t{constructor(t,e,s){this.data=t,this.xyxy=e,this.origShape=s}data;xyxy;origShape;get length(){return this.data.length}get shape(){return[this.length]}[Symbol.iterator](){return this.data[Symbol.iterator]()}}class Lt{constructor(t,e,s,n,r,a=null,i={}){this.boxes=t,this.detections=e,this.names=s,this.origImg=n,this.origShape=r,this.path=a,this.speed=i}boxes;detections;names;origImg;origShape;path;speed;get length(){return this.detections.length}get(t){return this.detections[t]}[Symbol.iterator](){return this.detections[Symbol.iterator]()}}class Nt{constructor(t,e,s,n,r,a=null,i={}){this.probs=t,this.result=e,this.names=s,this.origImg=n,this.origShape=r,this.path=a,this.speed=i}probs;result;names;origImg;origShape;path;speed;get cls(){return this.probs.top1}get conf(){return this.probs.top1conf}get name(){return this.names[this.cls]??`class_${this.cls}`}get probabilities(){return this.result.probabilities}}class Ot{constructor(t,e,s,n,r,a,i=null,c={}){this.boxes=t,this.masks=e,this.detections=s,this.names=n,this.origImg=r,this.origShape=a,this.path=i,this.speed=c}boxes;masks;detections;names;origImg;origShape;path;speed;get length(){return this.detections.length}get(t){return this.detections[t]}[Symbol.iterator](){return this.detections[Symbol.iterator]()}}const Ft=Object.freeze(["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"]),vt={coco:Ft};function W(o,t={}){const e=Zt(o,t.numClasses);if(t.numClasses!==void 0&&e.length!==t.numClasses)throw new j(`Resolved ${e.length} labels but the model has ${t.numClasses} classes.`);return e}function Zt(o,t){if(o==null){if(t===void 0)throw new j("Cannot auto-generate labels without numClasses. Pass an explicit labels spec or numClasses.");return Array.from({length:t},(e,s)=>`class_${s}`)}if(Array.isArray(o))return[...o];if(typeof o=="string"){const e=vt[o];if(e!==void 0)return e;throw new j(`Unknown labels preset: ${JSON.stringify(o)}. Known presets: ${Object.keys(vt).join(", ")}.`)}if(typeof o=="object"){const e=o,s=Object.keys(e).map(r=>Number(r));if(s.length===0)return[];const n=Math.max(...s);return Array.from({length:n+1},(r,a)=>e[a]??`class_${a}`)}throw new j(`Unsupported labels spec type: ${typeof o}.`)}const it=["webgpu","wasm"];function Dt(o){return o===void 0?[...it]:o.length===0?[...it]:[...o]}class V{constructor(t,e){this._session=t,this.providers=e}_session;providers;static async create(t,e={}){const s=Dt(e.providers),n={...e.sessionOptions??{},executionProviders:s};let r;try{typeof t=="string"?r=await Q.InferenceSession.create(t,n):t instanceof Uint8Array?r=await Q.InferenceSession.create(t,n):r=await Q.InferenceSession.create(t,n)}catch(a){throw new Ct(`Failed to load ONNX model: ${a.message}`,{cause:a})}return new V(r,s)}get inputNames(){return this._session.inputNames}get inputName(){const t=this._session.inputNames[0];if(t===void 0)throw new at("Model has no inputs.");return t}get outputNames(){return this._session.outputNames}get raw(){return this._session}async run(t){try{return await this._session.run(t)}catch(e){throw new at(`Inference failed: ${e.message}`,{cause:e})}}}function ct(o,t){if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(o,t);if(typeof document<"u"){const e=document.createElement("canvas");return e.width=o,e.height=t,e}throw new T("No canvas implementation available in this environment.")}function lt(o){const t=o.getContext("2d");if(t===null)throw new T("Failed to obtain 2D rendering context.");return t}function ut(o){const{data:t,width:e,height:s}=o;if(t.length!==e*s*4)throw new T(`Unexpected ImageData length ${t.length} for ${e}x${s} (expected ${e*s*4}).`);const n=new Uint8Array(e*s*3);for(let r=0,a=0;r<t.length;r+=4,a+=3)n[a]=t[r],n[a+1]=t[r+1],n[a+2]=t[r+2];return new F(n,e,s)}function te(o){const t=new Uint8ClampedArray(o.width*o.height*4);for(let e=0,s=0;e<o.data.length;e+=3,s+=4)t[s]=o.data[e],t[s+1]=o.data[e+1],t[s+2]=o.data[e+2],t[s+3]=255;return new ImageData(t,o.width,o.height)}async function Z(o){if(o instanceof F)return o;if(typeof ImageData<"u"&&o instanceof ImageData)return ut(o);if(typeof o=="string")return ee(o);if(typeof Blob<"u"&&o instanceof Blob)return Rt(o);if(typeof HTMLImageElement<"u"&&o instanceof HTMLImageElement)return await ne(o),P(o,o.naturalWidth,o.naturalHeight);if(typeof HTMLCanvasElement<"u"&&o instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&o instanceof OffscreenCanvas||typeof ImageBitmap<"u"&&o instanceof ImageBitmap)return P(o,o.width,o.height);throw new T(`Unsupported image source type: ${Object.prototype.toString.call(o)}`)}async function ee(o){let t;try{t=await fetch(o)}catch(s){throw new T(`Failed to fetch image from ${o}: ${s.message}`,{cause:s})}if(!t.ok)throw new T(`Failed to fetch image from ${o}: HTTP ${t.status} ${t.statusText}`);const e=await t.blob();return Rt(e)}async function Rt(o){let t;try{t=await createImageBitmap(o)}catch(e){throw new T(`Failed to decode image blob: ${e.message}`,{cause:e})}try{return P(t,t.width,t.height)}finally{t.close()}}function ne(o){return o.complete&&o.naturalWidth>0?Promise.resolve():new Promise((t,e)=>{const s=()=>{r(),t()},n=()=>{r(),e(new T("Failed to load HTMLImageElement (load event errored)"))},r=()=>{o.removeEventListener("load",s),o.removeEventListener("error",n)};o.addEventListener("load",s,{once:!0}),o.addEventListener("error",n,{once:!0})})}function P(o,t,e){if(t===0||e===0)throw new T(`Cannot load image with zero dimension (${t}x${e}).`);const s=ct(t,e),n=lt(s);n.drawImage(o,0,0);const r=n.getImageData(0,0,t,e);return ut(r)}function ft(o,t,e){if(t<=0||e<=0)throw new Error(`Invalid resize target ${t}x${e}.`);if(t===o.width&&e===o.height)return o;const s=ct(o.width,o.height);lt(s).putImageData(te(o),0,0);const r=ct(t,e),a=lt(r);a.imageSmoothingEnabled=!0,a.imageSmoothingQuality="high",a.drawImage(s,0,0,t,e);const i=a.getImageData(0,0,t,e);return ut(i)}function zt(o,t,e,s=1/255){const n=new Float32Array(o.data.length),r=o.data,a=t[0],i=t[1],c=t[2],l=e[0],u=e[1],d=e[2];for(let h=0;h<r.length;h+=3)n[h]=(r[h]*s-a)/l,n[h+1]=(r[h+1]*s-i)/u,n[h+2]=(r[h+2]*s-c)/d;return n}function tt(o,t=1/255){const e=new Float32Array(o.data.length),s=o.data;for(let n=0;n<s.length;n++)e[n]=s[n]*t;return e}function X(o,t,e,s=3){const n=t*e*s;if(o.length!==n)throw new Error(`toCHW: expected length ${n} for ${t}x${e}x${s}, got ${o.length}.`);const r=new Float32Array(n),a=t*e;for(let i=0;i<e;i++)for(let c=0;c<t;c++){const l=(i*t+c)*s,u=i*t+c;for(let d=0;d<s;d++)r[d*a+u]=o[l+d]}return r}function et(o,t){return new Q.Tensor("float32",o,t)}function se(o){const t=tt(o);return X(t,o.width,o.height,3)}function oe(o,t,e){if(o.length!==t*e*3)throw new Error(`fromCv2: data length ${o.length} does not match width * height * 3 = ${t*e*3}.`);const s=new Uint8Array(o.length);for(let n=0;n<o.length;n+=3)s[n]=o[n+2],s[n+1]=o[n+1],s[n+2]=o[n];return new F(s,t,e)}function re(o){const t=o.data,e=new Uint8Array(t.length);for(let s=0;s<t.length;s+=3)e[s]=t[s+2],e[s+1]=t[s+1],e[s+2]=t[s];return e}function mt(o,t,e,s=[114,114,114]){const n=Math.min(t/o.width,e/o.height),r=Math.round(o.width*n),a=Math.round(o.height*n),i=ft(o,r,a),c=new Uint8Array(t*e*3),l=s[0],u=s[1],d=s[2];for(let f=0;f<c.length;f+=3)c[f]=l,c[f+1]=u,c[f+2]=d;const h=Math.floor((t-r)/2),w=Math.floor((e-a)/2),g=r*3;for(let f=0;f<a;f++){const m=f*g,x=((w+f)*t+h)*3;c.set(i.data.subarray(m,m+g),x)}return{image:new F(c,t,e),scale:n,padLeft:h,padTop:w}}function Yt(o){const t=o.length,e=new Float32Array(t);let s=-1/0;for(let r=0;r<t;r++){const a=o[r];a>s&&(s=a)}let n=0;for(let r=0;r<t;r++){const a=Math.exp(o[r]-s);e[r]=a,n+=a}for(let r=0;r<t;r++)e[r]=e[r]/n;return e}function Ut(o,t){const e=o.length,s=t===null?e:Math.min(Math.max(0,t),e),n=new Array(e);for(let i=0;i<e;i++)n[i]=i;n.sort((i,c)=>o[c]-o[i]);const r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=n[i];r[i]=c,a[i]=o[c]}return{indices:r,values:a}}function Bt(o,t,e){const s=t.length;if(s===0)return new Int32Array(0);const n=new Float32Array(s);for(let c=0;c<s;c++){const l=o[c*4],u=o[c*4+1],d=o[c*4+2],h=o[c*4+3];n[c]=Math.max(0,d-l)*Math.max(0,h-u)}const r=new Array(s);for(let c=0;c<s;c++)r[c]=c;r.sort((c,l)=>t[l]-t[c]);const a=new Uint8Array(s),i=[];for(let c=0;c<r.length;c++){const l=r[c];if(a[l])continue;i.push(l);const u=o[l*4],d=o[l*4+1],h=o[l*4+2],w=o[l*4+3],g=n[l];for(let f=c+1;f<r.length;f++){const m=r[f];if(a[m])continue;const x=o[m*4],_=o[m*4+1],p=o[m*4+2],E=o[m*4+3],S=Math.max(u,x),M=Math.max(d,_),v=Math.min(h,p),C=Math.min(w,E),k=Math.max(0,v-S),y=Math.max(0,C-M),b=k*y,A=g+n[m]-b;(A>0?b/A:0)>e&&(a[m]=1)}}return Int32Array.from(i)}function Vt(o,t,e,s){if(t.length===0)return new Int32Array(0);const n=new Map;for(let a=0;a<e.length;a++){const i=e[a],c=n.get(i);c===void 0?n.set(i,[a]):c.push(a)}const r=[];for(const a of n.values()){const i=a.length,c=new Float32Array(i*4),l=new Float32Array(i);for(let d=0;d<i;d++){const h=a[d];c[d*4]=o[h*4],c[d*4+1]=o[h*4+1],c[d*4+2]=o[h*4+2],c[d*4+3]=o[h*4+3],l[d]=t[h]}const u=Bt(c,l,s);for(let d=0;d<u.length;d++)r.push(a[u[d]])}return r.sort((a,i)=>t[i]-t[a]),Int32Array.from(r)}function nt(o,t,e){let s=t;if(s.length===3){if(s[0]!==1)throw new Error(`decodeYoloAnchors: expected batch size 1, got ${s[0]}.`);s=[s[1],s[2]]}if(s.length!==2)throw new Error(`decodeYoloAnchors: expected 2-D output after batch removal, got dims=${JSON.stringify(t)}.`);const n=s[0],r=s[1],{numClasses:a,originalWidth:i,originalHeight:c,padLeft:l,padTop:u,scale:d,confThreshold:h,iouThreshold:w,maxDetections:g}=e;if(a<1||a+4>n)throw new Error(`decodeYoloAnchors: invalid numClasses=${a} for channels=${n}.`);if(o.length!==n*r)throw new Error(`decodeYoloAnchors: data length ${o.length} does not match channels*numAnchors=${n*r}.`);const f=[];for(let y=0;y<r;y++){let b=0,A=-1/0;for(let Y=0;Y<a;Y++){const R=o[(4+Y)*r+y];R!==void 0&&R>A&&(A=R,b=Y)}if(A<h)continue;const I=o[y],H=o[r+y],G=o[2*r+y],K=o[3*r+y];let z=I-G/2,$=H-K/2,L=I+G/2,D=H+K/2;z=(z-l)/d,$=($-u)/d,L=(L-l)/d,D=(D-u)/d,z=Math.max(0,Math.min(i,z)),$=Math.max(0,Math.min(c,$)),L=Math.max(0,Math.min(i,L)),D=Math.max(0,Math.min(c,D)),f.push({anchorIdx:y,x1:z,y1:$,x2:L,y2:D,classId:b,confidence:A})}if(f.length===0)return Mt();const m=new Float32Array(f.length*4),x=new Float32Array(f.length),_=new Int32Array(f.length);for(let y=0;y<f.length;y++){const b=f[y];m[y*4]=b.x1,m[y*4+1]=b.y1,m[y*4+2]=b.x2,m[y*4+3]=b.y2,x[y]=b.confidence,_[y]=b.classId}const p=Vt(m,x,_,w);if(p.length===0)return Mt();const E=Array.from(p).slice(0,g),S=E.length,M=new Int32Array(S),v=new Float32Array(S*4),C=new Int32Array(S),k=new Float32Array(S);for(let y=0;y<S;y++){const b=f[E[y]];M[y]=b.anchorIdx,v[y*4]=b.x1,v[y*4+1]=b.y1,v[y*4+2]=b.x2,v[y*4+3]=b.y2,C[y]=b.classId,k[y]=b.confidence}return{anchorIndices:M,boxesXyxy:v,classIds:C,confidences:k}}function Mt(){return{anchorIndices:new Int32Array(0),boxesXyxy:new Float32Array(0),classIds:new Int32Array(0),confidences:new Float32Array(0)}}function yt(o,t,e){const s=t.length===3?t[1]:t[0];if(s===void 0||s<5)throw new Error(`decodeYolo: invalid output channel count ${s} (expected >= 5).`);const n=s-4,r=nt(o,t,{numClasses:n,...e}),a=[];for(let i=0;i<r.classIds.length;i++)a.push({bbox:new ht(r.boxesXyxy[i*4],r.boxesXyxy[i*4+1],r.boxesXyxy[i*4+2],r.boxesXyxy[i*4+3]),classId:r.classIds[i],confidence:r.confidences[i]});return a}let St=!1,At=!1;function ae(o,t,e){return St||(St=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8 is deprecated since 0.2.0; use decodeYolo. The alias will be removed in 0.3.0.")),yt(o,t,e)}function ie(o,t,e){return At||(At=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8Anchors is deprecated since 0.2.0; use decodeYoloAnchors. The alias will be removed in 0.3.0.")),nt(o,t,e)}function wt(o,t,e,s,n){let r=s;if(r.length===4){if(r[0]!==1)throw new Error(`decodeYoloSeg: expected batch size 1 in prototypes, got ${r[0]}.`);r=[r[1],r[2],r[3]]}if(r.length!==3)throw new Error(`decodeYoloSeg: expected 3-D prototypes after batch removal, got dims=${JSON.stringify(s)}.`);const a=r[0],i=r[1],c=r[2],l=t.length===3?t[1]:t[0],u=t.length===3?t[2]:t[1];if(l===void 0||u===void 0)throw new Error(`decodeYoloSeg: cannot read channels/numAnchors from dims=${JSON.stringify(t)}.`);const d=4+n.numClasses+a;if(l!==d)throw new Error(`decodeYoloSeg: channels=${l} does not match 4 + numClasses(${n.numClasses}) + numMaskCoefs(${a}) = ${d}.`);if(e.length!==a*i*c)throw new Error(`decodeYoloSeg: prototype length ${e.length} does not match dims=${JSON.stringify(s)}.`);const h=nt(o,t,{numClasses:n.numClasses,originalWidth:n.originalWidth,originalHeight:n.originalHeight,padLeft:n.padLeft,padTop:n.padTop,scale:n.scale,confThreshold:n.confThreshold,iouThreshold:n.iouThreshold,maxDetections:n.maxDetections});if(h.anchorIndices.length===0)return[];const w=n.maskThreshold??.5,g=c/n.inputWidth,f=i/n.inputHeight,m=i*c,x=(4+n.numClasses)*u,_=[];for(let p=0;p<h.anchorIndices.length;p++){const E=h.anchorIndices[p],S=h.boxesXyxy[p*4],M=h.boxesXyxy[p*4+1],v=h.boxesXyxy[p*4+2],C=h.boxesXyxy[p*4+3],k=new ht(S,M,v,C),y=h.classIds[p],b=h.confidences[p],A=Math.max(0,Math.trunc(v)-Math.trunc(S)),I=Math.max(0,Math.trunc(C)-Math.trunc(M));if(A===0||I===0){_.push({bbox:k,classId:y,confidence:b,mask:new U(new Uint8Array(0),0,0)});continue}const H=S*n.scale+n.padLeft,G=M*n.scale+n.padTop,K=v*n.scale+n.padLeft,z=C*n.scale+n.padTop,$=Math.max(0,Math.floor(H*g)),L=Math.max(0,Math.floor(G*f)),D=Math.min(c,Math.ceil(K*g)),Y=Math.min(i,Math.ceil(z*f));if(D<=$||Y<=L){_.push({bbox:k,classId:y,confidence:b,mask:new U(new Uint8Array(A*I),A,I)});continue}const R=D-$,ot=Y-L,bt=new Float32Array(R*ot);for(let N=0;N<ot;N++){const Ht=L+N;for(let J=0;J<R;J++){const Gt=$+J;let _t=0;for(let q=0;q<a;q++){const Kt=o[x+q*u+E],Jt=e[q*m+Ht*c+Gt];_t+=Kt*Jt}bt[N*R+J]=ce(_t)}}const Xt=he(bt,R,ot,A,I),rt=new Uint8Array(A*I);for(let N=0;N<rt.length;N++)rt[N]=Xt[N]>=w?255:0;_.push({bbox:k,classId:y,confidence:b,mask:new U(rt,A,I)})}return _}function ce(o){if(o>=0)return 1/(1+Math.exp(-o));const t=Math.exp(o);return t/(1+t)}let Et=!1;function le(o,t,e,s,n){return Et||(Et=!0,console.warn("[@ort-vision-sdk/web] decodeYoloV8Seg is deprecated since 0.2.0; use decodeYoloSeg. The alias will be removed in 0.3.0.")),wt(o,t,e,s,n)}function he(o,t,e,s,n){const r=new Float32Array(s*n);if(s===0||n===0||t===0||e===0)return r;if(s===t&&n===e)return r.set(o),r;const a=t/s,i=e/n;for(let c=0;c<n;c++){const l=(c+.5)*i-.5,u=Math.max(0,Math.floor(l)),d=Math.min(e-1,u+1),h=Math.max(0,Math.min(1,l-u));for(let w=0;w<s;w++){const g=(w+.5)*a-.5,f=Math.max(0,Math.floor(g)),m=Math.min(t-1,f+1),x=Math.max(0,Math.min(1,g-f)),_=o[u*t+f],p=o[u*t+m],E=o[d*t+f],S=o[d*t+m],M=_*(1-x)+p*x,v=E*(1-x)+S*x;r[c*s+w]=M*(1-h)+v*h}}return r}class st{constructor(t){this._session=t}_session;get session(){return this._session}}const de=[.485,.456,.406],ue=[.229,.224,.225];class gt extends st{constructor(t,e,s,n,r,a,i){super(t),this._labels=e,this._names=s,this._inputSize=n,this._mean=r,this._std=a,this._applySoftmax=i}_labels;_names;_inputSize;_mean;_std;_applySoftmax;static async create(t,e){const s=await V.create(t,e),n=W(e.labels,{numClasses:e.numClasses}),r={};for(let a=0;a<n.length;a++)r[a]=n[a];return new gt(s,n,r,e.inputSize??[224,224],e.mean??de,e.std??ue,e.applySoftmax??!0)}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await Z(t),r=this._preprocess(n),a=await this._session.run({[this._session.inputName]:r}),i=this._session.outputNames[0];if(i===void 0)throw new Error("Classifier model has no outputs.");const c=a[i];if(c===void 0)throw new Error(`Classifier model output ${i} missing from run() result.`);const l=this._postprocess(c.data),{indices:u,values:d}=Ut(l,e.topK??null),h=[];for(let m=0;m<u.length;m++){const x=u[m],_=this._labels[x]??`class_${x}`;h.push({classId:x,className:_,probability:d[m],cls:x,name:_,conf:d[m]})}if(h.length===0)throw new Error("Classifier produced no probabilities (empty output).");const w=h[0],g={classId:w.classId,className:w.className,confidence:w.probability,cls:w.classId,name:w.className,conf:w.probability,image:n,probabilities:h},f=[n.height,n.width];return[new Nt(new kt(l),g,this._names,n,f,s)]}_preprocess(t){const[e,s]=this._inputSize,n=ft(t,e,s),r=zt(n,this._mean,this._std),a=X(r,n.width,n.height,3);return et(a,[1,3,n.height,n.width])}_postprocess(t){return this._applySoftmax?Yt(t):new Float32Array(t)}}class pt extends st{constructor(t,e,s,n,r,a,i,c){super(t),this._head=e,this._labels=s,this._names=n,this._inputSize=r,this._confThreshold=a,this._iouThreshold=i,this._maxDetections=c}_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;static async create(t,e={}){const s=e.head??"yolo";if(s!=="yolo")throw new Error(`Unsupported detector head '${s}'. Supported: 'yolo'.`);const n=await V.create(t,e),r=W(e.labels??"coco",{numClasses:e.numClasses}),a={};for(let i=0;i<r.length;i++)a[i]=r[i];return new pt(n,s,r,a,e.inputSize??[640,640],e.confThreshold??.25,e.iouThreshold??.45,e.maxDetections??300)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await Z(t),{tensor:r,scale:a,padLeft:i,padTop:c}=this._preprocess(n),l=await this._session.run({[this._session.inputName]:r}),u=this._session.outputNames[0];if(u===void 0)throw new Error("Detector model has no outputs.");const d=l[u];if(d===void 0)throw new Error(`Detector model output ${u} missing from run() result.`);const h=yt(d.data,d.dims,{originalWidth:n.width,originalHeight:n.height,padLeft:i,padTop:c,scale:a,confThreshold:e.confThreshold??this._confThreshold,iouThreshold:e.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections}),g=(e.classes!==void 0?(()=>{const m=new Set(e.classes);return h.filter(x=>m.has(x.classId))})():h).map(m=>this._buildResult(n,m.bbox,m.classId,m.confidence)),f=[n.height,n.width];return[new Lt(this._buildBoxes(g,f),g,this._names,n,f,s)]}_preprocess(t){const[e,s]=this._inputSize,n=mt(t,e,s),r=tt(n.image),a=X(r,n.image.width,n.image.height,3);return{tensor:et(a,[1,3,n.image.height,n.image.width]),scale:n.scale,padLeft:n.padLeft,padTop:n.padTop}}_buildResult(t,e,s,n){const[r,a,i,c]=e.asIntXyxy(),l=Math.max(0,r),u=Math.max(0,a),d=Math.min(t.width,i),h=Math.min(t.height,c);let w;if(d>l&&h>u){const f=d-l,m=h-u,x=new Uint8Array(f*m*3);for(let _=0;_<m;_++){const p=((u+_)*t.width+l)*3;x.set(t.data.subarray(p,p+f*3),_*f*3)}w=new F(x,f,m)}else w=new F(new Uint8Array(0),0,0);const g=this._names[s]??`class_${s}`;return{classId:s,className:g,confidence:n,bbox:e,cls:s,name:g,conf:n,box:e,croppedImage:w}}_buildBoxes(t,e){const s=t.length,n=new Float32Array(s*4),r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=t[i];n[i*4]=c.bbox.x1,n[i*4+1]=c.bbox.y1,n[i*4+2]=c.bbox.x2,n[i*4+3]=c.bbox.y2,r[i]=c.classId,a[i]=c.confidence}return new dt(n,r,a,e)}}class xt extends st{constructor(t,e,s,n,r,a,i,c,l){super(t),this._head=e,this._labels=s,this._names=n,this._inputSize=r,this._confThreshold=a,this._iouThreshold=i,this._maxDetections=c,this._maskThreshold=l}_head;_labels;_names;_inputSize;_confThreshold;_iouThreshold;_maxDetections;_maskThreshold;static async create(t,e={}){const s=e.head??"yolo-seg";if(s!=="yolo-seg")throw new Error(`Unsupported segmenter head '${s}'. Supported: 'yolo-seg'.`);const n=await V.create(t,e),r=W(e.labels??"coco",{numClasses:e.numClasses}),a={};for(let i=0;i<r.length;i++)a[i]=r[i];return new xt(n,s,r,a,e.inputSize??[640,640],e.confThreshold??.25,e.iouThreshold??.45,e.maxDetections??300,e.maskThreshold??.5)}get head(){return this._head}get labels(){return this._labels}get names(){return this._names}get numClasses(){return this._labels.length}async call(t,e={}){return this.predict(t,e)}async predict(t,e={}){const s=typeof t=="string"?t:null,n=await Z(t),{tensor:r,scale:a,padLeft:i,padTop:c}=this._preprocess(n),l=await this._session.run({[this._session.inputName]:r}),{perAnchor:u,prototypes:d}=this._splitOutputs(l),h=wt(u.data,u.dims,d.data,d.dims,{numClasses:this._labels.length,inputWidth:this._inputSize[0],inputHeight:this._inputSize[1],originalWidth:n.width,originalHeight:n.height,padLeft:i,padTop:c,scale:a,confThreshold:e.confThreshold??this._confThreshold,iouThreshold:e.iouThreshold??this._iouThreshold,maxDetections:this._maxDetections,maskThreshold:this._maskThreshold}),g=(e.classes!==void 0?(()=>{const m=new Set(e.classes);return h.filter(x=>m.has(x.classId))})():h).map(m=>this._buildResult(n,m.bbox,m.classId,m.confidence,m.mask)),f=[n.height,n.width];return[new Ot(this._buildBoxes(g,f),this._buildMasks(g,f),g,this._names,n,f,s)]}_preprocess(t){const[e,s]=this._inputSize,n=mt(t,e,s),r=tt(n.image),a=X(r,n.image.width,n.image.height,3);return{tensor:et(a,[1,3,n.image.height,n.image.width]),scale:n.scale,padLeft:n.padLeft,padTop:n.padTop}}_splitOutputs(t){let e,s;for(const n of this._session.outputNames){const r=t[n];r!==void 0&&(r.dims.length===3&&e===void 0?e=r:r.dims.length===4&&s===void 0&&(s=r))}if(e===void 0||s===void 0){const n=this._session.outputNames.map(r=>`${r}: ${JSON.stringify(t[r]?.dims??[])}`);throw new Error(`Segmenter expected one 3-D and one 4-D output, got [${n.join(", ")}].`)}return{perAnchor:e,prototypes:s}}_buildResult(t,e,s,n,r){const[a,i,c,l]=e.asIntXyxy(),u=Math.max(0,a),d=Math.max(0,i),h=Math.min(t.width,c),w=Math.min(t.height,l);let g,f=r;if(h>u&&w>d&&r.data.length>0){const x=h-u,_=w-d,p=Math.min(r.width,x),E=Math.min(r.height,_),S=new Uint8Array(p*E*3);for(let M=0;M<E;M++){const v=((d+M)*t.width+u)*3,C=M*p*3,k=M*r.width;for(let y=0;y<p;y++)if(r.data[k+y]!==0){const A=v+y*3,I=C+y*3;S[I]=t.data[A],S[I+1]=t.data[A+1],S[I+2]=t.data[A+2]}}if(g=new F(S,p,E),p!==r.width||E!==r.height){const M=new Uint8Array(p*E);for(let v=0;v<E;v++)M.set(r.data.subarray(v*r.width,v*r.width+p),v*p);f=new U(M,p,E)}}else f=new U(new Uint8Array(0),0,0),g=new F(new Uint8Array(0),0,0);const m=this._names[s]??`class_${s}`;return{classId:s,className:m,confidence:n,bbox:e,cls:s,name:m,conf:n,box:e,mask:f,segmentedImage:g}}_buildBoxes(t,e){const s=t.length,n=new Float32Array(s*4),r=new Int32Array(s),a=new Float32Array(s);for(let i=0;i<s;i++){const c=t[i];n[i*4]=c.bbox.x1,n[i*4+1]=c.bbox.y1,n[i*4+2]=c.bbox.x2,n[i*4+3]=c.bbox.y2,r[i]=c.classId,a[i]=c.confidence}return new dt(n,r,a,e)}_buildMasks(t,e){const s=new Float32Array(t.length*4);for(let n=0;n<t.length;n++){const r=t[n];s[n*4]=r.bbox.x1,s[n*4+1]=r.bbox.y1,s[n*4+2]=r.bbox.x2,s[n*4+3]=r.bbox.y2}return new $t(t.map(n=>n.mask),s,e)}}const fe="0.2.1",It={video:{facingMode:{ideal:"environment"},width:{ideal:1920},height:{ideal:1080}},audio:!1};function Tt(o){if(typeof window>"u")return{kind:"unsupported",message:"Camera is unavailable in this environment."};if(!window.isSecureContext)return{kind:"insecure",message:"Camera access requires a secure (HTTPS) connection."};if(o instanceof DOMException)switch(o.name){case"NotAllowedError":case"SecurityError":return{kind:"permission-denied",message:"Camera permission denied. Enable access in your browser settings."};case"NotFoundError":case"OverconstrainedError":return{kind:"no-camera",message:"No camera available on this device."};case"NotReadableError":case"AbortError":return{kind:"in-use",message:"The camera is in use by another app. Close it and try again."}}return{kind:"unknown",message:o instanceof Error?o.message:"Unexpected error while accessing the camera."}}function me(o={}){const[t,e]=O.useState("loading"),[s,n]=O.useState(null),[r,a]=O.useState(0),i=O.useRef(null),c=O.useRef(o.constraints??It);return c.current=o.constraints??It,O.useEffect(()=>{let l=!1,u=null,d=null;async function h(){if(e("loading"),n(null),typeof navigator>"u"||!navigator.mediaDevices||typeof navigator.mediaDevices.getUserMedia!="function"){l||(n(typeof window<"u"&&!window.isSecureContext?Tt(null):{kind:"unsupported",message:"Camera capture is not supported in this browser."}),e("error"));return}try{if(u=await navigator.mediaDevices.getUserMedia(c.current),l){u.getTracks().forEach(g=>g.stop());return}const w=i.current;if(!w){u.getTracks().forEach(g=>g.stop());return}d=w,w.srcObject=u,await w.play().catch(()=>{}),l||e("ready")}catch(w){l||(n(Tt(w)),e("error"))}}return h(),()=>{l=!0,u&&u.getTracks().forEach(w=>w.stop()),d&&(d.srcObject=null)}},[r]),{status:t,error:s,videoRef:i,retry:()=>a(l=>l+1)}}const Pt=256;function ye(o){return o instanceof HTMLVideoElement?{width:o.videoWidth,height:o.videoHeight}:o instanceof HTMLCanvasElement?{width:o.width,height:o.height}:{width:o.naturalWidth||o.width,height:o.naturalHeight||o.height}}function jt(o,t){const{width:e,height:s}=ye(o);if(e===0||s===0)return 0;const n=Math.min(1,Pt/Math.max(e,s)),r=Math.max(1,Math.round(e*n)),a=Math.max(1,Math.round(s*n)),i=t??document.createElement("canvas");i.width=r,i.height=a;const c=i.getContext("2d",{willReadFrequently:!0});if(!c)return 0;c.drawImage(o,0,0,r,a);const l=c.getImageData(0,0,r,a).data;let u=0;const d=r*a;for(let h=0;h<l.length;h+=4)u+=.2126*l[h]+.7152*l[h+1]+.0722*l[h+2];return u/d}function we(o,t){return o>=t}class ge extends Error{luminance;threshold;constructor(t,e){super("Image is too dark to analyse. Capture again in a brighter environment."),this.name="LowLuminanceError",this.luminance=t,this.threshold=e}}function pe(o,{enabled:t=!0,intervalMs:e=160}={}){const[s,n]=O.useState(0),r=O.useRef(null);return O.useEffect(()=>{if(!t||typeof window>"u")return;r.current||(r.current=document.createElement("canvas"));let a=0,i=0;const c=l=>{const u=o.current;if(!u||u.readyState<2||u.videoWidth===0){a=window.requestAnimationFrame(c);return}l-i>=e&&(i=l,n(jt(u,r.current??void 0))),a=window.requestAnimationFrame(c)};return a=window.requestAnimationFrame(c),()=>window.cancelAnimationFrame(a)},[o,t,e]),s}exports.BoundingBox=ht;exports.Boxes=dt;exports.COCO_CLASSES=Ft;exports.ClassificationResults=Nt;exports.Classifier=gt;exports.DEFAULT_PROVIDERS=it;exports.DetectionResults=Lt;exports.Detector=pt;exports.ImageLoadError=T;exports.InferenceError=at;exports.LUMINANCE_SAMPLE_MAX_EDGE=Pt;exports.LabelMapError=j;exports.LowLuminanceError=ge;exports.Mask=U;exports.Masks=$t;exports.ModelLoadError=Ct;exports.OrtSession=V;exports.OrtVisionError=B;exports.Probs=kt;exports.ProviderNotAvailableError=Wt;exports.RGBImage=F;exports.SegmentationResults=Ot;exports.Segmenter=xt;exports.VERSION=fe;exports.VisionTask=st;exports.batchedNms=Vt;exports.computeImageLuminance=jt;exports.decodeYolo=yt;exports.decodeYoloAnchors=nt;exports.decodeYoloSeg=wt;exports.decodeYoloV8=ae;exports.decodeYoloV8Anchors=ie;exports.decodeYoloV8Seg=le;exports.fromCv2=oe;exports.isLuminanceAcceptable=we;exports.letterbox=mt;exports.loadImage=Z;exports.nms=Bt;exports.normalize=zt;exports.resize=ft;exports.resolveLabels=W;exports.resolveProviders=Dt;exports.softmax=Yt;exports.toCHW=X;exports.toCv2=re;exports.toFloat32=tt;exports.toFloat32Tensor=et;exports.toTensor=se;exports.topK=Ut;exports.useCameraStream=me;exports.useLiveLuminance=pe;
2
2
  //# sourceMappingURL=vision.cjs.map