human/dist/human.node.js

33 lines
244 KiB
JavaScript
Raw Normal View History

2020-11-20 14:53:40 +01:00
/*
Human library
homepage: <https://github.com/vladmandic/human>
author: <https://github.com/vladmandic>'
*/
2020-11-26 16:37:04 +01:00
var __create=Object.create,__defProp=Object.defineProperty,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__getOwnPropNames=Object.getOwnPropertyNames,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__markAsModule=target=>__defProp(target,"__esModule",{value:!0}),__commonJS=(callback,module2)=>()=>(module2||(module2={exports:{}},callback(module2.exports,module2)),module2.exports),__export=(target,all)=>{__markAsModule(target);for(var name in all)__defProp(target,name,{get:all[name],enumerable:!0})},__exportStar=(target,module2,desc)=>{if(__markAsModule(target),module2&&typeof module2=="object"||typeof module2=="function")for(let key of __getOwnPropNames(module2))!__hasOwnProp.call(target,key)&&key!=="default"&&__defProp(target,key,{get:()=>module2[key],enumerable:!(desc=__getOwnPropDesc(module2,key))||desc.enumerable});return target},__toModule=module2=>module2&&module2.__esModule?module2:__exportStar(__defProp(module2!=null?__create(__getProtoOf(module2)):{},"default",{value:module2,enumerable:!0}),module2),require_tfjs_esm=__commonJS(exports2=>{var __create2=Object.create,__defProp2=Object.defineProperty,__getProtoOf2=Object.getPrototypeOf,__hasOwnProp2=Object.prototype.hasOwnProperty,__getOwnPropNames2=Object.getOwnPropertyNames,__getOwnPropDesc2=Object.getOwnPropertyDescriptor,__markAsModule2=target=>__defProp2(target,"__esModule",{value:!0}),__exportStar2=(target,module22,desc)=>{if(__markAsModule2(target),module22&&typeof module22=="object"||typeof module22=="function")for(let key of __getOwnPropNames2(module22))!__hasOwnProp2.call(target,key)&&key!=="default"&&__defProp2(target,key,{get:()=>module22[key],enumerable:!(desc=__getOwnPropDesc2(module22,key))||desc.enumerable});return target},__toModule2=module22=>module22&&module22.__esModule?module22:__exportStar2(__defProp2(module22!=null?__create2(__getProtoOf2(module22)):{},"default",{value:module22,enumerable:!0}),module22);__exportStar2(exports2,__toModule2(require("@tensorflow/tfjs-node")))}),require_blazeface=__commonJS(exports2=>{var tf3=__toModule(require_tfjs_esm()),NUM_LANDMARKS=6;function generateAnchors(inputSize){let spec={strides:[inputSize/16,inputSize/8],anchors:[2,6]},anchors=[];for(let i=0;i<spec.strides.length;i++){let stride=spec.strides[i],gridRows=Math.floor((inputSize+stride-1)/stride),gridCols=Math.floor((inputSize+stride-1)/stride),anchorsNum=spec.anchors[i];for(let gridY=0;gridY<gridRows;gridY++){let anchorY=stride*(gridY+.5);for(let gridX=0;gridX<gridCols;gridX++){let anchorX=stride*(gridX+.5);for(let n=0;n<anchorsNum;n++)anchors.push([anchorX,anchorY])}}}return anchors}var disposeBox=box=>{box.startEndTensor.dispose(),box.startPoint.dispose(),box.endPoint.dispose()},createBox=startEndTensor=>({startEndTensor,startPoint:tf3.slice(startEndTensor,[0,0],[-1,2]),endPoint:tf3.slice(startEndTensor,[0,2],[-1,2])}),scaleBox=(box,factors)=>{let starts=tf3.mul(box.startPoint,factors),ends=tf3.mul(box.endPoint,factors),newCoordinates=tf3.concat2d([starts,ends],1);return createBox(newCoordinates)};function decodeBounds(boxOutputs,anchors,inputSize){let boxStarts=tf3.slice(boxOutputs,[0,1],[-1,2]),centers=tf3.add(boxStarts,anchors),boxSizes=tf3.slice(boxOutputs,[0,3],[-1,2]),boxSizesNormalized=tf3.div(boxSizes,inputSize),centersNormalized=tf3.div(centers,inputSize),halfBoxSize=tf3.div(boxSizesNormalized,2),starts=tf3.sub(centersNormalized,halfBoxSize),ends=tf3.add(centersNormalized,halfBoxSize),startNormalized=tf3.mul(starts,inputSize),endNormalized=tf3.mul(ends,inputSize),concatAxis=1;return tf3.concat2d([startNormalized,endNormalized],concatAxis)}function scaleBoxFromPrediction(face2,scaleFactor){return tf3.tidy(()=>{let box=face2.box?face2.box:face2;return scaleBox(box,scaleFactor).startEndTensor.squeeze()})}var BlazeFaceModel=class{constructor(model,config2){this.blazeFaceModel=model,this.width=config2.face.detector.inputSize,this.height=config2.face.detector.inputSize,this.anchorsData=generateAnchors(config2.face.detector.inputSize),this.anchors=tf3.tensor2d(this.anchorsData),this.inputSize=tf3.tensor1d([this.wid
`),SHADER.FRAGMENT_IDENTITY=["precision highp float;","varying vec2 vUv;","uniform sampler2D texture;","void main(void) {","gl_FragColor = texture2D(texture, vUv);","}"].join(`
2020-11-25 15:13:19 +01:00
`);let _filter={};_filter.colorMatrix=function(matrix){let m=new Float32Array(matrix);m[4]/=255,m[9]/=255,m[14]/=255,m[19]/=255;let shader=m[18]===1&&m[3]===0&&m[8]===0&&m[13]===0&&m[15]===0&&m[16]===0&&m[17]===0&&m[19]===0?_filter.colorMatrix.SHADER.WITHOUT_ALPHA:_filter.colorMatrix.SHADER.WITH_ALPHA,program=_compileShader(shader);gl.uniform1fv(program.uniform.m,m),_draw()},_filter.colorMatrix.SHADER={},_filter.colorMatrix.SHADER.WITH_ALPHA=["precision highp float;","varying vec2 vUv;","uniform sampler2D texture;","uniform float m[20];","void main(void) {","vec4 c = texture2D(texture, vUv);","gl_FragColor.r = m[0] * c.r + m[1] * c.g + m[2] * c.b + m[3] * c.a + m[4];","gl_FragColor.g = m[5] * c.r + m[6] * c.g + m[7] * c.b + m[8] * c.a + m[9];","gl_FragColor.b = m[10] * c.r + m[11] * c.g + m[12] * c.b + m[13] * c.a + m[14];","gl_FragColor.a = m[15] * c.r + m[16] * c.g + m[17] * c.b + m[18] * c.a + m[19];","}"].join(`
`),_filter.colorMatrix.SHADER.WITHOUT_ALPHA=["precision highp float;","varying vec2 vUv;","uniform sampler2D texture;","uniform float m[20];","void main(void) {","vec4 c = texture2D(texture, vUv);","gl_FragColor.r = m[0] * c.r + m[1] * c.g + m[2] * c.b + m[4];","gl_FragColor.g = m[5] * c.r + m[6] * c.g + m[7] * c.b + m[9];","gl_FragColor.b = m[10] * c.r + m[11] * c.g + m[12] * c.b + m[14];","gl_FragColor.a = c.a;","}"].join(`
2020-11-25 15:13:19 +01:00
`),_filter.brightness=function(brightness){let b=(brightness||0)+1;_filter.colorMatrix([b,0,0,0,0,0,b,0,0,0,0,0,b,0,0,0,0,0,1,0])},_filter.saturation=function(amount){let x=(amount||0)*2/3+1,y=(x-1)*-.5;_filter.colorMatrix([x,y,y,0,0,y,x,y,0,0,y,y,x,0,0,0,0,0,1,0])},_filter.desaturate=function(){_filter.saturation(-1)},_filter.contrast=function(amount){let v=(amount||0)+1,o=-128*(v-1);_filter.colorMatrix([v,0,0,0,o,0,v,0,0,o,0,0,v,0,o,0,0,0,1,0])},_filter.negative=function(){_filter.contrast(-2)},_filter.hue=function(rotation){rotation=(rotation||0)/180*Math.PI;let cos=Math.cos(rotation),sin=Math.sin(rotation),lumR=.213,lumG=.715,lumB=.072;_filter.colorMatrix([lumR+cos*(1-lumR)+sin*-lumR,lumG+cos*-lumG+sin*-lumG,lumB+cos*-lumB+sin*(1-lumB),0,0,lumR+cos*-lumR+sin*.143,lumG+cos*(1-lumG)+sin*.14,lumB+cos*-lumB+sin*-.283,0,0,lumR+cos*-lumR+sin*-(1-lumR),lumG+cos*-lumG+sin*lumG,lumB+cos*(1-lumB)+sin*lumB,0,0,0,0,0,1,0])},_filter.desaturateLuminance=function(){_filter.colorMatrix([.2764723,.929708,.0938197,0,-37.1,.2764723,.929708,.0938197,0,-37.1,.2764723,.929708,.0938197,0,-37.1,0,0,0,1,0])},_filter.sepia=function(){_filter.colorMatrix([.393,.7689999,.18899999,0,0,.349,.6859999,.16799999,0,0,.272,.5339999,.13099999,0,0,0,0,0,1,0])},_filter.brownie=function(){_filter.colorMatrix([.5997023498159715,.34553243048391263,-.2708298674538042,0,47.43192855600873,-.037703249837783157,.8609577587992641,.15059552388459913,0,-36.96841498319127,.24113635128153335,-.07441037908422492,.44972182064877153,0,-7.562075277591283,0,0,0,1,0])},_filter.vintagePinhole=function(){_filter.colorMatrix([.6279345635605994,.3202183420819367,-.03965408211312453,0,9.651285835294123,.02578397704808868,.6441188644374771,.03259127616149294,0,7.462829176470591,.0466055556782719,-.0851232987247891,.5241648018700465,0,5.159190588235296,0,0,0,1,0])},_filter.kodachrome=function(){_filter.colorMatrix([1.1285582396593525,-.3967382283601348,-.03992559172921793,0,63.72958762196502,-.16404339962244616,1.0835251566291304,-.05498805115633132,0,24.732407896706203,-.16786010706155763,-.5603416277695248,1.6014850761964943,0,35.62982807460946,0,0,0,1,0])},_filter.technicolor=function(){_filter.colorMatrix([1.9125277891456083,-.8545344976951645,-.09155508482755585,0,11.793603434377337,-.3087833385928097,1.7658908555458428,-.10601743074722245,0,-70.35205161461398,-.231103377548616,-.7501899197440212,1.847597816108189,0,30.950940869491138,0,0,0,1,0])},_filter.polaroid=function(){_filter.colorMatrix([1.438,-.062,-.062,0,0,-.122,1.378,-.122,0,0,-.016,-.016,1.483,0,0,0,0,0,1,0])},_filter.shiftToBGR=function(){_filter.colorMatrix([0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0])},_filter.convolution=function(matrix){let m=new Float32Array(matrix),pixelSizeX=1/_width,pixelSizeY=1/_height,program=_compileShader(_filter.convolution.SHADER);gl.uniform1fv(program.uniform.m,m),gl.uniform2f(program.uniform.px,pixelSizeX,pixelSizeY),_draw()},_filter.convolution.SHADER=["precision highp float;","varying vec2 vUv;","uniform sampler2D texture;","uniform vec2 px;","uniform float m[9];","void main(void) {","vec4 c11 = texture2D(texture, vUv - px);","vec4 c12 = texture2D(texture, vec2(vUv.x, vUv.y - px.y));","vec4 c13 = texture2D(texture, vec2(vUv.x + px.x, vUv.y - px.y));","vec4 c21 = texture2D(texture, vec2(vUv.x - px.x, vUv.y) );","vec4 c22 = texture2D(texture, vUv);","vec4 c23 = texture2D(texture, vec2(vUv.x + px.x, vUv.y) );","vec4 c31 = texture2D(texture, vec2(vUv.x - px.x, vUv.y + px.y) );","vec4 c32 = texture2D(texture, vec2(vUv.x, vUv.y + px.y) );","vec4 c33 = texture2D(texture, vUv + px );","gl_FragColor = ","c11 * m[0] + c12 * m[1] + c22 * m[2] +","c21 * m[3] + c22 * m[4] + c23 * m[5] +","c31 * m[6] + c32 * m[7] + c33 * m[8];","gl_FragColor.a = c22.a;","}"].join(`
`),_filter.detectEdges=function(){_filter.convolution.call(this,[0,1,0,1,-4,1,0,1,0])},_filter.sobelX=function(){_filter.convolution.call(this,[-1,0,1,-2,0,2,-1,0,1])},_filter.sobelY=function(){_filter.convolution.call(this,[-1,-2,-1,0,0,0,1,2,1])},_filter.sharpen=function(amount){let a=amount||1;_filter.convolution.call(this,[0,-1*a,0,-1*a,1+4*a,-1*a,0,-1*a,0])},_filter.emboss=function(size){let s=size||1;_filter.convolution.call(this,[-2*s,-1*s,0,-1*s,1,1*s,0,1*s,2*s])},_filter.blur=function(size){let blurSizeX=size/7/_width,blurSizeY=size/7/_height,program=_compileShader(_filter.blur.SHADER);gl.uniform2f(program.uniform.px,0,blurSizeY),_draw(DRAW.INTERMEDIATE),gl.uniform2f(program.uniform.px,blurSizeX,0),_draw()},_filter.blur.SHADER=["precision highp float;","varying vec2 vUv;","uniform sampler2D texture;","uniform vec2 px;","void main(void) {","gl_FragColor = vec4(0.0);","gl_FragColor += texture2D(texture, vUv + vec2(-7.0*px.x, -7.0*px.y))*0.0044299121055113265;","gl_FragColor += texture2D(texture, vUv + vec2(-6.0*px.x, -6.0*px.y))*0.00895781211794;","gl_FragColor += texture2D(texture, vUv + vec2(-5.0*px.x, -5.0*px.y))*0.0215963866053;","gl_FragColor += texture2D(texture, vUv + vec2(-4.0*px.x, -4.0*px.y))*0.0443683338718;","gl_FragColor += texture2D(texture, vUv + vec2(-3.0*px.x, -3.0*px.y))*0.0776744219933;","gl_FragColor += texture2D(texture, vUv + vec2(-2.0*px.x, -2.0*px.y))*0.115876621105;","gl_FragColor += texture2D(texture, vUv + vec2(-1.0*px.x, -1.0*px.y))*0.147308056121;","gl_FragColor += texture2D(texture, vUv )*0.159576912161;","gl_FragColor += texture2D(texture, vUv + vec2( 1.0*px.x, 1.0*px.y))*0.147308056121;","gl_FragColor += texture2D(texture, vUv + vec2( 2.0*px.x, 2.0*px.y))*0.115876621105;","gl_FragColor += texture2D(texture, vUv + vec2( 3.0*px.x, 3.0*px.y))*0.0776744219933;","gl_FragColor += texture2D(texture, vUv + vec2( 4.0*px.x, 4.0*px.y))*0.0443683338718;","gl_FragColor += texture2D(texture, vUv + vec2( 5.0*px.x, 5.0*px.y))*0.0215963866053;","gl_FragColor += texture2D(texture, vUv + vec2( 6.0*px.x, 6.0*px.y))*0.00895781211794;","gl_FragColor += texture2D(texture, vUv + vec2( 7.0*px.x, 7.0*px.y))*0.0044299121055113265;","}"].join(`
`),_filter.pixelate=function(size){let blurSizeX=size/_width,blurSizeY=size/_height,program=_compileShader(_filter.pixelate.SHADER);gl.uniform2f(program.uniform.size,blurSizeX,blurSizeY),_draw()},_filter.pixelate.SHADER=["precision highp float;","varying vec2 vUv;","uniform vec2 size;","uniform sampler2D texture;","vec2 pixelate(vec2 coord, vec2 size) {","return floor( coord / size ) * size;","}","void main(void) {","gl_FragColor = vec4(0.0);","vec2 coord = pixelate(vUv, size);","gl_FragColor += texture2D(texture, coord);","}"].join(`
`)};exports2.Canvas=WebGLImageFilter}),require_image=__commonJS(exports2=>{var tf3=__toModule(require_tfjs_esm()),fxImage=__toModule(require_imagefx()),inCanvas=null,outCanvas=null;function process3(input,config2){let tensor;if(input instanceof tf3.Tensor)tensor=tf3.clone(input);else{let originalWidth=input.naturalWidth||input.videoWidth||input.width||input.shape&&input.shape[1]>0,originalHeight=input.naturalHeight||input.videoHeight||input.height||input.shape&&input.shape[2]>0,targetWidth=originalWidth,targetHeight=originalHeight;if(config2.filter.width>0?targetWidth=config2.filter.width:config2.filter.height>0&&(targetWidth=originalWidth*(config2.filter.height/originalHeight)),config2.filter.height>0?targetHeight=config2.filter.height:config2.filter.width>0&&(targetHeight=originalHeight*(config2.filter.width/originalWidth)),!targetWidth||!targetHeight)return console.log("Human: invalid input",input),null;(!inCanvas||inCanvas.width!==targetWidth||inCanvas.height!==targetHeight)&&(inCanvas=typeof OffscreenCanvas!="undefined"?new OffscreenCanvas(targetWidth,targetHeight):document.createElement("canvas"),inCanvas.width!==targetWidth&&(inCanvas.width=targetWidth),inCanvas.height!==targetHeight&&(inCanvas.height=targetHeight));let ctx=inCanvas.getContext("2d");if(input instanceof ImageData?ctx.putImageData(input,0,0):ctx.drawImage(input,0,0,originalWidth,originalHeight,0,0,inCanvas.width,inCanvas.height),config2.filter.enabled){(!this.fx||!outCanvas||inCanvas.width!==outCanvas.width||inCanvas.height!==outCanvas.height)&&(outCanvas=typeof OffscreenCanvas!="undefined"?new OffscreenCanvas(inCanvas.width,inCanvas.height):document.createElement("canvas"),outCanvas.width!==inCanvas.width&&(outCanvas.width=inCanvas.width),outCanvas.height!==inCanvas.height&&(outCanvas.height=inCanvas.height),this.fx=tf3.ENV.flags.IS_BROWSER?new fxImage.Canvas({canvas:outCanvas}):null),this.fx.reset(),this.fx.addFilter("brightness",config2.filter.brightness),config2.filter.contrast!==0&&this.fx.addFilter("contrast",config2.filter.contrast),config2.filter.sharpness!==0&&this.fx.addFilter("sharpen",config2.filter.sharpness),config2.filter.blur!==0&&this.fx.addFilter("blur",config2.filter.blur),config2.filter.saturation!==0&&this.fx.addFilter("saturation",config2.filter.saturation),config2.filter.hue!==0&&this.fx.addFilter("hue",config2.filter.hue),config2.filter.negative&&this.fx.addFilter("negative"),config2.filter.sepia&&this.fx.addFilter("sepia"),config2.filter.vintage&&this.fx.addFilter("brownie"),config2.filter.sepia&&this.fx.addFilter("sepia"),config2.filter.kodachrome&&this.fx.addFilter("kodachrome"),config2.filter.technicolor&&this.fx.addFilter("technicolor"),config2.filter.polaroid&&this.fx.addFilter("polaroid"),config2.filter.pixelate!==0&&this.fx.addFilter("pixelate",config2.filter.pixelate),this.fx.apply(inCanvas);let gl=!1;if(gl){let glBuffer=new Uint8Array(outCanvas.width*outCanvas.height*4),pixBuffer=new Uint8Array(outCanvas.width*outCanvas.height*3);gl.readPixels(0,0,outCanvas.width,outCanvas.height,gl.RGBA,gl.UNSIGNED_BYTE,glBuffer);let i=0;for(let y=outCanvas.height-1;y>=0;y--)for(let x=0;x<outCanvas.width;x++){let index=(x+y*outCanvas.width)*4;pixBuffer[i++]=glBuffer[index+0],pixBuffer[i++]=glBuffer[index+1],pixBuffer[i++]=glBuffer[index+2]}outCanvas.data=pixBuffer}}else outCanvas=inCanvas;let pixels;if(outCanvas.data){let shape=[outCanvas.height,outCanvas.width,3];pixels=tf3.tensor3d(outCanvas.data,shape,"int32")}else if(config2.backend==="webgl"||outCanvas instanceof ImageData)pixels=tf3.browser.fromPixels(outCanvas);else{let tempCanvas=typeof OffscreenCanvas!="undefined"?new OffscreenCanvas(targetWidth,targetHeight):document.createElement("canvas");tempCanvas.width=targetWidth,tempCanvas.height=targetHeight;let tempCtx=tempCanvas.getContext("2d");tempCtx.drawImage(outCanvas,0,0);let data2=tempCtx.getImageData(0,0,targetWidth,targetHeight);pixels=tf3.browser.fromPixels(data2)}let casted=pixels.toFloat();tensor=casted.expandDims(0),pixels.dispose(),casted.dispose()}return{tensor,canvas:config2.filter.return?outCanvas:null}}exports
2020-11-04 07:11:24 +01:00
/**
* @license
* Copyright 2020 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
*
* 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.
* =============================================================================
*/
2020-10-27 17:49:52 +01:00
//# sourceMappingURL=human.node.js.map