Skip to content

Commit 01adb56

Browse files
updated readme
1 parent e781c56 commit 01adb56

File tree

1 file changed

+122
-30
lines changed

1 file changed

+122
-30
lines changed

README.md

Lines changed: 122 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,137 @@
11
# face-api.js
22

3-
**JavaScript API for face detection and face recognition in the browser with tensorflow.js**
3+
**JavaScript API for face detection and face recognition in the browser implemented on top of the tensorflow.js core API ([tensorflow/tfjs-core](https://github.com/tensorflow/tfjs-core))**
44

5-
This project implements a ResNet-34 like architecture using the tensorflow.js core API ([@tensorflow/tfjs-core](https://github.com/tensorflow/tfjs-core)) for realtime face recognition in the browser. The neural net is equivalent to the **FaceRecognizerNet** used in [face-recognition.js](https://github.com/justadudewhohacks/face-recognition.js) and the net used in the [dlib](https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp) face recognition example. The weights have been trained by [davisking](https://github.com/davisking) and the model achieves a prediction accuracy of 99.38% on the LFW (Labeled Faces in the Wild) benchmark for face recognition.
5+
* **[Running the Examples](#running-the-examples)**
6+
* **[About Face Detection](#about-face-detection)**
7+
* **[About Face Recognition](#about-face-recognition)**
8+
* **[Usage](#usage)**
69

7-
## What does it do?
10+
## Examples
811

9-
The neural net computes a vector with 128 values (face descriptor) from any given face image, which is **not** limited to the set of faces used for training the model. You can determine the similarity of two arbitrary faces by comparing their face descriptors, for example by computing the euclidean distance or using any other classifier of your choice.
12+
### Face Detection
1013

11-
## Face Recognition
14+
![preview_face-detection](https://user-images.githubusercontent.com/31125521/41238639-b72fb0fc-6d96-11e8-9f50-27159dd534d1.jpg)
1215

13-
![preview_face-recognition](https://user-images.githubusercontent.com/31125521/40313021-c3afdfec-5d14-11e8-86df-cf89a00668e2.gif)
16+
### Live Video Face Detection
1417

15-
## Face Similarity
18+
![preview_video-facedetection](https://user-images.githubusercontent.com/31125521/41238649-bbf10046-6d96-11e8-9041-1de46c6adccd.jpg)
19+
20+
### Face Recognition
21+
22+
![preview_face-recognition](https://user-images.githubusercontent.com/31125521/41238648-bbd2cc52-6d96-11e8-9fd1-051745fa0128.jpg)
23+
24+
![preview_face-recognition_gif](https://user-images.githubusercontent.com/31125521/40313021-c3afdfec-5d14-11e8-86df-cf89a00668e2.gif)
25+
26+
### Face Similarity
1627

1728
![preview_face-similarity](https://user-images.githubusercontent.com/31125521/40316573-0a1190c0-5d1f-11e8-8797-f6deaa344523.gif)
1829

30+
### Extracting Face Images
31+
32+
![preview_face-extraction](https://user-images.githubusercontent.com/31125521/41238647-bbb64c6c-6d96-11e8-8ca9-2d0fda779bb6.jpg)
33+
34+
<a name="running-the-examples"></a>
35+
36+
## Running the Examples
37+
38+
``` bash
39+
cd examples
40+
npm i
41+
npm start
42+
```
43+
44+
Browse to http://localhost:3000/.
45+
46+
<a name="about-face-detection"></a>
47+
48+
## About Face Detection
49+
50+
For face recognition, this project implements a SSD (Single Shot Multibox Detector) based on MobileNetV1. The neural net will compute the locations of each face in an image and will return the bounding boxes together with it's probability for each face.
51+
52+
The face detection model has been trained on the [WIDERFACE dataset](http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/) and the weights are provided by [yeephycho](https://github.com/yeephycho) in [this](https://github.com/yeephycho/tensorflow-face-detection) repo.
53+
54+
<a name="about-face-recognition"></a>
55+
56+
## About Face Recognition
57+
58+
For face recognition, a ResNet-34 like architecture is implemented to compute a face descriptor (a feature vector with 128 values) from any given face image, which is used to describe the characteristics of a persons face. The model is **not** limited to the set of faces used for training, meaning you can use it for face recognition of any person, for example yourself. You can determine the similarity of two arbitrary faces by comparing their face descriptors, for example by computing the euclidean distance or using any other classifier of your choice.
59+
60+
The neural net is equivalent to the **FaceRecognizerNet** used in [face-recognition.js](https://github.com/justadudewhohacks/face-recognition.js) and the net used in the [dlib](https://github.com/davisking/dlib/blob/master/examples/dnn_face_recognition_ex.cpp) face recognition example. The weights have been trained by [davisking](https://github.com/davisking) and the model achieves a prediction accuracy of 99.38% on the LFW (Labeled Faces in the Wild) benchmark for face recognition.
61+
62+
<a name="usage"></a>
63+
1964
## Usage
2065

21-
Get the latest build from dist/face-recognition.min.js and include the script:
66+
Get the latest build from dist/face-api.js or dist/face-api.min.js and include the script:
2267

2368
``` html
24-
<script src="face-recognition.min.js"></script>
69+
<script src="face-api.js"></script>
70+
```
71+
72+
Or install the package:
73+
74+
``` bash
75+
npm i face-api
2576
```
2677

27-
Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file):
78+
### Face Detection
79+
80+
Download the weights file from your server and initialize the net (note, that your server has to host the *face_detection_model.weights* file).
81+
82+
``` javascript
83+
// initialize the face detector
84+
const res = await axios.get('face_detection_model.weights', { responseType: 'arraybuffer' })
85+
const weights = new Float32Array(res.data)
86+
const detectionNet = faceapi.faceDetectionNet(weights)
87+
```
88+
89+
Detect faces and get the bounding boxes and scores:
90+
91+
``` javascript
92+
// optional arguments
93+
const minConfidence = 0.8
94+
const maxResults = 10
95+
96+
// inputs can be html canvas, img or video element or their ids ...
97+
const myImg = document.getElementById('myImg')
98+
const detections = await detectionNet.locateFaces(myImg, minConfidence, maxResults)
99+
```
100+
101+
Draw the detected faces to a canvas:
28102

29103
``` javascript
104+
// resize the detected boxes to the image dimensions (since the net returns relative coordinates)
105+
const detectionsForSize = detections.map(det => det.forSize(myImg.width, myImg.height))
106+
const canvas = document.getElementById('overlay')
107+
canvas.width = myImg.width
108+
canvas.height = myImg.height
109+
faceapi.drawDetection(canvas, detectionsForSize, { withScore: false })
110+
```
111+
112+
You can also obtain the tensors of the unfiltered bounding boxes and scores for each image in the batch (tensors have to be disposed manually):
113+
114+
``` javascript
115+
const { boxes, scores } = detectionNet.forward('myImg')
116+
```
117+
118+
### Face Recognition
119+
120+
Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file).
121+
122+
``` javascript
123+
// initialize the face recognizer
30124
const res = await axios.get('face_recognition_model.weights', { responseType: 'arraybuffer' })
31125
const weights = new Float32Array(res.data)
32-
const net = faceapi.faceRecognitionNet(weights)
126+
const recognitionNet = faceapi.faceRecognitionNet(weights)
33127
```
34128

35-
Compute and compare two 150 x 150 sized face images:
129+
Compute and compare the descriptors of two face images:
36130

37131
``` javascript
38-
// input can be an ImageData object obtained from a 150 x 150 canvas via ctx.getImageData(0, 0, 150, 150)
39-
// or a flat array with length 3*150*150 with pixel values in rgb order
40-
// also have a look at the examples how to get the image data from a base64 string, from an <img>, from a <canvas> ...
41-
const imgData1 = ...
42-
const imgData2 = ...
43-
44-
const descriptor1 = await net.computeFaceDescriptor(imgData1)
45-
const descriptor2 = await net.computeFaceDescriptor(imgData2)
132+
// inputs can be html canvas, img or video element or their ids ...
133+
const descriptor1 = await recognitionNet.computeFaceDescriptor('myImg')
134+
const descriptor2 = await recognitionNet.computeFaceDescriptor(document.getElementById('myCanvas'))
46135
const distance = faceapi.euclidianDistance(descriptor1, descriptor2)
47136

48137
if (distance < 0.6)
@@ -54,21 +143,24 @@ else
54143
You can also get the face descriptor data synchronously:
55144

56145
``` javascript
57-
const desc = net.computeFaceDescriptorSync(imgData1)
146+
const desc = recognitionNet.computeFaceDescriptorSync('myImg')
58147
```
59148

60-
Or simply obtain the tensor:
149+
Or simply obtain the tensor (tensor has to be disposed manually):
61150

62151
``` javascript
63-
const t = net.forward(imgData1)
152+
const t = recognitionNet.forward('myImg')
64153
```
65154

66-
## Running the examples
155+
### Compute the Face Descriptors for detected Faces
67156

68-
``` bash
69-
cd examples/faceRecognition
70-
npm i
71-
npm start
72-
```
157+
``` javascript
158+
const detections = await detectionNet.locateFaces(input)
73159

74-
Browse to http://localhost:3000/.
160+
// get the face tensors from the image (have to be disposed manually)
161+
const faceTensors = await faceapi.extractFaceTensors(input, detections)
162+
const descriptors = await Promise.all(faceTensors.map(t => recognitionNet.computeFaceDescriptor(t)))
163+
164+
// free memory for face image tensors after we computed their descriptors
165+
faceTensors.forEach(t => t.dispose())
166+
```

0 commit comments

Comments
 (0)