UNPKG

handsfree

Version:

Quickly integrate face, hand, and/or pose tracking to your frontend projects in a snap ✨👌

46 lines (44 loc) 18.5 kB
(function(){/* Copyright The Closure Library Authors. SPDX-License-Identifier: Apache-2.0 */ 'use strict';function u(a){var b=0;return function(){return b<a.length?{done:!1,value:a[b++]}:{done:!0}}}function x(a){var b="undefined"!=typeof Symbol&&Symbol.iterator&&a[Symbol.iterator];return b?b.call(a):{next:u(a)}}function aa(a){if(!(a instanceof Array)){a=x(a);for(var b,d=[];!(b=a.next()).done;)d.push(b.value);a=d}return a}var y="function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,d){if(a==Array.prototype||a==Object.prototype)return a;a[b]=d.value;return a}; function ba(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var b=0;b<a.length;++b){var d=a[b];if(d&&d.Math==Math)return d}throw Error("Cannot find global object");}var z=ba(this);function B(a,b){if(b)a:{var d=z;a=a.split(".");for(var c=0;c<a.length-1;c++){var f=a[c];if(!(f in d))break a;d=d[f]}a=a[a.length-1];c=d[a];b=b(c);b!=c&&null!=b&&y(d,a,{configurable:!0,writable:!0,value:b})}}var C; if("function"==typeof Object.setPrototypeOf)C=Object.setPrototypeOf;else{var G;a:{var ca={a:!0},H={};try{H.__proto__=ca;G=H.a;break a}catch(a){}G=!1}C=G?function(a,b){a.__proto__=b;if(a.__proto__!==b)throw new TypeError(a+" is not extensible");return a}:null}var I=C;function J(){this.l=!1;this.h=null;this.j=void 0;this.g=1;this.s=this.m=0;this.i=null}function M(a){if(a.l)throw new TypeError("Generator is already running");a.l=!0}J.prototype.o=function(a){this.j=a}; function N(a,b){a.i={K:b,L:!0};a.g=a.m||a.s}J.prototype.return=function(a){this.i={return:a};this.g=this.s};function O(a,b,d){a.g=d;return{value:b}}function da(a){this.g=new J;this.h=a}function ja(a,b){M(a.g);var d=a.g.h;if(d)return P(a,"return"in d?d["return"]:function(c){return{value:c,done:!0}},b,a.g.return);a.g.return(b);return Q(a)} function P(a,b,d,c){try{var f=b.call(a.g.h,d);if(!(f instanceof Object))throw new TypeError("Iterator result "+f+" is not an object");if(!f.done)return a.g.l=!1,f;var k=f.value}catch(e){return a.g.h=null,N(a.g,e),Q(a)}a.g.h=null;c.call(a.g,k);return Q(a)}function Q(a){for(;a.g.g;)try{var b=a.h(a.g);if(b)return a.g.l=!1,{value:b.value,done:!1}}catch(d){a.g.j=void 0,N(a.g,d)}a.g.l=!1;if(a.g.i){b=a.g.i;a.g.i=null;if(b.L)throw b.K;return{value:b.return,done:!0}}return{value:void 0,done:!0}} function ka(a){this.next=function(b){M(a.g);a.g.h?b=P(a,a.g.h.next,b,a.g.o):(a.g.o(b),b=Q(a));return b};this.throw=function(b){M(a.g);a.g.h?b=P(a,a.g.h["throw"],b,a.g.o):(N(a.g,b),b=Q(a));return b};this.return=function(b){return ja(a,b)};this[Symbol.iterator]=function(){return this}}function R(a,b){b=new ka(new da(b));I&&a.prototype&&I(b,a.prototype);return b} B("Symbol",function(a){function b(f){if(this instanceof b)throw new TypeError("Symbol is not a constructor");return new d("jscomp_symbol_"+(f||"")+"_"+c++,f)}function d(f,k){this.g=f;y(this,"description",{configurable:!0,writable:!0,value:k})}if(a)return a;d.prototype.toString=function(){return this.g};var c=0;return b}); B("Symbol.iterator",function(a){if(a)return a;a=Symbol("Symbol.iterator");for(var b="Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "),d=0;d<b.length;d++){var c=z[b[d]];"function"===typeof c&&"function"!=typeof c.prototype[a]&&y(c.prototype,a,{configurable:!0,writable:!0,value:function(){return la(u(this))}})}return a});function la(a){a={next:a};a[Symbol.iterator]=function(){return this};return a} var ma="function"==typeof Object.assign?Object.assign:function(a,b){for(var d=1;d<arguments.length;d++){var c=arguments[d];if(c)for(var f in c)Object.prototype.hasOwnProperty.call(c,f)&&(a[f]=c[f])}return a};B("Object.assign",function(a){return a||ma}); B("Promise",function(a){function b(e){this.h=0;this.i=void 0;this.g=[];this.o=!1;var g=this.j();try{e(g.resolve,g.reject)}catch(h){g.reject(h)}}function d(){this.g=null}function c(e){return e instanceof b?e:new b(function(g){g(e)})}if(a)return a;d.prototype.h=function(e){if(null==this.g){this.g=[];var g=this;this.i(function(){g.l()})}this.g.push(e)};var f=z.setTimeout;d.prototype.i=function(e){f(e,0)};d.prototype.l=function(){for(;this.g&&this.g.length;){var e=this.g;this.g=[];for(var g=0;g<e.length;++g){var h= e[g];e[g]=null;try{h()}catch(l){this.j(l)}}}this.g=null};d.prototype.j=function(e){this.i(function(){throw e;})};b.prototype.j=function(){function e(l){return function(m){h||(h=!0,l.call(g,m))}}var g=this,h=!1;return{resolve:e(this.G),reject:e(this.l)}};b.prototype.G=function(e){if(e===this)this.l(new TypeError("A Promise cannot resolve to itself"));else if(e instanceof b)this.I(e);else{a:switch(typeof e){case "object":var g=null!=e;break a;case "function":g=!0;break a;default:g=!1}g?this.A(e):this.m(e)}}; b.prototype.A=function(e){var g=void 0;try{g=e.then}catch(h){this.l(h);return}"function"==typeof g?this.J(g,e):this.m(e)};b.prototype.l=function(e){this.s(2,e)};b.prototype.m=function(e){this.s(1,e)};b.prototype.s=function(e,g){if(0!=this.h)throw Error("Cannot settle("+e+", "+g+"): Promise already settled in state"+this.h);this.h=e;this.i=g;2===this.h&&this.H();this.D()};b.prototype.H=function(){var e=this;f(function(){if(e.F()){var g=z.console;"undefined"!==typeof g&&g.error(e.i)}},1)};b.prototype.F= function(){if(this.o)return!1;var e=z.CustomEvent,g=z.Event,h=z.dispatchEvent;if("undefined"===typeof h)return!0;"function"===typeof e?e=new e("unhandledrejection",{cancelable:!0}):"function"===typeof g?e=new g("unhandledrejection",{cancelable:!0}):(e=z.document.createEvent("CustomEvent"),e.initCustomEvent("unhandledrejection",!1,!0,e));e.promise=this;e.reason=this.i;return h(e)};b.prototype.D=function(){if(null!=this.g){for(var e=0;e<this.g.length;++e)k.h(this.g[e]);this.g=null}};var k=new d;b.prototype.I= function(e){var g=this.j();e.B(g.resolve,g.reject)};b.prototype.J=function(e,g){var h=this.j();try{e.call(g,h.resolve,h.reject)}catch(l){h.reject(l)}};b.prototype.then=function(e,g){function h(q,n){return"function"==typeof q?function(r){try{l(q(r))}catch(p){m(p)}}:n}var l,m,t=new b(function(q,n){l=q;m=n});this.B(h(e,l),h(g,m));return t};b.prototype.catch=function(e){return this.then(void 0,e)};b.prototype.B=function(e,g){function h(){switch(l.h){case 1:e(l.i);break;case 2:g(l.i);break;default:throw Error("Unexpected state: "+ l.h);}}var l=this;null==this.g?k.h(h):this.g.push(h);this.o=!0};b.resolve=c;b.reject=function(e){return new b(function(g,h){h(e)})};b.race=function(e){return new b(function(g,h){for(var l=x(e),m=l.next();!m.done;m=l.next())c(m.value).B(g,h)})};b.all=function(e){var g=x(e),h=g.next();return h.done?c([]):new b(function(l,m){function t(r){return function(p){q[r]=p;n--;0==n&&l(q)}}var q=[],n=0;do q.push(void 0),n++,c(h.value).B(t(q.length-1),m),h=g.next();while(!h.done)})};return b}); function na(a,b){a instanceof String&&(a+="");var d=0,c=!1,f={next:function(){if(!c&&d<a.length){var k=d++;return{value:b(k,a[k]),done:!1}}c=!0;return{done:!0,value:void 0}}};f[Symbol.iterator]=function(){return f};return f}B("Array.prototype.keys",function(a){return a?a:function(){return na(this,function(b){return b})}});var oa=this||self; function S(a,b){a=a.split(".");var d=oa;a[0]in d||"undefined"==typeof d.execScript||d.execScript("var "+a[0]);for(var c;a.length&&(c=a.shift());)a.length||void 0===b?d[c]&&d[c]!==Object.prototype[c]?d=d[c]:d=d[c]={}:d[c]=b};function W(a,b){var d=void 0;return new (d||(d=Promise))(function(c,f){function k(h){try{g(b.next(h))}catch(l){f(l)}}function e(h){try{g(b["throw"](h))}catch(l){f(l)}}function g(h){h.done?c(h.value):(new d(function(l){l(h.value)})).then(k,e)}g((b=b.apply(a,void 0)).next())})};function X(a,b,d){d=a.createShader(0===d?a.VERTEX_SHADER:a.FRAGMENT_SHADER);a.shaderSource(d,b);a.compileShader(d);if(!a.getShaderParameter(d,a.COMPILE_STATUS))throw Error("Could not compile WebGL shader.\n\n"+a.getShaderInfoLog(d));return d};function pa(a,b){this.h=a;this.j=b;this.i=0};var qa=new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,9,1,7,0,65,0,253,15,26,11]);function ra(a,b){return b+a}function sa(a,b){window[a]=b}function ta(a){var b=document.createElement("script");b.setAttribute("src",a);b.setAttribute("crossorigin","anonymous");document.body.appendChild(b);return new Promise(function(d){b.addEventListener("load",function(){d()},!1)})} function ua(a){for(var b=[],d=a.size(),c=0;c<d;++c){var f=a.get(c);b.push({x:f.x,y:f.y,visibility:f.hasVisibility?f.visibility:void 0})}return b}function va(a,b,d){this.graph=a;this.locateFile=b;this.C=d}va.prototype.toArrayBuffer=function(){return W(this,function b(){var d=this,c;return R(b,function(f){return 1==f.g?(d.graph.url?f=O(f,fetch(d.locateFile(d.graph.url,d.C)),3):(f.g=2,f=void 0),f):2!=f.g&&(c=f.j,c.body)?f.return(c.arrayBuffer()):f.return(new ArrayBuffer(0))})})}; function wa(){return W(this,function b(){return R(b,function(d){switch(d.g){case 1:return d.m=2,O(d,WebAssembly.instantiate(qa),4);case 4:d.g=3;d.m=0;break;case 2:return d.m=0,d.i=null,d.return(!1);case 3:return d.return(!0)}})})} function Y(a){this.h=a;this.listeners={};this.s={};this.A={};this.l=this.F=!0;this.o=Promise.resolve();this.locateFile=a&&a.locateFile||ra;if("object"===typeof window)a=window.location.pathname.toString().substring(0,window.location.pathname.toString().lastIndexOf("/"))+"/";else if("undefined"!==typeof location)a=location.pathname.toString().substring(0,location.pathname.toString().lastIndexOf("/"))+"/";else throw Error("solutions can only be loaded on a web page or in a web worker");this.C=a} Y.prototype.close=function(){this.i&&this.i.delete();return Promise.resolve()}; function xa(a){return W(a,function d(){var c=this,f,k,e,g,h,l,m,t,q;return R(d,function(n){switch(n.g){case 1:f=c;if(!c.F)return n.return();sa("createMediapipeSolutionsWasm",{locateFile:c.locateFile});sa("createMediapipeSolutionsPackedAssets",{locateFile:c.locateFile});k=c.h.files||[];return O(n,wa(),2);case 2:return e=n.j,O(n,Promise.all(k.map(function(r){return void 0===r.simd||r.simd&&e||!r.simd&&!e?ta(f.locateFile(r.url,f.C)):Promise.resolve()})),3);case 3:return g=window.createMediapipeSolutionsWasm, h=window.createMediapipeSolutionsPackedAssets,O(n,g(h),4);case 4:return c.g=n.j,c.j=document.createElement("canvas"),c.g.canvas=c.j,c.g.createContext(c.j,!0,!0,{}),c.i=new c.g.SolutionWasm,l=new va(c.h.graph,c.locateFile,c.C),O(n,c.loadGraph(l),5);case 5:if(c.h.listeners)for(m=x(c.h.listeners),t=m.next();!t.done;t=m.next())q=t.value,ya(c,q);c.F=!1;n.g=0}})})} Y.prototype.setOptions=function(a){if(this.h.options){for(var b=[],d=x(Object.keys(a)),c=d.next();!c.done;c=d.next()){var f=c.value;(c=this.h.options[f])&&c.graphOptionXref&&(f={valueNumber:0===c.type?a[f]:0,valueBoolean:1===c.type?a[f]:!1},c=Object.assign(Object.assign(Object.assign({},{calculatorName:"",calculatorIndex:0}),c.graphOptionXref),f),b.push(c))}0!==b.length&&(this.l=!0,this.m=b)}}; function za(a){return W(a,function d(){var c=this,f,k,e,g,h;return R(d,function(l){if(!c.l)return l.return();f=c.j.getContext("webgl2");if(!f)return alert("Failed to create WebGL canvas context when passing video frame."),l.return();c.D=f;if(c.m){k=new c.g.GraphOptionChangeRequestList;e=x(c.m);for(g=e.next();!g.done;g=e.next())h=g.value,k.push_back(h);c.i.changeOptions(k);k.delete();c.m=void 0}c.l=!1;l.g=0})})} Y.prototype.initialize=function(){return W(this,function b(){var d=this;return R(b,function(c){return 1==c.g?O(c,xa(d),2):O(c,za(d),0)})})};Y.prototype.loadGraph=function(a){return W(this,function d(){var c,f=this;return R(d,function(k){if(1==k.g)return O(k,a.toArrayBuffer(),2);c=k.j;f.i.loadGraph(c);k.g=0})})}; Y.prototype.send=function(a,b){return W(this,function c(){var f=this,k,e,g,h,l,m,t,q;return R(c,function(n){if(1==n.g){if(!f.h.inputs)return n.return();k=1E3*(b||performance.now());return O(n,f.o,2)}if(3!=n.g)return O(n,f.initialize(),3);e=new f.g.PacketDataList;g=x(Object.keys(a));for(h=g.next();!h.done;h=g.next())if(l=h.value,m=f.h.inputs[l]){a:{var r=a[l];switch(m.type){case "video":var p=f.s[m.stream];p||(p=new pa(f.g,f.D),f.s[m.stream]=p);0===p.i&&(p.i=p.h.createTexture());var v=p.j;v.canvas.width= r.videoWidth;v.canvas.height=r.videoHeight;v.activeTexture(v.TEXTURE0);p.h.bindTexture2d(p.i);v.texImage2D(v.TEXTURE_2D,0,v.RGBA,v.RGBA,v.UNSIGNED_BYTE,r);p.h.bindTexture2d(0);p={glName:p.i,width:r.videoWidth,height:r.videoHeight};break a;default:p={}}}t=p;q=m.stream;e.pushTexture2d(Object.assign(Object.assign({},t),{stream:q,timestamp:k}))}f.i.send(e);e.delete();n.g=0})})}; function Aa(a,b,d){if(d.isNumber())return d.getNumber();if(d.isRect())return d.getRect();if(d.isLandmarks())return d.getLandmarks();if(d.isLandmarksList())return d.getLandmarksList();if(d.isClassificationsList())return d.getClassificationsList();if(d.isTexture2d()){var c=a.A[b];c||(c=new pa(a.g,a.D),a.A[b]=c);a=c;d=d.getTexture2d();b=a.j;if(void 0===a.l){c=X(b,"\n attribute vec2 aVertex;\n attribute vec2 aTex;\n varying vec2 vTex;\n void main(void) {\n gl_Position = vec4(aVertex, 0.0, 1.0);\n vTex = aTex;\n }", 0);var f=X(b,"\n precision highp float;\n varying vec2 vTex;\n uniform sampler2D sampler0;\n void main(){\n gl_FragColor = texture2D(sampler0, vTex);\n }",1),k=b.createProgram();b.attachShader(k,c);b.attachShader(k,f);b.linkProgram(k);if(!b.getProgramParameter(k,b.LINK_STATUS))throw Error("Could not compile WebGL program.\n\n"+b.getProgramInfoLog(k));c=a.l=k;b.useProgram(c);f=b.getUniformLocation(c,"sampler0");a.g={v:b.getAttribLocation(c,"aVertex"),u:b.getAttribLocation(c,"aTex"),X:f};a.o= b.createBuffer();b.bindBuffer(b.ARRAY_BUFFER,a.o);b.enableVertexAttribArray(a.g.v);b.vertexAttribPointer(a.g.v,2,b.FLOAT,!1,0,0);b.bufferData(b.ARRAY_BUFFER,new Float32Array([-1,-1,-1,1,1,1,1,-1]),b.STATIC_DRAW);b.bindBuffer(b.ARRAY_BUFFER,null);a.m=b.createBuffer();b.bindBuffer(b.ARRAY_BUFFER,a.m);b.enableVertexAttribArray(a.g.u);b.vertexAttribPointer(a.g.u,2,b.FLOAT,!1,0,0);b.bufferData(b.ARRAY_BUFFER,new Float32Array([0,1,0,0,1,0,1,1]),b.STATIC_DRAW);b.bindBuffer(b.ARRAY_BUFFER,null);b.uniform1i(f, 0)}c=a.g;b.useProgram(a.l);b.canvas.width=d.width;b.canvas.height=d.height;b.viewport(0,0,d.width,d.height);b.activeTexture(b.TEXTURE0);a.h.bindTexture2d(d.glName);b.enableVertexAttribArray(c.v);b.bindBuffer(b.ARRAY_BUFFER,a.o);b.vertexAttribPointer(c.v,2,b.FLOAT,!1,0,0);b.enableVertexAttribArray(c.u);b.bindBuffer(b.ARRAY_BUFFER,a.m);b.vertexAttribPointer(c.u,2,b.FLOAT,!1,0,0);b.bindFramebuffer(b.DRAW_FRAMEBUFFER,null);b.drawArrays(b.TRIANGLE_FAN,0,4);b.disableVertexAttribArray(c.v);b.disableVertexAttribArray(c.u); b.bindBuffer(b.ARRAY_BUFFER,null);a.h.bindTexture2d(0);return a.j.canvas}} function ya(a,b){for(var d=b.name||"$",c=[].concat(aa(b.wants)),f=new a.g.StringList,k=x(b.wants),e=k.next();!e.done;e=k.next())f.push_back(e.value);k=a.g.PacketListener.implement({onResults:function(g){return W(a,function l(){var m,t,q=this,n,r;return R(l,function(p){if(1==p.g){m={};for(t=0;t<b.wants.length;++t)m[c[t]]=Aa(q,b.wants[t],g.get(t));if(b.outs){for(var v=m,ea=b.outs,D={},fa=x(Object.keys(ea)),w=fa.next();!w.done;w=fa.next()){w=w.value;var A=ea[w];if("string"===typeof A)D[w]=v[A];else{var F= v[A.stream];if(void 0!==F){if("landmarks"===A.type)D[w]=ua(F);else if("landmarks_list"===A.type){for(var K=[],T=F.size(),E=0;E<T;++E){var L=F.get(E);K.push(ua(L))}D[w]=K}else if("classifications_list"===A.type){K=[];T=F.size();for(E=0;E<T;++E){var ha=F.get(E);L=K;for(var Ba=L.push,ia=[],Ca=ha.size(),U=0;U<Ca;++U){var V=ha.get(U);ia.push({index:V.index,score:V.score,label:V.label})}Ba.call(L,ia)}D[w]=K}else throw Error("Unknown output config type: '"+A.type+"'");A.transform&&(D[w]=A.transform(D[w]))}}}m= D}(n=q.listeners[d])?p=O(p,q.o,3):(p.g=0,p=void 0);return p}if(r=n(m))return q.o=r,p.return(r);p.g=0})})}});a.i.attachMultiListener(f,k);f.delete()}Y.prototype.onResults=function(a,b){this.listeners[b||"$"]=a};S("Solution",Y);S("OptionType",{NUMBER:0,BOOL:1,0:"NUMBER",1:"BOOL"});function Z(a){a=a||{};this.g=new Y({locateFile:a.locateFile,files:[{url:"pose_solution_packed_assets_loader.js"},{simd:!1,url:"pose_solution_wasm_bin.js"},{simd:!0,url:"pose_solution_simd_wasm_bin.js"}],graph:{url:"pose_web.binarypb"},listeners:[{wants:["pose_landmarks","image_transformed"],outs:{image:"image_transformed",poseLandmarks:{type:"landmarks",stream:"pose_landmarks"}}}],inputs:{image:{type:"video",stream:"input_frames_gpu"}},options:{selfieMode:{type:1,graphOptionXref:{calculatorType:"GlScalerCalculator", calculatorIndex:1,fieldName:"flip_horizontal"}},upperBodyOnly:{type:1,graphOptionXref:{calculatorType:"ConstantSidePacketCalculator",calculatorName:"ConstantSidePacketCalculatorUpperBodyOnly",fieldName:"bool_value"}},smoothLandmarks:{type:1,graphOptionXref:{calculatorType:"ConstantSidePacketCalculator",calculatorName:"ConstantSidePacketCalculatorSmoothLandmarks",fieldName:"bool_value"}},minDetectionConfidence:{type:0,graphOptionXref:{calculatorType:"TensorsToDetectionsCalculator",calculatorName:"poselandmarkgpu__posedetectiongpu__TensorsToDetectionsCalculator", fieldName:"min_score_thresh"}},minTrackingConfidence:{type:0,graphOptionXref:{calculatorType:"ThresholdingCalculator",calculatorName:"poselandmarkgpu__poselandmarkbyroigpu__ThresholdingCalculator",fieldName:"threshold"}}}})}Z.prototype.close=function(){this.g.close();return Promise.resolve()};Z.prototype.onResults=function(a){this.g.onResults(a)};Z.prototype.initialize=function(){return W(this,function b(){var d=this;return R(b,function(c){return O(c,d.g.initialize(),0)})})}; Z.prototype.send=function(a,b){return W(this,function c(){var f=this;return R(c,function(k){return O(k,f.g.send(a,b),0)})})};Z.prototype.setOptions=function(a){this.g.setOptions(a)};S("Pose",Z);S("POSE_CONNECTIONS",[[0,1],[1,2],[2,3],[3,7],[0,4],[4,5],[5,6],[6,8],[9,10],[11,12],[11,13],[13,15],[15,17],[15,19],[15,21],[17,19],[12,14],[14,16],[16,18],[16,20],[16,22],[18,20],[11,23],[12,24],[23,24],[23,25],[24,26],[25,27],[26,28],[27,29],[28,30],[29,31],[30,32],[27,31],[28,32]]); S("POSE_LANDMARKS",{NOSE:0,LEFT_EYE_INNER:1,LEFT_EYE:2,LEFT_EYE_OUTER:3,RIGHT_EYE_INNER:4,RIGHT_EYE:5,RIGHT_EYE_OUTER:6,LEFT_EAR:7,RIGHT_EAR:8,R:9,W:10,LEFT_SHOULDER:11,RIGHT_SHOULDER:12,LEFT_ELBOW:13,RIGHT_ELBOW:14,LEFT_WRIST:15,RIGHT_WRIST:16,LEFT_PINKY:17,RIGHT_PINKY:18,LEFT_INDEX:19,RIGHT_INDEX:20,LEFT_THUMB:21,RIGHT_THUMB:22,LEFT_HIP:23,RIGHT_HIP:24,P:25,V:26,M:27,S:28,O:29,U:30,N:31,T:32});}).call(this);