2021-03-08 20:26:04 +01:00





2021-03-08 21:07:31 +01:00
# FaceAPI
2021-03-07 16:05:35 +01:00
**Face detection and recognition libary for Browser and NodeJS implemented on top of TensorFlow/JS**
< br >
**Live Demo**: < https: / / vladmandic . github . io / face-api / example / webcam . html >
< br >
2020-08-27 14:58:00 +02:00
## Note
2020-09-18 19:51:40 +02:00
This is updated **face-api.js** with latest available TensorFlow/JS as the original face-api.js is not compatible with **tfjs 2.0+** .
2020-08-27 14:58:00 +02:00
2021-03-07 13:26:23 +01:00
Forked from [face-api.js ](https://github.com/justadudewhohacks/face-api.js ) version **0.22.2** which was released on March 22nd, 2020
2020-08-18 13:54:53 +02:00
2021-03-09 23:32:33 +01:00
Currently based on ** `TensorFlow/JS` 3.3.0**
2020-10-13 22:57:06 +02:00
2020-10-15 12:48:39 +02:00
### Why?
2020-12-02 23:24:40 +01:00
Because I needed Face-API that does not cause version conflict with newer TFJS 2.0 that I use accross my projects
And since original Face-API was open-source, I've released this version as well
Unfortunately, changes ended up being too large for a simple pull request on original Face-API and it ended up being a full-fledged version on its own
2020-10-15 12:48:39 +02:00
### Differences
2021-02-17 15:22:31 +01:00
- Compatible with `TensorFlow/JS 2.0+ & 3.0+`
2020-12-02 22:46:41 +01:00
- Compatible with `WebGL` , `CPU` and `WASM` TFJS Browser backends
- Compatible with both `tfjs-node` and `tfjs-node-gpu` TFJS NodeJS backends
2020-10-15 12:48:39 +02:00
- Updated all type castings for TypeScript type checking to `TypeScript 4.1`
2020-12-02 22:46:41 +01:00
- Switched bundling from `UMD` to `ESM` + `CommonJS` with fallback to `IIFE`
2020-12-02 23:24:40 +01:00
Resulting code is optimized per-platform instead of being universal
2020-10-15 12:48:39 +02:00
Fully tree shakable when imported as an `ESM` module
Browser bundle process uses `ESBuild` instead of `Rollup`
- Typescript build process now targets `ES2018` and instead of dual ES5/ES6
Resulting code is clean ES2018 JavaScript without polyfills
2020-10-13 22:57:06 +02:00
- Removed old tests, docs, examples
2020-10-15 12:48:39 +02:00
- Removed old package dependencies (`karma`, `jasmine` , `babel` , etc.)
2020-10-13 22:57:06 +02:00
- Updated all package dependencies
2020-10-15 12:48:39 +02:00
- Updated TensorFlow/JS dependencies since backends were removed from `@tensorflow/tfjs-core`
- Updated mobileNetv1 model due to `batchNorm()` dependency
- Added `version` class that returns JSON object with version of FaceAPI as well as linked TFJS
2021-01-10 16:35:51 +01:00
- Added test/dev built-in HTTP & HTTPS Web server
2020-10-15 12:48:39 +02:00
- Removed `mtcnn` and `tinyYolov2` models as they were non-functional in latest public version of `Face-API`
*If there is a demand, I can re-implement them back.*
2021-03-07 15:58:20 +01:00
- Added `face angle` calculations that returns `roll` , `yaw` and `pitch`
2020-08-18 13:54:53 +02:00
2020-08-26 00:24:48 +02:00
Which means valid models are **tinyFaceDetector** and **mobileNetv1**
2020-08-27 14:58:00 +02:00
2020-12-08 14:33:00 +01:00
< br >
< hr >
< br >
## Examples
< br >
### Browser
2021-01-10 16:35:51 +01:00
Browser example that uses static images and showcases both models as well as all of the extensions is included in `/example/index.html`
Example can be accessed directly using Git pages using URL: < https: / / vladmandic . github . io / face-api / example / index . html >
Browser example that uses live webcam is included in `/example/webcam.html`
Example can be accessed directly using Git pages using URL: < https: / / vladmandic . github . io / face-api / example / webcam . html >
2020-12-08 14:33:00 +01:00
< br >
*Note: Photos shown below are taken by me*
2021-03-07 13:26:23 +01:00

2020-12-08 14:33:00 +01:00
< br >
### NodeJS
Two NodeJS examples are:
- `/example/node-singleprocess.js` : Regular usage of `FaceAPI` from `NodeJS`
- `/example/node-multiprocess.js` : Multiprocessing showcase that uses pool of worker processes (`node-multiprocess-worker.js`
Main starts fixed pool of worker processes with each worker having it's instance of `FaceAPI`
Workers communicate with main when they are ready and main dispaches job to each ready worker until job queue is empty
```json
2020-12-08 08:30:01 INFO: @vladmandic/face -api version 0.9.1
2020-12-08 08:30:01 INFO: User: vlado Platform: linux Arch: x64 Node: v15.0.1
2020-12-08 08:30:01 INFO: FaceAPI multi-process test
2020-12-08 08:30:01 STATE: Main: started worker: 265238
2020-12-08 08:30:01 STATE: Main: started worker: 265244
2020-12-08 08:30:02 STATE: Worker: PID: 265238 TensorFlow/JS 2.7.0 FaceAPI 0.9.1 Backend: tensorflow
2020-12-08 08:30:02 STATE: Worker: PID: 265244 TensorFlow/JS 2.7.0 FaceAPI 0.9.1 Backend: tensorflow
2020-12-08 08:30:02 STATE: Main: dispatching to worker: 265238
2020-12-08 08:30:02 STATE: Main: dispatching to worker: 265244
2020-12-08 08:30:02 DATA: Worker received message: 265238 { image: 'example/sample (1).jpg' }
2020-12-08 08:30:02 DATA: Worker received message: 265244 { image: 'example/sample (2).jpg' }
2020-12-08 08:30:04 DATA: Main: worker finished: 265238 detected faces: 3
2020-12-08 08:30:04 STATE: Main: dispatching to worker: 265238
2020-12-08 08:30:04 DATA: Main: worker finished: 265244 detected faces: 3
2020-12-08 08:30:04 STATE: Main: dispatching to worker: 265244
2020-12-08 08:30:04 DATA: Worker received message: 265238 { image: 'example/sample (3).jpg' }
2020-12-08 08:30:04 DATA: Worker received message: 265244 { image: 'example/sample (4).jpg' }
2020-12-08 08:30:06 DATA: Main: worker finished: 265238 detected faces: 3
2020-12-08 08:30:06 STATE: Main: dispatching to worker: 265238
2020-12-08 08:30:06 DATA: Worker received message: 265238 { image: 'example/sample (5).jpg' }
2020-12-08 08:30:06 DATA: Main: worker finished: 265244 detected faces: 4
2020-12-08 08:30:06 STATE: Main: dispatching to worker: 265244
2020-12-08 08:30:06 DATA: Worker received message: 265244 { image: 'example/sample (6).jpg' }
2020-12-08 08:30:07 DATA: Main: worker finished: 265238 detected faces: 5
2020-12-08 08:30:07 STATE: Main: worker exit: 265238 0
2020-12-08 08:30:08 DATA: Main: worker finished: 265244 detected faces: 4
2020-12-08 08:30:08 INFO: Processed 12 images in 6826 ms
2020-12-08 08:30:08 STATE: Main: worker exit: 265244 0
```
Note that `@tensorflow/tfjs-node` or `@tensorflow/tfjs-node-gpu` must be installed before using NodeJS example
< br >
< hr >
< br >
2020-08-27 14:58:00 +02:00
## Installation
2020-08-26 00:24:48 +02:00
2020-10-15 12:48:39 +02:00
Face-API ships with several pre-build versions of the library:
2020-12-02 23:24:40 +01:00
2021-02-21 16:00:57 +01:00
- `dist/face-api.js` : IIFE format for client-side Browser execution
*with* TFJS pre-bundled
- `dist/face-api.esm.js` : ESM format for client-side Browser execution
*with* TFJS pre-bundled
- `dist/face-api.esm-nobundle.js` : ESM format for client-side Browser execution
*without* TFJS pre-bundled
- `dist/face-api.node.js` : CommonJS format for server-side NodeJS execution
*without* TFJS pre-bundled
- `dist/face-api.node-gpu.js` : CommonJS format for server-side NodeJS execution
*without* TFJS pre-bundled and optimized for CUDA GPU acceleration
- `dist/face-api.node-cpu.js` : CommonJS format for server-side NodeJS execution
*without* TFJS pre-bundled and using JS engine for platforms where tensorflow binary library version is not available
2020-10-15 12:48:39 +02:00
Defaults are:
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
```json
{
2020-12-02 23:24:40 +01:00
"main": "dist/face-api.node-js",
2020-10-15 12:48:39 +02:00
"module": "dist/face-api.esm.js",
"browser": "dist/face-api.esm.js",
}
```
2020-12-02 22:46:41 +01:00
Bundled `TFJS` can be used directly via export: `faceapi.tf`
Reason for additional `nobundle` version is if you want to include a specific version of TFJS and not rely on pre-packaged one
2020-10-15 12:48:39 +02:00
`FaceAPI` is compatible with TFJS 2.0+
2020-12-02 22:46:41 +01:00
All versions include `sourcemap` and `asset manifest`
2020-12-08 14:33:00 +01:00
< br >
< hr >
2020-12-02 22:46:41 +01:00
< br >
2020-10-15 12:48:39 +02:00
2021-03-07 13:26:23 +01:00
There are several ways to use Face-API:
2020-09-09 15:29:24 +02:00
2020-10-11 18:50:50 +02:00
### 1. IIFE script
2020-10-13 22:57:06 +02:00
*Recommened for quick tests and backward compatibility with older Browsers that do not support ESM such as IE*
This is simplest way for usage within Browser
Simply download `dist/face-api.js` , include it in your `HTML` file & it's ready to use
2020-10-11 18:50:50 +02:00
```html
< script src = "dist/face-api.js" > < s c r i p t >
2021-03-07 13:26:23 +01:00
```
2020-10-11 18:50:50 +02:00
2020-10-15 12:48:39 +02:00
IIFE script bundles TFJS and auto-registers global namespace `faceapi` within Window object which can be accessed directly from a `<script>` tag or from your JS file.
2020-10-11 18:50:50 +02:00
2020-12-02 22:46:41 +01:00
< br >
2020-10-13 22:57:06 +02:00
### 2. ESM module
2020-10-11 18:50:50 +02:00
2020-10-13 22:57:06 +02:00
*Recommended for usage within Browser*
2020-10-11 18:50:50 +02:00
2020-10-15 12:48:39 +02:00
#### 2.1. Direct Import
2020-10-11 18:50:50 +02:00
2020-10-13 22:57:06 +02:00
To use ESM import directly in a Browser, you must import your script (e.g. `index.js` ) with a `type="module"`
```html
< script src = "./index.js" type = "module" >
```
2021-03-07 13:26:23 +01:00
2020-10-13 22:57:06 +02:00
and then in your `index.js`
2020-10-11 18:50:50 +02:00
```js
import * as faceapi from 'dist/face-api.esm.js';
```
2020-10-15 12:48:39 +02:00
#### 2.2. With Bundler
2020-10-13 22:57:06 +02:00
2020-12-02 22:46:41 +01:00
Same as above, but expectation is that you've installed `@vladmandic/faceapi` package:
2021-03-07 13:26:23 +01:00
2020-12-02 22:46:41 +01:00
```shell
npm install @vladmandic/face -api
```
and that you'll package your application using a bundler such as `webpack` , `rollup` or `esbuild`
2020-10-15 12:48:39 +02:00
in which case, you do not need to import a script as module - that depends on your bundler configuration
2020-10-11 18:50:50 +02:00
```js
2020-10-15 12:48:39 +02:00
import * as faceapi from '@vladmandic/face-api';
```
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
or if your bundler doesn't recognize `recommended` type, force usage with:
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
```js
import * as faceapi from '@vladmandic/face-api/dist/face-api.esm.js';
```
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
or to use non-bundled version
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
```js
import * as tf from `@tensorflow/tfjs` ;
2020-12-02 22:46:41 +01:00
import * as faceapi from '@vladmandic/face-api/dist/face-api.esm-nobundle.js';
2020-10-11 18:50:50 +02:00
```
2020-12-02 22:46:41 +01:00
< br >
2020-10-11 18:50:50 +02:00
### 3. NPM module
2020-10-15 12:48:39 +02:00
#### 3.1. Import CommonJS
2020-10-13 22:57:06 +02:00
*Recommended for NodeJS projects*
2020-10-11 18:50:50 +02:00
2020-12-02 22:46:41 +01:00
*Node: Face-API for NodeJS does not bundle TFJS due to binary dependencies that are installed during TFJS installation*
2020-10-11 18:50:50 +02:00
Install with:
2021-03-07 13:26:23 +01:00
2020-10-11 18:50:50 +02:00
```shell
2020-12-02 22:46:41 +01:00
npm install @tensorflow/tfjs -node
2020-10-13 22:57:06 +02:00
npm install @vladmandic/face -api
2020-10-11 18:50:50 +02:00
```
2021-03-07 13:26:23 +01:00
2020-10-11 18:50:50 +02:00
And then use with:
2021-03-07 13:26:23 +01:00
2020-10-11 18:50:50 +02:00
```js
2020-12-02 22:46:41 +01:00
const tf = require('@tensorflow/tfjs-node')
2020-10-15 12:48:39 +02:00
const faceapi = require('@vladmandic/face-api');
2020-10-11 18:50:50 +02:00
```
2020-12-02 22:46:41 +01:00
If you want to force CommonJS module instead of relying on `recommended` field:
2021-03-07 13:26:23 +01:00
2020-10-13 22:57:06 +02:00
```js
2020-10-27 21:35:40 +01:00
const faceapi = require('@vladmandic/face-api/dist/face-api.node.js');
2020-10-15 12:48:39 +02:00
```
2020-12-02 22:46:41 +01:00
If you want to GPU Accelerated execution in NodeJS, you must have CUDA libraries already installed and working
Then install appropriate version of `Face-API` :
2020-10-15 12:48:39 +02:00
```shell
2020-12-02 22:46:41 +01:00
npm install @tensorflow/tfjs -node
2020-10-15 12:48:39 +02:00
npm install @vladmandic/face -api
```
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
And then use with:
2021-03-07 13:26:23 +01:00
2020-10-15 12:48:39 +02:00
```js
2020-12-02 22:46:41 +01:00
const tf = require('@tensorflow/tfjs-node-gpu')
const faceapi = require('@vladmandic/face-api/dist/face-api.node-gpu.js'); // this loads face-api version with correct bindings for tfjs-node-gpu
2020-10-15 12:48:39 +02:00
```
2020-09-01 15:28:44 +02:00
2021-02-21 16:00:57 +01:00
If you want to use `Face-API` in a NodeJS on platforms where NodeJS binary libraries are not supported, you can use JavaScript CPU backend.
```shell
npm install @tensorflow/tfjs
npm install @vladmandic/face -api
```
2021-03-07 13:26:23 +01:00
2021-02-21 16:00:57 +01:00
And then use with:
2021-03-07 13:26:23 +01:00
2021-02-21 16:00:57 +01:00
```js
const tf = require('@tensorflow/tfjs')
2021-03-07 13:26:23 +01:00
const faceapi = require('@vladmandic/face-api/dist/face-api.node-cpu.js');
```
If you want to use graphical functions inside NodeJS, you must provide appropriate graphical library as NodeJS does not include implementation for DOM elements such as HTMLImageElement or HTMLCanvasElement:
Install `Canvas` for NodeJS:
```shell
npm install canvas
```
Patch NodeJS environment to use newly installed `Canvas` library:
```js
const canvas = require('canvas');
const faceapi = require('@vladmandic/face-api');
const { Canvas, Image, ImageData } = canvas
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })
2021-02-21 16:00:57 +01:00
```
2020-12-08 14:33:00 +01:00
< br >
< hr >
< br >
2020-08-26 00:24:48 +02:00
## Weights
2020-08-27 15:17:39 +02:00
Pretrained models and their weights are includes in `./model` .
2020-08-18 14:01:56 +02:00
2021-01-10 16:35:51 +01:00
< br >
< hr >
< br >
## Test & Dev Web Server
Built-in test& dev web server can be started using
2021-03-07 13:26:23 +01:00
2021-01-10 16:35:51 +01:00
```shell
npm run dev
```
By default it starts HTTP server on port 8000 and HTTPS server on port 8001 and can be accessed as:
2021-03-07 13:26:23 +01:00
2021-01-10 16:35:51 +01:00
- < https: // localhost:8001 / example / index . html >
- < https: // localhost:8001 / example / webcam . html >
```json
2021-01-10 08:39:00 INFO: @vladmandic/face -api version 0.10.2
2021-01-10 08:39:00 INFO: User: vlado Platform: linux Arch: x64 Node: v15.4.0
2021-01-10 08:39:00 INFO: Build: file startup all target: es2018
2021-01-10 08:39:00 STATE: HTTP server listening: 8000
2021-01-10 08:39:00 STATE: HTTP2 server listening: 8001
2021-01-10 08:39:00 STATE: Monitoring: [ 'package.json', 'config.js', 'example', 'src', [length]: 4 ]
2021-01-10 08:39:00 STATE: Monitoring: [ 'package.json', 'config.js', 'example', 'src', [length]: 4 ]
2021-01-10 08:39:01 STATE: Build for: browserBundle type: tfjs: { modules: 1253, moduleBytes: 3997175, imports: 7, importBytes: 276, outputBytes: 1565414, outputFiles: 'dist/tfjs.esm.js' }
2021-01-10 08:39:01 STATE: Build for: browserBundle type: iife: { imports: 160, importBytes: 1797487, outputBytes: 1699552, outputFiles: 'dist/face-api.js' }
2021-01-10 08:39:01 STATE: Build for: browserBundle type: esm: { imports: 160, importBytes: 1797487, outputBytes: 1697086, outputFiles: 'dist/face-api.esm.js' }
2021-01-10 08:39:01 INFO: Compile: [ 'src/index.ts', [length]: 1 ]
```
< br >
< hr >
2020-12-08 14:33:00 +01:00
< br >
2020-08-18 14:01:56 +02:00
## Build
2020-10-11 18:41:17 +02:00
If you want to do a full rebuild, either download npm module
2021-03-07 13:26:23 +01:00
2020-10-11 18:41:17 +02:00
```shell
npm install @vladmandic/face -api
cd node_modules/@vladmandic/face-api
```
2020-08-31 00:45:06 +02:00
2020-10-11 18:41:17 +02:00
or clone a git project
2021-03-07 13:26:23 +01:00
2020-10-11 18:41:17 +02:00
```shell
git clone https://github.com/vladmandic/face-api
cd face-api
```
2020-08-18 14:01:56 +02:00
2020-10-11 18:41:17 +02:00
Then install all dependencies and run rebuild:
2021-03-07 13:26:23 +01:00
2020-08-18 14:01:56 +02:00
```shell
2020-10-11 18:41:17 +02:00
npm install
2020-08-18 14:01:56 +02:00
npm run build
```
2020-12-02 22:46:41 +01:00
Build process uses script `build.js` that creates optimized build for each target:
2020-08-18 13:54:53 +02:00
2020-12-02 22:46:41 +01:00
```text
npm run build
> @vladmandic/face-api@0.8.9 build /home/vlado/dev/face-api
> rimraf dist/* && node ./build.js
```
2021-01-10 16:35:51 +01:00
2020-12-02 22:46:41 +01:00
```json
2021-01-10 16:35:51 +01:00
2021-01-10 08:42:01 INFO: @vladmandic/face -api version 0.10.2
2021-01-10 08:42:01 INFO: User: vlado Platform: linux Arch: x64 Node: v15.4.0
2021-01-10 08:42:01 INFO: Build: file startup all target: es2018
2021-01-10 08:42:01 STATE: Build for: node type: tfjs: { imports: 1, importBytes: 143, outputBytes: 1042, outputFiles: 'dist/tfjs.esm.js' }
2021-01-10 08:42:01 STATE: Build for: node type: node: { imports: 160, importBytes: 233115, outputBytes: 132266, outputFiles: 'dist/face-api.node.js' }
2021-01-10 08:42:01 STATE: Build for: nodeGPU type: tfjs: { imports: 1, importBytes: 147, outputBytes: 1046, outputFiles: 'dist/tfjs.esm.js' }
2021-01-10 08:42:01 STATE: Build for: nodeGPU type: node: { imports: 160, importBytes: 233119, outputBytes: 132274, outputFiles: 'dist/face-api.node-gpu.js' }
2021-01-10 08:42:01 STATE: Build for: browserNoBundle type: tfjs: { imports: 1, importBytes: 276, outputBytes: 244, outputFiles: 'dist/tfjs.esm.js' }
2021-01-10 08:42:01 STATE: Build for: browserNoBundle type: esm: { imports: 160, importBytes: 232317, outputBytes: 129069, outputFiles: 'dist/face-api.esm-nobundle.js' }
2021-01-10 08:42:01 STATE: Build for: browserBundle type: tfjs: { modules: 1253, moduleBytes: 3997175, imports: 7, importBytes: 276, outputBytes: 1565414, outputFiles: 'dist/tfjs.esm.js' }
2021-01-10 08:42:02 STATE: Build for: browserBundle type: iife: { imports: 160, importBytes: 1797487, outputBytes: 1699552, outputFiles: 'dist/face-api.js' }
2021-01-10 08:42:02 STATE: Build for: browserBundle type: esm: { imports: 160, importBytes: 1797487, outputBytes: 1697086, outputFiles: 'dist/face-api.esm.js' }
2021-01-10 08:42:02 INFO: Compile: [ 'src/index.ts', [length]: 1 ]```
2020-12-02 22:46:41 +01:00
```
2020-08-18 13:54:53 +02:00
2020-12-08 14:33:00 +01:00
< br >
< hr >
2020-12-02 22:46:41 +01:00
< br >
2021-03-07 13:26:23 +01:00
## Face Mesh
2021-03-07 15:58:20 +01:00
`FaceAPI` landmark model returns 68-point face mesh as detailed in the image below:
2021-03-07 13:26:23 +01:00

## Documentation
- [**Tutorial** ](TUTORIAL.md )
- [**API Documentation** ](https://justadudewhohacks.github.io/face-api.js/docs/globals.html )
## Credits
2020-12-02 22:46:41 +01:00
2021-03-07 13:26:23 +01:00
- Original project: [Face-API ](https://github.com/justadudewhohacks/face-api.js )
2020-12-02 22:46:41 +01:00
- Original model weighs: [Face-API ](https://github.com/justadudewhohacks/face-api.js-models )
- ML API Documentation: [Tensorflow/JS ](https://js.tensorflow.org/api/latest/ )
2021-03-08 20:26:04 +01:00
< br >


