2020-10-16 16:48:10 +02:00
|
|
|
var We=Object.defineProperty;var y=(e,t)=>()=>(t||(t={exports:{}},e(t.exports,t)),t.exports),B0=e=>We(e,"__esModule",{value:!0}),Ke=(e,t)=>{B0(e);for(var n in t)We(e,n,{get:t[n],enumerable:!0})};var Qe=y(se=>{const f=require("@tensorflow/tfjs"),Ye=6;function T0(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 h=0;h<r;h++){const u=i*(h+.5);for(let d=0;d<a;d++)n.push([u,l])}}}return n}const Ge=e=>{e.startEndTensor.dispose(),e.startPoint.dispose(),e.endPoint.dispose()},Xe=e=>({startEndTensor:e,startPoint:f.slice(e,[0,0],[-1,2]),endPoint:f.slice(e,[0,2],[-1,2])}),k0=(e,t)=>{const n=f.mul(e.startPoint,t),o=f.mul(e.endPoint,t),i=f.concat2d([n,o],1);return Xe(i)};function _0(e,t,n){const o=f.slice(e,[0,1],[-1,2]),i=f.add(o,t),s=f.slice(e,[0,3],[-1,2]),r=f.div(s,n),a=f.div(i,n),c=f.div(r,2),l=f.sub(a,c),h=f.add(a,c),u=f.mul(l,n),d=f.mul(h,n),m=1;return f.concat2d([u,d],m)}function z0(e,t){return f.tidy(()=>{const n=e.box?e.box:e;return k0(n,t).startEndTensor.squeeze()})}class Ve{constructor(e,t){this.blazeFaceModel=e,this.width=t.detector.inputSize,this.height=t.detector.inputSize,this.maxFaces=t.detector.maxFaces,this.anchorsData=T0(t.detector.inputSize),this.anchors=f.tensor2d(this.anchorsData),this.inputSize=f.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]=f.tidy(()=>{const c=e.resizeBilinear([this.width,this.height]),l=f.mul(f.sub(c.div(255),.5),2),h=this.blazeFaceModel.predict(l);let u;if(Array.isArray(h)){const x=h.sort((N,v)=>N.size-v.size),E=f.concat([x[0],x[2]],2),P=f.concat([x[1],x[3]],2),w=f.concat([P,E],1);u=w.squeeze(0)}else u=h.squeeze();const d=_0(u,this.anchors,this.inputSize),m=f.slice(u,[0,0],[-1,1]),p=f.sigmoid(m).squeeze();return[u,d,p]}),i=await f.image.nonMaxSuppressionAsync(n,o,this.maxFaces,this.iouThreshold,this.scoreThreshold),s=await i.array();i.dispose();let r=s.map(c=>f.slice(n,[c,0],[1,-1]));r=await Promise.all(r.map(async c=>{const l=await c.array();return c.dispose(),l}));const a=[];for(let c=0;c<r.length;c++){const l=r[c],h=f.tidy(()=>{const u=Xe(l),d=s[c],m=this.anchorsData[d],p=f.slice(t,[d,Ye-1],[1,-1]).squeeze().reshape([Ye,-1]),x=f.slice(o,[d],[1]);return{box:u,landmarks:p,probability:x,anchor:m}});a.push(h)}return n.dispose(),o.dispose(),t.dispose(),{boxes:a,scaleFactor:[e.shape[2]/this.width,e.shape[1]/this.height]}}async estimateFaces(e){const t=f.tidy(()=>(e instanceof f.Tensor||(e=f.browser.fromPixels(e)),e.toFloat().expandDims(0))),{boxes:n,scaleFactor:o}=await this.getBoundingBoxes(t);return t.dispose(),Promise.all(n.map(async i=>{const s=z0(i,o),[r,a,c]=await Promise.all([i.landmarks,s,i.probability].map(async p=>p.array())),l=i.anchor,[h,u]=o,d=r.map(p=>[(p[0]+l[0])*h,(p[1]+l[1])*u]),m={topLeft:a.slice(0,2),bottomRight:a.slice(2),landmarks:d,probability:c};return Ge(i.box),i.landmarks.dispose(),i.probability.dispose(),s.dispose(),m}))}}async function A0(e){const t=await f.loadGraphModel(e.detector.modelPath,{fromTFHub:e.detector.modelPath.includes("tfhub.dev")}),n=new Ve(t,e);return n}se.load=A0;se.BlazeFaceModel=Ve;se.disposeBox=Ge});var pe=y(me=>{me.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],rightEyeLower2:[226,31,228,229,230,231,232
|