human/README.md

418 lines
16 KiB
Markdown
Raw Normal View History

2020-10-15 14:16:34 +02:00
# Human Library
## 3D Face Detection, Body Pose, Hand & Finger Tracking, Iris Tracking, Age & Gender Prediction & Emotion Prediction
2020-10-12 01:22:43 +02:00
2020-10-14 17:43:33 +02:00
- [**Documentation**](https://github.com/vladmandic/human#readme)
- [**Code Repository**](https://github.com/vladmandic/human)
2020-10-15 14:16:34 +02:00
- [**NPM Package**](https://www.npmjs.com/package/@vladmandic/human)
2020-10-14 17:43:33 +02:00
- [**Issues Tracker**](https://github.com/vladmandic/human/issues)
- [**Live Demo**](https://vladmandic.github.io/human/demo/demo-esm.html)
2020-10-12 01:22:43 +02:00
2020-10-16 21:13:29 +02:00
Compatible with Browser, WebWorker and NodeJS execution!
(and maybe with React-Native as it doesn't use any DOM objects)
2020-10-14 17:43:33 +02:00
*This is a pre-release project, see [issues](https://github.com/vladmandic/human/issues) for list of known limitations*
2020-10-14 02:52:30 +02:00
2020-10-12 01:22:43 +02:00
*Suggestions are welcome!*
2020-10-13 16:06:49 +02:00
<hr>
2020-10-16 21:04:51 +02:00
## Examples
2020-10-13 16:06:49 +02:00
2020-10-16 21:04:51 +02:00
**Using static images:**
![Example Using Image](assets/screenshot1.jpg)
**Using webcam:**
![Example Using WebCam](assets/screenshot2.jpg)
2020-10-12 16:59:55 +02:00
2020-10-12 16:08:00 +02:00
<hr>
## Installation
**Important**
2020-10-12 20:33:07 +02:00
*The packaged (IIFE and ESM) version of `Human` includes `TensorFlow/JS (TFJS) 2.6.0` library which can be accessed via `human.tf`*
*You should NOT manually load another instance of `tfjs`, but if you do, be aware of possible version conflicts*
2020-10-12 16:08:00 +02:00
2020-10-12 16:27:22 +02:00
There are multiple ways to use `Human` library, pick one that suits you:
2020-10-15 14:16:34 +02:00
### Included
- `dist/human.js`: IIFE format minified bundle with TFJS for Browsers
- `dist/human.esm.js`: ESM format minified bundle with TFJS for Browsers
- `dist/human.esm-nobundle.js`: ESM format non-minified bundle without TFJS for Browsers
- `dist/human.cjs`: CommonJS format minified bundle with TFJS for NodeJS
- `dist/human-nobundle.cjs`: CommonJS format non-minified bundle without TFJS for NodeJS
All versions include `sourcemap`
Defaults:
```json
{
"main": "dist/human.cjs",
"module": "dist/human.esm.js",
"browser": "dist/human.esm.js",
}
```
2020-10-12 20:41:50 +02:00
### 1. [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) script
*Simplest way for usage within Browser*
2020-10-12 16:08:00 +02:00
Simply download `dist/human.js`, include it in your `HTML` file & it's ready to use.
```html
2020-10-14 17:43:33 +02:00
<script src="dist/human.js"><script>
2020-10-12 16:08:00 +02:00
```
2020-10-12 20:41:50 +02:00
IIFE script auto-registers global namespace `human` within global `Window` object
This way you can also use `Human` library within embbedded `<script>` tag within your `html` page for all-in-one approach
### 2. [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) module
2020-10-12 01:22:43 +02:00
2020-10-12 20:41:50 +02:00
*Recommended for usage within `Browser`*
2020-10-12 16:08:00 +02:00
#### 2.1 With Bundler
2020-10-12 17:05:29 +02:00
If you're using bundler *(such as rollup, webpack, esbuild)* to package your client application, you can import ESM version of `Human` library which supports full tree shaking
2020-10-12 16:08:00 +02:00
2020-10-15 14:16:34 +02:00
Install with:
```shell
npm install @vladmandic/human
```
2020-10-12 16:08:00 +02:00
```js
2020-10-14 17:43:33 +02:00
import human from '@vladmandic/human'; // points to @vladmandic/human/dist/human.esm.js
2020-10-12 16:08:00 +02:00
```
2020-10-14 17:43:33 +02:00
Or if you prefer to package your version of `tfjs`, you can use `nobundle` version
2020-10-15 14:16:34 +02:00
Install with:
```shell
npm install @vladmandic/human @tensorflow/tfjs-node
```
2020-10-14 17:43:33 +02:00
```js
import tf from '@tensorflow/tfjs'
2020-10-15 14:16:34 +02:00
import human from '@vladmandic/human/dist/human.esm-nobundle.js'; // same functionality as default import, but without tfjs bundled
2020-10-14 17:43:33 +02:00
```
2020-10-12 16:08:00 +02:00
#### 2.2 Using Script Module
You could use same syntax within your main `JS` file if it's imported with `<script type="module">`
```html
<script src="./index.js" type="module">
```
and then in your `index.js`
```js
2020-10-15 14:16:34 +02:00
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 human from 'dist/human.esm.js'; // for direct import must use path to module, not package name
2020-10-12 16:08:00 +02:00
```
2020-10-12 20:41:50 +02:00
### 3. [NPM](https://www.npmjs.com/) module
2020-10-12 16:08:00 +02:00
2020-10-14 02:59:09 +02:00
*Recommended for `NodeJS` projects that will execute in the backend*
2020-10-12 20:33:07 +02:00
2020-10-14 02:59:09 +02:00
Entry point is bundle in CJS format `dist/human.node.js`
You also need to install and include `tfjs-node` or `tfjs-node-gpu` in your project so it can register an optimized backend
2020-10-12 16:08:00 +02:00
Install with:
2020-10-12 01:22:43 +02:00
```shell
2020-10-15 14:16:34 +02:00
npm install @vladmandic/human
```
And then use with:
```js
const human = require('@vladmandic/human'); // points to @vladmandic/human/dist/human.cjs
```
or
```shell
npm install @vladmandic/human @tensorflow/tfjs-node
2020-10-12 16:08:00 +02:00
```
And then use with:
```js
2020-10-15 14:16:34 +02:00
const tf = require('@tensorflow/tfjs-node'); // can also use '@tensorflow/tfjs-node-gpu' if you have environment with CUDA extensions
const human = require('@vladmandic/human/dist/human-nobundle.cjs');
2020-10-14 17:43:33 +02:00
```
2020-10-15 14:16:34 +02:00
2020-10-14 17:43:33 +02:00
Since NodeJS projects load `weights` from local filesystem instead of using `http` calls, you must modify default configuration to include correct paths with `file://` prefix
For example:
```js
const config = {
body: { enabled: true, modelPath: 'file://models/posenet/model.json' },
}
2020-10-12 01:22:43 +02:00
```
2020-10-14 02:52:30 +02:00
2020-10-14 17:43:33 +02:00
Note that when using `Human` in NodeJS, you must load and parse the image *before* you pass it for detection
For example:
```js
const buffer = fs.readFileSync(input);
const image = tf.node.decodeImage(buffer);
const result = human.detect(image, config);
image.dispose();
```
2020-10-12 01:22:43 +02:00
2020-10-12 16:08:00 +02:00
### Weights
2020-10-12 16:31:36 +02:00
Pretrained model weights are includes in `./models`
Default configuration uses relative paths to you entry script pointing to `../models`
If your application resides in a different folder, modify `modelPath` property in configuration of each module
2020-10-12 16:08:00 +02:00
<hr>
2020-10-12 01:22:43 +02:00
## Demo
2020-10-12 16:08:00 +02:00
Demos are included in `/demo`:
2020-10-12 01:22:43 +02:00
2020-10-14 02:52:30 +02:00
Browser:
2020-10-15 15:43:16 +02:00
- `demo-esm`: Full demo using Browser with ESM module, includes selectable backends and webworkers
- `demo-iife`: Older demo using Browser with IIFE module
2020-10-12 01:22:43 +02:00
2020-10-14 02:52:30 +02:00
NodeJS:
- `demo-node`: Demo using NodeJS with CJS module
This is a very simple demo as althought `Human` library is compatible with NodeJS execution
and is able to load images and models from local filesystem,
2020-10-12 16:08:00 +02:00
<hr>
2020-10-12 01:22:43 +02:00
## Usage
`Human` library does not require special initialization.
All configuration is done in a single JSON object and all model weights will be dynamically loaded upon their first usage(and only then, `Human` will not load weights that it doesn't need according to configuration).
There is only *ONE* method you need:
```js
2020-10-14 17:43:33 +02:00
import * as tf from '@tensorflow/tfjs';
import human from '@vladmandic/human';
2020-10-12 01:22:43 +02:00
2020-10-14 17:43:33 +02:00
// 'image': can be of any type of an image object: HTMLImage, HTMLVideo, HTMLMedia, Canvas, Tensor4D
// 'options': optional parameter used to override any options present in default configuration
const result = await human.detect(image, options?)
```
or if you want to use promises
```js
human.detect(image, options?).then((result) => {
// your code
})
2020-10-12 01:22:43 +02:00
```
2020-10-12 16:08:00 +02:00
Additionally, `Human` library exposes several classes:
2020-10-12 01:22:43 +02:00
```js
2020-10-15 21:25:58 +02:00
human.config // access to configuration object, normally set as parameter to detect()
human.defaults // read-only view of default configuration object
human.models // dynamically maintained list of object of any loaded models
2020-10-14 17:43:33 +02:00
human.tf // instance of tfjs used by human
2020-10-12 01:22:43 +02:00
```
2020-10-12 16:08:00 +02:00
<hr>
2020-10-12 01:22:43 +02:00
## Configuration
Below is output of `human.defaults` object
Any property can be overriden by passing user object during `human.detect()`
Note that user object and default configuration are merged using deep-merge, so you do not need to redefine entire configuration
2020-10-16 21:04:51 +02:00
Configurtion object is large, but typically you only need to modify few values:
- `enabled`: Choose which models to use
- `skipFrames`: Must be set to 0 for static images
- `modelPath`: Update as needed to reflect your application's relative path
2020-10-12 01:22:43 +02:00
```js
2020-10-16 21:04:51 +02:00
export default {
backend: 'webgl', // select tfjs backend to use
console: true, // enable debugging output to console
2020-10-12 01:22:43 +02:00
face: {
2020-10-16 21:04:51 +02:00
enabled: true, // controls if specified modul is enabled
// face.enabled is required for all face models: detector, mesh, iris, age, gender, emotion
// note: module is not loaded until it is required
2020-10-12 01:22:43 +02:00
detector: {
2020-10-16 21:04:51 +02:00
modelPath: '../models/blazeface/back/model.json', // can be 'tfhub', 'front' or 'back'.
// 'front' is optimized for large faces such as front-facing camera and 'back' is optimized for distanct faces.
inputSize: 256, // fixed value: 128 for front and 'tfhub' and 'front' and 256 for 'back'
maxFaces: 10, // maximum number of faces detected in the input, should be set to the minimum number for performance
skipFrames: 10, // how many frames to go without re-running the face bounding box detector
// if model is running st 25 FPS, we can re-use existing bounding box for updated face mesh analysis
// as face probably hasn't moved much in short time (10 * 1/25 = 0.25 sec)
minConfidence: 0.5, // threshold for discarding a prediction
iouThreshold: 0.3, // threshold for deciding whether boxes overlap too much in non-maximum suppression
scoreThreshold: 0.7, // threshold for deciding when to remove boxes based on score in non-maximum suppression
2020-10-12 01:22:43 +02:00
},
mesh: {
enabled: true,
2020-10-12 20:46:08 +02:00
modelPath: '../models/facemesh/model.json',
2020-10-16 21:04:51 +02:00
inputSize: 192, // fixed value
2020-10-12 01:22:43 +02:00
},
iris: {
enabled: true,
2020-10-12 20:46:08 +02:00
modelPath: '../models/iris/model.json',
2020-10-16 21:04:51 +02:00
enlargeFactor: 2.3, // empiric tuning
inputSize: 64, // fixed value
2020-10-12 01:22:43 +02:00
},
age: {
enabled: true,
2020-10-16 02:20:37 +02:00
modelPath: '../models/ssrnet-age/imdb/model.json', // can be 'imdb' or 'wiki'
2020-10-16 21:04:51 +02:00
// which determines training set for model
inputSize: 64, // fixed value
skipFrames: 10, // how many frames to go without re-running the detector
2020-10-12 01:22:43 +02:00
},
gender: {
enabled: true,
2020-10-16 21:04:51 +02:00
minConfidence: 0.8, // threshold for discarding a prediction
modelPath: '../models/ssrnet-gender/imdb/model.json',
2020-10-12 01:22:43 +02:00
},
2020-10-15 00:22:38 +02:00
emotion: {
enabled: true,
2020-10-16 21:04:51 +02:00
inputSize: 64, // fixed value
minConfidence: 0.5, // threshold for discarding a prediction
skipFrames: 10, // how many frames to go without re-running the detector
useGrayscale: true, // convert image to grayscale before prediction or use highest channel
2020-10-15 00:22:38 +02:00
modelPath: '../models/emotion/model.json',
},
2020-10-12 01:22:43 +02:00
},
body: {
enabled: true,
2020-10-12 20:46:08 +02:00
modelPath: '../models/posenet/model.json',
2020-10-16 21:04:51 +02:00
inputResolution: 257, // fixed value
outputStride: 16, // fixed value
maxDetections: 10, // maximum number of people detected in the input, should be set to the minimum number for performance
scoreThreshold: 0.7, // threshold for deciding when to remove boxes based on score in non-maximum suppression
nmsRadius: 20, // radius for deciding points are too close in non-maximum suppression
2020-10-12 01:22:43 +02:00
},
hand: {
enabled: true,
2020-10-16 21:04:51 +02:00
inputSize: 256, // fixed value
skipFrames: 10, // how many frames to go without re-running the hand bounding box detector
// if model is running st 25 FPS, we can re-use existing bounding box for updated hand skeleton analysis
// as face probably hasn't moved much in short time (10 * 1/25 = 0.25 sec)
minConfidence: 0.5, // threshold for discarding a prediction
iouThreshold: 0.3, // threshold for deciding whether boxes overlap too much in non-maximum suppression
scoreThreshold: 0.7, // threshold for deciding when to remove boxes based on score in non-maximum suppression
enlargeFactor: 1.65, // empiric tuning as skeleton prediction prefers hand box with some whitespace
maxHands: 10, // maximum number of hands detected in the input, should be set to the minimum number for performance
2020-10-12 01:22:43 +02:00
detector: {
2020-10-12 20:46:08 +02:00
anchors: '../models/handdetect/anchors.json',
modelPath: '../models/handdetect/model.json',
2020-10-12 01:22:43 +02:00
},
skeleton: {
2020-10-12 20:46:08 +02:00
modelPath: '../models/handskeleton/model.json',
2020-10-12 01:22:43 +02:00
},
},
};
```
2020-10-12 16:08:00 +02:00
<hr>
2020-10-12 01:22:43 +02:00
## Outputs
Result of `humand.detect()` is a single object that includes data for all enabled modules and all detected objects:
```js
result = {
2020-10-15 21:25:58 +02:00
version: // <string> version string of the human library
2020-10-14 02:52:30 +02:00
face: // <array of detected objects>
2020-10-12 01:22:43 +02:00
[
{
2020-10-14 02:52:30 +02:00
confidence, // <number>
box, // <array [x, y, width, height]>
2020-10-15 00:22:38 +02:00
mesh, // <array of 3D points [x, y, z]> 468 base points & 10 iris points
annotations, // <list of object { landmark: array of points }> 32 base annotated landmarks & 2 iris annotations
iris, // <number> relative distance of iris to camera, multiple by focal lenght to get actual distance
age, // <number> estimated age
gender, // <string> 'male', 'female'
2020-10-12 01:22:43 +02:00
}
],
2020-10-14 02:52:30 +02:00
body: // <array of detected objects>
2020-10-12 01:22:43 +02:00
[
{
2020-10-14 02:52:30 +02:00
score, // <number>,
2020-10-15 00:22:38 +02:00
keypoints, // <array of 2D landmarks [ score, landmark, position [x, y] ]> 17 annotated landmarks
2020-10-12 01:22:43 +02:00
}
],
hand: // <array of detected objects>
[
2020-10-12 17:05:29 +02:00
{
2020-10-14 02:52:30 +02:00
confidence, // <number>,
box, // <array [x, y, width, height]>,
2020-10-15 00:22:38 +02:00
landmarks, // <array of 3D points [x, y,z]> 21 points
annotations, // <array of 3D landmarks [ landmark: <array of points> ]> 5 annotated landmakrs
}
],
emotion: // <array of emotions>
[
{
score, // <number> probabily of emotion
emotion, // <string> 'angry', 'discust', 'fear', 'happy', 'sad', 'surpise', 'neutral'
2020-10-12 17:05:29 +02:00
}
2020-10-14 02:52:30 +02:00
],
2020-10-15 21:25:58 +02:00
performance = { // performance data of last execution for each module measuredin miliseconds
body,
hand,
face,
agegender,
2020-10-15 00:22:38 +02:00
emotion,
total,
}
2020-10-15 21:25:58 +02:00
}
```
2020-10-12 16:08:00 +02:00
<hr>
2020-10-12 17:19:52 +02:00
## Build
If you want to modify the library and perform a full rebuild:
2020-10-12 20:46:08 +02:00
2020-10-12 17:19:52 +02:00
*clone repository, install dependencies, check for errors and run full rebuild from which creates bundles from `/src` into `/dist`:*
```shell
git clone https://github.com/vladmandic/human
cd human
npm install # installs all project dependencies
npm run lint
npm run build
```
2020-10-12 20:46:08 +02:00
Project is written in pure `JavaScript` [ECMAScript version 2020](https://www.ecma-international.org/ecma-262/11.0/index.html)
2020-10-12 17:19:52 +02:00
2020-10-12 20:46:08 +02:00
Only project depdendency is [@tensorflow/tfjs](https://github.com/tensorflow/tfjs)
Development dependencies are [eslint](https://github.com/eslint) used for code linting and [esbuild](https://github.com/evanw/esbuild) used for IIFE and ESM script bundling
2020-10-12 17:19:52 +02:00
<hr>
2020-10-12 01:22:43 +02:00
## Performance
2020-10-12 17:05:29 +02:00
Performance will vary depending on your hardware, but also on number of resolution of input video/image, enabled modules as well as their parameters
2020-10-12 17:19:52 +02:00
For example, on a desktop with a low-end nVidia GTX1050 it can perform multiple face detections at 60+ FPS, but drops to 10 FPS on a medium complex images if all modules are enabled
Performance per module:
- Enabled all: 10 FPS
2020-10-16 02:20:37 +02:00
- Face Detect: 80 FPS (standalone)
- Face Geometry: 30 FPS (includes face detect)
- Face Iris: 25 FPS (includes face detect and face geometry)
- Age: 60 FPS (includes face detect)
- Gender: 60 FPS (includes face detect)
2020-10-15 00:22:38 +02:00
- Emotion: 60 FPS (includes face detect)
2020-10-16 02:20:37 +02:00
- Hand: 40 FPS (standalone)
- Body: 50 FPS (standalone)
2020-10-12 17:19:52 +02:00
2020-10-12 17:05:29 +02:00
Library can also be used on mobile devices
2020-10-12 01:22:43 +02:00
2020-10-12 16:08:00 +02:00
<hr>
2020-10-15 00:22:38 +02:00
## Credits
- Face Detection: [**MediaPipe BlazeFace**](https://drive.google.com/file/d/1f39lSzU5Oq-j_OXgS67KfN5wNsoeAZ4V/view)
- Facial Spacial Geometry: [**MediaPipe FaceMesh**](https://drive.google.com/file/d/1VFC_wIpw4O7xBOiTgUldl79d9LA-LsnA/view)
- Eye Iris Details: [**MediaPipe Iris**](https://drive.google.com/file/d/1bsWbokp9AklH2ANjCfmjqEzzxO1CNbMu/view)
- Hand Detection & Skeleton: [**MediaPipe HandPose**](https://drive.google.com/file/d/1sv4sSb9BSNVZhLzxXJ0jBv9DqD-4jnAz/view)
- Body Pose Detection: [**PoseNet**](https://medium.com/tensorflow/real-time-human-pose-estimation-in-the-browser-with-tensorflow-js-7dd0bc881cd5)
- Age & Gender Prediction: [**SSR-Net**](https://github.com/shamangary/SSR-Net)
- Emotion Prediction: [**Oarriaga**](https://github.com/oarriaga/face_classification)
<hr>