2020-10-18 14:07:45 +02:00
|
|
|
var Ge=Object.defineProperty;var x=(e,t)=>()=>(t||(t={exports:{}},e(t.exports,t)),t.exports),N0=e=>Ge(e,"__esModule",{value:!0}),Qe=(e,t)=>{N0(e);for(var n in t)Ge(e,n,{get:t[n],enumerable:!0})};var tt=x(ae=>{const g=require("@tensorflow/tfjs"),Ze=6;function R0(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 d=i*(c+.5);for(let u=0;u<r;u++){const h=i*(u+.5);for(let l=0;l<a;l++)n.push([h,d])}}}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])}),z0=(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 A0(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),d=g.sub(a,c),u=g.add(a,c),h=g.mul(d,n),l=g.mul(u,n),m=1;return g.concat2d([h,l],m)}function O0(e,t){return g.tidy(()=>{const n=e.box?e.box:e;return z0(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=R0(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 d=e.resizeBilinear([this.width,this.height]),u=g.mul(g.sub(d.div(255),.5),2),h=this.blazeFaceModel.predict(u);let l;if(Array.isArray(h)){const b=h.sort((C,L)=>C.size-L.size),M=g.concat([b[0],b[2]],2),w=g.concat([b[1],b[3]],2),O=g.concat([w,M],1);l=O.squeeze(0)}else l=h.squeeze();const m=A0(l,this.anchors,this.inputSize),p=g.slice(l,[0,0],[-1,1]),P=g.sigmoid(p).squeeze();return[l,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(d=>g.slice(n,[d,0],[1,-1])),a=await Promise.all(r.map(async d=>{const u=await d.array();return d.dispose(),u})),c=[];for(let d=0;d<a.length;d++){const u=a[d],h=Je(u),l=s[d],m=this.anchorsData[l],p=g.slice(t,[l,Ze-1],[1,-1]),P=p.squeeze(),b=P.reshape([Ze,-1]),M=g.slice(o,[l],[1]),w={box:h,landmarks:b,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,d,u]=await Promise.all([r.landmarks,a,r.probability].map(async b=>b.array())),h=r.anchor,[l,m]=s,p=c.map(b=>[(b[0]+h[0])*l,(b[1]+h[1])*m]),P={topLeft:d.slice(0,2),bottomRight:d.slice(2),landmarks:p,probability:u};return $e(r.box),r.landmarks.dispose(),r.probability.dispose(),a.dispose(),P}))}}async function v0(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=v0;ae.BlazeFaceModel=et;ae.disposeBox=$e});var be=x(ge=>{ge.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
|