@tensorflow-models/body-pix
Version:
Pretrained BodyPix model in TensorFlow.js
310 lines (308 loc) • 50.6 kB
JavaScript
/**
* @license
* Copyright 2023 Google LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import*as t from"@tensorflow/tfjs-converter";import*as e from"@tensorflow/tfjs-core";import{getBackend as n}from"@tensorflow/tfjs-core";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var r=function(t,e){return r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])},r(t,e)};function o(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},i.apply(this,arguments)};function a(t,e,n,r){return new(n||(n=Promise))((function(o,i){function a(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,s)}u((r=r.apply(t,e||[])).next())}))}function s(t,e){var n,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=e.call(t,a)}catch(t){i=[6,t],r=0}finally{n=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function u(t){var n=t.shape[2],r=e.argMax(t,2),o=e.reshape(r,[-1]);return e.oneHot(o,n)}function c(t,n){return e.tidy((function(){return e.cast(e.greater(t,e.scalar(n)),"int32")}))}function f(t,n){var r=n.shape,o=r[0],i=r[1],a=r[2];return e.tidy((function(){var r,s,c=u(n),f=e.expandDims(e.range(0,a,1,"int32"),1),d=e.cast(e.matMul(c,f),"int32"),l=e.reshape(d,[o,i]),h=e.add(l,e.scalar(1,"int32"));return e.sub((r=h,s=t,e.mul(r,s)),e.scalar(1,"int32"))}))}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
var d=function(){function t(t,n){this.model=t,this.outputStride=n;var r=this.model.inputs[0].shape;e.util.assert(-1===r[1]&&-1===r[2],(function(){return"Input shape [".concat(r[1],", ").concat(r[2],"] ")+"must both be equal to or -1"}))}return t.prototype.predict=function(t){var n=this;return e.tidy((function(){var r=n.preprocessInput(e.cast(t,"float32")),o=e.expandDims(r,0),i=n.model.predict(o).map((function(t){return e.squeeze(t,[0])})),a=n.nameOutputResults(i);return{heatmapScores:e.sigmoid(a.heatmap),offsets:a.offsets,displacementFwd:a.displacementFwd,displacementBwd:a.displacementBwd,segmentation:a.segmentation,partHeatmaps:a.partHeatmaps,longOffsets:a.longOffsets,partOffsets:a.partOffsets}}))},t.prototype.dispose=function(){this.model.dispose()},t}(),l=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.preprocessInput=function(t){return e.tidy((function(){return e.sub(e.div(t,127.5),1)}))},n.prototype.nameOutputResults=function(t){return{offsets:t[0],segmentation:t[1],partHeatmaps:t[2],longOffsets:t[3],heatmap:t[4],displacementFwd:t[5],displacementBwd:t[6],partOffsets:t[7]}},n}(d),h=["nose","leftEye","rightEye","leftEar","rightEar","leftShoulder","rightShoulder","leftElbow","rightElbow","leftWrist","rightWrist","leftHip","rightHip","leftKnee","rightKnee","leftAnkle","rightAnkle"],p=h.length,m=h.reduce((function(t,e,n){return t[e]=n,t}),{});
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
function g(t,e,n){var r=t[0],o=t[1],i=e[0],a=e[1],s=n.top,u=n.bottom;return[a/(n.left+n.right+o),i/(s+u+r)]}function v(t,e,n,r){return{y:r.get(t,e,n),x:r.get(t,e,n+p)}}function w(t,e,n){var r=v(t.heatmapY,t.heatmapX,t.id,n),o=r.y,i=r.x;return{x:t.heatmapX*e+i,y:t.heatmapY*e+o}}function y(t,e,n){return t<e?e:t>n?n:t}function b(t,e){return{x:t.x+e.x,y:t.y+e.y}}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function S(t,e,n){void 0===n&&(n=.3);for(var r=0,o=0,i=0;i<t.length;i++)e.keypoints[i].score>n&&(o+=1,r+=Math.pow(t[i].x-e.keypoints[i].position.x,2)+Math.pow(t[i].y-e.keypoints[i].position.y,2));return 0===o?r=1/0:r/=o,r}function x(t,e,n,r,o,i,a){for(var s=a[0],u=a[1],c=n(t),f=c.y*r+c.x,d=o[p*(2*f)+e],l=o[p*(2*f+1)+e],h=t.y+d,m=t.x+l,g=0;g<i;g++){h=Math.min(h,s-1);var v=n({x:m=Math.min(m,u-1),y:h}),w=v.y*r+v.x;h+=d=o[p*(2*w)+e],m+=l=o[p*(2*w+1)+e]}return{x:m,y:h}}function M(t,e,n,r,o,i,a,s,u,c){for(var f=o[0],d=o[1],l=i[0],h=i[1],p=s[0],m=s[1],g=[],v=function(t){return function(t,e,n,r){var o=e[0],i=e[1],a=n[0],s=n[1],u=Math.round(((o+t.y+1)*s-1)/r);return{x:Math.round(((i+t.x+1)*a-1)/r),y:u}}(t,[f,d],[l,h],u)},w=0;w<r;w++){var y=x(t,w,v,a,e,c,[p,m]);g.push(y)}for(var b=-1,M=1/0,k=0;k<n.length;k++){var E=S(g,n[k]);E<M&&(b=k,M=E)}return b}function k(t,e){var n=t[0],r=t[1];return[Math.round((r-1)/e+1),Math.round((n-1)/e+1)]}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
function E(t,n,r,o,i,a,s,u,c,f,d){for(var l=s[0],h=s[1],m=t.shape,v=m[0],w=m[1],y=n.shape.slice(0,2),b=y[0],S=y[1],x=e.reshape(n,[b,S,2,p]),M=new Float32Array(d*p*3).fill(0),k=0;k<r.length;k++)for(var E=k*p*3,O=r[k],_=0;_<p;_++){var I=O.keypoints[_],P=E+3*_;M[P]=I.score,M[P+1]=I.position.y,M[P+2]=I.position.x}var A=g([o,i],[l,h],u),R=A[0],H=A[1],T=e.tensor(M,[d,p,3]),F=u.top,B=u.left,D={variableNames:["segmentation","longOffsets","poses"],outputShape:[v,w],userCode:"\n int convertToPositionInOutput(int pos, int pad, float scale, int stride) {\n return round(((float(pos + pad) + 1.0) * scale - 1.0) / float(stride));\n }\n\n float convertToPositionInOutputFloat(\n int pos, int pad, float scale, int stride) {\n return ((float(pos + pad) + 1.0) * scale - 1.0) / float(stride);\n }\n\n float dist(float x1, float y1, float x2, float y2) {\n return pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0);\n }\n\n float sampleLongOffsets(float h, float w, int d, int k) {\n float fh = fract(h);\n float fw = fract(w);\n int clH = int(ceil(h));\n int clW = int(ceil(w));\n int flH = int(floor(h));\n int flW = int(floor(w));\n float o11 = getLongOffsets(flH, flW, d, k);\n float o12 = getLongOffsets(flH, clW, d, k);\n float o21 = getLongOffsets(clH, flW, d, k);\n float o22 = getLongOffsets(clH, clW, d, k);\n float o1 = mix(o11, o12, fw);\n float o2 = mix(o21, o22, fw);\n return mix(o1, o2, fh);\n }\n\n int findNearestPose(int h, int w) {\n float prob = getSegmentation(h, w);\n if (prob < 1.0) {\n return -1;\n }\n\n // Done(Tyler): convert from output space h/w to strided space.\n float stridedH = convertToPositionInOutputFloat(\n h, ".concat(F,", ").concat(H,", ").concat(a,");\n float stridedW = convertToPositionInOutputFloat(\n w, ").concat(B,", ").concat(R,", ").concat(a,");\n\n float minDist = 1000000.0;\n int iMin = -1;\n for (int i = 0; i < ").concat(d,"; i++) {\n float curDistSum = 0.0;\n int numKpt = 0;\n for (int k = 0; k < ").concat(p,"; k++) {\n float dy = sampleLongOffsets(stridedH, stridedW, 0, k);\n float dx = sampleLongOffsets(stridedH, stridedW, 1, k);\n\n float y = float(h) + dy;\n float x = float(w) + dx;\n\n for (int s = 0; s < ").concat(c,"; s++) {\n int yRounded = round(min(y, float(").concat(o-1,")));\n int xRounded = round(min(x, float(").concat(i-1,")));\n\n float yStrided = convertToPositionInOutputFloat(\n yRounded, ").concat(F,", ").concat(H,", ").concat(a,");\n float xStrided = convertToPositionInOutputFloat(\n xRounded, ").concat(B,", ").concat(R,", ").concat(a,");\n\n float dy = sampleLongOffsets(yStrided, xStrided, 0, k);\n float dx = sampleLongOffsets(yStrided, xStrided, 1, k);\n\n y = y + dy;\n x = x + dx;\n }\n\n float poseScore = getPoses(i, k, 0);\n float poseY = getPoses(i, k, 1);\n float poseX = getPoses(i, k, 2);\n if (poseScore > ").concat(f,") {\n numKpt = numKpt + 1;\n curDistSum = curDistSum + dist(x, y, poseX, poseY);\n }\n }\n if (numKpt > 0 && curDistSum / float(numKpt) < minDist) {\n minDist = curDistSum / float(numKpt);\n iMin = i;\n }\n }\n return iMin;\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int nearestPose = findNearestPose(coords[0], coords[1]);\n setOutput(float(nearestPose));\n }\n ")};return e.backend().compileAndRun(D,[t,x,T])}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function O(){return"webgl"===n()}function _(t,n,r,o,i,u,c,f,d,l,h,p){var m=c[0],v=c[1];return void 0===d&&(d=.2),void 0===l&&(l=8),void 0===h&&(h=.3),void 0===p&&(p=10),a(this,void 0,void 0,(function(){var a,c,w,y,b;return s(this,(function(s){switch(s.label){case 0:return a=r.filter((function(t){return t.score>=d})),O()?(w=e.tidy((function(){var r=E(t,n,a,o,i,u,[m,v],f,l,h,p),s=e.engine().makeTensorFromDataId(r.dataId,r.shape,r.dtype);return a.map((function(t,n){return function(t,n){return e.tidy((function(){return e.cast(e.equal(t,e.scalar(n)),"int32")}))}(s,n)}))})),[4,Promise.all(w.map((function(t){return t.data()})))]):[3,2];case 1:return c=s.sent(),w.forEach((function(t){return t.dispose()})),[3,5];case 2:return[4,t.data()];case 3:return y=s.sent(),[4,n.data()];case 4:b=s.sent(),c=function(t,e,n,r,o,i,a,s,u,c){var f=a[0],d=a[1];void 0===c&&(c=5);for(var l=n.map((function(t){return new Uint8Array(r*o).fill(0)})),h=s.top,p=s.left,m=g([r,o],[f,d],s),v=m[0],w=m[1],y=k([f,d],i)[0],b=0;b<r;b+=1)for(var S=0;S<o;S+=1){var x=b*o+S;if(1===t[x]){var E=M({x:S,y:b},e,n,c,[h,p],[v,w],y,[r,o],i,u);E>=0&&(l[E][x]=1)}}return l}(y,b,a,o,i,u,[m,v],f,l),s.label=5;case 5:return[2,c.map((function(t,e){return{data:t,pose:a[e],width:i,height:o}}))]}}))}))}function I(t,n,r,o,i,u,c,f,d,l,h,p,m){var v=f[0],w=f[1];return void 0===l&&(l=.2),void 0===h&&(h=8),void 0===p&&(p=.3),void 0===m&&(m=10),a(this,void 0,void 0,(function(){var a,f,y,b,S,x;return s(this,(function(s){switch(s.label){case 0:return a=o.filter((function(t){return t.score>=l})),O()?(y=e.tidy((function(){var o=E(t,n,a,i,u,c,[v,w],d,h,p,m),s=e.engine().makeTensorFromDataId(o.dataId,o.shape,o.dtype);return a.map((function(t,n){return function(t,n,r){return e.tidy((function(){return e.sub(e.mul(e.cast(e.equal(t,e.scalar(r)),"int32"),e.add(n,1)),1)}))}(s,r,n)}))})),[4,Promise.all(y.map((function(t){return t.data()})))]):[3,2];case 1:return f=s.sent(),y.forEach((function(t){return t.dispose()})),[3,6];case 2:return[4,t.data()];case 3:return b=s.sent(),[4,n.data()];case 4:return S=s.sent(),[4,r.data()];case 5:x=s.sent(),f=function(t,e,n,r,o,i,a,s,u,c,f){var d=s[0],l=s[1];void 0===f&&(f=5);for(var h=r.map((function(t){return new Int32Array(o*i).fill(-1)})),p=u.top,m=u.left,v=g([o,i],[d,l],u),w=v[0],y=v[1],b=k([d,l],a)[0],S=0;S<o;S+=1)for(var x=0;x<i;x+=1){var E=S*i+x;if(1===t[E]){var O=M({x:x,y:S},e,r,f,[p,m],[w,y],b,[o,i],a,c);O>=0&&(h[O][E]=n[E])}}return h}(b,S,x,a,i,u,c,[v,w],d,h),s.label=6;case 6:return[2,f.map((function(t,e){return{pose:a[e],data:t,height:i,width:u}}))]}}))}))}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function P(t){return Math.floor(t/2)}[["leftHip","leftShoulder"],["leftElbow","leftShoulder"],["leftElbow","leftWrist"],["leftHip","leftKnee"],["leftKnee","leftAnkle"],["rightHip","rightShoulder"],["rightElbow","rightShoulder"],["rightElbow","rightWrist"],["rightHip","rightKnee"],["rightKnee","rightAnkle"],["leftShoulder","rightShoulder"],["leftHip","rightHip"]].map((function(t){var e=t[0],n=t[1];return[m[e],m[n]]}));var A=function(){function t(t,e){this.priorityQueue=new Array(t),this.numberOfElements=-1,this.getElementValue=e}return t.prototype.enqueue=function(t){this.priorityQueue[++this.numberOfElements]=t,this.swim(this.numberOfElements)},t.prototype.dequeue=function(){var t=this.priorityQueue[0];return this.exchange(0,this.numberOfElements--),this.sink(0),this.priorityQueue[this.numberOfElements+1]=null,t},t.prototype.empty=function(){return-1===this.numberOfElements},t.prototype.size=function(){return this.numberOfElements+1},t.prototype.all=function(){return this.priorityQueue.slice(0,this.numberOfElements+1)},t.prototype.max=function(){return this.priorityQueue[0]},t.prototype.swim=function(t){for(;t>0&&this.less(P(t),t);)this.exchange(t,P(t)),t=P(t)},t.prototype.sink=function(t){for(;2*t<=this.numberOfElements;){var e=2*t;if(e<this.numberOfElements&&this.less(e,e+1)&&e++,!this.less(t,e))break;this.exchange(t,e),t=e}},t.prototype.getValueAt=function(t){return this.getElementValue(this.priorityQueue[t])},t.prototype.less=function(t,e){return this.getValueAt(t)<this.getValueAt(e)},t.prototype.exchange=function(t,e){var n=this.priorityQueue[t];this.priorityQueue[t]=this.priorityQueue[e],this.priorityQueue[e]=n},t}();
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function R(t,e,n,r,o,i){for(var a=i.shape,s=a[0],u=a[1],c=!0,f=Math.max(n-o,0),d=Math.min(n+o+1,s),l=f;l<d;++l){for(var h=Math.max(r-o,0),p=Math.min(r+o+1,u),m=h;m<p;++m)if(i.get(l,m,t)>e){c=!1;break}if(!c)break}return c}
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
var H=[["nose","leftEye"],["leftEye","leftEar"],["nose","rightEye"],["rightEye","rightEar"],["nose","leftShoulder"],["leftShoulder","leftElbow"],["leftElbow","leftWrist"],["leftShoulder","leftHip"],["leftHip","leftKnee"],["leftKnee","leftAnkle"],["nose","rightShoulder"],["rightShoulder","rightElbow"],["rightElbow","rightWrist"],["rightShoulder","rightHip"],["rightHip","rightKnee"],["rightKnee","rightAnkle"]].map((function(t){var e=t[0],n=t[1];return[m[e],m[n]]})),T=H.map((function(t){return t[1]})),F=H.map((function(t){return t[0]}));function B(t,e,n,r){return{y:y(Math.round(t.y/e),0,n-1),x:y(Math.round(t.x/e),0,r-1)}}function D(t,e,n,r,o,i,a,s){void 0===s&&(s=2);for(var u=r.shape,c=u[0],f=u[1],d=function(t,e,n){var r=n.shape[2]/2;return{y:n.get(e.y,e.x,t),x:n.get(e.y,e.x,r+t)}}(t,B(e.position,i,c,f),a),l=b(e.position,d),p=0;p<s;p++){var m=B(l,i,c,f),g=v(m.y,m.x,n,o);l=b({x:m.x*i,y:m.y*i},{x:g.x,y:g.y})}var w=B(l,i,c,f),y=r.get(w.y,w.x,n);return{position:l,part:h[n],score:y}}function L(t,e,n,r,o,i){var a=e.shape[2],s=T.length,u=new Array(a),c=t.part,f=t.score,d=w(c,r,n);u[c.id]={score:f,part:h[c.id],position:d};for(var l=s-1;l>=0;--l){var p=T[l],m=F[l];u[p]&&!u[m]&&(u[m]=D(l,u[p],m,e,n,r,i))}for(l=0;l<s;++l){p=F[l],m=T[l];u[p]&&!u[m]&&(u[m]=D(l,u[p],m,e,n,r,o))}return u}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/function C(t,e,n,r){var o=n.x,i=n.y;return t.some((function(t){var n,a,s,u,c,f,d=t.keypoints[r].position;return n=i,a=o,s=d.y,u=d.x,(c=s-n)*c+(f=u-a)*f<=e}))}function q(t,e,n){var r=n.reduce((function(n,r,o){var i=r.position,a=r.score;return C(t,e,i,o)||(n+=a),n}),0);return r/n.length}var W=1;function K(t,e,n,r,o,i,a,s){void 0===a&&(a=.5),void 0===s&&(s=20);for(var u=[],c=function(t,e,n){for(var r=n.shape,o=r[0],i=r[1],a=r[2],s=new A(o*i*a,(function(t){return t.score})),u=0;u<o;++u)for(var c=0;c<i;++c)for(var f=0;f<a;++f){var d=n.get(u,c,f);d<t||R(f,d,u,c,e,n)&&s.enqueue({score:d,part:{heatmapY:u,heatmapX:c,id:f}})}return s}(a,W,t),f=s*s;u.length<i&&!c.empty();){var d=c.dequeue();if(!C(u,f,w(d.part,o,e),d.part.id)){var l=L(d,t,e,o,n,r),h=q(u,f,l);u.push({keypoints:l,score:h})}}return u}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/var z,j=[-123.15,-115.9,-103.06],N=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.preprocessInput=function(t){return e.add(t,j)},n.prototype.nameOutputResults=function(t){var e=t[0],n=t[1],r=t[2],o=t[3],i=t[4],a=t[5];return{offsets:i,segmentation:t[6],partHeatmaps:a,longOffsets:o,heatmap:r,displacementFwd:n,displacementBwd:e,partOffsets:t[7]}},n}(d),V="https://storage.googleapis.com/tfjs-models/savedmodel/bodypix/resnet50/",Q="https://storage.googleapis.com/tfjs-models/savedmodel/bodypix/mobilenet/";function U(t){if("undefined"!=typeof HTMLCanvasElement&&t instanceof HTMLCanvasElement||"undefined"!=typeof OffscreenCanvas&&t instanceof OffscreenCanvas||"undefined"!=typeof HTMLImageElement&&t instanceof HTMLImageElement)return function(t){if("offsetHeight"in t&&0!==t.offsetHeight&&"offsetWidth"in t&&0!==t.offsetWidth)return[t.offsetHeight,t.offsetWidth];if(null!=t.height&&null!=t.width)return[t.height,t.width];throw new Error("HTMLImageElement must have height and width attributes set.")}(t);if("undefined"!=typeof ImageData&&t instanceof ImageData)return[t.height,t.width];if("undefined"!=typeof HTMLVideoElement&&t instanceof HTMLVideoElement)return function(t){return t.hasAttribute("height")&&t.hasAttribute("width")?[t.height,t.width]:[t.videoHeight,t.videoWidth]}(t);if(t instanceof e.Tensor)return[t.shape[0],t.shape[1]];throw new Error("error: Unknown input type: ".concat(t,"."))}function X(t,e){return function(t,e){return(t-1)%e==0}(t,e)?t:Math.floor(t/e)*e+1}var Y={low:"low",medium:"medium",high:"high",full:"full"},G=((z={})[Y.low]=.25,z[Y.medium]=.5,z[Y.high]=.75,z[Y.full]=1,z),J=.1,Z=2;function $(t,n,r){var o=r[0],i=r[1],a=function(t){if("string"==typeof t){var n=G[t];return e.util.assert("number"==typeof n,(function(){return"string value of inputResolution must be one of ".concat(Object.values(Y).join(",")," but was ").concat(t,".")})),n}return e.util.assert("number"==typeof t&&t<=Z&&t>=J,(function(){return"inputResolution must be a string or number between ".concat(J," and ").concat(Z,", but ")+"was ".concat(t)})),t}(t);return[X(o*a,n),X(i*a,n)]}function tt(t,n,r){var o=n[0],i=n[1];void 0===r&&(r=!1);var a,s,u,c,f,d,l=t.shape,h=l[0],p=l[1]/h;if(p>i/o){a=i;var m=o-(s=Math.ceil(a/p));u=0,c=0,f=Math.floor(m/2),d=o-(s+f)}else{s=o;var g=i-(a=Math.ceil(o*p));u=Math.floor(g/2),c=i-(a+u),f=0,d=0}return{resizedAndPadded:e.tidy((function(){var n;return n=r?e.image.resizeBilinear(e.reverse(t,1),[s,a]):e.image.resizeBilinear(t,[s,a]),e.pad3d(n,[[f,d],[u,c],[0,0]])})),paddedBy:[[f,d],[u,c]]}}function et(t,n,r,o,i){var a=n[0],s=n[1],u=r[0],c=r[1],f=o[0],d=f[0],l=f[1],h=o[1],p=h[0],m=h[1];return void 0===i&&(i=!1),e.tidy((function(){var n=e.image.resizeBilinear(t,[u,c],!0);return i&&(n=e.sigmoid(n)),function(t,n,r){var o=n[0],i=n[1],a=r[0],s=a[0],u=a[1],c=r[1],f=c[0],d=c[1];return e.tidy((function(){var n=e.expandDims(t);return e.squeeze(e.image.cropAndResize(n,[[s/(o+s+u-1),f/(i+f+d-1),(s+o-1)/(o+s+u-1),(f+i-1)/(i+f+d-1)]],[0],[o,i]),[0])}))}(n,[a,s],[[d,l],[p,m]])}))}function nt(t,n){var r=n[0],o=n[1],i=U(t),a=i[0],s=i[1],u=o/r,c=[0,0,0,0],f=c[0],d=c[1],l=c[2],h=c[3];s/a<u?(f=0,d=0,l=Math.round(.5*(u*a-s)),h=Math.round(.5*(u*a-s))):(f=Math.round(.5*(1/u*s-a)),d=Math.round(.5*(1/u*s-a)),l=0,h=0);var p=e.tidy((function(){var n=function(t){return t instanceof e.Tensor?t:e.browser.fromPixels(t)}(t);return n=e.pad3d(n,[[f,d],[l,h],[0,0]]),e.image.resizeBilinear(n,[r,o])}));return{resized:p,padding:{top:f,left:l,right:h,bottom:d}}}function rt(t){return a(this,void 0,void 0,(function(){return s(this,(function(e){return[2,Promise.all(t.map((function(t){return t.buffer()})))]}))}))}function ot(t,e){return{score:t.score,keypoints:t.keypoints.map((function(t){var n=t.score,r=t.part,o=t.position;return{score:n,part:r,position:{x:e-1-o.x,y:o.y}}}))}}function it(t,e,n,r,o){var i=e[0],a=e[1],s=n[0],u=n[1],c=function(t,e,n,r,o){return void 0===r&&(r=0),void 0===o&&(o=0),1===n&&1===e&&0===r&&0===o?t:t.map((function(t){return function(t,e,n,r,o){return void 0===r&&(r=0),void 0===o&&(o=0),{score:t.score,keypoints:t.keypoints.map((function(t){var i=t.score,a=t.part,s=t.position;return{score:i,part:a,position:{x:s.x*n+o,y:s.y*e+r}}}))}}(t,e,n,r,o)}))}(t,(i+r.top+r.bottom)/s,(a+r.left+r.right)/u,-r.top,-r.left);return o?function(t,e){return e<=0?t:t.map((function(t){return ot(t,e)}))}(c,a):c}
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/var at=!0,st=!1,ut={architecture:"MobileNetV1",outputStride:16,quantBytes:4,multiplier:.75},ct=["MobileNetV1","ResNet50"],ft={MobileNetV1:[8,16,32],ResNet50:[32,16]},dt={MobileNetV1:[.5,.75,1],ResNet50:[1]},lt=[1,2,4];var ht={flipHorizontal:!1,internalResolution:"medium",segmentationThreshold:.7,maxDetections:10,scoreThreshold:.4,nmsRadius:20},pt={flipHorizontal:!1,internalResolution:"medium",segmentationThreshold:.7,maxDetections:10,scoreThreshold:.4,nmsRadius:20,minKeypointScore:.3,refineSteps:10};function mt(t){var e=t.segmentationThreshold,n=t.maxDetections,r=t.scoreThreshold,o=t.nmsRadius;if(e<0||e>1)throw new Error("segmentationThreshold ".concat(e,". ")+"Should be in range [0.0, 1.0]");if(n<=0)throw new Error("Invalid maxDetections ".concat(n,". ")+"Should be > 0");if(r<0||r>1)throw new Error("Invalid scoreThreshold ".concat(r,". ")+"Should be in range [0.0, 1.0]");if(o<=0)throw new Error("Invalid nmsRadius ".concat(o,"."))}function gt(t){var e=t.segmentationThreshold,n=t.maxDetections,r=t.scoreThreshold,o=t.nmsRadius,i=t.minKeypointScore,a=t.refineSteps;if(e<0||e>1)throw new Error("segmentationThreshold ".concat(e,". ")+"Should be in range [0.0, 1.0]");if(n<=0)throw new Error("Invalid maxDetections ".concat(n,". ")+"Should be > 0");if(r<0||r>1)throw new Error("Invalid scoreThreshold ".concat(r,". ")+"Should be in range [0.0, 1.0]");if(o<=0)throw new Error("Invalid nmsRadius ".concat(o,"."));if(i<0||i>1)throw new Error("Invalid minKeypointScore ".concat(i,".")+"Should be in range [0.0, 1.0]");if(a<=0||a>20)throw new Error("Invalid refineSteps ".concat(a,".")+"Should be in range [1, 20]")}var vt=function(){function t(t){this.baseModel=t}return t.prototype.predictForPersonSegmentation=function(t){var e=this.baseModel.predict(t);return{segmentLogits:e.segmentation,heatmapScores:e.heatmapScores,offsets:e.offsets,displacementFwd:e.displacementFwd,displacementBwd:e.displacementBwd}},t.prototype.predictForPersonSegmentationAndPart=function(t){var e=this.baseModel.predict(t);return{segmentLogits:e.segmentation,partHeatmapLogits:e.partHeatmaps,heatmapScores:e.heatmapScores,offsets:e.offsets,displacementFwd:e.displacementFwd,displacementBwd:e.displacementBwd}},t.prototype.predictForMultiPersonInstanceSegmentationAndPart=function(t){var e=this.baseModel.predict(t);return{segmentLogits:e.segmentation,longOffsets:e.longOffsets,heatmapScores:e.heatmapScores,offsets:e.offsets,displacementFwd:e.displacementFwd,displacementBwd:e.displacementBwd,partHeatmaps:e.partHeatmaps}},t.prototype.segmentPersonActivation=function(t,n,r){var o=this;void 0===r&&(r=.5);var i=U(t),a=i[0],s=i[1],u=$(n,this.baseModel.outputStride,[a,s]),f=nt(t,u),d=f.resized,l=f.padding,h=e.tidy((function(){var t=o.predictForPersonSegmentation(d),n=t.segmentLogits,i=t.heatmapScores,u=t.offsets,f=t.displacementFwd,h=t.displacementBwd,p=d.shape,m=p[0],g=p[1],v=et(n,[a,s],[m,g],[[l.top,l.bottom],[l.left,l.right]],at);return{segmentation:c(e.squeeze(v),r),heatmapScores:i,offsets:u,displacementFwd:f,displacementBwd:h}})),p=h.segmentation,m=h.heatmapScores,g=h.offsets,v=h.displacementFwd,w=h.displacementBwd;return d.dispose(),{segmentation:p,heatmapScores:m,offsets:g,displacementFwd:v,displacementBwd:w,padding:l,internalResolutionHeightAndWidth:u}},t.prototype.segmentPerson=function(t,e){return void 0===e&&(e=ht),a(this,void 0,void 0,(function(){var n,r,o,a,u,c,f,d,l,h,p,m,g,v,w,y,b,S;return s(this,(function(s){switch(s.label){case 0:return mt(e=i(i({},ht),e)),n=this.segmentPersonActivation(t,e.internalResolution,e.segmentationThreshold),r=n.segmentation,o=n.heatmapScores,a=n.offsets,u=n.displacementFwd,c=n.displacementBwd,f=n.padding,d=n.internalResolutionHeightAndWidth,l=r.shape,h=l[0],p=l[1],[4,r.data()];case 1:return m=s.sent(),r.dispose(),[4,rt([o,a,u,c])];case 2:return g=s.sent(),v=g[0],w=g[1],y=g[2],b=g[3],S=it(S=K(v,w,y,b,this.baseModel.outputStride,e.maxDetections,e.scoreThreshold,e.nmsRadius),[h,p],d,f,st),o.dispose(),a.dispose(),u.dispose(),c.dispose(),[2,{height:h,width:p,data:m,allPoses:S}]}}))}))},t.prototype.segmentMultiPerson=function(t,n){return void 0===n&&(n=pt),a(this,void 0,void 0,(function(){var r,o,a,u,f,d,l,h,p,m,g,v,w,y,b,S,x,M,k,E,O,I=this;return s(this,(function(s){switch(s.label){case 0:return gt(n=i(i({},pt),n)),r=U(t),o=r[0],a=r[1],u=$(n.internalResolution,this.baseModel.outputStride,[o,a]),f=nt(t,u),d=f.resized,l=f.padding,h=e.tidy((function(){var t,r=I.predictForMultiPersonInstanceSegmentationAndPart(d),i=r.segmentLogits,s=r.longOffsets,f=r.heatmapScores,h=r.offsets,p=r.displacementFwd,m=r.displacementBwd,g=et(i,[o,a],u,[[l.top,l.bottom],[l.left,l.right]],at);return t=s,{segmentation:c(e.squeeze(g),n.segmentationThreshold),longOffsets:t,heatmapScoresRaw:f,offsetsRaw:h,displacementFwdRaw:p,displacementBwdRaw:m}})),p=h.segmentation,m=h.longOffsets,g=h.heatmapScoresRaw,v=h.offsetsRaw,w=h.displacementFwdRaw,y=h.displacementBwdRaw,[4,rt([g,v,w,y])];case 1:return b=s.sent(),S=b[0],x=b[1],M=b[2],k=b[3],E=it(E=K(S,x,M,k,this.baseModel.outputStride,n.maxDetections,n.scoreThreshold,n.nmsRadius),[o,a],u,l,st),[4,_(p,m,E,o,a,this.baseModel.outputStride,u,l,n.scoreThreshold,n.refineSteps,n.minKeypointScore,n.maxDetections)];case 2:return O=s.sent(),d.dispose(),p.dispose(),m.dispose(),g.dispose(),v.dispose(),w.dispose(),y.dispose(),[2,O]}}))}))},t.prototype.segmentPersonPartsActivation=function(t,n,r){var o=this;void 0===r&&(r=.5);var i=U(t),a=i[0],s=i[1],u=$(n,this.baseModel.outputStride,[a,s]),d=nt(t,u),l=d.resized,h=d.padding,p=e.tidy((function(){var t=o.predictForPersonSegmentationAndPart(l),n=t.segmentLogits,i=t.partHeatmapLogits,u=t.heatmapScores,d=t.offsets,p=t.displacementFwd,m=t.displacementBwd,g=l.shape,v=g[0],w=g[1],y=et(n,[a,s],[v,w],[[h.top,h.bottom],[h.left,h.right]],at),b=et(i,[a,s],[v,w],[[h.top,h.bottom],[h.left,h.right]],at);return{partSegmentation:f(c(e.squeeze(y),r),b),heatmapScores:u,offsets:d,displacementFwd:p,displacementBwd:m}})),m=p.partSegmentation,g=p.heatmapScores,v=p.offsets,w=p.displacementFwd,y=p.displacementBwd;return l.dispose(),{partSegmentation:m,heatmapScores:g,offsets:v,displacementFwd:w,displacementBwd:y,padding:h,internalResolutionHeightAndWidth:u}},t.prototype.segmentPersonParts=function(t,e){return void 0===e&&(e=ht),a(this,void 0,void 0,(function(){var n,r,o,a,u,c,f,d,l,h,p,m,g,v,w,y,b,S;return s(this,(function(s){switch(s.label){case 0:return mt(e=i(i({},ht),e)),n=this.segmentPersonPartsActivation(t,e.internalResolution,e.segmentationThreshold),r=n.partSegmentation,o=n.heatmapScores,a=n.offsets,u=n.displacementFwd,c=n.displacementBwd,f=n.padding,d=n.internalResolutionHeightAndWidth,l=r.shape,h=l[0],p=l[1],[4,r.data()];case 1:return m=s.sent(),r.dispose(),[4,rt([o,a,u,c])];case 2:return g=s.sent(),v=g[0],w=g[1],y=g[2],b=g[3],S=it(S=K(v,w,y,b,this.baseModel.outputStride,e.maxDetections,e.scoreThreshold,e.nmsRadius),[h,p],d,f,st),o.dispose(),a.dispose(),u.dispose(),c.dispose(),[2,{height:h,width:p,data:m,allPoses:S}]}}))}))},t.prototype.segmentMultiPersonParts=function(t,n){return void 0===n&&(n=pt),a(this,void 0,void 0,(function(){var r,o,a,f,d,l,h,p,m,g,v,w,y,b,S,x,M,k,E,O,_,P,A=this;return s(this,(function(s){switch(s.label){case 0:return gt(n=i(i({},pt),n)),r=U(t),o=r[0],a=r[1],f=$(n.internalResolution,this.baseModel.outputStride,[o,a]),d=nt(t,f),l=d.resized,h=d.padding,p=e.tidy((function(){var t=A.predictForMultiPersonInstanceSegmentationAndPart(l),r=t.segmentLogits,i=t.longOffsets,s=t.heatmapScores,d=t.offsets,p=t.displacementFwd,m=t.displacementBwd,g=t.partHeatmaps,v=et(r,[o,a],f,[[h.top,h.bottom],[h.left,h.right]],at),w=et(g,[o,a],f,[[h.top,h.bottom],[h.left,h.right]],at),y=i,b=c(e.squeeze(v),n.segmentationThreshold),S=function(t){var n=t.shape,r=n[0],o=n[1],i=n[2];return e.tidy((function(){var n=u(t),a=e.expandDims(e.range(0,i,1,"int32"),1),s=e.cast(e.matMul(n,a),"int32");return e.reshape(s,[r,o])}))}(w);return{segmentation:b,longOffsets:y,heatmapScoresRaw:s,offsetsRaw:d,displacementFwdRaw:p,displacementBwdRaw:m,partSegmentation:S}})),m=p.segmentation,g=p.longOffsets,v=p.heatmapScoresRaw,w=p.offsetsRaw,y=p.displacementFwdRaw,b=p.displacementBwdRaw,S=p.partSegmentation,[4,rt([v,w,y,b])];case 1:return x=s.sent(),M=x[0],k=x[1],E=x[2],O=x[3],_=it(_=K(M,k,E,O,this.baseModel.outputStride,n.maxDetections,n.scoreThreshold,n.nmsRadius),[o,a],f,h,st),[4,I(m,g,S,_,o,a,this.baseModel.outputStride,f,h,n.scoreThreshold,n.refineSteps,n.minKeypointScore,n.maxDetections)];case 2:return P=s.sent(),l.dispose(),m.dispose(),g.dispose(),v.dispose(),w.dispose(),y.dispose(),b.dispose(),S.dispose(),[2,P]}}))}))},t.prototype.dispose=function(){this.baseModel.dispose()},t}();function wt(n){return a(this,void 0,void 0,(function(){var r,o,i,a,u,c;return s(this,(function(s){switch(s.label){case 0:if(r=n.outputStride,o=n.quantBytes,i=n.multiplier,null==e)throw new Error("Cannot find TensorFlow.js. If you are using a <script> tag, please also include @tensorflow/tfjs on the page before using this\n model.");return a=function(t,e,n){var r={1:"100",.75:"075",.5:"050"},o="model-stride".concat(t,".json");return 4===n?Q+"float/".concat(r[e],"/")+o:Q+"quant".concat(n,"/").concat(r[e],"/")+o}
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/(r,i,o),[4,t.loadGraphModel(n.modelUrl||a)];case 1:return u=s.sent(),c=new l(u,r),[2,new vt(c)]}}))}))}function yt(n){return a(this,void 0,void 0,(function(){var r,o,i,a,u;return s(this,(function(s){switch(s.label){case 0:if(r=n.outputStride,o=n.quantBytes,null==e)throw new Error("Cannot find TensorFlow.js. If you are using a <script> tag, please also include @tensorflow/tfjs on the page before using this\n model.");return i=function(t,e){var n="model-stride".concat(t,".json");return 4===e?V+"float/"+n:V+"quant".concat(e,"/")+n}(r,o),[4,t.loadGraphModel(n.modelUrl||i)];case 1:return a=s.sent(),u=new N(a,r),[2,new vt(u)]}}))}))}function bt(t){return void 0===t&&(t=ut),a(this,void 0,void 0,(function(){return s(this,(function(e){return"ResNet50"===(t=function(t){if(null==(t=t||ut).architecture&&(t.architecture="MobileNetV1"),ct.indexOf(t.architecture)<0)throw new Error("Invalid architecture ".concat(t.architecture,". ")+"Should be one of ".concat(ct));if(null==t.outputStride&&(t.outputStride=16),ft[t.architecture].indexOf(t.outputStride)<0)throw new Error("Invalid outputStride ".concat(t.outputStride,". ")+"Should be one of ".concat(ft[t.architecture]," ")+"for architecture ".concat(t.architecture,"."));if(null==t.multiplier&&(t.multiplier=1),dt[t.architecture].indexOf(t.multiplier)<0)throw new Error("Invalid multiplier ".concat(t.multiplier,". ")+"Should be one of ".concat(dt[t.architecture]," ")+"for architecture ".concat(t.architecture,"."));if(null==t.quantBytes&&(t.quantBytes=4),lt.indexOf(t.quantBytes)<0)throw new Error("Invalid quantBytes ".concat(t.quantBytes,". ")+"Should be one of ".concat(lt," ")+"for architecture ".concat(t.architecture,"."));return t}(t)).architecture?[2,yt(t)]:"MobileNetV1"===t.architecture?[2,wt(t)]:[2,null]}))}))}
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
var St={};function xt(t,e,n,r){var o=t.width,i=t.height,a=e.width,s=e.height;if(o!==a||i!==s)throw new Error("error: dimensions must match. ".concat(n," has dimensions ").concat(o,"x").concat(i,", ").concat(r," has dimensions ").concat(a,"x").concat(s))}function Mt(t){var e=t.getContext("2d");e.scale(-1,1),e.translate(-t.width,0)}function kt(t,e,n){t.globalCompositeOperation=n,t.drawImage(e,0,0)}function Et(t){return St[t]||(St[t]=function(){if("undefined"!=typeof document)return document.createElement("canvas");if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(0,0);throw new Error("Cannot create a canvas in this context")}()),St[t]}function Ot(t,e,n){var r=t.height,o=t.width,i=n.getContext("2d");n.width=o,n.height=r,i.clearRect(0,0,o,r),i.save(),/^((?!chrome|android).)*safari/i.test(navigator.userAgent)?function(t,e,n){for(var r=t.getContext("2d"),o=0,i=1/(2*Math.PI*5*5),a=n<3?1:2,s=-n;s<=n;s+=a)for(var u=-n;u<=n;u+=a)o+=i*Math.exp(-(u*u+s*s)/50);for(s=-n;s<=n;s+=a)for(u=-n;u<=n;u+=a)r.globalAlpha=i*Math.exp(-(u*u+s*s)/50)/o*n,r.drawImage(e,u,s);r.globalAlpha=1}(n,t,e):(i.filter="blur(".concat(e,"px)"),i.drawImage(t,0,0,o,r)),i.restore()}function _t(t,e,n){var r=Et(n);return 0===e?function(t,e){var n=t.width,r=t.height;e.width=n,e.height=r;var o=e.getContext("2d");o.drawImage(t,0,0,n,r)}(t,r):Ot(t,e,r),r}function It(t,e){var n=Et(e);return function(t,e){e.width=t.width,e.height=t.height,e.getContext("2d").putImageData(t,0,0)}(t,n),n}function Pt(t,e,n,r,o){if(void 0===e&&(e={r:0,g:0,b:0,a:0}),void 0===n&&(n={r:0,g:0,b:0,a:255}),void 0===r&&(r=!1),void 0===o&&(o=[1]),Array.isArray(t)&&0===t.length)return null;var i,a=(i=Array.isArray(t)?t:[t])[0],s=a.width,u=a.height,c=new Uint8ClampedArray(s*u*4);function f(t,e,n,r,o,i){void 0===i&&(i={r:0,g:255,b:255,a:255});for(var a=-o;a<=o;a++)for(var s=-o;s<=o;s++)if(0!==a&&0!==s){var u=(e+a)*r+(n+s);t[4*u+0]=i.r,t[4*u+1]=i.g,t[4*u+2]=i.b,t[4*u+3]=i.a}}function d(t,e,n,r,o,i){void 0===o&&(o=[1]),void 0===i&&(i=1);for(var a=0,s=-i;s<=i;s++)for(var u=function(i){if(0!==s&&0!==i){var u=(e+s)*r+(n+i);o.some((function(e){return e===t[u]}))||(a+=1)}},c=-i;c<=i;c++)u(c);return a>0}for(var l=0;l<u;l+=1)for(var h=function(t){var a=l*s+t;c[4*a+0]=n.r,c[4*a+1]=n.g,c[4*a+2]=n.b,c[4*a+3]=n.a;for(var h=function(n){if(o.some((function(t){return t===i[n].data[a]}))){c[4*a]=e.r,c[4*a+1]=e.g,c[4*a+2]=e.b,c[4*a+3]=e.a;var h=d(i[n].data,l,t,s,o);r&&l-1>=0&&l+1<u&&t-1>=0&&t+1<s&&h&&f(c,l,t,s,1)}},p=0;p<i.length;p++)h(p)},p=0;p<s;p+=1)h(p);return new ImageData(c,s,u)}var At=[[110,64,170],[143,61,178],[178,60,178],[210,62,167],[238,67,149],[255,78,125],[255,94,99],[255,115,75],[255,140,56],[239,167,47],[217,194,49],[194,219,64],[175,240,91],[135,245,87],[96,247,96],[64,243,115],[40,234,141],[28,219,169],[26,199,194],[33,176,213],[47,150,224],[65,125,224],[84,101,214],[99,81,195]];function Rt(t,e){if(void 0===e&&(e=At),Array.isArray(t)&&0===t.length)return null;for(var n,r=(n=Array.isArray(t)?t:[t])[0],o=r.width,i=r.height,a=new Uint8ClampedArray(o*i*4),s=0;s<i*o;++s){var u=4*s;a[u+0]=255,a[u+1]=255,a[u+2]=255,a[u+3]=255;for(var c=0;c<n.length;c++){var f=n[c].data[s];if(-1!==f){var d=e[f];if(!d)throw new Error("No color could be found for part id ".concat(f));a[u+0]=d[0],a[u+1]=d[1],a[u+2]=d[2],a[u+3]=255}}}return new ImageData(a,o,i)}var Ht={blurred:"blurred",blurredMask:"blurred-mask",mask:"mask",lowresPartMask:"lowres-part-mask"};function Tt(t,e,n,r,o,i){void 0===r&&(r=.7),void 0===o&&(o=0),void 0===i&&(i=!1);var a=U(e),s=a[0],u=a[1];t.width=u,t.height=s;var c=t.getContext("2d");if(c.save(),i&&Mt(t),c.drawImage(e,0,0),c.globalAlpha=r,n){xt({width:u,height:s},n,"image","mask");var f=_t(It(n,Ht.mask),o,Ht.blurredMask);c.drawImage(f,0,0,u,s)}c.restore()}function Ft(t,e,n,r,o,i,a){void 0===r&&(r=.7),void 0===o&&(o=0),void 0===i&&(i=!1),void 0===a&&(a=10);var s=U(e),u=s[0];xt({width:s[1],height:u},n,"image","mask");var c=_t(It(n,Ht.mask),o,Ht.blurredMask);t.width=c.width,t.height=c.height;var f=t.getContext("2d");f.save(),i&&Mt(t);var d=Et(Ht.lowresPartMask),l=d.getContext("2d");d.width=c.width*(1/a),d.height=c.height*(1/a),l.drawImage(c,0,0,c.width,c.height,0,0,d.width,d.height),f.imageSmoothingEnabled=!1,f.drawImage(d,0,0,d.width,d.height,0,0,t.width,t.height);for(var h=0;h<d.width;h++)f.beginPath(),f.strokeStyle="#ffffff",f.moveTo(a*h,0),f.lineTo(a*h,t.height),f.stroke();for(h=0;h<d.height;h++)f.beginPath(),f.strokeStyle="#ffffff",f.moveTo(0,a*h),f.lineTo(t.width,a*h),f.stroke();f.globalAlpha=1-r,f.drawImage(e,0,0,c.width,c.height),f.restore()}function Bt(t,e,n,r,o,i){void 0===r&&(r=3),void 0===o&&(o=3),void 0===i&&(i=!1);var a=_t(e,r,Ht.blurred);t.width=a.width,t.height=a.height;var s=t.getContext("2d");if(Array.isArray(n)&&0===n.length)s.drawImage(a,0,0);else{var u=function(t,e){var n=It(Pt(t,{r:0,g:0,b:0,a:255},{r:0,g:0,b:0,a:0}),Ht.mask);return 0===e?n:_t(n,e,Ht.blurredMask)}(n,o);s.save(),i&&Mt(t);var c=U(e),f=c[0],d=c[1];s.drawImage(e,0,0,d,f),kt(s,u,"destination-in"),kt(s,a,"destination-over"),s.restore()}}function Dt(t,e,n,r,o,i,a){void 0===r&&(r=[0,1]),void 0===o&&(o=3),void 0===i&&(i=3),void 0===a&&(a=!1);var s=_t(e,o,Ht.blurred);t.width=s.width,t.height=s.height;var u=t.getContext("2d");if(Array.isArray(n)&&0===n.length)u.drawImage(s,0,0);else{var c=function(t,e,n){var r=It(Pt(t,{r:0,g:0,b:0,a:0},{r:0,g:0,b:0,a:255},!0,e),Ht.mask);return 0===n?r:_t(r,n,Ht.blurredMask)}(n,r,i);u.save(),a&&Mt(t);var f=U(e),d=f[0],l=f[1];u.drawImage(e,0,0,l,d),kt(u,c,"destination-in"),kt(u,s,"destination-over"),u.restore()}}
/**
* @license
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/var Lt=["left_face","right_face","left_upper_arm_front","left_upper_arm_back","right_upper_arm_front","right_upper_arm_back","left_lower_arm_front","left_lower_arm_back","right_lower_arm_front","right_lower_arm_back","left_