2020-10-17 17:38:24 +02:00
|
|
|
var Ve=Object.defineProperty;var y=(e,t)=>()=>(t||(t={exports:{}},e(t.exports,t)),t.exports),z0=e=>Ve(e,"__esModule",{value:!0}),Qe=(e,t)=>{z0(e);for(var n in t)Ve(e,n,{get:t[n],enumerable:!0})};var tt=y(ae=>{const g=require("@tensorflow/tfjs"),Ze=6;function N0(e){const t={strides:[e/16,e/8],anchors:[2,6]},n=[];for(let o=0;o<t.strides.length;o++){const i=t.strides[o],s=Math.floor((e+i-1)/i),r=Math.floor((e+i-1)/i),a=t.anchors[o];for(let c=0;c<s;c++){const l=i*(c+.5);for(let u=0;u<r;u++){const h=i*(u+.5);for(let d=0;d<a;d++)n.push([h,l])}}}return n}const $e=e=>{e.startEndTensor.dispose(),e.startPoint.dispose(),e.endPoint.dispose()},Je=e=>({startEndTensor:e,startPoint:g.slice(e,[0,0],[-1,2]),endPoint:g.slice(e,[0,2],[-1,2])}),A0=(e,t)=>{const n=g.mul(e.startPoint,t),o=g.mul(e.endPoint,t),i=g.concat2d([n,o],1);return Je(i)};function R0(e,t,n){const o=g.slice(e,[0,1],[-1,2]),i=g.add(o,t),s=g.slice(e,[0,3],[-1,2]),r=g.div(s,n),a=g.div(i,n),c=g.div(r,2),l=g.sub(a,c),u=g.add(a,c),h=g.mul(l,n),d=g.mul(u,n),m=1;return g.concat2d([h,d],m)}function O0(e,t){return g.tidy(()=>{const n=e.box?e.box:e;return A0(n,t).startEndTensor.squeeze()})}class et{constructor(e,t){this.blazeFaceModel=e,this.width=t.detector.inputSize,this.height=t.detector.inputSize,this.maxFaces=t.detector.maxFaces,this.anchorsData=N0(t.detector.inputSize),this.anchors=g.tensor2d(this.anchorsData),this.inputSize=g.tensor1d([this.width,this.height]),this.iouThreshold=t.detector.iouThreshold,this.scaleFaces=.8,this.scoreThreshold=t.detector.scoreThreshold}async getBoundingBoxes(e){if(!e||e.isDisposedInternal||e.shape.length!==4||e.shape[1]<1||e.shape[2]<1)return null;const[t,n,o]=g.tidy(()=>{const l=e.resizeBilinear([this.width,this.height]),u=g.mul(g.sub(l.div(255),.5),2),h=this.blazeFaceModel.predict(u);let d;if(Array.isArray(h)){const x=h.sort((C,F)=>C.size-F.size),M=g.concat([x[0],x[2]],2),w=g.concat([x[1],x[3]],2),R=g.concat([w,M],1);d=R.squeeze(0)}else d=h.squeeze();const m=R0(d,this.anchors,this.inputSize),p=g.slice(d,[0,0],[-1,1]),P=g.sigmoid(p).squeeze();return[d,m,P]}),i=await g.image.nonMaxSuppressionAsync(n,o,this.maxFaces,this.iouThreshold,this.scoreThreshold),s=await i.array();i.dispose();const r=s.map(l=>g.slice(n,[l,0],[1,-1])),a=await Promise.all(r.map(async l=>{const u=await l.array();return l.dispose(),u})),c=[];for(let l=0;l<a.length;l++){const u=a[l],h=Je(u),d=s[l],m=this.anchorsData[d],p=g.slice(t,[d,Ze-1],[1,-1]),P=p.squeeze(),x=P.reshape([Ze,-1]),M=g.slice(o,[d],[1]),w={box:h,landmarks:x,probability:M,anchor:m};c.push(w),p.dispose(),P.dispose()}return t.dispose(),n.dispose(),o.dispose(),t.dispose(),{boxes:c,scaleFactor:[e.shape[2]/this.width,e.shape[1]/this.height]}}async estimateFaces(e){const t=e instanceof g.Tensor?e:g.browser.fromPixels(e),n=t.toFloat(),o=n.expandDims(0);t.dispose(),n.dispose();const{boxes:i,scaleFactor:s}=await this.getBoundingBoxes(o);return o.dispose(),Promise.all(i.map(async r=>{const a=O0(r,s),[c,l,u]=await Promise.all([r.landmarks,a,r.probability].map(async x=>x.array())),h=r.anchor,[d,m]=s,p=c.map(x=>[(x[0]+h[0])*d,(x[1]+h[1])*m]),P={topLeft:l.slice(0,2),bottomRight:l.slice(2),landmarks:p,probability:u};return $e(r.box),r.landmarks.dispose(),r.probability.dispose(),a.dispose(),P}))}}async function D0(e){const t=await g.loadGraphModel(e.detector.modelPath,{fromTFHub:e.detector.modelPath.includes("tfhub.dev")}),n=new et(t,e);return n}ae.load=D0;ae.BlazeFaceModel=et;ae.disposeBox=$e});var ye=y(be=>{be.MESH_ANNOTATIONS={silhouette:[10,338,297,332,284,251,389,356,454,323,361,288,397,365,379,378,400,377,152,148,176,149,150,136,172,58,132,93,234,127,162,21,54,103,67,109],lipsUpperOuter:[61,185,40,39,37,0,267,269,270,409,291],lipsLowerOuter:[146,91,181,84,17,314,405,321,375,291],lipsUpperInner:[78,191,80,81,82,13,312,311,310,415,308],lipsLowerInner:[78,95,88,178,87,14,317,402,318,324,308],rightEyeUpper0:[246,161,160,159,158,157,173],rightEyeLower0:[33,7,163,144,145,153,154,155,133],rightEyeUpper1:[247,30,29,27,28,56,190],rightEyeLower1:[130,25,110,24,23,22,26,112,243],rightEyeUpper2:[113,225,224,223,222,221,189],rightEyeLo
|