




# FaceAPI
**Face detection and recognition libary for Browser and NodeJS implemented on top of TensorFlow/JS**
**Live Demo**:
## Note
This is updated **face-api.js** with latest available TensorFlow/JS as the original face-api.js is not compatible with **tfjs 2.0+**.
Forked from [face-api.js](https://github.com/justadudewhohacks/face-api.js) version **0.22.2** which was released on March 22nd, 2020
Currently based on **`TensorFlow/JS` 3.3.0**
### Why?
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
### Differences
- Compatible with `TensorFlow/JS 2.0+ & 3.0+`
- Compatible with `WebGL`, `CPU` and `WASM` TFJS Browser backends
- Compatible with both `tfjs-node` and `tfjs-node-gpu` TFJS NodeJS backends
- Updated all type castings for TypeScript type checking to `TypeScript 4.1`
- Switched bundling from `UMD` to `ESM` + `CommonJS` with fallback to `IIFE`
Resulting code is optimized per-platform instead of being universal
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
- Removed old tests, docs, examples
- Removed old package dependencies (`karma`, `jasmine`, `babel`, etc.)
- Updated all package dependencies
- 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
- Added test/dev built-in HTTP & HTTPS Web server
- 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.*
- Added `face angle` calculations that returns `roll`, `yaw` and `pitch`
Which means valid models are **tinyFaceDetector** and **mobileNetv1**
## Examples
### Browser
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:
Browser example that uses live webcam is included in `/example/webcam.html`
Example can be accessed directly using Git pages using URL:
*Note: Photos shown below are taken by me*

### 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
## Installation
Face-API ships with several pre-build versions of the library:
- `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
Defaults are:
```json
{
"main": "dist/face-api.node-js",
"module": "dist/face-api.esm.js",
"browser": "dist/face-api.esm.js",
}
```
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
`FaceAPI` is compatible with TFJS 2.0+
All versions include `sourcemap`
There are several ways to use Face-API:
### 1. IIFE script
*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
```html