npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-face-detection

v0.0.9

Published

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.2.0.

Downloads

40

Readme

ngx-face-detection

A library for face detection based on vladmandic/human and TensorFlow/JS

repo

demo

npm i ngx-face-detection @vladmandic/human ismobilejs mathjs

or

yarn add ngx-face-detection @vladmandic/human ismobilejs mathjs

get-started

1. Project. Json or angular.json of the required app

Add it to assets


{
  "glob": "human.{js,js.map}",
  "input": "node_modules/@vladmandic/human/dist",
  "output": "./human"
},
{
"glob": "*.{bin,json}",
"input": "node_modules/@vladmandic/human/models",
"output": "./human/models"
},
{
"glob": "*.wasm",
"input": "node_modules/@vladmandic/human/assets",
"output": "./human/assets"
}

examples:


"ngx-face-detection": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
},
"@schematics/angular:application": {
"strict": true
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/ngx-face-detection",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "human.{js,js.map}",
"input": "node_modules/@vladmandic/human/dist",
"output": "./human"
},
{
"glob": "*.{bin,json}",
"input": "node_modules/@vladmandic/human/models",
"output": "./human/models"
},
{
"glob": "*.wasm",
"input": "node_modules/@vladmandic/human/assets",
"output": "./human/assets"
}
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "ngx-face-detection:build:production"
},
"development": {
"browserTarget": "ngx-face-detection:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "ngx-face-detection:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
}
}
}

These variables are used when the module is loaded.

2. Import in appModule

examples:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { environment } from '../environments/environment';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FaceDetectionModule } from "../../projects/face-detection/src/lib/face-detection.module";
import {FaceDetectionPageModule} from "./face-detection/face-detection.module";

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FaceDetectionPageModule,
    // ----------------------------------------------------------------------------- [ local lib ]
    FaceDetectionModule.forRoot({
      script: 'human/human.js',
      // baseHref
      resourcesUrl: '/ngx-face-detection/',
      production: environment.production
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

3. in required html file

examples:

<ngx-face-detection (noAvailableStream)="noAvailableStream($event)" [stream]="stream" [bioassay]="live" [iris]="iris" [debug]="debug"></ngx-face-detection>

4. in required ts file

examples:

import {Subscription} from 'rxjs';

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {
  FaceDetectionComponent,
  FaceDetectionService,
  isMobile,
  getUserMedia
} from "../../../projects/face-detection/src/public-api";

const img = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';

@Component({
  selector: 'app-face-detection-page',
  templateUrl: './face-detection.page.html',
  styleUrls: ['./face-detection.page.scss']
})
export class FaceDetectionPage implements OnInit, AfterViewInit {
  loading = true;
  title = 'dev-face-camera';
  live = true;
  iris = true;
  debug = true;
  delay = 100;
  photo: string = img;
  rectPhoto: string = img;
  stream!: MediaStream;
  public isMobile = isMobile(window);

  videoMaxWidth = 1440;
  videoMaxHeight = 720;

  get width() {
    return this.el.nativeElement.clientWidth || document.body.clientWidth;
  }

  get height() {
    return this.el.nativeElement.clientHeight || document.body.clientHeight;
  }

  // a subscription for screenshot of videos
  lastfaceDetectionSub!: Subscription;
  @ViewChild(FaceDetectionComponent, {static: true}) faceDetection!: FaceDetectionComponent;
  @ViewChild('rectPhoto') rectPhotoEle!: ElementRef<HTMLImageElement>;

  constructor(
    private el: ElementRef<HTMLElement>,
    private faceDetectionService: FaceDetectionService
  ) {
  }

  getCamera() {
    try {
      const {width, height, videoMaxWidth, videoMaxHeight, isMobile} = this;
      getUserMedia(width, height, videoMaxWidth, videoMaxHeight, isMobile).then(async media => {
        this.stream = media;
      });
    } catch (err) {
    }
  }

  ngOnInit(): void {

    this.getCamera();
    this.faceDetection.beginDetect$.subscribe(
      () => {
        this.loading = false;
        console.log('detection started');
      },
      error => {
        console.log('error when detection start');

      }
    );
  }

  ngAfterViewInit() {
    this.takePhoto();
  }

  preload() {
    this.faceDetectionService.preload({live: this.live, iris: this.iris, debug: this.debug});
  }

  get rect() {
    return isMobile(window)
      ? {
        x: 0,
        y: 0,
        width: this.el.nativeElement.clientWidth,
        height: this.el.nativeElement.clientHeight
      }
      : {
        x: this.el.nativeElement.clientWidth / 4,
        y: 0,
        width: this.el.nativeElement.clientWidth / 2,
        height: this.el.nativeElement.clientHeight
      };
  }

  takePhoto() {
    if (this.lastfaceDetectionSub) {
      this.lastfaceDetectionSub.unsubscribe();
    }
    this.photo = img;
    this.rectPhoto = img;
    console.log(this.rect);
    this.lastfaceDetectionSub = this.faceDetection.takePhoto(600, 800, this.rect, true).subscribe(result => {
      const {photo, rectPhoto} = result;
      this.photo = photo || img;
      this.rectPhoto = rectPhoto || img;
    });
  }

  takeBetterPhoto() {
    if (this.lastfaceDetectionSub) {
      this.lastfaceDetectionSub.unsubscribe();
    }
    this.photo = img;
    this.rectPhoto = img;
    this.lastfaceDetectionSub = this.faceDetection.takeBetterPhoto(600, 800, this.rect, true).subscribe(d => {
      const {photo, rectPhoto} = d;
      this.photo = photo || img;
      this.rectPhoto = rectPhoto || img;
      console.log('find a photo can be used');
    });
  }

  liveness(action: string) {
    if (this.lastfaceDetectionSub) {
      this.lastfaceDetectionSub.unsubscribe();
    }
    this.lastfaceDetectionSub = this.faceDetection.liveness(action as any, this.rect).subscribe(result => {
      console.log(result);
      console.log('successful liveness detection');
    });
  }

  livenessArray() {
    if (this.lastfaceDetectionSub) {
      this.lastfaceDetectionSub.unsubscribe();
    }
    this.lastfaceDetectionSub = this.faceDetection.livenessArray(['facingLeft', 'facingRight'], this.rect).subscribe(
      () => {
      },
      () => {
      },
      () => {
        console.log('successful liveness detection');
      }
    );
  }

  livenessArrayTakeBetterPhoto(action: any) {
    if (this.lastfaceDetectionSub) {
      this.lastfaceDetectionSub.unsubscribe();
    }
    this.photo = img;
    this.rectPhoto = img;
    this.lastfaceDetectionSub = this.faceDetection
      .livenessArrayTakeBetterPhoto(
        [action],
        600,
        800,
        this.rect,
        true
      )
      .subscribe(d => {
        const {photo, rectPhoto} = d;
        this.photo = photo || img;
        this.rectPhoto = rectPhoto || img;
        console.log('successful liveness detection');
      });
  }

  play() {
    this.faceDetection.play();
  }

  pause() {
    this.faceDetection.pause();
  }

  /**
   * can't find available video stream
   */
  noAvailableStream(res: boolean) {
    //
    console.log('no camera available')
  }
}