framefields 2.2.0-beta.1 → 2.2.0-beta.3
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/effects/index.d.mts +1 -1
- package/dist/effects/index.mjs +1 -1
- package/dist/{index-DLbtJPrc.d.mts → index-YSmPWdZw.d.mts} +31 -10
- package/dist/index-YSmPWdZw.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +3 -3
- package/dist/preview/index.mjs +1 -1
- package/dist/{preview-BQ3BhcVX.mjs → preview-QjhWe5QE.mjs} +2 -2
- package/dist/{preview-BQ3BhcVX.mjs.map → preview-QjhWe5QE.mjs.map} +1 -1
- package/dist/preview-engine/{chunk-Z457JJCO.mjs → chunk-3NXJVSZY.mjs} +1 -1
- package/dist/preview-engine/chunk-NECOKNHA.mjs +174 -0
- package/dist/preview-engine/chunk-X6HUH6K4.mjs +1 -0
- package/dist/preview-engine/{chunk-HFUA47QH.mjs → chunk-XHDUZY7Y.mjs} +1 -1
- package/dist/preview-engine/effects.mjs +1 -1
- package/dist/preview-engine/framefields.mjs +1 -1
- package/dist/preview-engine/player.mjs +1 -1
- package/dist/preview-engine/{renderers-HVTQ2B3H.mjs → renderers-VFV5QWO3.mjs} +1 -1
- package/dist/renderer/index.mjs +1 -1
- package/dist/renderer-Bxa4SDm9.mjs +3 -0
- package/dist/{renderer-dc39dBVA.mjs → renderer-CE7fwGgw.mjs} +1015 -110
- package/dist/renderer-CE7fwGgw.mjs.map +1 -0
- package/dist/{renderer-DzDZlez7.mjs → renderer-CivH0UuT.mjs} +6 -3
- package/dist/renderer-CivH0UuT.mjs.map +1 -0
- package/package.json +55 -55
- package/dist/index-DLbtJPrc.d.mts.map +0 -1
- package/dist/preview-engine/chunk-7OMK352W.mjs +0 -1
- package/dist/preview-engine/chunk-K7M5EIFC.mjs +0 -98
- package/dist/renderer-CZKFEf_6.mjs +0 -3
- package/dist/renderer-DzDZlez7.mjs.map +0 -1
- package/dist/renderer-dc39dBVA.mjs.map +0 -1
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import{a as je}from"./chunk-VXUJWPIK.mjs";import{a as mt}from"./chunk-XKO4RYKN.mjs";import{i as ft}from"./chunk-7OMK352W.mjs";import{I as ot,a as S,b as L}from"./chunk-NQZKBFC6.mjs";import{b as rt,h as be,i as Ee,l as te,z as st}from"./chunk-63SFBHEH.mjs";import{a as ct,e as lt,i as Ve,j as dt}from"./chunk-4GEWSUX5.mjs";import{b as ye,c as it,e as at}from"./chunk-MTJHQATU.mjs";import{d as ut,e as xe,g as Ae}from"./chunk-YAQFFQPG.mjs";import{a as Jt,c as en,d as A,e as y}from"./chunk-BVF64D6H.mjs";var At=Jt((wo,Vt)=>{y();var we=()=>new Proxy(function(){},{get:(r,e)=>e==="__esModule"?!0:e===Symbol.toPrimitive?()=>"":we(),apply:()=>we(),construct:()=>we()});Vt.exports=we()});y();y();var ht=class{signals;config;_frameHooks=[];constructor(e){this.config={...e},this.signals={};for(let[t,n]of Object.entries(e))be(n)?this.signals[t]=n:this.signals[t]=Ee(n);return new Proxy(this,{get(t,n,s){if(typeof n=="string"&&n in t.signals){let o=t.signals[n];return o?o.value:void 0}return Reflect.get(t,n,s)},set(t,n,s,o){if(typeof n=="string"&&n in t.signals){let i=t.signals[n];return be(s)?(t.signals[n]=s,t.config[n]=s):i&&typeof i.set=="function"?(i.set(s),t.config[n]=s):(t.signals[n]=Ee(s),t.config[n]=s),!0}return Reflect.set(t,n,s,o)}})}onRequestFrame(e){return this._frameHooks.push(e),this}notifyFrame(e){for(let t of this._frameHooks)t(e)}resolveUniforms(e){let t={};for(let[n,s]of Object.entries(this.signals))t[n]=s.get(e);return t}resolveTextures(){let e={};for(let[t,n]of Object.entries(this.signals)){let s=n.gpuBinding;s?.textureView&&(e[`${t}TextureView`]=s.textureView),s?.texture&&(e[`${t}Texture`]=s.texture)}return e}toOperation(){let e={op:this.op,effect:this,onRequestFrame:t=>this.notifyFrame(t),signals:this.signals,...this.config};for(let[t,n]of Object.entries(this.signals)){let s=this.config[t];!be(s)&&(typeof s=="string"||typeof s=="boolean")?e[t]=n.value:e[t]=n}return e}};y();var gt=class extends Error{constructor(e){super(e),this.name="ModerationError"}},pt=class extends Error{constructor(e){super(e),this.name="ModerationUnrefundableError"}};y();y();y();var bt="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";var yt=(r=21)=>{let e="",t=crypto.getRandomValues(new Uint8Array(r|=0));for(;r--;)e+=bt[t[r]&63];return e};var tr=r=>yt(r);y();var xt=L.object({dataTypes:L.custom(),label:L.string(),required:L.boolean().optional(),order:L.number(),description:L.string().optional()}),tn=L.object({configKey:L.string(),dataTypes:L.custom(),label:L.string(),description:L.string().optional()}),sr=L.object({type:L.string().min(1),version:L.number().int().positive().optional(),baseType:L.string().optional(),displayName:L.string().min(1),description:L.string().optional(),showInQuickAccess:L.boolean().optional(),showInSidebar:L.boolean().optional(),advanced:L.boolean().optional(),category:L.string().min(1),subcategory:L.string().optional(),defaultDimensions:L.object({width:L.number().positive().optional(),height:L.number().positive().optional()}).optional(),handles:L.object({inputs:L.array(xt),outputs:L.array(xt)}),variableInputs:L.discriminatedUnion("enabled",[L.object({enabled:L.literal(!0),dataTypes:L.custom()}),L.object({enabled:L.literal(!1),dataTypes:L.custom().optional()})]).optional(),variableOutputs:L.discriminatedUnion("enabled",[L.object({enabled:L.literal(!0),dataTypes:L.custom()}),L.object({enabled:L.literal(!1),dataTypes:L.custom().optional()})]).optional(),isTerminal:L.boolean().optional(),isTransient:L.boolean().optional(),configSchema:L.custom().optional(),defaultConfig:L.record(L.string(),L.unknown()).optional(),validation:L.any().optional(),resultSchema:L.custom().optional(),configHandles:L.array(tn).optional()});y();var cr={Text:{bg:"bg-blue-500",stroke:"stroke-blue-500",hex:"#3b82f6",text:"text-blue-500",border:"border-blue-500"},Image:{bg:"bg-purple-500",stroke:"stroke-purple-500",hex:"#a855f7",text:"text-purple-500",border:"border-purple-500"},Video:{bg:"bg-teal-500",stroke:"stroke-teal-500",hex:"oklch(60% 0.118 184.704)",text:"text-teal-500",border:"border-teal-500"},Audio:{bg:"bg-orange-500",stroke:"stroke-orange-500",hex:"#f97316",text:"text-orange-500",border:"border-orange-500"},Number:{bg:"bg-green-600",stroke:"stroke-green-600",hex:"#16a34a",text:"text-green-600",border:"border-green-600"},Boolean:{bg:"bg-amber-600",stroke:"stroke-amber-600",hex:"#d97706",text:"text-amber-600",border:"border-amber-600"},SVG:{bg:"bg-lime-600",stroke:"stroke-lime-600",hex:"#65a30d",text:"text-lime-600",border:"border-lime-600"},Caption:{bg:"bg-rose-500",stroke:"stroke-rose-500",hex:"#f43f5e",text:"text-rose-500",border:"border-rose-500"},GIF:{bg:"bg-pink-500",stroke:"stroke-pink-500",hex:"#ec4899",text:"text-pink-500",border:"border-pink-500"},Lottie:{bg:"bg-indigo-500",stroke:"stroke-indigo-500",hex:"#6366f1",text:"text-indigo-500",border:"border-indigo-500"},Signal:{bg:"bg-fuchsia-500",stroke:"stroke-fuchsia-500",hex:"#d946ef",text:"text-fuchsia-500",border:"border-fuchsia-500"},LUT:{bg:"bg-cyan-600",stroke:"stroke-cyan-600",hex:"#0891b2",text:"text-cyan-600",border:"border-cyan-600"}};y();var mr={Image:"png",Video:"mp4",Audio:"mp3",Text:"txt",Number:"txt",Boolean:"txt",SVG:"svg",Caption:"srt",Lottie:"json",ThreeD:"glb",GIF:"gif",Signal:"txt",LUT:"cube"},fr={png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",mp4:"video/mp4",webm:"video/webm",mov:"video/quicktime",mp3:"audio/mpeg",wav:"audio/wav",ogg:"audio/ogg",pdf:"application/pdf",json:"application/json",txt:"text/plain",srt:"text/srt",aac:"audio/aac",flac:"audio/flac"},nn=["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","copy","xor","multiply","screen","overlay","darken","lighten","color-dodge","color-burn","hard-light","soft-light","difference","exclusion","hue","saturation","color","luminosity"],Tt=S.enum(nn),kt=class r{static schemas=new Map;static register(e,t){r.schemas.set(e,t)}static get(e){return r.schemas.get(e)}static clear(){r.schemas.clear()}};y();var pr={FONT_FAMILY:"Inter",FONT_SIZE:64,FILL:"#ffffff",LINE_HEIGHT:1.1,ALIGN:"left",VERTICAL_ALIGN:"top",LETTER_SPACING:0,STROKE_WIDTH:0,STROKE:"#000000",CORNER_RADIUS:0,PADDING:0};y();var rn=S.enum(["fade-in","fade-out","slide-in-left","slide-in-right","slide-in-top","slide-in-bottom","zoom-in","zoom-out","rotate-cw","rotate-ccw","bounce","shake"]),on=S.object({id:S.string(),type:rn,value:S.number()}),sn=S.object({x:S.number().default(0),y:S.number().default(0),width:S.number().optional(),height:S.number().optional(),rotation:S.number().default(0),scale:S.number().min(0).default(1),opacity:S.number().min(0).max(1).default(1),zIndex:S.number().int().optional(),blendMode:Tt.optional()}),an=S.object({startFrame:S.number().int().min(0).default(0),durationInMS:S.number().int().min(1).optional(),trimStart:S.number().int().min(0).optional(),trimEnd:S.number().int().min(0).optional(),speed:S.number().min(.25).max(4).optional()}),cn=S.object({text:S.string().optional(),fontSize:S.number().optional(),fontFamily:S.string().optional(),fontStyle:S.string().optional(),fontWeight:S.union([S.number(),S.string()]).optional(),fill:S.string().optional(),align:S.string().optional(),direction:S.enum(["ltr","rtl","auto"]).optional(),verticalAlign:S.string().optional(),letterSpacing:S.number().optional(),lineHeight:S.number().optional(),padding:S.number().optional(),stroke:S.string().optional(),strokeWidth:S.number().optional(),strokeAlign:S.enum(["inside","center","outside"]).optional(),textShadow:S.string().optional(),textBackgroundColor:S.string().optional(),shadows:S.array(S.object({color:S.string(),offset:S.object({x:S.number(),y:S.number()}).optional(),blurRadius:S.number().optional()})).optional(),textAnimation:S.any().optional(),bottomPadding:S.number().optional(),maxWidth:S.number().optional()}),ln=S.object({backgroundColor:S.string().optional(),borderColor:S.string().optional(),borderWidth:S.number().optional(),strokeRadius:S.number().optional(),autoDimensions:S.boolean().optional()}),De=S.enum(["Video","Image","Audio","Text","Caption","SVG","GIF","Lottie"]),St=sn.merge(an).merge(cn).merge(ln).extend({type:De.optional(),animations:S.array(on).optional()});y();y();var dn=S.enum(["none","crossfade","wipe-left","wipe-right","slide-up","slide-down"]),Be=S.object({type:dn,durationFrames:S.number().min(1)});var Or=St.extend({id:S.string(),name:S.string().optional(),type:De,virtualMedia:ot.optional(),transitionIn:Be.optional(),transitionOut:Be.optional(),durationInMS:S.number(),maxDurationInMS:S.number().optional(),src:S.string().optional(),volume:S.number().default(1),inputHandleId:S.string().optional(),isPlaceholder:S.boolean().optional(),lockAspect:S.boolean().optional(),lockRatio:S.boolean().optional(),zIndex:S.number().optional()}).strict();y();function _t(r,e){if(r===e||typeof r=="number"&&typeof e=="number"&&Number.isNaN(r)&&Number.isNaN(e))return!0;if(r&&e&&typeof r=="object"&&typeof e=="object"){if(r.constructor!==e.constructor)return!1;if(Array.isArray(r)){if(r.length!==e.length)return!1;for(let n=0;n<r.length;n++)if(!_t(r[n],e[n]))return!1;return!0}if(r instanceof Date)return r.getTime()===e.getTime();if(r instanceof RegExp)return r.toString()===e.toString();let t=Object.keys(r);if(t.length!==Object.keys(e).length)return!1;for(let n of t)if(!Object.hasOwn(e,n)||!_t(r[n],e[n]))return!1;return!0}return!1}y();function Ar(r,e){if(!r?.sourceFingerprint)return null;let t=je(e);return r.sourceFingerprint!==t?null:r}function Dr(r,e){return{...r,sourceFingerprint:je(e)}}y();function Ur(r){try{let t=r.toString("utf-8").match(/<svg[^>]*>/i);if(!t)return null;let n=t[0],s=a=>{let c=n.match(new RegExp(`${a}=["']([^"']+)["']`,"i"));if(!c)return null;let h=parseFloat(c[1].replace(/[a-z%]/gi,""));return isNaN(h)?null:h},o=s("width"),i=s("height"),m=n.match(/viewBox=["']([^"']+)["']/i);if(m){let a=m[1].split(/[\s,]+/).map(parseFloat);if(a.length>=4&&!isNaN(a[2])&&!isNaN(a[3])){let c=a[2],h=a[3];o==null&&i==null?(o=c,i=h):o!=null&&i==null?i=o*(h/c):i!=null&&o==null&&(o=i*(c/h))}}return o!=null&&i!=null&&o>0&&i>0?{w:o,h:i}:null}catch(e){return console.error("Error parsing SVG dimensions",e),null}}y();y();y();y();var se=(r,e,t)=>`https://huggingface.co/${r}/resolve/${e}/${t}`,Fe="tori29umai/rtmdet-ins-onnx-person-masks",Ue="e5218e8d47d7c50077f27f9893a28e0ef2ad5656",He="RTMDet-Ins (OpenMMLab mmdetection, Apache-2.0), mmdeploy ONNX export",ze="RTMO (OpenMMLab mmpose, Apache-2.0), ONNX export",wt="MediaPipe Selfie Segmenter (Google, Apache-2.0), ONNX export",oe={"rtmdet-ins-t":{key:"rtmdet-ins-t",family:"rtmdet-ins",filename:"rtmdet-ins-t.onnx",url:se(Fe,Ue,"models/tiny/rtmdet-ins_tiny_640x640.onnx"),bytes:24032356,sha256:"99ee670820c4493aa13e20578b6a8e39781c319846dc41796a77765734beac5c",input:[640,640],source:He},"rtmdet-ins-s":{key:"rtmdet-ins-s",family:"rtmdet-ins",filename:"rtmdet-ins-s.onnx",url:se(Fe,Ue,"models/s/rtmdet-ins_s_640x640.onnx"),bytes:43236976,sha256:"9cd1787fbf3eb2bd64cc2d8154f41c036f5484b85599e1cbe0227a2dc5a4a08b",input:[640,640],source:He},"rtmdet-ins-m":{key:"rtmdet-ins-m",family:"rtmdet-ins",filename:"rtmdet-ins-m.onnx",url:se(Fe,Ue,"models/m/rtmdet-ins_m_640x640.onnx"),bytes:115735979,sha256:"a003bce80316e03467c2c2b483df2d5c0398220c2357a44f9bf985929f7eb052",input:[640,640],source:He},"rtmo-t":{key:"rtmo-t",family:"rtmo",filename:"rtmo-t.onnx",url:se("Xenova/RTMO-t","f29d13ce5ee291fbe505e9d06a049992f05699dc","onnx/model.onnx"),bytes:27360569,sha256:"cb54dd042bf997df86d938721c94238940f2bff98ba8d2ccc834430de5c65c13",input:[416,416],source:ze},"rtmo-s":{key:"rtmo-s",family:"rtmo",filename:"rtmo-s.onnx",url:se("Xenova/RTMO-s","d8c526187f341d287753831c9c8b1ecc4855bba1","onnx/model.onnx"),bytes:39636400,sha256:"1cd6a3517658903233e9ccb78666554862f57f0944b6886e2048093594aef4e7",input:[640,640],source:ze},"rtmo-m":{key:"rtmo-m",family:"rtmo",filename:"rtmo-m.onnx",url:se("Xenova/RTMO-m","3aba1280472b98ee2bf663482e27e243038d23fe","onnx/model.onnx"),bytes:89291929,sha256:"76d82c45e5c4810baf587ecf2638d15cbf7ed196afb2055e977382532021b59e",input:[640,640],source:ze},"selfie-square":{key:"selfie-square",family:"selfie",filename:"selfie-square.onnx",url:se("onnx-community/mediapipe_selfie_segmentation","be49485c8e027524be38591817fc5cd31bd9d00e","onnx/model.onnx"),bytes:462352,sha256:"3241ac4ad8aa35bdaf33946776db29f7c283a413aa0b0dacb9483594b4531aad",input:[256,256],source:wt},"selfie-landscape":{key:"selfie-landscape",family:"selfie",filename:"selfie-landscape.onnx",url:se("onnx-community/mediapipe_selfie_segmentation_landscape","2497d5bec26c626c7b3c4edc6e1fefc21b64f6c3","onnx/model.onnx"),bytes:462338,sha256:"7a0adcfdb1715d3b0ff0f61486d8a181c33f4a208343a5abfccbc6540e872d24",input:[256,144],source:wt}};function vt(r,e="s",t=1){return r==="pose"?`rtmo-${e}`:r==="matte"?t>1.3?"selfie-landscape":"selfie-square":`rtmdet-ins-${e}`}var ke=["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"];y();function ae(r,e){let t=Math.max(r.originX,e.originX),n=Math.max(r.originY,e.originY),s=Math.min(r.originX+r.width,e.originX+e.width),o=Math.min(r.originY+r.height,e.originY+e.height),i=Math.max(0,s-t),m=Math.max(0,o-n),a=i*m;if(a<=0)return 0;let c=r.width*r.height,h=e.width*e.height,l=c+h-a;return l>0?a/l:0}var ue=class{_nextTrackId=1;_tracks=[];_iouThreshold;_maxMissedFrames;_minHits;_positionSmoothing;_activeDuringCoast;constructor(e={}){this._iouThreshold=e.iouThreshold??.25,this._maxMissedFrames=e.maxMissedFrames??15,this._minHits=e.minHits??1,this._positionSmoothing=Math.max(.1,Math.min(1,e.positionSmoothing??.75)),this._activeDuringCoast=e.activeDuringCoast!==!1}update(e,t,n=24){let s=n>0?1/n:.0416;for(let a of this._tracks)if(a.age++,a.timeSinceUpdate++,a.timeSinceUpdate>0){let c=a.centerX+a.vx*s,h=a.centerY+a.vy*s;a.centerX=c,a.centerY=h;let l=a.box.width,p=a.box.height,T=a.box.normalizedWidth,j=a.box.normalizedHeight,P=a.box.normalizedX+T/2+a.vx*s/Math.max(1,a.box.width/T),E=a.box.normalizedY+j/2+a.vy*s/Math.max(1,a.box.height/j);a.box={originX:c-l/2,originY:h-p/2,width:l,height:p,normalizedX:P-T/2,normalizedY:E-j/2,normalizedWidth:T,normalizedHeight:j},a.vx*=.92,a.vy*=.92,a.isCoasting=!0}let o=new Set,i=new Set,m=[];for(let a=0;a<this._tracks.length;a++){let c=this._tracks[a];for(let h=0;h<e.length;h++){let l=e[h];if(c.category!==l.category)continue;let p=ae(c.box,l.boundingBox);p>=this._iouThreshold&&m.push({trackIdx:a,detIdx:h,iou:p})}}m.sort((a,c)=>c.iou-a.iou);for(let a of m){if(i.has(a.trackIdx)||o.has(a.detIdx))continue;i.add(a.trackIdx),o.add(a.detIdx);let c=this._tracks[a.trackIdx],h=e[a.detIdx],l=h.boundingBox,p=l.originX+l.width/2,T=l.originY+l.height/2,j=(p-c.centerX)/s,P=(T-c.centerY)/s;c.vx=c.vx*.4+j*.6,c.vy=c.vy*.4+P*.6;let E=this._positionSmoothing,g=c.box.width*(1-E)+l.width*E,_=c.box.height*(1-E)+l.height*E,b=c.centerX*(1-E)+p*E,V=c.centerY*(1-E)+T*E;c.centerX=b,c.centerY=V,c.score=h.score,c.timeSinceUpdate=0,c.hits++,c.isCoasting=!1,c.hits>=this._minHits&&(c.active=!0),c.box={originX:b-g/2,originY:V-_/2,width:g,height:_,normalizedX:l.normalizedX,normalizedY:l.normalizedY,normalizedWidth:l.normalizedWidth,normalizedHeight:l.normalizedHeight}}for(let a=0;a<e.length;a++){if(o.has(a))continue;let c=e[a],h=c.boundingBox,l=h.originX+h.width/2,p=h.originY+h.height/2;this._tracks.push({trackId:this._nextTrackId++,category:c.category,score:c.score,box:h,centerX:l,centerY:p,vx:0,vy:0,age:1,hits:1,timeSinceUpdate:0,active:this._minHits<=1,isCoasting:!1})}return this._tracks=this._tracks.filter(a=>a.timeSinceUpdate<=this._maxMissedFrames),this._tracks.filter(a=>a.active).map(a=>{let c=a.box.normalizedWidth,h=a.box.normalizedHeight,l=a.box.normalizedX+c/2,p=a.box.normalizedY+h/2,T=Math.sqrt(a.vx*a.vx+a.vy*a.vy);return{trackId:a.trackId,category:a.category,score:a.score,boundingBox:a.box,centerX:a.centerX,centerY:a.centerY,normalizedCenterX:l,normalizedCenterY:p,velocity:{vx:a.vx,vy:a.vy},speed:T,age:a.age,hits:a.hits,active:this._activeDuringCoast?!0:a.timeSinceUpdate===0,isCoasting:a.isCoasting}})}trackSequence(e,t,n=24){this.reset();let s=n>0?1/n:.0416,o=new Map;for(let a=0;a<t;a++){let c=e[a]??[],h=this.update(c,a,n);for(let l of h)o.has(l.trackId)||o.set(l.trackId,{category:l.category,observations:[]}),l.isCoasting||o.get(l.trackId).observations.push({frame:a,box:l.boundingBox,score:l.score})}let i=new Map;for(let[a,c]of o.entries()){let h=c.observations;if(h.length===0)continue;h.sort((g,_)=>g.frame-_.frame);let l=new Array(t).fill(null);for(let g of h)l[g.frame]={box:g.box,score:g.score};let p=h[0];for(let g=0;g<p.frame;g++)l[g]={box:{...p.box},score:p.score*.9};for(let g=0;g<h.length-1;g++){let _=h[g],b=h[g+1],V=b.frame-_.frame;if(!(V<=1))for(let f=_.frame+1;f<b.frame;f++){let d=(f-_.frame)/V,M=_.box.originX+(b.box.originX-_.box.originX)*d,x=_.box.originY+(b.box.originY-_.box.originY)*d,w=_.box.width+(b.box.width-_.box.width)*d,I=_.box.height+(b.box.height-_.box.height)*d,U=_.box.normalizedX+(b.box.normalizedX-_.box.normalizedX)*d,C=_.box.normalizedY+(b.box.normalizedY-_.box.normalizedY)*d,v=_.box.normalizedWidth+(b.box.normalizedWidth-_.box.normalizedWidth)*d,u=_.box.normalizedHeight+(b.box.normalizedHeight-_.box.normalizedHeight)*d;l[f]={box:{originX:M,originY:x,width:w,height:I,normalizedX:U,normalizedY:C,normalizedWidth:v,normalizedHeight:u},score:_.score*(1-d)+b.score*d}}}let T=h[h.length-1];for(let g=T.frame+1;g<t;g++)l[g]={box:{...T.box},score:T.score*.9};let j=[],P=2,E=[.06136,.24477,.38774,.24477,.06136];for(let g=0;g<t;g++){let _=0,b=0,V=0,f=0,d=0,M=0,x=0,w=0,I=0,U=0;for(let v=-P;v<=P;v++){let u=Math.max(0,Math.min(t-1,g+v)),R=l[u];if(R){let z=E[v+P];_+=z,b+=R.box.originX*z,V+=R.box.originY*z,f+=R.box.width*z,d+=R.box.height*z,M+=R.box.normalizedX*z,x+=R.box.normalizedY*z,w+=R.box.normalizedWidth*z,I+=R.box.normalizedHeight*z,U+=R.score*z}}let C=_>0?1/_:1;j.push({box:{originX:b*C,originY:V*C,width:f*C,height:d*C,normalizedX:M*C,normalizedY:x*C,normalizedWidth:w*C,normalizedHeight:I*C},score:U*C})}i.set(a,{category:c.category,frames:j})}let m=[];for(let a=0;a<t;a++){let c=[];for(let[h,l]of i.entries()){let p=l.frames[a];if(!p)continue;let T=l.frames[Math.max(0,a-1)]??p,j=l.frames[Math.min(t-1,a+1)]??p,P=p.box.originX+p.box.width/2,E=p.box.originY+p.box.height/2,g=T.box.originX+T.box.width/2,_=T.box.originY+T.box.height/2,b=j.box.originX+j.box.width/2,V=j.box.originY+j.box.height/2,f=a===0||a===t-1?s:2*s,d=(b-g)/f,M=(V-_)/f,x=Math.sqrt(d*d+M*M),w=p.box.normalizedWidth,I=p.box.normalizedHeight;c.push({trackId:h,category:l.category,score:p.score,boundingBox:p.box,centerX:P,centerY:E,normalizedCenterX:p.box.normalizedX+w/2,normalizedCenterY:p.box.normalizedY+I/2,velocity:{vx:d,vy:M},speed:x,age:a+1,hits:10,active:!0,isCoasting:!1})}m.push(c)}return m}reset(){this._tracks=[],this._nextTrackId=1}};var Mt=()=>typeof performance<"u"&&typeof performance.now=="function"?performance.now():Date.now();async function Wr(r,e){let{runner:t}=e,n=e.fps??24,s=e.tasks&&e.tasks.length>0?e.tasks:["detect"],o=new Set(s),i=e.categories?new Set(e.categories.map(v=>v.toLowerCase())):null,m=e.includeMasks===!0&&o.has("segment"),a=o.has("detect")||o.has("segment"),c=[],h={},l=new Map,p=0,T=0,j=v=>!i||i.has(v.toLowerCase()),P=async(v,u,R)=>{let z=t.modelKeyFor(v,u.width/u.height),F=t.downloadStatus.get(z)==="ready",O=Mt(),D=await R(),$=Mt()-O;return p+=$,!F&&!h[z]&&(h[z]={bytes:oe[z].bytes,ms:$}),D},E=v=>{for(let u of v){if(!j(u.category))continue;let R=l.get(u.category)??{sum:0,count:0};R.sum+=u.coverage,R.count++,l.set(u.category,R)}};for await(let v of r){let u=[];if(m){let R=await P("segment",v,()=>t.segment(v));E(R.masks),u=R.detections}else a&&(u=await P("detect",v,()=>t.detect(v)));o.has("pose")&&await P("pose",v,()=>t.pose(v)),o.has("matte")&&await P("matte",v,()=>t.matte(v)),c.push(i?u.filter(R=>j(R.category)):u),T++}let g=T,b=new ue({iouThreshold:e.iouThreshold??.25,maxMissedFrames:e.maxMissedFrames??15}).trackSequence(c,g,n),V=e.pathStride??Math.max(1,Math.ceil(g/50)),f=new Map,d=new Map,M=new Map,x=new Map,w=(v,u,R)=>{let z=v.get(u);return z===void 0&&(z=R(),v.set(u,z)),z};for(let v=0;v<g;v++)for(let u of b[v]??[]){if(!j(u.category))continue;let R=f.get(u.trackId);R||(R={category:u.category,firstFrame:v,lastFrame:v,speedSum:0,speedCount:0,centers:[]},f.set(u.trackId,R)),R.lastFrame=v,Number.isFinite(u.speed)&&(R.speedSum+=u.speed,R.speedCount++),(v-R.firstFrame)%V===0&&R.centers.push({frame:v,x:u.centerX,y:u.centerY}),w(d,u.category,()=>new Set).add(v),M.set(u.category,Math.max(M.get(u.category)??0,u.score)),w(x,u.category,()=>new Set).add(u.trackId)}let I=Array.from(f.entries()).map(([v,u])=>({trackId:v,category:u.category,frames:[u.firstFrame,u.lastFrame],speedMeanPxS:u.speedCount>0?u.speedSum/u.speedCount:0,centerPath:u.centers.map(R=>[R.x,R.y])})).sort((v,u)=>v.trackId-u.trackId),U={};for(let[v,u]of d)U[v]={framesPresent:u.size,totalFrames:g,maxConfidence:M.get(v)??0,tracks:x.get(v)?.size??0};let C={};for(let[v,u]of l)C[v]={meanCoverage:u.count>0?u.sum/u.count:0};return{source:e.source??"frames",totalFrames:g,fps:n,durationSec:n>0?g/n:0,tasks:Array.from(o),modelDownloads:h,inferenceMs:p,trackCount:I.length,classes:U,tracks:I,masks:C}}y();var un=[103.53,116.28,123.675],mn=[57.375,57.12,58.395],fn={"rtmdet-ins":{fit:"topLeft",channels:"bgr",mean:un,std:mn,padValue:114},rtmo:{fit:"center",channels:"bgr",mean:[0,0,0],std:[1,1,1],padValue:114},selfie:{fit:"stretch",channels:"rgb",mean:[0,0,0],std:[255,255,255],padValue:0}};function Pt(r,e){return{...fn[r],width:e[0],height:e[1]}}function hn(r,e,t){if(t.fit==="stretch")return{scaleX:t.width/r,scaleY:t.height/e,offsetX:0,offsetY:0,inputWidth:t.width,inputHeight:t.height};let n=Math.min(t.width/r,t.height/e),s=Math.round(r*n),o=Math.round(e*n),i=t.fit==="center";return{scaleX:n,scaleY:n,offsetX:i?(t.width-s)/2:0,offsetY:i?(t.height-o)/2:0,inputWidth:t.width,inputHeight:t.height}}function ce(r,e,t){return{x:(r-t.offsetX)/t.scaleX,y:(e-t.offsetY)/t.scaleY}}function Rt(r,e,t){let{width:n,height:s,data:o}=r,{width:i,height:m}=e,a=hn(n,s,e),{scaleX:c,scaleY:h,offsetX:l,offsetY:p}=a,T=i*m;(!t||t.length<3*T)&&(t=new Float32Array(3*T));let j=e.channels==="bgr"?[2,1,0]:[0,1,2],[P,E,g]=e.mean,_=1/e.std[0],b=1/e.std[1],V=1/e.std[2],f=(e.padValue-P)*_,d=(e.padValue-E)*b,M=(e.padValue-g)*V,[x,w,I]=j,U=Math.ceil(l),C=Math.ceil(p),v=Math.min(i,Math.floor(l+n*c))-1,u=Math.min(m,Math.floor(p+s*h))-1;for(let R=0;R<m;R++){let z=R*i,F=R<C||R>u,O=Math.max(0,(R+.5-p)/h-.5),D=Math.min(s-1,Math.floor(O)),$=Math.min(s-1,D+1),N=O-D;for(let X=0;X<i;X++){let q=z+X;if(F||X<U||X>v){t[q]=f,t[T+q]=d,t[2*T+q]=M;continue}let K=Math.max(0,(X+.5-l)/c-.5),H=Math.min(n-1,Math.floor(K)),ne=Math.min(n-1,H+1),Q=K-H,Y=(D*n+H)*4,re=(D*n+ne)*4,B=($*n+H)*4,G=($*n+ne)*4,Z=(1-Q)*(1-N),J=Q*(1-N),ee=(1-Q)*N,W=Q*N;t[q]=(o[Y+x]*Z+o[re+x]*J+o[B+x]*ee+o[G+x]*W-P)*_,t[T+q]=(o[Y+w]*Z+o[re+w]*J+o[B+w]*ee+o[G+w]*W-E)*b,t[2*T+q]=(o[Y+I]*Z+o[re+I]*J+o[B+I]*ee+o[G+I]*W-g)*V}}return{tensor:t,transform:a}}y();var Te=8;function Ye(r,e){let{sourceWidth:t,sourceHeight:n,transform:s}=e,o=e.classes?new Set(e.classes.map(P=>P.toLowerCase())):null,i=[];for(let P=0;P<r.count;P++){let E=r.dets[P*5+4];if(!(E>=e.confidence))continue;let g=Number(r.labels[P]),_=e.classNames[g]??`class_${g}`;if(o&&!o.has(_.toLowerCase()))continue;let b=ce(r.dets[P*5],r.dets[P*5+1],s),V=ce(r.dets[P*5+2],r.dets[P*5+3],s),f=ie(b.x,0,t),d=ie(b.y,0,n),M=ie(V.x,0,t)-f,x=ie(V.y,0,n)-d;M<=0||x<=0||i.push({index:P,detection:{category:_,score:E,boundingBox:{originX:f,originY:d,width:M,height:x,normalizedX:f/t,normalizedY:d/n,normalizedWidth:M/t,normalizedHeight:x/n}}})}i.sort((P,E)=>E.detection.score-P.detection.score);let m=i.map(P=>P.detection),a=[];if(!r.masks)return{detections:m,masks:a};let c=Ne(e.maskThreshold??.5,e.featherRadius??.05),{maskWidth:h,maskHeight:l}=r,p=h*l,T=h/s.inputWidth,j=l/s.inputHeight;for(let P=0;P<i.length;P++){let{index:E,detection:g}=i[P],_=g.boundingBox,b=E*p,V=Math.max(0,Math.floor(_.originX)-Te),f=Math.max(0,Math.floor(_.originY)-Te),d=Math.min(t-1,Math.ceil(_.originX+_.width)+Te),M=Math.min(n-1,Math.ceil(_.originY+_.height)+Te),x=new Uint8Array(t*n),w=0;for(let I=f;I<=M;I++){let U=((I+.5)*s.scaleY+s.offsetY)*j-.5,C=ie(Math.floor(U),0,l-1),v=Math.min(l-1,C+1),u=ie(U-C,0,1),R=I*t;for(let z=V;z<=d;z++){let F=((z+.5)*s.scaleX+s.offsetX)*T-.5,O=ie(Math.floor(F),0,h-1),D=Math.min(h-1,O+1),$=ie(F-O,0,1),N=r.masks[b+C*h+O]*(1-$)*(1-u)+r.masks[b+C*h+D]*$*(1-u)+r.masks[b+v*h+O]*(1-$)*u+r.masks[b+v*h+D]*$*u,X=c(N);X>0&&(x[R+z]=X,w+=X/255)}}w=Math.round(w),w!==0&&a.push({category:g.category,mask:x,width:t,height:n,area:w,coverage:w/(t*n),detectionIndex:P})}return{detections:m,masks:a}}function Ne(r,e){if(e<=0)return s=>s>r?255:0;let t=r-e,n=255/(2*e);return s=>s<=t?0:s>=r+e?255:(s-t)*n}function ie(r,e,t){return r<e?e:r>t?t:r}y();var Ct=17,Xe=.3;function Ot(r,e){let{sourceWidth:t,sourceHeight:n,transform:s}=e,o=r.keypointStride,i=[];for(let c=0;c<r.count;c++){let h=r.dets[c*5+4];if(!(h>=e.confidence))continue;let l=ce(r.dets[c*5],r.dets[c*5+1],s),p=ce(r.dets[c*5+2],r.dets[c*5+3],s),T=le(l.x,0,t),j=le(l.y,0,n),P=le(p.x,0,t)-T,E=le(p.y,0,n)-j;if(P<=0||E<=0)continue;let g=[],_=c*Ct*o;for(let V=0;V<Ct;V++){let f=_+V*o,d=ce(r.keypoints[f],r.keypoints[f+1],s);g.push({x:le(d.x,0,t),y:le(d.y,0,n),visibility:le(r.keypoints[f+2],0,1)})}let b=0;for(let V of g)V.visibility>=Xe&&b++;b<(e.minVisibleKeypoints??3)||i.push({score:h,boundingBox:{originX:T,originY:j,width:P,height:E,normalizedX:T/t,normalizedY:j/n,normalizedWidth:P/t,normalizedHeight:E/n},keypoints:g})}i.sort((c,h)=>h.score-c.score);let m=e.iouThreshold??.6,a=[];for(let c of i)a.some(h=>gn(h,c,m))||a.push(c);return{people:a}}function le(r,e,t){return r<e?e:r>t?t:r}function gn(r,e,t){if(ae(r.boundingBox,e.boundingBox)>t)return!0;let n=Math.sqrt(r.boundingBox.width*r.boundingBox.height),s=0,o=0;for(let i=0;i<r.keypoints.length;i++){let m=r.keypoints[i],a=e.keypoints[i];m.visibility<Xe||a.visibility<Xe||(s+=Math.hypot(m.x-a.x,m.y-a.y),o++)}return o>=3&&s/o<.1*n}y();function It(r,e,t,n){let{sourceWidth:s,sourceHeight:o}=n,i=Ne(n.maskThreshold??.5,n.featherRadius??.2),m=new Uint8Array(s*o),a=e/s,c=t/o,h=0;for(let l=0;l<o;l++){let p=Math.max(0,(l+.5)*c-.5),T=Math.min(t-1,Math.floor(p)),j=Math.min(t-1,T+1),P=p-T;for(let E=0;E<s;E++){let g=Math.max(0,(E+.5)*a-.5),_=Math.min(e-1,Math.floor(g)),b=Math.min(e-1,_+1),V=g-_,f=r[T*e+_]*(1-V)*(1-P)+r[T*e+b]*V*(1-P)+r[j*e+_]*(1-V)*P+r[j*e+b]*V*P,d=i(f);m[l*s+E]=d,h+=d}}return{mask:m,width:s,height:o,coverage:s*o>0?h/(255*s*o):0}}y();y();y();var Lt=`
|
|
2
|
-
struct Bone {
|
|
3
|
-
start: vec2<f32>,
|
|
4
|
-
end: vec2<f32>,
|
|
5
|
-
color: vec4<f32>,
|
|
6
|
-
radius: f32,
|
|
7
|
-
enabled: f32,
|
|
8
|
-
pad: vec2<f32>,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
struct SkeletonParams {
|
|
12
|
-
width: u32,
|
|
13
|
-
height: u32,
|
|
14
|
-
numBones: u32,
|
|
15
|
-
pad: u32,
|
|
16
|
-
bones: array<Bone, 24>,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
@group(0) @binding(0) var outputTex: texture_storage_2d<rgba8unorm, write>;
|
|
20
|
-
@group(0) @binding(1) var<uniform> params: SkeletonParams;
|
|
21
|
-
|
|
22
|
-
@compute @workgroup_size(16, 16)
|
|
23
|
-
fn computePoseSkeleton(@builtin(global_invocation_id) id: vec3<u32>) {
|
|
24
|
-
if (id.x >= params.width || id.y >= params.height) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let p = vec2<f32>(f32(id.x) + 0.5, f32(id.y) + 0.5);
|
|
29
|
-
var outColor = vec4<f32>(0.0, 0.0, 0.0, 0.0);
|
|
30
|
-
|
|
31
|
-
let count = min(params.numBones, 24u);
|
|
32
|
-
for (var i = 0u; i < count; i = i + 1u) {
|
|
33
|
-
let bone = params.bones[i];
|
|
34
|
-
if (bone.enabled < 0.5) {
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
let ab = bone.end - bone.start;
|
|
39
|
-
let ap = p - bone.start;
|
|
40
|
-
let l2 = dot(ab, ab);
|
|
41
|
-
|
|
42
|
-
var t: f32 = 0.0;
|
|
43
|
-
if (l2 > 0.0001) {
|
|
44
|
-
t = clamp(dot(ap, ab) / l2, 0.0, 1.0);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let closest = bone.start + t * ab;
|
|
48
|
-
let dist = length(p - closest);
|
|
49
|
-
|
|
50
|
-
if (dist <= bone.radius) {
|
|
51
|
-
// Sub-pixel edge antialiasing
|
|
52
|
-
let edgeAlpha = clamp(bone.radius - dist + 0.5, 0.0, 1.0);
|
|
53
|
-
let a = edgeAlpha * bone.color.a;
|
|
54
|
-
outColor = vec4<f32>(mix(outColor.rgb, bone.color.rgb, a), max(outColor.a, a));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
textureStore(outputTex, id.xy, outColor);
|
|
59
|
-
}
|
|
60
|
-
`;var pn=[{from:0,to:11,color:[0,1,1,1]},{from:0,to:12,color:[0,1,1,1]},{from:11,to:12,color:[1,0,0,1]},{from:11,to:13,color:[1,.333,0,1]},{from:13,to:15,color:[1,.667,0,1]},{from:12,to:14,color:[1,1,0,1]},{from:14,to:16,color:[.667,1,0,1]},{from:11,to:23,color:[.333,1,0,1]},{from:12,to:24,color:[0,1,0,1]},{from:23,to:24,color:[0,1,.333,1]},{from:23,to:25,color:[0,1,.667,1]},{from:25,to:27,color:[0,1,1,1]},{from:24,to:26,color:[0,.667,1,1]},{from:26,to:28,color:[0,.333,1,1]}],Se=class{constructor(e){this.device=e;let t=e.createShaderModule({label:"pose_skeleton.wgsl",code:Lt});this.computePipeline=e.createComputePipeline({label:"PoseSkeletonComputePipeline",layout:"auto",compute:{module:t,entryPoint:"computePoseSkeleton"}}),this.uniformBuffer=e.createBuffer({size:1280,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST,label:"pose_skeleton_uniform_buffer"})}device;computePipeline;uniformBuffer;cachedOutputTexture;execute(e,t={},n){let s=n??pn,o=t.width??1024,i=t.height??1024,m=t.lineWidth??6,a=Math.max(1,m/2),c=1280,h=new ArrayBuffer(c),l=new Uint32Array(h),p=new Float32Array(h);l[0]=o,l[1]=i,l[2]=s.length,l[3]=0;let T=4;for(let g=0;g<s.length;g++){let _=s[g],b=e[_.from],V=e[_.to],f=T+g*12;if(!b||!V||(b.visibility??1)<.2||(V.visibility??1)<.2){p[f+9]=0;continue}p[f+0]=b.x*o,p[f+1]=b.y*i,p[f+2]=V.x*o,p[f+3]=V.y*i,p[f+4]=_.color[0],p[f+5]=_.color[1],p[f+6]=_.color[2],p[f+7]=_.color[3],p[f+8]=a,p[f+9]=1}this.device.queue.writeBuffer(this.uniformBuffer,0,h),(!this.cachedOutputTexture||this.cachedOutputTexture.width!==o||this.cachedOutputTexture.height!==i)&&(this.cachedOutputTexture?.destroy(),this.cachedOutputTexture=this.device.createTexture({label:"openpose_skeleton_texture",size:[o,i],format:"rgba8unorm",usage:GPUTextureUsage.STORAGE_BINDING|GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_SRC}));let j=this.device.createCommandEncoder({label:"pose_skeleton_command_encoder"}),P=this.device.createBindGroup({label:"pose_skeleton_bind_group",layout:this.computePipeline.getBindGroupLayout(0),entries:[{binding:0,resource:this.cachedOutputTexture.createView({label:"pose_skeleton_output_view"})},{binding:1,resource:{buffer:this.uniformBuffer}}]}),E=j.beginComputePass({label:"pose_skeleton_compute_pass"});return E.setPipeline(this.computePipeline),E.setBindGroup(0,P),E.dispatchWorkgroups(Math.ceil(o/16),Math.ceil(i/16),1),E.end(),this.device.queue.submit([j.finish()]),this.cachedOutputTexture}destroy(){this.cachedOutputTexture?.destroy(),this.cachedOutputTexture=void 0,this.uniformBuffer.destroy()}};y();var k={NOSE:0,LEFT_EYE:1,RIGHT_EYE:2,LEFT_EAR:3,RIGHT_EAR:4,LEFT_SHOULDER:5,RIGHT_SHOULDER:6,LEFT_ELBOW:7,RIGHT_ELBOW:8,LEFT_WRIST:9,RIGHT_WRIST:10,LEFT_HIP:11,RIGHT_HIP:12,LEFT_KNEE:13,RIGHT_KNEE:14,LEFT_ANKLE:15,RIGHT_ANKLE:16},Ge=["nose","leftEye","rightEye","leftEar","rightEar","leftShoulder","rightShoulder","leftElbow","rightElbow","leftWrist","rightWrist","leftHip","rightHip","leftKnee","rightKnee","leftAnkle","rightAnkle"],Et=[{from:k.NOSE,to:k.LEFT_SHOULDER,color:[0,1,1,1]},{from:k.NOSE,to:k.RIGHT_SHOULDER,color:[0,1,1,1]},{from:k.LEFT_SHOULDER,to:k.RIGHT_SHOULDER,color:[1,0,0,1]},{from:k.LEFT_SHOULDER,to:k.LEFT_ELBOW,color:[1,.333,0,1]},{from:k.LEFT_ELBOW,to:k.LEFT_WRIST,color:[1,.667,0,1]},{from:k.RIGHT_SHOULDER,to:k.RIGHT_ELBOW,color:[1,1,0,1]},{from:k.RIGHT_ELBOW,to:k.RIGHT_WRIST,color:[.667,1,0,1]},{from:k.LEFT_SHOULDER,to:k.LEFT_HIP,color:[.333,1,0,1]},{from:k.RIGHT_SHOULDER,to:k.RIGHT_HIP,color:[0,1,0,1]},{from:k.LEFT_HIP,to:k.RIGHT_HIP,color:[0,1,.333,1]},{from:k.LEFT_HIP,to:k.LEFT_KNEE,color:[0,1,.667,1]},{from:k.LEFT_KNEE,to:k.LEFT_ANKLE,color:[0,1,1,1]},{from:k.RIGHT_HIP,to:k.RIGHT_KNEE,color:[0,.667,1,1]},{from:k.RIGHT_KNEE,to:k.RIGHT_ANKLE,color:[0,.333,1,1]}];var _e=class{pipeline;constructor(e){this.pipeline=new Se(e)}renderToTexture(e,t,n=Et){return this.pipeline.execute(e,t,n)}destroy(){this.pipeline.destroy()}};y();var jt=class{device;pool=new Map;constructor(e){this.device=e}getOrCreateTexture(e,t){let n=this.pool.get(e);if(n&&n.width===t.width&&n.height===t.height)return n;n&&n.destroy();let s=t.format??"rgba8unorm",o=this.device.createTexture({label:t.label??`vision_segmentation_${e}`,size:[t.width,t.height,1],format:s,usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST|GPUTextureUsage.RENDER_ATTACHMENT});return this.pool.set(e,o),o}uploadMask(e,t,n,s){let o=t.length>=n*s*4,i=o?"rgba8unorm":"r8unorm",m=this.getOrCreateTexture(e,{width:n,height:s,format:i}),a;if(t instanceof Float32Array){a=new Uint8Array(t.length);for(let T=0;T<t.length;T++)a[T]=Math.round(Math.max(0,Math.min(1,t[T]))*255)}else a=t;let h=n*(o?4:1),l=Math.ceil(h/256)*256,p;if(l===h)p=a;else{p=new Uint8Array(l*s);for(let T=0;T<s;T++)p.set(a.subarray(T*h,(T+1)*h),T*l)}return this.device.queue.writeTexture({texture:m},p.buffer,{offset:p.byteOffset,bytesPerRow:l,rowsPerImage:s},{width:n,height:s}),m}destroy(){for(let e of this.pool.values())e.destroy();this.pool.clear()}};y();var Dt=en(At(),1);function bn(){return A.env.FRAMEFIELDS_MODELS_DIR?xe(A.env.FRAMEFIELDS_MODELS_DIR):xe(mt(),".cache/framefields/models")}var ve=class{_modelsDir;_baseUrl;_timeoutMs;_retries;_retryDelayMs;_verify;_fetch;_onProgress;_inflight=new Map;_buffers=new Map;_status=new Map;constructor(e={}){this._modelsDir=e.modelsDir?xe(e.modelsDir):bn(),this._baseUrl=e.baseUrl??A.env.FRAMEFIELDS_MODELS_BASE_URL??void 0,this._timeoutMs=e.timeoutMs,this._retries=Math.max(0,e.retries??2),this._retryDelayMs=e.retryDelayMs??1e3,this._verify=e.verify!==!1,this._fetch=e.fetchImpl??globalThis.fetch,this._onProgress=e.onProgress}get modelsDir(){return this._modelsDir}pathFor(e){return ut(this._modelsDir,oe[e].filename)}descriptor(e){return oe[e]}urlFor(e){let t=oe[e];return this._baseUrl?`${this._baseUrl.replace(/\/+$/,"")}/${t.filename}`:t.url}has(e){let t=this.pathFor(e);if(!ye(t))return!1;try{let n=it(t).size;return this._verify?n===oe[e].bytes:n>0}catch{return!1}}async evict(e){this._buffers.delete(e),this._status.set(e,"pending"),await Ve(this.pathFor(e)).catch(()=>{})}status(e){return this._status.get(e)??"pending"}statuses(){return new Map(this._status)}isLoaded(e){return this._buffers.has(e)}async ensure(e){let t=this._buffers.get(e);if(t)return t;let n=this._inflight.get(e);if(n)return n;let s=this.load(e).finally(()=>this._inflight.delete(e));return this._inflight.set(e,s),s}async preload(e){await Promise.all([...new Set(e)].map(t=>this.ensure(t)))}async load(e){let t=this.pathFor(e);if(this.has(e))return this._status.set(e,"ready"),this.readBuffer(e,t);let n=this.urlFor(e);this._status.set(e,"downloading"),ye(Ae(t))||at(Ae(t),{recursive:!0});let s=`${t}.tmp.${Date.now()}`;try{let{buffer:o,totalBytes:i}=await this.download(e,n);if(o.byteLength===0)throw new Error(`Downloaded vision model '${e}' is empty (0 bytes).`);return this._verify&&this.verifyBytes(e,o,n),this._onProgress?.(e,o.byteLength,i||o.byteLength),await lt(s,o),await dt(s,t),this._buffers.set(e,o),this._status.set(e,"ready"),o}catch(o){throw this._status.set(e,"error"),ye(s)&&await Ve(s).catch(()=>{}),o}}async download(e,t){for(let n=0;;n++){let s;try{let o=await this._fetch(t,{signal:this._timeoutMs?AbortSignal.timeout(this._timeoutMs):void 0});if(o.ok){let m=Number(o.headers.get("content-length")??0);return{buffer:new Uint8Array(await o.arrayBuffer()),totalBytes:m}}let i=new Error(`Failed to download vision model '${e}' from ${t}: HTTP ${o.status} ${o.statusText}`);if(o.status!==429&&o.status<500)throw i;s=i}catch(o){if(!(o instanceof TypeError))throw o;s=o}if(n>=this._retries)throw new Error(`Failed to download vision model '${e}' from ${t} after ${n+1} attempts: ${String(s)}`,{cause:s});await new Promise(o=>setTimeout(o,this._retryDelayMs*2**n))}}verifyBytes(e,t,n){let s=oe[e];if(t.byteLength!==s.bytes)throw new Error(`Vision model '${e}' from ${n} is ${t.byteLength} bytes, expected ${s.bytes}.`);let o=(0,Dt.createHash)("sha256").update(t).digest("hex");if(o!==s.sha256)throw new Error(`Vision model '${e}' from ${n} failed its SHA-256 check (got ${o}, expected ${s.sha256}).`)}async readBuffer(e,t){let n=await ct(t),s=new Uint8Array(n.buffer,n.byteOffset,n.byteLength);return this._buffers.set(e,s),s}clearMemoryCache(){this._buffers.clear()}};y();function Bt(){return{objects:[],rawDetections:[]}}function Ft(){return{people:[]}}function Ut(){let r=[];for(let e=0;e<17;e++)r.push({x:.5,y:.5,z:0,visibility:0});return r}y();var yn=4,fe=class r{variant;confidence;classes;maskThreshold;featherRadius;_core;_ownsCore;constructor(e,t){this.variant=e.variant??"s",this.confidence=e.confidence??.3,this.classes=e.classes,this.maskThreshold=e.maskThreshold??.5,this.featherRadius=e.featherRadius,this._ownsCore=t===void 0,this._core=t??{store:e.store??new ve({modelsDir:e.modelsDir,baseUrl:e.baseUrl,timeoutMs:e.timeoutMs,onProgress:e.onProgress}),provider:e.provider??ft(),sessions:new Map,tensorScratch:new Map,runQueue:new Map,instancePasses:[]}}static create(e={}){return new r(e)}derive(e){return new r({variant:this.variant,confidence:e.confidence??this.confidence,classes:"classes"in e?e.classes:this.classes,maskThreshold:e.maskThreshold??this.maskThreshold,featherRadius:"featherRadius"in e?e.featherRadius:this.featherRadius},this._core)}get modelsDir(){return this._core.store.modelsDir}get downloadStatus(){let e=new Map;for(let t of Object.keys(oe))e.set(t,"pending");for(let[t,n]of this._core.store.statuses())e.set(t,n);return e}modelKeyFor(e,t=1){return vt(e,this.variant,t)}async preload(e=["detect"]){let t=new Set;for(let n of e)n==="matte"?(t.add("selfie-square"),t.add("selfie-landscape")):t.add(this.modelKeyFor(n));await this._core.store.preload([...t]),await Promise.all([...t].map(n=>this.session(n)))}async detect(e){let t=await this.instancePass(e);return Ye({...t.outputs,masks:void 0},this.instanceDecodeOptions(e,t.transform)).detections}async segment(e){let t=await this.instancePass(e);return Ye(t.outputs,this.instanceDecodeOptions(e,t.transform))}async pose(e){let t=this.modelKeyFor("pose"),{outputs:n,transform:s}=await this.infer(t,e),o=me(n,"dets",t),i=me(n,"keypoints",t);return Ot({dets:o.data,keypoints:i.data,count:o.dims[1]??0,keypointStride:i.dims[3]??3},{confidence:this.confidence,transform:s,sourceWidth:e.width,sourceHeight:e.height})}async matte(e){let t=this.modelKeyFor("matte",e.width/e.height),[n,s]=oe[t].input,{outputs:o}=await this.infer(t,e),i=me(o,"alphas",t);return It(i.data,n,s,{sourceWidth:e.width,sourceHeight:e.height,maskThreshold:this.maskThreshold,...this.featherRadius!==void 0?{featherRadius:this.featherRadius}:{}})}close(){if(!this._ownsCore)return;let e=this._core;for(let t of e.sessions.values())t.then(n=>n.release()).catch(()=>{});e.sessions.clear(),e.runQueue.clear(),e.instancePasses.length=0,e.store.clearMemoryCache()}instancePass(e){let t=this._core.instancePasses,n=xn(e),s=t.find(a=>a.content===n);if(s)return s.result;let o=this.modelKeyFor("segment"),i=this.infer(o,e).then(({outputs:a,transform:c})=>{let h=me(a,"dets",o),l=me(a,"masks",o);return{transform:c,outputs:{dets:h.data,labels:me(a,"labels",o).data,masks:l.data,count:h.dims[1]??0,maskHeight:l.dims[l.dims.length-2]??0,maskWidth:l.dims[l.dims.length-1]??0}}}),m={content:n,result:i};return t.push(m),t.length>yn&&t.shift(),i.catch(()=>{let a=t.indexOf(m);a>=0&&t.splice(a,1)}),i}instanceDecodeOptions(e,t){return{confidence:this.confidence,classes:this.classes,classNames:ke,transform:t,sourceWidth:e.width,sourceHeight:e.height,maskThreshold:this.maskThreshold,...this.featherRadius!==void 0?{featherRadius:this.featherRadius}:{}}}infer(e,t){let n=this._core.runQueue,o=(n.get(e)??Promise.resolve()).then(()=>this.inferNow(e,t),()=>this.inferNow(e,t)),i=o.catch(()=>{});return n.set(e,i),i.then(()=>{n.get(e)===i&&n.delete(e)}),o}async inferNow(e,t){let n=await this.session(e),s=oe[e],o=Pt(s.family,s.input),i=3*o.width*o.height,m=this._core.tensorScratch,a=m.get(e);(!a||a.length<i)&&(a=new Float32Array(i),m.set(e,a));let{tensor:c,transform:h}=Rt(t,o,a);return{outputs:await n.run({name:"input",data:c,dims:[1,3,o.height,o.width]}),transform:h}}session(e){let{sessions:t,store:n,provider:s}=this._core,o=t.get(e);return o||(o=n.ensure(e).then(i=>s.createSession(i)).catch(i=>{throw t.delete(e),n.evict(e),i}),t.set(e,o)),o}};function xn(r){let{data:e}=r,t=e.byteOffset%4===0?e.byteLength>>>2:0,n=new Uint32Array(e.buffer,e.byteOffset,t),s=2166136261,o=16777619^e.byteLength;for(let i=0;i<t;i++){let m=n[i];s=Math.imul(s^m,2654435761),o=Math.imul(o^m,2246822519)^s>>>15}for(let i=t*4;i<e.length;i++)s=Math.imul(s^e[i],2654435761);return`${r.width}x${r.height}:${(s>>>0).toString(36)}:${(o>>>0).toString(36)}`}function me(r,e,t){let n=r[e];if(!n)throw new Error(`Vision model '${t}' returned no '${e}' output (got: ${Object.keys(r).join(", ")||"none"}). Expected the ${oe[t].source}.`);return n}y();function $e(r){let e;for(let t of r){if(!e){e=t;continue}let n=Ht(t),s=Ht(e);n!==s?n&&(e=t):(n?t.area>e.area:t.detectionIndex<e.detectionIndex)&&(e=t)}return e}function Ht(r){return r.category.toLowerCase()==="person"}function Me(r){let{mask:e,width:t,height:n}=r,s=t,o=n,i=-1,m=-1;for(let a=0;a<n;a++){let c=a*t;for(let h=0;h<t;h++)e[c+h]!==0&&(h<s&&(s=h),h>i&&(i=h),a<o&&(o=a),a>m&&(m=a))}return i<0?null:{x0:s,y0:o,x1:i,y1:m}}function zt(r,e=.5,t=2){let n=$e(r);if(!n)return;let s=Me(n);if(!s)return n;let o=Pe(s),i=r.filter(c=>{if(c===n)return!1;let h=Me(c);return h!==null&&Pe(h)<=o*t&&kn(s,h)>=e});if(i.length===0)return n;let m=n.mask.slice();for(let c of i){let h=c.mask;for(let l=0;l<m.length;l++)h[l]>m[l]&&(m[l]=h[l])}let a=0;for(let c=0;c<m.length;c++)a+=m[c];return a=Math.round(a/255),{...n,mask:m,area:a,coverage:a/(n.width*n.height)}}function Pe(r){return(r.x1-r.x0+1)*(r.y1-r.y0+1)}function kn(r,e){let t=Math.min(r.x1,e.x1)-Math.max(r.x0,e.x0)+1,n=Math.min(r.y1,e.y1)-Math.max(r.y0,e.y0)+1;return t<=0||n<=0?0:t*n/Math.min(Pe(r),Pe(e))}y();y();var Re=class{width;height;fovRad;focalDistance;constructor(e){this.width=e.width,this.height=e.height;let t=e.fov??60;this.fovRad=t*Math.PI/180,this.focalDistance=e.cameraDistance??this.height/2/Math.tan(this.fovRad/2)}projectNormalizedLandmark(e,t=0){let n=e.x*this.width,s=e.y*this.height,o=e.z*this.width+t,i=Math.min(o,this.focalDistance-10),m=this.focalDistance/(this.focalDistance-i);return{x:n,y:s,z:o,scale:Math.max(.1,m)}}projectWorldLandmark(e,t=2,n=800){let s=this.width/2,o=this.height/2,i=e.x*n,m=e.y*n,a=(e.z+t)*n,c=Math.min(a,this.focalDistance-10),h=this.focalDistance/(this.focalDistance-c);return{x:s+i*h,y:o+m*h,z:a,scale:Math.max(.1,h)}}};y();function Ke(r){if(r.boundingBox&&r.boundingBox.width>0&&r.boundingBox.height>0)return{originX:r.boundingBox.originX,originY:r.boundingBox.originY,width:r.boundingBox.width,height:r.boundingBox.height};let e=1/0,t=1/0,n=-1/0,s=-1/0,o=0;for(let i of r.keypoints)i.visibility>.1&&(o++,i.x<e&&(e=i.x),i.y<t&&(t=i.y),i.x>n&&(n=i.x),i.y>s&&(s=i.y));return o===0||e>=n||t>=s?{originX:r.boundingBox?.originX??0,originY:r.boundingBox?.originY??0,width:r.boundingBox?.width??0,height:r.boundingBox?.height??0}:{originX:e,originY:t,width:n-e,height:s-t}}function Yt(r,e,t=.15){if(r.length===0)return[];let n=e.filter(a=>a.category.toLowerCase()==="person"&&a.active);if(n.length===0)return[...r];let s=[];for(let a=0;a<r.length;a++){let c=r[a];if(c.trackId!==void 0)continue;let h=Ke(c),l=h.width<=1.05&&h.height<=1.05;for(let p of n){let T=l&&p.boundingBox.normalizedWidth>0?{originX:p.boundingBox.normalizedX,originY:p.boundingBox.normalizedY,width:p.boundingBox.normalizedWidth,height:p.boundingBox.normalizedHeight}:p.boundingBox,j=ae(h,T);j>=t&&s.push({personIdx:a,trackId:p.trackId,iou:j})}}s.sort((a,c)=>c.iou-a.iou);let o=new Set,i=new Set,m=new Map;for(let a of s)o.has(a.personIdx)||i.has(a.trackId)||(o.add(a.personIdx),i.add(a.trackId),m.set(a.personIdx,a.trackId));return r.length===1&&n.length===1&&!m.has(0)&&r[0].trackId===void 0&&m.set(0,n[0].trackId),r.map((a,c)=>{if(a.trackId!==void 0)return a;let h=m.get(c);return h!==void 0?{...a,trackId:h}:a})}var Ce=class{objects;poseLandmarks;masks;segmentation;classes;poseLandmarksTensor;objectsTensor;masksTensor;histogramTensor;_totalFrames;_fps;_width;_height;_transformer;_stencilTexture;_poseCache=new Map;_objectCache=new Map;_maskCache=new Map;_matteCache=new Map;_poseLmCache=new Map;_trackCache=new Map;_categoryCache=new Map;_classCache=new Map;_maskTrackCache=new Map;_registeredSignals=new Set;constructor(e={}){this._totalFrames=e.totalFrames??120,this._fps=e.fps??24,this._width=e.width??1920,this._height=e.height??1080,this._transformer=new Re({width:this._width,height:this._height,fov:e.cameraFov??60});let t=this,n=(f,d)=>{let M=rt(f,d);return this._registeredSignals.add(M),M},s=f=>this.getPoseResult(f).people[0]?.keypoints??Ut(),o=(f,d)=>{let M=v=>s(v)[f]??{x:.5,y:.5,z:0},x=n(v=>M(v.frame).x,{fps:this._fps,label:`${d}_x`}),w=n(v=>M(v.frame).y,{fps:this._fps,label:`${d}_y`}),I=n(v=>M(v.frame).z??0,{fps:this._fps,label:`${d}_z`}),U=n(v=>this._transformer.projectNormalizedLandmark(M(v.frame)).x,{fps:this._fps,label:`${d}_screenX`}),C=n(v=>this._transformer.projectNormalizedLandmark(M(v.frame)).y,{fps:this._fps,label:`${d}_screenY`});return{x,y:w,z:I,screenX:U,screenY:C}},i=f=>{let d=this._poseLmCache.get(f);return d||(d=o(f,Ge[f]??`kpt_${f}`),this._poseLmCache.set(f,d)),d},m={nose:i(k.NOSE),leftEye:i(k.LEFT_EYE),rightEye:i(k.RIGHT_EYE),leftEar:i(k.LEFT_EAR),rightEar:i(k.RIGHT_EAR),shoulder:i(k.LEFT_SHOULDER),leftShoulder:i(k.LEFT_SHOULDER),rightShoulder:i(k.RIGHT_SHOULDER),leftElbow:i(k.LEFT_ELBOW),rightElbow:i(k.RIGHT_ELBOW),leftWrist:i(k.LEFT_WRIST),rightWrist:i(k.RIGHT_WRIST),leftHip:i(k.LEFT_HIP),rightHip:i(k.RIGHT_HIP),leftKnee:i(k.LEFT_KNEE),rightKnee:i(k.RIGHT_KNEE),leftAnkle:i(k.LEFT_ANKLE),rightAnkle:i(k.RIGHT_ANKLE),get:i};this.poseLandmarks=new Proxy(m,{get(f,d,M){return typeof d=="string"&&/^\d+$/.test(d)?f.get(Number(d)):typeof d=="number"?f.get(d):Reflect.get(f,d,M)}});let a=(f,d="unknown")=>({trackId:f,category:d,score:0,boundingBox:{originX:this._width/2,originY:this._height/2,width:0,height:0,normalizedX:.5,normalizedY:.5,normalizedWidth:0,normalizedHeight:0},centerX:this._width/2,centerY:this._height/2,normalizedCenterX:.5,normalizedCenterY:.5,velocity:{vx:0,vy:0},speed:0,age:0,hits:0,active:!1,isCoasting:!1}),c=(f,d)=>{let M=n(C=>f(C.frame).sx,{fps:this._fps,label:`${d}_screenX`}),x=n(C=>f(C.frame).sy,{fps:this._fps,label:`${d}_screenY`}),w=n(C=>this._width>0?f(C.frame).sx/this._width:0,{fps:this._fps,label:`${d}_x`}),I=n(C=>this._height>0?f(C.frame).sy/this._height:0,{fps:this._fps,label:`${d}_y`}),U=n(()=>0,{fps:this._fps,label:`${d}_z`});return{x:w,y:I,z:U,screenX:M,screenY:x}},h=(f,d,M)=>{let x=F=>{let O=t.getPoseResult(F);if(O.people.length===0)return;let D=f(F),$=M??D.trackId,N=O.people.find(K=>K.trackId===$);if(N)return N;if(!D.active&&O.people.length>1)return;if(O.people.length===1)return O.people[0];let X,q=.1;for(let K of O.people){let H=Ke(K),Q=H.width<=1.05&&H.height<=1.05&&D.boundingBox.normalizedWidth>0?{originX:D.boundingBox.normalizedX,originY:D.boundingBox.normalizedY,width:D.boundingBox.normalizedWidth,height:D.boundingBox.normalizedHeight}:D.boundingBox,Y=ae(H,Q);Y>q&&(q=Y,X=K)}return X},w=n(F=>x(F.frame)?1:0,{fps:this._fps,label:`${d}_hasPose`}),I=(F,O)=>{let D=H=>{let Q=x(H)?.keypoints[F];if(Q&&Q.visibility>.05)return{x:Q.x,y:Q.y,z:0};let Y=f(H);return{x:Y.normalizedCenterX,y:Y.normalizedCenterY,z:0}},$=n(H=>D(H.frame).x,{fps:this._fps,label:`${d}_${O}_x`}),N=n(H=>D(H.frame).y,{fps:this._fps,label:`${d}_${O}_y`}),X=n(H=>D(H.frame).z??0,{fps:this._fps,label:`${d}_${O}_z`}),q=n(H=>t._transformer.projectNormalizedLandmark(D(H.frame)).x,{fps:this._fps,label:`${d}_${O}_screenX`}),K=n(H=>t._transformer.projectNormalizedLandmark(D(H.frame)).y,{fps:this._fps,label:`${d}_${O}_screenY`});return{x:$,y:N,z:X,screenX:q,screenY:K}},U=new Map,C=F=>{let O=U.get(F);return O||(O=I(F,Ge[F]??`kpt_${F}`),U.set(F,O)),O},v=n(F=>{let O=F.frame,D=x(O);if(!D)return 0;let $=x(Math.max(0,O-1)),N=D.keypoints,X=$?.keypoints,q=N[k.RIGHT_WRIST],K=X?.[k.RIGHT_WRIST],H=0;if(q&&K&&q.visibility>.1&&K.visibility>.1){let re=(q.x-K.x)*this._width,B=(q.y-K.y)*this._height;H=Math.sqrt(re*re+B*B)*this._fps}let ne=N[k.LEFT_WRIST],Q=X?.[k.LEFT_WRIST],Y=0;if(ne&&Q&&ne.visibility>.1&&Q.visibility>.1){let re=(ne.x-Q.x)*this._width,B=(ne.y-Q.y)*this._height;Y=Math.sqrt(re*re+B*B)*this._fps}return Math.max(H,Y)},{fps:this._fps,label:`${d}_wristSpeed`}),u=n(F=>{let O=x(F.frame);if(!O)return 0;let D=O.keypoints,$=D[k.LEFT_WRIST],N=D[k.LEFT_SHOULDER],X=D[k.RIGHT_WRIST],q=D[k.RIGHT_SHOULDER],K=$&&N&&$.visibility>.1&&N.visibility>.1&&$.y<N.y,H=X&&q&&X.visibility>.1&&q.visibility>.1&&X.y<q.y;return K||H?1:0},{fps:this._fps,label:`${d}_handRaised`}),R=n(F=>{let O=x(F.frame);if(!O)return 0;let D=O.keypoints,$=D[k.LEFT_SHOULDER],N=D[k.RIGHT_SHOULDER],X=D[k.LEFT_HIP],q=D[k.RIGHT_HIP];if(!$||!N||!X||!q)return 0;let K=($.x+N.x)/2*this._width,H=($.y+N.y)/2*this._height,ne=(X.x+q.x)/2*this._width,Q=(X.y+q.y)/2*this._height,Y=K-ne,re=H-Q;return Y===0&&re===0?0:Math.atan2(Y,-re)},{fps:this._fps,label:`${d}_bodyTiltAngle`}),z={hasPose:w,wristSpeed:v,handRaised:u,bodyTiltAngle:R,nose:C(k.NOSE),leftEye:C(k.LEFT_EYE),rightEye:C(k.RIGHT_EYE),leftEar:C(k.LEFT_EAR),rightEar:C(k.RIGHT_EAR),shoulder:C(k.LEFT_SHOULDER),leftShoulder:C(k.LEFT_SHOULDER),rightShoulder:C(k.RIGHT_SHOULDER),leftElbow:C(k.LEFT_ELBOW),rightElbow:C(k.RIGHT_ELBOW),leftWrist:C(k.LEFT_WRIST),rightWrist:C(k.RIGHT_WRIST),leftHip:C(k.LEFT_HIP),rightHip:C(k.RIGHT_HIP),leftKnee:C(k.LEFT_KNEE),rightKnee:C(k.RIGHT_KNEE),leftAnkle:C(k.LEFT_ANKLE),rightAnkle:C(k.RIGHT_ANKLE),get:C};return new Proxy(z,{get(F,O,D){return typeof O=="string"&&/^\d+$/.test(O)?F.get(Number(O)):typeof O=="number"?F.get(O):Reflect.get(F,O,D)}})},l=(f,d,M,x)=>{let w=u=>f(u).boundingBox,I=h(f,`${d}_pose`,x),U=(u,R,z)=>c(F=>({sx:R(w(F)),sy:z(w(F))}),`${d}_${u}`),C=U("center",u=>u.originX+u.width/2,u=>u.originY+u.height/2),v=n(u=>f(u.frame).speed,{fps:this._fps,label:`${d}_speed`});return{get trackId(){return x??f(0).trackId},get category(){return M??f(0).category},bounds:{x:n(u=>w(u.frame).normalizedX,{fps:this._fps,label:`${d}_normX`}),y:n(u=>w(u.frame).normalizedY,{fps:this._fps,label:`${d}_normY`}),width:n(u=>w(u.frame).normalizedWidth,{fps:this._fps,label:`${d}_normWidth`}),height:n(u=>w(u.frame).normalizedHeight,{fps:this._fps,label:`${d}_normHeight`}),screenX:n(u=>w(u.frame).originX,{fps:this._fps,label:`${d}_screenX`}),screenY:n(u=>w(u.frame).originY,{fps:this._fps,label:`${d}_screenY`}),screenWidth:n(u=>w(u.frame).width,{fps:this._fps,label:`${d}_screenWidth`}),screenHeight:n(u=>w(u.frame).height,{fps:this._fps,label:`${d}_screenHeight`}),aspectRatio:n(u=>{let R=w(u.frame);return R.height>0?R.width/R.height:1},{fps:this._fps,label:`${d}_aspectRatio`}),area:n(u=>{let R=w(u.frame);return R.width*R.height},{fps:this._fps,label:`${d}_area`})},anchors:{topLeft:U("topLeft",u=>u.originX,u=>u.originY),topCenter:U("topCenter",u=>u.originX+u.width/2,u=>u.originY),topRight:U("topRight",u=>u.originX+u.width,u=>u.originY),centerLeft:U("centerLeft",u=>u.originX,u=>u.originY+u.height/2),center:C,centerRight:U("centerRight",u=>u.originX+u.width,u=>u.originY+u.height/2),bottomLeft:U("bottomLeft",u=>u.originX,u=>u.originY+u.height),bottomCenter:U("bottomCenter",u=>u.originX+u.width/2,u=>u.originY+u.height),bottomRight:U("bottomRight",u=>u.originX+u.width,u=>u.originY+u.height)},kinematics:{vx:n(u=>f(u.frame).velocity.vx,{fps:this._fps,label:`${d}_vx`}),vy:n(u=>f(u.frame).velocity.vy,{fps:this._fps,label:`${d}_vy`}),speed:v,acceleration:v.velocity(1),headingRad:n(u=>{let R=f(u.frame).velocity;return Math.atan2(R.vy,R.vx)},{fps:this._fps,label:`${d}_headingRad`}),headingDeg:n(u=>{let R=f(u.frame).velocity;return Math.atan2(R.vy,R.vx)*180/Math.PI},{fps:this._fps,label:`${d}_headingDeg`})},confidence:n(u=>f(u.frame).score,{fps:this._fps,label:`${d}_score`}),active:n(u=>f(u.frame).active?1:0,{fps:this._fps,label:`${d}_active`}),isCoasting:n(u=>f(u.frame).isCoasting?1:0,{fps:this._fps,label:`${d}_isCoasting`}),age:n(u=>f(u.frame).age,{fps:this._fps,label:`${d}_age`}),pose:I,center:C,topCenter:this.__pending,bottomCenter:C,topLeft:C,bottomLeft:C}},p=(f,d,M,x)=>{let w=l(f,d,M,x),I=w.anchors;return Object.assign(w,{topCenter:I.topCenter,bottomCenter:I.bottomCenter,topLeft:I.topLeft,bottomLeft:I.bottomLeft}),w},T=f=>{let d=this._trackCache.get(f);return d||(d=p(M=>this.getObjectResult(M).objects.find(x=>x.trackId===f)??a(f),`track_${f}`,void 0,f),this._trackCache.set(f,d)),d},j=(f,d=0)=>{let M=`${f.toLowerCase()}_${d}`,x=this._categoryCache.get(M);return x||(x=p(w=>this.getObjectResult(w).objects.filter(C=>C.category.toLowerCase()===f.toLowerCase()&&C.active)[d]??a(0,f),`cat_${f}_${d}`,f),this._categoryCache.set(M,x)),x},P=p(f=>{let d=this.getObjectResult(f);if(d.objects.length===0)return a(0);let M=d.objects[0];for(let x=1;x<d.objects.length;x++)d.objects[x].score>M.score&&(M=d.objects[x]);return M},"primary_object"),E=f=>{let d=new Set;for(let M of this.getObjectResult(f).objects)M.active&&d.add(M.category);return Array.from(d).sort()},g=f=>{let d=f.toLowerCase(),M=this._classCache.get(d);return M||(M={count:n(x=>this.getObjectResult(x.frame).objects.filter(w=>w.active&&w.category.toLowerCase()===d).length,{fps:this._fps,label:`class_${d}_count`}),maxConfidence:n(x=>{let w=0;for(let I of this.getObjectResult(x.frame).objects)I.active&&I.category.toLowerCase()===d&&I.score>w&&(w=I.score);return w},{fps:this._fps,label:`class_${d}_maxConf`}),present:n(x=>this.getObjectResult(x.frame).objects.some(w=>w.active&&w.category.toLowerCase()===d)?1:0,{fps:this._fps,label:`class_${d}_present`}),primary:j(f)},this._classCache.set(d,M),M)};this.classes={get:g,get names(){return E(te.value)},histogram:{get:f=>t.getClassHistogramTensor(f?.frame??te.value),get value(){return t.getClassHistogramTensor(te.value)}}},this.objects={get:T,byCategory:j,primary:P,count:n(f=>this.getObjectResult(f.frame).objects.filter(d=>d.active).length,{fps:this._fps,label:"tracked_objects_count"}),hasCategory:f=>g(f).present,getActiveTracks:f=>this.getObjectResult(f).objects.filter(d=>d.active),get detectedCategories(){return E(te.value)}};let _=(f,d)=>this.getMaskResult(f).find(M=>M.trackId===d),b=f=>$e(this.getMaskResult(f)),V=(f,d)=>{let M=x=>{let w=f(x);if(!(!w||w.trackId===void 0))return this.getObjectResult(x).objects.find(I=>I.trackId===w.trackId)};return{get trackId(){return f(te.value)?.trackId??0},get category(){return f(te.value)?.category??"unknown"},area:n(x=>f(x.frame)?.area??0,{fps:this._fps,label:`${d}_area`}),coverage:n(x=>f(x.frame)?.coverage??0,{fps:this._fps,label:`${d}_coverage`}),solidity:n(x=>{let w=f(x.frame),I=M(x.frame);if(!w||!I)return 0;let U=I.boundingBox.width*I.boundingBox.height;return U>0?w.area/U:0},{fps:this._fps,label:`${d}_solidity`}),bboxFill:n(x=>{let w=f(x.frame);if(!w||w.width*w.height===0)return 0;let I=M(x.frame);return I?I.boundingBox.width*I.boundingBox.height/(w.width*w.height):0},{fps:this._fps,label:`${d}_bboxFill`})}};this.masks={get:f=>{let d=this._maskTrackCache.get(f);return d||(d=V(M=>_(M,f),`mask_${f}`),this._maskTrackCache.set(f,d)),d},subject:V(b,"subject"),count:n(f=>new Set(this.getMaskResult(f.frame).map(d=>d.trackId??-1)).size,{fps:this._fps,label:"mask_count"})},this.segmentation={humanSilhouette:this.masks.subject,subject:this.masks.subject,instanceMasks:this.masks,matte:{coverage:n(f=>t.getMatteResult(f.frame)?.coverage??0,{fps:this._fps,label:"matte_coverage"}),at:f=>t.getMatteResult(f)},get stencilTexture(){return t._stencilTexture}},this.poseLandmarksTensor={get:f=>t.getPoseLandmarksTensor(f?.frame??te.value),get value(){return t.getPoseLandmarksTensor(te.value)}},this.objectsTensor={get:f=>t.getObjectsTensor(f?.frame??te.value),get value(){return t.getObjectsTensor(te.value)}},this.masksTensor={get:f=>t.getMasksTensor(f?.frame??te.value),get value(){return t.getMasksTensor(te.value)}},this.histogramTensor={get:f=>t.getClassHistogramTensor(f?.frame??te.value),get value(){return t.getClassHistogramTensor(te.value)}}}setStencilTexture(e){this._stencilTexture=e}get stencilTexture(){return this._stencilTexture}latestAtOrBefore(e,t,n=8){let s=this.clamp(t),o=Math.max(0,s-n);for(let i=s;i>=o;i--){let m=e.get(i);if(m!==void 0)return m}}invalidateSignals(){for(let e of this._registeredSignals)e.invalidate()}setPoseResult(e,t){this._poseCache.set(this.clamp(e),t),this.invalidateSignals()}getPoseResult(e){return this.latestAtOrBefore(this._poseCache,e)??Ft()}setObjectResult(e,t){let n="objects"in t?t:{objects:t,rawDetections:[]};this._objectCache.set(this.clamp(e),{objects:n.objects,rawDetections:n.rawDetections??[]}),this.invalidateSignals()}getObjectResult(e){return this.latestAtOrBefore(this._objectCache,e)??Bt()}setMaskResult(e,t){this._maskCache.set(this.clamp(e),t),this.invalidateSignals()}getMaskResult(e){return this.latestAtOrBefore(this._maskCache,e)??[]}setMatteResult(e,t){this._matteCache.set(this.clamp(e),t),this.invalidateSignals()}getMatteResult(e){return this.latestAtOrBefore(this._matteCache,e)}summary(e=te.value){let t=this.clamp(e),n=this.getObjectResult(t),s=n.objects.map(m=>({trackId:m.trackId,category:m.category,score:m.score,center:[m.centerX,m.centerY],speed:m.speed,active:m.active})),o=new Set;for(let m of n.objects)m.active&&o.add(m.category);let i={};for(let m of this.getMaskResult(t))i[m.category]=(i[m.category]??0)+1;return{frame:t,objects:s,classes:Array.from(o).sort(),masks:i}}getPoseLandmarksTensor(e){let n=this.getPoseResult(e).people[0]?.keypoints??[],s=new Float32Array(51);for(let o=0;o<17;o++)s[o*3]=n[o]?.x??.5,s[o*3+1]=n[o]?.y??.5,s[o*3+2]=n[o]?.visibility??0;return{data:s,shape:[17,3],dtype:"float32"}}getObjectsTensor(e,t=16){let n=this.getObjectResult(e),s=new Float32Array(t*8),o=Math.min(t,n.objects.length);for(let i=0;i<o;i++){let m=n.objects[i],a=i*8,c=0;for(let h=0;h<m.category.length;h++)c=c*31+m.category.charCodeAt(h)&65535;s[a]=m.active?1:0,s[a+1]=c,s[a+2]=m.centerX,s[a+3]=m.centerY,s[a+4]=m.boundingBox.width,s[a+5]=m.boundingBox.height,s[a+6]=m.velocity.vx,s[a+7]=m.velocity.vy}return{data:s,shape:[t,8],dtype:"float32"}}getMasksTensor(e,t=16){let n=this.getMaskResult(e),s=new Float32Array(t*2),o=Math.min(t,n.length);for(let i=0;i<o;i++){let m=n[i];s[i*2]=m.coverage;let a=this.getObjectResult(e).objects.find(h=>h.trackId===m.trackId),c=a?a.boundingBox.width*a.boundingBox.height:0;s[i*2+1]=c>0?m.area/c:0}return{data:s,shape:[t,2],dtype:"float32"}}getClassHistogramTensor(e,t=80){let n=new Float32Array(t);for(let s of this.getObjectResult(e).objects)if(s.active){let o=Tn.get(s.category.toLowerCase());o!==void 0&&(n[o]+=1)}return{data:n,shape:[t],dtype:"float32"}}clamp(e){return Math.max(0,Math.min(this._totalFrames-1,Math.round(e)))}};var Tn=new Map(ke.map((r,e)=>[r,e]));y();function Nt(r,e,t={}){let n=t.offsetX??0,s=t.offsetY??0,o=t.offsetZ??0;if(!e||typeof e!="object")return r;if("screenX"in e&&e.screenX&&typeof e.screenX=="object"){let i=e;r.x=n!==0?i.screenX.add(n):i.screenX,r.y=s!==0?i.screenY.add(s):i.screenY,r.z=o!==0?i.z.add(o):i.z}else if("x"in e&&typeof e.x=="object"&&e.x!==null){let i=e;r.x=n!==0?i.x.add(n):i.x,r.y=s!==0?i.y.add(s):i.y,r.z=o!==0?i.z.add(o):i.z}else{let i=e;r.x=(i.screenX??i.x)+n,r.y=(i.screenY??i.y)+s,r.z=(i.z??0)+o}return r}function as(r,e,t={}){if(!e||typeof e!="object")return r;if("anchors"in e&&e.anchors){let n=e,s=t.anchor??(t.matchWidth||t.matchHeight?"topLeft":"center"),o=n.anchors[s]??n.center,i=o;return t.smoothFrames&&t.smoothFrames>1&&(i={x:o.x.smooth(t.smoothFrames),y:o.y.smooth(t.smoothFrames),z:o.z,screenX:o.screenX.smooth(t.smoothFrames),screenY:o.screenY.smooth(t.smoothFrames)}),t.matchWidth&&(r.width=t.smoothFrames&&t.smoothFrames>1?n.bounds.screenWidth.smooth(t.smoothFrames):n.bounds.screenWidth),t.matchHeight&&(r.height=t.smoothFrames&&t.smoothFrames>1?n.bounds.screenHeight.smooth(t.smoothFrames):n.bounds.screenHeight),t.hideWhenLost&&(r.opacity=n.active),Nt(r,i,t)}return Nt(r,e,t)}y();function Sn(r){let e=[];return r.enableDetection===!0&&e.push("detect"),r.enableSegmentation===!0&&e.push("segment"),r.enablePose===!0&&e.push("pose"),r.enableMatte===!0&&e.push("matte"),e.length>0?e:["detect"]}var We=class r{id;kind="vision";source;config;vision;_runner;_runnerOptions;constructor(e,t={},n={},s={}){this.id=`vision-${Math.random().toString(36).slice(2,9)}`,this.source=e,this.config=t,this._runnerOptions=s,this.vision=new Ce({...n,config:t})}runner(){if(!this._runner){let{variant:e,confidence:t,classes:n,modelsDir:s,baseUrl:o,maskThreshold:i,featherRadius:m}=this.config;this._runner=fe.create({...this._runnerOptions,...e!==void 0?{variant:e}:{},...t!==void 0?{confidence:t}:{},...n!==void 0?{classes:n}:{},...s!==void 0?{modelsDir:s}:{},...o!==void 0?{baseUrl:o}:{},...i!==void 0?{maskThreshold:i}:{},...m!==void 0?{featherRadius:m}:{}})}return this._runner}async ready(){let e=this.runner();return await e.preload(Sn(this.config)),e}close(){this._runner?.close(),this._runner=void 0}static attach(e,t={},n={}){let s=new r(e,t,n),o=s.vision;return Object.defineProperties(o,{node:{value:s.toNode(),enumerable:!0},ready:{value:()=>s.ready()},runner:{value:()=>s.runner()},close:{value:()=>s.close()}}),o}toNode(){return{id:this.id,kind:"vision",source:this.source,config:this.config}}};y();var qe=new Uint8Array(0),Xt=new Uint32Array(0);function _n(r,e,t){qe.length<r.length&&(qe=new Uint8Array(r.length),Xt=new Uint32Array(r.length));let n=qe,s=Xt,o=0,i=0;for(let m=1;m<=r.length;m++)(m===r.length||r[m]!==r[i])&&(n[o]=r[i],s[o]=m-i,o++,i=m);return{width:e,height:t,values:n.slice(0,o),lengths:s.slice(0,o)}}function wn(r){let e=new Uint8Array(r.width*r.height),t=0;for(let n=0;n<r.values.length;n++){let s=t+r.lengths[n];r.values[n]!==0&&e.fill(r.values[n],t,s),t=s}return e}function Gt(r){return r.values.byteLength+r.lengths.byteLength}function Qe(r){let{mask:e,...t}=r;return{...t,rle:_n(e,r.width,r.height)}}function Je(r){let{rle:e,...t}=r;return{...t,mask:wn(e)}}function Ze(r){let e=512+r.detections.length*128+r.objects.length*256;for(let t of r.masks??[])e+=Gt(t.rle);return r.subject&&(e+=Gt(r.subject.mask.rle)),e}var Oe=class{constructor(e=256*1024*1024){this.maxBytes=e}maxBytes;entries=new Map;bytes=0;get(e,t){let n=`${e}#${t}`,s=this.entries.get(n);return s&&(this.entries.delete(n),this.entries.set(n,s)),s}set(e,t,n){let s=`${e}#${t}`,o=this.entries.get(s);o&&(this.bytes-=Ze(o),this.entries.delete(s)),this.entries.set(s,n),this.bytes+=Ze(n);for(let[i,m]of this.entries){if(this.bytes<=this.maxBytes)break;this.entries.delete(i),this.bytes-=Ze(m)}}clear(){this.entries.clear(),this.bytes=0}get size(){return this.entries.size}};y();var vn=`
|
|
61
|
-
struct Params {
|
|
62
|
-
mode: u32, // 0 matte, 1 mask, 2 crop
|
|
63
|
-
_pad: u32,
|
|
64
|
-
cropMin: vec2<f32>, // uv of the subject bounds (crop)
|
|
65
|
-
cropMax: vec2<f32>,
|
|
66
|
-
_pad2: vec2<f32>,
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
@group(0) @binding(0) var<uniform> params: Params;
|
|
70
|
-
@group(0) @binding(1) var picture: texture_2d<f32>;
|
|
71
|
-
@group(0) @binding(2) var mask: texture_2d<f32>;
|
|
72
|
-
@group(0) @binding(3) var samp: sampler;
|
|
73
|
-
|
|
74
|
-
struct VSOut {
|
|
75
|
-
@builtin(position) pos: vec4<f32>,
|
|
76
|
-
@location(0) uv: vec2<f32>,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
@vertex fn vs(@builtin(vertex_index) i: u32) -> VSOut {
|
|
80
|
-
var p = array<vec2<f32>, 3>(vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
|
|
81
|
-
var out: VSOut;
|
|
82
|
-
out.pos = vec4(p[i], 0.0, 1.0);
|
|
83
|
-
out.uv = vec2(p[i].x * 0.5 + 0.5, 0.5 - p[i].y * 0.5);
|
|
84
|
-
return out;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
@fragment fn fs(in: VSOut) -> @location(0) vec4<f32> {
|
|
88
|
-
var uv = in.uv;
|
|
89
|
-
if (params.mode == 2u) {
|
|
90
|
-
uv = mix(params.cropMin, params.cropMax, uv);
|
|
91
|
-
}
|
|
92
|
-
let m = textureSampleLevel(mask, samp, uv, 0.0).r;
|
|
93
|
-
if (params.mode == 1u) {
|
|
94
|
-
return vec4(m, m, m, 1.0);
|
|
95
|
-
}
|
|
96
|
-
return textureSampleLevel(picture, samp, uv, 0.0) * m;
|
|
97
|
-
}
|
|
98
|
-
`,Ie=class{constructor(e,t){this.device=e;let n=e.createShaderModule({label:"vision_matte_composite",code:vn});this.pipeline=e.createRenderPipeline({label:"vision_matte_composite",layout:"auto",vertex:{module:n,entryPoint:"vs"},fragment:{module:n,entryPoint:"fs",targets:[{format:t}]},primitive:{topology:"triangle-list"}}),this.sampler=e.createSampler({magFilter:"linear",minFilter:"linear"})}device;pipeline;sampler;uniforms=[];uniformIndex=0;masks=new Map;uploadMask(e,t,n,s){let o=this.masks.get(e);(!o||o.width!==n||o.height!==s)&&(o?.destroy(),o=this.device.createTexture({label:`vision_mask_${e}`,size:[n,s],format:"r8unorm",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),this.masks.set(e,o));let i=Math.ceil(n/256)*256,m=t;if(i!==n){m=new Uint8Array(i*s);for(let a=0;a<s;a++)m.set(t.subarray(a*n,(a+1)*n),a*i)}return this.device.queue.writeTexture({texture:o},m.buffer,{offset:m.byteOffset,bytesPerRow:i,rowsPerImage:s},{width:n,height:s}),o}draw(e,t,n,s,o,i){let m=s.width,a=s.height,c=new Float32Array(8),h=new Uint32Array(c.buffer);h[0]=o==="matte"?0:o==="mask"?1:2,o==="crop"&&i?(c[2]=i.x0/m,c[3]=i.y0/a,c[4]=(i.x1+1)/m,c[5]=(i.y1+1)/a):(c[2]=0,c[3]=0,c[4]=1,c[5]=1,o==="crop"&&(h[0]=0)),this.uniformIndex>=this.uniforms.length&&this.uniforms.push(this.device.createBuffer({size:c.byteLength,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}));let l=this.uniforms[this.uniformIndex];this.uniformIndex=(this.uniformIndex+1)%16,this.device.queue.writeBuffer(l,0,c);let p=this.device.createBindGroup({layout:this.pipeline.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:l}},{binding:1,resource:n.createView()},{binding:2,resource:s.createView()},{binding:3,resource:this.sampler}]}),T=e.beginRenderPass({label:"vision_matte_composite",colorAttachments:[{view:t,loadOp:"clear",storeOp:"store",clearValue:{r:0,g:0,b:0,a:0}}]});T.setPipeline(this.pipeline),T.setBindGroup(0,p),T.draw(3),T.end()}};var pe=new Map,Mn=4,et=null,Le=null,$t=null,Kt=new Oe,Wt=new Map,Pn=new Map,Rn=new Map,qt=new Map,tt=new WeakMap,de=new Map,nt=new Map,Cn=3;function Zt(r,e,t,n,s,o){let i=r.get(t);return(!i||i.width!==n.width||i.height!==n.height||i.format!==n.format)&&(i?.destroy(),i=e.createTexture({size:[n.width,n.height],format:n.format,usage:s,label:o}),r.set(t,i)),i}var On=(r,e,t)=>Zt(Pn,r,e,t,GPUTextureUsage.COPY_DST|GPUTextureUsage.TEXTURE_BINDING,"vision_held_frame"),In=(r,e,t)=>Zt(Rn,r,e,t,GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_SRC,"vision_matte");function Ln(r,e,t,n,s){let o=e;return JSON.stringify([r,o?.operation?.id??o?.id??null,n,s,t.mode??"passthrough",t.variant,t.confidence,t.classes,t.maskThreshold,t.featherRadius,t.matteSource,t.keyBackground===!0,t.maxMissedFrames])}function En(r,e,t){let n=r.backgroundKeyThreshold;return typeof n=="number"?n:typeof n?.get=="function"?Number(n.get({frame:e,fps:t})):typeof n?._value=="number"?Number(n._value):70}function jn(r,e,t){if(r)return nt.set(e,{mask:r,frame:t}),r;let n=nt.get(e);if(n&&t-n.frame<=Cn)return n.mask}function Vn(r){let e={variant:r.variant,modelsDir:r.modelsDir,baseUrl:r.baseUrl},t=JSON.stringify(e),n=pe.get(t);if(!n){if(pe.size>=Mn){let[m,a]=pe.entries().next().value;a.runner.close(),pe.delete(m)}n={runner:fe.create(e),derived:new Map},pe.set(t,n)}let s={confidence:r.confidence,classes:r.classes,maskThreshold:r.maskThreshold,featherRadius:r.featherRadius},o=JSON.stringify(s),i=n.derived.get(o);return i||(i=n.runner.derive(s),n.derived.set(o,i)),i}function An(r,e){let t=qt.get(r);return t||(t=new ue({maxMissedFrames:e.maxMissedFrames??15}),qt.set(r,t)),t}function Dn(r,e,t){return e<=0||t<=0?r:{people:r.people.map(n=>({...n,keypoints:n.keypoints.map(s=>({x:s.x/e,y:s.y/t,visibility:s.visibility}))}))}}function Bn(r,e){return r.map(t=>{let{cx:n,cy:s}=zn(t),o=null;for(let i of e){let m=i.centerX-n,a=i.centerY-s,c=m*m+a*a;(!o||c<o.distance)&&(o={trackId:i.trackId,distance:c})}return o?{...t,trackId:o.trackId}:t})}function Fn(r,e,t){let n=e*t,s=new Uint8Array(n),o=new Int32Array(n),i=0,m=c=>{s[c]||r[c]>0||(s[c]=1,o[i++]=c)};for(let c=0;c<e;c++)m(c),m((t-1)*e+c);for(let c=0;c<t;c++)m(c*e),m(c*e+e-1);let a=0;for(;a<i;){let c=o[a++],h=c%e,l=c+1;h+1<e&&!s[l]&&r[l]===0&&(s[l]=1,o[i++]=l),l=c-1,h>0&&!s[l]&&r[l]===0&&(s[l]=1,o[i++]=l),l=c+e,l<n&&!s[l]&&r[l]===0&&(s[l]=1,o[i++]=l),l=c-e,l>=0&&!s[l]&&r[l]===0&&(s[l]=1,o[i++]=l)}for(let c=0;c<n;c++)r[c]===0&&s[c]===0&&(r[c]=255)}function Un(r,e,t,n=2){if(n<=0)return r;let s=new Uint8Array(e*t),o=new Uint8Array(e*t),i=2*n+1;for(let a=0;a<t;a++){let c=a*e,h=0;for(let l=-n;l<=n;l++){let p=Math.max(0,Math.min(e-1,l));h+=r[c+p]}for(let l=0;l<e;l++){s[c+l]=Math.round(h/i);let p=Math.max(0,l-n),T=Math.min(e-1,l+n+1);h+=r[c+T]-r[c+p]}}let m=new Float64Array(e);for(let a=-n;a<=n;a++){let c=Math.max(0,Math.min(t-1,a))*e;for(let h=0;h<e;h++)m[h]+=s[c+h]}for(let a=0;a<t;a++){let c=a*e,h=Math.max(0,a-n)*e,l=Math.min(t-1,a+n+1)*e;for(let p=0;p<e;p++)o[c+p]=Math.round(m[p]/i),m[p]+=s[l+p]-s[h+p]}return o}function Hn(r,e,t,n,s,o){let i=0,m=0,a=0,c=0,h=(g,_)=>{let b=_*e+g;if(r[b])return;let V=b*4;i+=n[V],m+=n[V+1],a+=n[V+2],c++};for(let g=0;g<e;g+=4)h(g,0),h(g,t-1);for(let g=0;g<t;g+=4)h(0,g),h(e-1,g);if(c===0)return r;i/=c,m/=c,a/=c;let l=e*t,p=new Uint8Array(l);for(let g=0,_=0;g<l;g++,_+=4)p[g]=Math.max(Math.abs(n[_]-i),Math.abs(n[_+1]-m),Math.abs(n[_+2]-a))>s?1:0;let T=new Int32Array(l),j=0;for(let g=0;g<l;g++)r[g]>0&&(p[g]&&(r[g]=255),T[j++]=g);let P=0;for(;P<j;){let g=T[P++],_=g%e,b=g+1;_+1<e&&r[b]!==255&&p[b]&&(r[b]=255,T[j++]=b),b=g-1,_>0&&r[b]!==255&&p[b]&&(r[b]=255,T[j++]=b),b=g+e,b<l&&r[b]!==255&&p[b]&&(r[b]=255,T[j++]=b),b=g-e,b>=0&&r[b]!==255&&p[b]&&(r[b]=255,T[j++]=b)}Fn(r,e,t);let E=o!==void 0&&o>0?Math.max(1,Math.min(8,Math.round(o*50))):2;return Un(r,e,t,E)}function zn(r){let{mask:e,width:t,height:n}=r,s=0,o=0,i=0;for(let m=0;m<n;m++){let a=m*t;for(let c=0;c<t;c++)e[a+c]>0&&(s+=c,o+=m,i++)}return i===0?{cx:0,cy:0}:{cx:s/i,cy:o/i}}var Qt=async r=>{let{ctx:e,encoder:t,pass:n,targetView:s,targetWidth:o,targetHeight:i,props:m,drawChild:a}=r,{virtualMedia:c}=m,h=c?.operation;if(h?.op!=="Vision")return;let l=h;n.end();let p=c?.children?.[0];if(!p)return;let T=l.effect,j=T!=null,P=c?.id??h?.id??`${o}x${i}_${l.mode}_${l.variant}_${l.keyBackground}_${l.backgroundKeyThreshold}`,E=`vision-${P}`,g=j?tt.get(T):de.get(E);g&&(g.width!==o||g.height!==i)&&(g.destroy(),g=void 0,j?tt.delete(T):de.delete(E));let _=g!==void 0;if(!g&&(g=e.device.createTexture({size:[o,i],format:e.renderer.format,usage:GPUTextureUsage.RENDER_ATTACHMENT|GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_SRC,label:"vision_child_persistent"}),j?tt.set(T,g):de.set(E,g),de.size>4)){let B=de.keys().next().value;B&&B!==E&&(de.get(B)?.destroy(),de.delete(B))}let b=m.frame??0,V=m.compositionFrame??b,f=m.fps??24,d=l.mode??"passthrough",M=d==="mask"||d==="matte"||d==="crop",x=l.visionBundle??c.visionBundle,w=l.enableDetection!==!1||d==="boxes"||d==="tracking",I=l.enableSegmentation===!0||M&&l.matteSource!=="selfie",U=l.enableMatte===!0||M&&l.matteSource==="selfie",C=l.enablePose===!0||d==="skeleton",v=!U&&!C&&(w||I),u=v?Ln(P,p,l,o,i):"",R=B=>l.keyBackground===!0?En(l,V-(b-B),f):void 0,z=B=>{if(!v||B===void 0)return;let G=Kt.get(u,B);if(!(!G||G.keyThreshold!==R(B))&&!(I&&x&&!G.masks))return G},F=_?Wt.get(P):void 0,O=z(b),D=O!==void 0;O||(O=z(F));let $;M&&_&&!D&&($=On(e.device,P,g),t.copyTextureToTexture({texture:g},{texture:$},[o,i,1]));let N=null;if(_&&!O){let G=o*4,Z=Math.ceil(G/256)*256,J=Z*i,ee=e.device.createBuffer({size:J,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ,label:"vision_frame_staging"}),W=e.device.createCommandEncoder({label:"vision_readback_encoder"});W.copyTextureToBuffer({texture:g},{buffer:ee,bytesPerRow:Z,rowsPerImage:i},[o,i,1]),e.device.queue.submit([W.finish()]),await ee.mapAsync(GPUMapMode.READ);let he=new Uint8Array(ee.getMappedRange());if(N=new Uint8ClampedArray(o*i*4),Z===G)N.set(he.subarray(0,N.length));else for(let ge=0;ge<i;ge++)N.set(he.subarray(ge*Z,ge*Z+G),ge*G);ee.unmap(),ee.destroy()}let X=g.createView();e.renderer.beginFrame(t,X,{r:0,g:0,b:0,a:0},o,i,"clear").end(),await a(p,{...m},X,g,o,i),Wt.set(P,b);let K=[],H=[],ne,Q,Y;if(O)K=O.objects,w&&x?.setObjectResult(b,{objects:O.objects,rawDetections:O.detections}),I&&x&&O.masks&&(H=O.masks.map(Je),x.setMaskResult(b,H)),O.subject&&(Y=Je(O.subject.mask),nt.set(P,{mask:Y,frame:b}));else if(N){let B=Vn(l),G={data:N,width:o,height:i},Z=[];if(w){Z=await B.detect(G);let J=An(P,l);b===0&&J.reset(),K=J.update(Z,b,f),x?.setObjectResult(b,{objects:K,rawDetections:Z})}if(I){let J=await B.segment(G);H=Bn(J.masks,K),x?.setMaskResult(b,H)}if(U&&(ne=await B.matte(G),x?.setMatteResult(b,ne)),C){let J=await B.pose(G),ee=Dn(J,G.width,G.height);Q={people:Yt(ee.people,K)},x?.setPoseResult(b,Q)}if(M){Y=jn(l.matteSource==="selfie"?Yn(ne):zt(H.length>0?H:x?.getMaskResult(b)??[]),P,b);let J=R(F??b);Y&&J!==void 0&&(Y={...Y,mask:Hn(Y.mask,Y.width,Y.height,N,J,l.featherRadius)})}v&&F!==void 0&&Kt.set(u,F,{detections:Z,objects:K,masks:x?H.map(Qe):null,subject:Y?{mask:Qe(Y)}:null,keyThreshold:R(F)})}if(M){let B=D?g:$;if(!Y||!B){e.renderer.beginFrame(t,s,{r:0,g:0,b:0,a:0},o,i,"clear").end();return}(!Le||$t!==e.renderer.format)&&(Le=new Ie(e.device,e.renderer.format),$t=e.renderer.format);let G=Le.uploadMask(P,Y.mask,Y.width,Y.height),Z=In(e.device,P,g);Le.draw(t,Z.createView(),B,G,d,d==="crop"?Me(Y)??void 0:void 0),x?.setStencilTexture(Z);let J=e.renderer.beginFrame(t,s,{r:0,g:0,b:0,a:0},o,i,"clear");e.renderer.drawTexture(J,Z,{x:0,y:0,width:o,height:i}),J.end();return}if(d==="skeleton"){et||(et=new _e(e.device));let G=(x?.getPoseResult(b)??Q)?.people[0];if(G&&G.keypoints.length>0){let Z=et.renderToTexture(G.keypoints,{width:o,height:i}),J=e.renderer.beginFrame(t,s,{r:0,g:0,b:0,a:0},o,i,"clear");e.renderer.drawTexture(J,Z,{x:0,y:0,width:o,height:i}),J.end();return}}if(d==="boxes"||d==="tracking"){let B=e.renderer.beginFrame(t,s,{r:0,g:0,b:0,a:0},o,i,"clear");e.renderer.drawTexture(B,g,{x:0,y:0,width:o,height:i});let G=K.length>0?K:x?.getObjectResult(b).objects??[];if(G.length>0){let Z="#38bdf8";for(let ee of G){if(!ee.active)continue;let W=ee.boundingBox;e.renderer.drawRect(B,{x:W.originX,y:W.originY,width:W.width,height:2},Z),e.renderer.drawRect(B,{x:W.originX,y:W.originY+W.height-2,width:W.width,height:2},Z),e.renderer.drawRect(B,{x:W.originX,y:W.originY,width:2,height:W.height},Z),e.renderer.drawRect(B,{x:W.originX+W.width-2,y:W.originY,width:2,height:W.height},Z);let he=Math.min(16,W.width/4,W.height/4);e.renderer.drawRect(B,{x:W.originX,y:W.originY,width:he,height:4},"#ffffff"),e.renderer.drawRect(B,{x:W.originX,y:W.originY,width:4,height:he},"#ffffff"),d==="tracking"&&e.renderer.drawRect(B,{x:ee.centerX-3,y:ee.centerY-3,width:6,height:6},"#ef4444",3)}}B.end();return}let re=e.renderer.beginFrame(t,s,{r:0,g:0,b:0,a:0},o,i,"clear");e.renderer.drawTexture(re,g,{x:0,y:0,width:o,height:i}),re.end()};function Yn(r){if(!r||r.coverage<=0)return;let e=Math.round(r.coverage*r.width*r.height);return{category:"person",mask:r.mask,width:r.width,height:r.height,area:e,coverage:r.coverage,detectionIndex:-1}}var Ws=st({WebGPURenderer:Qt});export{ht as a,gt as b,pt as c,tr as d,tn as e,sr as f,cr as g,mr as h,fr as i,nn as j,Tt as k,kt as l,pr as m,rn as n,on as o,sn as p,an as q,cn as r,ln as s,De as t,St as u,dn as v,Be as w,Or as x,_t as y,Ar as z,Dr as A,Ur as B,Se as C,ke as D,ue as E,Wr as F,k as G,Ge as H,Et as I,_e as J,jt as K,At as L,fe as M,Re as N,Ce as O,Nt as P,as as Q,We as R,Ws as S};
|