2020-08-18 13:54:53 +02:00
# FaceAPI
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
2020-08-22 18:13:02 +02:00
Forked from **face-api.js** version **0.22.2** released on March 22nd, 2020
- < https: // github . com / justadudewhohacks / face-api . js >
- < https: // www . npmjs . com / package / face-api . js >
2020-08-18 13:54:53 +02:00
2020-10-13 22:57:06 +02:00
Currently based on ** `TensorFlow/JS` 2.6.0**
If you want to access `TFJS` classes and methods directly, they are exported as `faceapi.tf`
2020-10-15 12:48:39 +02:00
### Why?
Because I needed FaceAPI that does not cause version conflict with newer TFJS 2.0 that I use accross my projects
And since original FaceAPI was open-source, I've released this version as well
Unfortunately, changes ended up being too large for a simple pull request on original FaceaPI and it ended up being a full-fledged version on its own
### Differences
- Compatible with `TensorFlow/JS 2.0+`
- Updated all type castings for TypeScript type checking to `TypeScript 4.1`
- Switched bundling from `UMD` to `ESM` + `CommonJS`
This does require separate process for usage in NodeJS vs Browser, but resulting code is much lighter
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
- 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.*
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
## 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:
- `dist/face-api.js` : IIFE format for client-side Browser exeuction
- `dist/face-api.esm.js` : ESM format for client-side Browser execution with TFJS pre-bundled
- `dist/face-api.nobundle.js` : ESM format for client-side Browser execution without TFJS and not minified
- `dist/face-api.cjs` : CommonJS format for server-side NodeJS execution with TFJS pre-bundled
- `dist/face-api.nobundle.cjs` : CommonJS format for server-side NodeJS execution without TFJS and not minified
Defaults are:
```json
{
"main": "dist/face-api.cjs",
"module": "dist/face-api.esm.js",
"browser": "dist/face-api.esm.js",
}
```
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+
Bundled versions are ~1.1MB minified and non-bundled versions are ~169KB non-minified
All versions include `sourcemap`
2020-10-11 18:41:17 +02: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 >
```
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-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" >
```
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
or to use non-bundled version:
```js
import * as tf from `https://cdnjs.cloudflare.com/ajax/libs/tensorflow/2.6.0/tf.es2017.min.js` ; // load tfjs directly from CDN link
import * as faceapi from 'dist/face-api.nobundle.js';
```
2020-10-11 18:50:50 +02:00
2020-10-15 12:48:39 +02:00
#### 2.2. With Bundler
2020-10-13 22:57:06 +02:00
2020-10-15 12:48:39 +02:00
Same as above, but expectation is that you've installed `@vladmandic/faceapi` package
and that you'll package your script using a bundler such as `webpack` , `rollup` or `esbuild`
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';
```
or if your bundler doesn't recognize `recommended` type, force usage with:
```js
import * as faceapi from '@vladmandic/face-api/dist/face-api.esm.js';
```
or to use non-bundled version
```js
import * as tf from `@tensorflow/tfjs` ;
import * as faceapi from '@vladmandic/face-api/dist/face-api.nobundle.js';
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
Install with:
```shell
2020-10-13 22:57:06 +02:00
npm install @vladmandic/face -api
2020-10-11 18:50:50 +02:00
```
And then use with:
```js
2020-10-15 12:48:39 +02:00
const faceapi = require('@vladmandic/face-api');
2020-10-11 18:50:50 +02:00
```
2020-10-15 12:48:39 +02:00
or if you want to force CommonJS module instead of relying on `recommended` field:
2020-10-13 22:57:06 +02:00
```js
2020-10-15 12:48:39 +02:00
const faceapi = require('@vladmandic/face-api/dist/face-api.cjs');
```
or if you want to use a non-bundled version:
Install with:
```shell
npm install @tensorflow/tfjs
npm install @vladmandic/face -api
```
And then use with:
```js
const tf = require('@tensorflow/tfjs');
const faceapi = require('@vladmandic/face-api/dist/face-api.nobundle.cjs');
2020-10-13 22:57:06 +02:00
```
2020-10-15 12:48:39 +02:00
### 4. Import Sources
2020-10-13 22:57:06 +02:00
*Recommended for complex NodeJS projects that use TFJS for other purposes and not just FaceaPI*
This way you're importing FaceAPI sources directly and not a bundle, so you have to import `@tensorflow/tfjs` explicitly
2020-10-15 12:48:39 +02:00
#### 4.1. For Browser with Bundler
##### 4.1.1. For JavaScript projects
2020-10-13 22:57:06 +02:00
```js
import * as tf from '@tensorflow/tfjs';
import * as faceapi from '@vladmandic/face-api/build/index.js';
```
2020-10-15 12:48:39 +02:00
##### 4.1.2. For TypeScript projects
2020-10-13 22:57:06 +02:00
```js
import * as tf from '@tensorflow/tfjs';
import * as faceapi from '@vladmandic/face-api/src/index.ts';
```
2020-10-15 12:48:39 +02:00
#### 4.2. For NodeJS
##### 4.2.1. For JavaScript projects
```js
const tf = require('@tensorflow/tfjs');
const faceapi = require('@vladmandic/face-api/build/index.js');
```
##### 4.1.2. For TypeScript projects
```js
const tf = require('@tensorflow/tfjs');
const faceapi = require('@vladmandic/face-api/src/index.ts');
```
2020-09-01 15:28:44 +02:00
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
## Build
2020-10-11 18:41:17 +02:00
If you want to do a full rebuild, either download npm module
```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
```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:
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-10-11 18:41:17 +02:00
Which will compile everything in `./src` into `./build` and create both ESM (standard) and IIFE (minified) bundles as well as sourcemaps in `./dist`
2020-08-18 13:54:53 +02:00
## Documentation
2020-08-27 14:58:00 +02:00
For documentation refer to original project at < https: / / github . com / justadudewhohacks / face-api . js >
2020-10-15 12:48:39 +02:00
For original weighs refer to < https: / / github . com / justadudewhohacks / face-api . js-models >
2020-08-27 14:58:00 +02:00
## Example
Single new example that uses both models as well as all of the extensions is included in `/example/index.html`
2020-08-27 15:19:03 +02:00
Example can be accessed directly using Git pages using URL: < https: / / vladmandic . github . io / face-api / example / >
2020-08-27 14:58:00 +02:00
2020-09-09 15:29:24 +02:00
*Note: Photos shown below are taken by me*
2020-08-27 14:58:00 +02:00
