qt-human 3.9.2 → 3.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.iife.js +1 -1
- package/dist/bundle.umd.js +1 -1
- package/dist/esm/chunks/human-core-COvRMT3l.js +1 -0
- package/dist/esm/chunks/index-DgEJCpaj.js +1 -0
- package/dist/esm/chunks/{render-DjEFzmMF.js → render-kGcOzzbC.js} +1 -1
- package/dist/esm/chunks/{shared-D2ogTnBH.js → shared-Bsx1K6Nf.js} +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/types/modules/human.d.ts +1 -1
- package/dist/esm/types/modules/human.d.ts.map +1 -1
- package/package.json +3 -1
- package/dist/esm/chunks/human-core-EKV7xbHB.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as t from"@tensorflow-models/pose-detection";import"@tensorflow/tfjs-backend-webgl";import*as e from"@tensorflow/tfjs-core";import*as i from"@vladmandic/face-api";import{h as s}from"./human-core-COvRMT3l.js";import"./shared-Bsx1K6Nf.js";import"./render-kGcOzzbC.js";import"three";import"three/examples/jsm/controls/OrbitControls.js";import"three/examples/jsm/loaders/RGBELoader.js";import"three/examples/jsm/loaders/GLTFLoader.js";import"three/examples/jsm/loaders/KTX2Loader.js";import"three/examples/jsm/libs/meshopt_decoder.module.js";import"three/examples/jsm/loaders/DRACOLoader.js";import"three/addons/controls/OrbitControls.js";class a{video;stream=null;facingMode;constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"user";this.video=t,this.facingMode=e}async open(){return this.stream=await navigator.mediaDevices.getUserMedia({video:{facingMode:this.facingMode},audio:!1}),this.video.srcObject=this.stream,await this.video.play(),!0}close(){this.stream?.getTracks().forEach((t=>t.stop())),this.stream=null,this.video.srcObject=null}switch(t){return this.close(),this.facingMode=t,this.open()}getVideoEl(){return this.video}getStream(){return this.stream}isOpen(){return null!==this.stream}}class n{baselineWidth=60;lastState="stable";threshold;callback;constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1.2;this.threshold=t}onChange(t){this.callback=t}update(t){const e=t.find((t=>"left_shoulder"===t.name)),i=t.find((t=>"right_shoulder"===t.name));if(!e||!i)return;const s=e.x-i.x,a=e.y-i.y,n=Math.sqrt(s*s+a*a);if(null===this.baselineWidth)return void(this.baselineWidth=n);let o="stable";n>this.baselineWidth*this.threshold?o="near":n<this.baselineWidth/this.threshold-20&&(o="far"),"stable"!==o&&o!==this.lastState&&(this.lastState=o,this.callback?.(o))}}const o="https://console-api.qtworld.top/static";const r="https://console-api.qtworld.top/static";const l={detection:class{detector=null;mediaStrea=null;interval=null;proximityDetector;constructor(){this.proximityDetector=new n}async init(i){this.mediaStrea=i,await e.setBackend("webgl"),await e.ready(),this.detector=await t.createDetector(t.SupportedModels.MoveNet,{modelType:"SinglePose.Lightning"})}on(t){this.interval&&clearInterval(this.interval),this.proximityDetector.onChange(t),this.interval=setInterval((async()=>{const t=await this.detect(this.mediaStrea.video);t.length>0&&this.proximityDetector.update(t[0].keypoints)}),300)}async detect(t){if(!this.detector)throw new Error("PoseDetector not initialized");return await this.detector.estimatePoses(t)}async destroy(){this.interval&&(clearInterval(this.interval),this.interval=null),this.detector&&(await this.detector.dispose(),this.detector=null),this.mediaStrea=null}},face:class{name="face";isInitialized=!1;interval=null;canvas=null;constructor(){}async initFaceAPI(){await e.setBackend("webgl"),await e.ready(),await i.nets.faceLandmark68Net.loadFromUri(`${o}/models`),await i.nets.ssdMobilenetv1.loadFromUri(`${o}/models`),await i.nets.faceRecognitionNet.loadFromUri(`${o}/models`)}async startFaceRecognition(t,e,a){this.interval&&clearInterval(this.interval);const{duration:n=3e3,minConfidence:o=.6,maxResults:r=5,drawDetections:l=!1,drawFaceLandmarks:c=!1}=e;!1===this.isInitialized&&(await this.initFaceAPI(),this.isInitialized=!0);const h=l||c,d=t.getVideoEl();let m=null,u={width:0,height:0};return h&&(m=i.createCanvasFromMedia(d),this.canvas=m,d.parentElement?(m.style.cssText="position: absolute;left:0;top:0;z-index:99;",d.parentElement?.appendChild(m)):(m.style.cssText="position: fixed;left:0;top:0;z-index:99;",document.body.appendChild(m)),u={width:d.clientWidth,height:d.clientHeight},i.matchDimensions(m,u)),this.interval=setInterval((async()=>{const t=await i.detectAllFaces(d,new i.SsdMobilenetv1Options({minConfidence:o,maxResults:r})).withFaceLandmarks().withFaceDescriptors();if(t.length>0){const t=new i.Box({x:0,y:0,width:d.clientWidth,height:d.clientHeight}),e=await this.cropFaceToBlob(d,t);try{const t=await(t=>{const e=new FormData;return e.append("file",t,`qthuman_face_${Date.now()}.png`),s.post("/human-auth/faces/recognition",e,{headers:{"Content-Type":"multipart/form-data"}})})(e);0===t.code&&a(t.data)}catch(t){this.interval&&clearInterval(this.interval)}}if(h&&m){m.getContext("2d",{willReadFrequently:!0})?.clearRect(0,0,d.clientWidth,d.clientHeight);const e=i.resizeResults(t,u);l&&i.draw.drawDetections(m,e),c&&i.draw.drawFaceLandmarks(m,e)}}),n),!0}expandBox(t,e,s){const a=Math.max(0,t.x-e),n=Math.max(0,t.y-e),o=Math.min(s.videoWidth-a,t.width+2*e),r=Math.min(s.videoHeight-n,t.height+2*e);return new i.Box({x:a,y:n,width:o,height:r})}cropFaceToBase64(t,e){const i=document.createElement("canvas");i.width=e.width,i.height=e.height;return i.getContext("2d").drawImage(t,0,0,e.width,e.height),i.toDataURL("image/png")}cropFaceToBlob(t,e){const i=document.createElement("canvas");i.width=e.width,i.height=e.height;return i.getContext("2d").drawImage(t,0,0,e.width,e.height),new Promise((t=>{i.toBlob((e=>{t(e)}),"image/png")}))}async stopFaceRecognition(){return this.interval&&clearInterval(this.interval),this.interval=null,!0}destroy(){this.interval&&(clearInterval(this.interval),this.interval=null),this.canvas&&(this.canvas.parentElement?.removeChild(this.canvas),this.canvas=null),this.isInitialized=!1}},expression:class{name="expression";isInitialized=!1;isDetecting=!1;running=!1;timeoutId=null;lastTriggerTime=0;lastSmileCount=0;lastTotalFaces=0;async initFaceAPI(t){await e.setBackend("webgl"),await e.ready(),await i.nets.ssdMobilenetv1.loadFromUri(t),await i.nets.faceExpressionNet.loadFromUri(t)}async startSmileDetection(t,e,s,a){this.stopSmileDetection();const{smileThreshold:n=.7,cooldownMs:o=3e4,modelUri:l=`${r}/models`,maxFaces:c=10}=e;this.isInitialized||(await this.initFaceAPI(l),this.isInitialized=!0);const h=t.getVideoEl();this.running=!0;const d=async()=>{if(this.running&&!this.isDetecting){this.isDetecting=!0;try{const t=await i.detectAllFaces(h,new i.SsdMobilenetv1Options({minConfidence:.5,maxResults:c})).withFaceExpressions(),e=t.length,r=t.filter((t=>t.expressions.happy>=n)).length,l=e>0?t.reduce(((t,e)=>Math.max(t,e.expressions.happy)),0):0,d=Date.now();r>0&&d-this.lastTriggerTime>=o&&(this.lastTriggerTime=d,s()),e>0?a({smileCount:r,totalFaces:e,maxHappiness:l,timestamp:d}):this.lastTotalFaces>0&&a({smileCount:0,totalFaces:0,maxHappiness:0,timestamp:d}),this.lastSmileCount=r,this.lastTotalFaces=e}catch(t){}finally{this.isDetecting=!1,this.running&&(this.timeoutId=setTimeout(d,1e3))}}};return this.timeoutId=setTimeout(d,0),!0}stopSmileDetection(){return this.running=!1,this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=null),this.isDetecting=!1,this.lastSmileCount=0,this.lastTotalFaces=0,!0}destroy(){this.stopSmileDetection(),this.isInitialized=!1}}};class c{manager;features=new Map;container;video;constructor(t){const e="string"==typeof t.el?document.getElementById(t.el):t.el;if(!e)throw new Error("video 容器初始化错误");this.container=e;const i=document.createElement("video");i.style.cssText="object-fit: cover;width:100%;height:100%;",i.muted=!0,e?.appendChild(i),this.video=i,this.manager=new a(i,t.facingMode),(t.features||[]).forEach((e=>{const i=l[e];if(i){const s=new i(t.overlayImage);this.features.set(e,s)}}))}async open(){return await this.manager.open()}isOpen(){return this.manager.isOpen()}close(){this.manager.close();for(const t of this.features.values())t.destroy&&t.destroy()}async takePhoto(){const t=this.features.get("photo");if(!t)throw new Error("Photo feature not enabled");return t.takePhoto(this.manager)}async switchCamera(t){await this.manager.switch(t)}async startFaceRecognition(t,e){const i=this.features.get("face");if(!i)throw new Error("FaceFeature not enabled");return i.startFaceRecognition(this.manager,t,e)}async stopFaceRecognition(){const t=this.features.get("face");if(!t)throw new Error("FaceFeature not enabled");return t.stopFaceRecognition()}async startBodyDetect(t){const e=this.features.get("detection");if(!e)throw new Error("PoseDetector feature not enabled");if(!this.video)throw new Error("video not enabled");return await e.init(this.manager),e.on(t),!0}async stopBodyDetect(){const t=this.features.get("detection");if(!t)throw new Error("PoseDetector feature not enabled");if(!this.video)throw new Error("video not enabled");return t.stopBodyDetect()}async startSmileDetection(t,e,i){const s=this.features.get("expression");if(!s)throw new Error("ExpressionFeature not enabled");return s.startSmileDetection(this.manager,t,e,i)}stopSmileDetection(){const t=this.features.get("expression");if(!t)throw new Error("ExpressionFeature not enabled");return t.stopSmileDetection()}destroy(){this.close(),this.container.innerHTML=""}}export{c as CameraSDK};
|