Skip to content

Commit 0657ac2

Browse files
updated README
1 parent fb378dd commit 0657ac2

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

README.md

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
**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

55
* **[Running the Examples](#running-the-examples)**
6-
* **[About Face Detection](#about-face-detection)**
7-
* **[About Face Recognition](#about-face-recognition)**
6+
* **[About the Package](#about-the-package)**
7+
* **[Face Detection](#about-face-detection)**
8+
* **[Face Recognition](#about-face-recognition)**
9+
* **[Face Landmark Detection](#about-face-landmark-detection)**
810
* **[Usage](#usage)**
11+
* **[Face Detection](#usage-face-detection)**
12+
* **[Face Recognition](#usage-face-recognition)**
13+
* **[Face Landmark Detection](#usage-face-landmark-detection)**
914

1015
## Examples
1116

@@ -27,6 +32,12 @@
2732

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

35+
### Face Landmarks
36+
37+
![preview_face_landmarks_boxes](https://user-images.githubusercontent.com/31125521/41507933-65f9b642-723c-11e8-8f4e-aab13303e7ff.jpg)
38+
39+
![preview_face_landmarks](https://user-images.githubusercontent.com/31125521/41507950-e121b05e-723c-11e8-89f2-d8f9348a8e86.png)
40+
3041
### Extracting Face Images
3142

3243
![preview_face-extraction](https://user-images.githubusercontent.com/31125521/41238647-bbb64c6c-6d96-11e8-8ca9-2d0fda779bb6.jpg)
@@ -43,22 +54,34 @@ npm start
4354

4455
Browse to http://localhost:3000/.
4556

57+
<a name="about-the-package"></a>
58+
59+
## About the Package
60+
4661
<a name="about-face-detection"></a>
4762

48-
## About Face Detection
63+
### Face Detection
4964

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.
65+
For face detection, 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.
5166

5267
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.
5368

5469
<a name="about-face-recognition"></a>
5570

56-
## About Face Recognition
71+
### Face Recognition
5772

5873
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.
5974

6075
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.
6176

77+
<a name="about-face-landmark-detection"></a>
78+
79+
### Face Landmark Detection
80+
81+
This package implements a CNN to detect the 68 point face landmarks for a given face image.
82+
83+
The model has been trained on a variety of public datasets and the model weights are provided by [yinguobing](https://github.com/yinguobing) in [this](https://github.com/yinguobing/head-pose-estimation) repo.
84+
6285
<a name="usage"></a>
6386

6487
## Usage
@@ -75,6 +98,8 @@ Or install the package:
7598
npm i face-api.js
7699
```
77100

101+
<a name="usage-face-detection"></a>
102+
78103
### Face Detection
79104

80105
Download the weights file from your server and initialize the net (note, that your server has to host the *face_detection_model.weights* file).
@@ -101,7 +126,7 @@ const detections = await detectionNet.locateFaces(myImg, minConfidence, maxResul
101126
Draw the detected faces to a canvas:
102127

103128
``` javascript
104-
// resize the detected boxes to the image dimensions (since the net returns relative coordinates)
129+
// resize the detected boxes in case your displayed image has a different size then the original
105130
const detectionsForSize = detections.map(det => det.forSize(myImg.width, myImg.height))
106131
const canvas = document.getElementById('overlay')
107132
canvas.width = myImg.width
@@ -115,6 +140,8 @@ You can also obtain the tensors of the unfiltered bounding boxes and scores for
115140
const { boxes, scores } = detectionNet.forward('myImg')
116141
```
117142

143+
<a name="usage-face-recognition"></a>
144+
118145
### Face Recognition
119146

120147
Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file).
@@ -152,7 +179,7 @@ Or simply obtain the tensor (tensor has to be disposed manually):
152179
const t = recognitionNet.forward('myImg')
153180
```
154181

155-
### Compute the Face Descriptors for detected Faces
182+
Compute the Face Descriptors for Detected Faces
156183

157184
``` javascript
158185
const detections = await detectionNet.locateFaces(input)
@@ -163,4 +190,64 @@ const descriptors = await Promise.all(faceTensors.map(t => recognitionNet.comput
163190

164191
// free memory for face image tensors after we computed their descriptors
165192
faceTensors.forEach(t => t.dispose())
166-
```
193+
```
194+
195+
<a name="usage-face-landmark-detection"></a>
196+
197+
### Face Landmark Detection
198+
199+
Download the weights file from your server and initialize the net (note, that your server has to host the *face_landmark_68_model.weights* file).
200+
201+
``` javascript
202+
// initialize the face recognizer
203+
const res = await axios.get('face_landmark_68_model.weights', { responseType: 'arraybuffer' })
204+
const weights = new Float32Array(res.data)
205+
const faceLandmarkNet = faceapi.faceLandmarkNet(weights)
206+
```
207+
208+
Detect face landmarks:
209+
210+
``` javascript
211+
// inputs can be html canvas, img or video element or their ids ...
212+
const myImg = document.getElementById('myImg')
213+
const landmarks = await faceLandmarkNet.detectLandmarks(myImg)
214+
```
215+
216+
Draw the detected face landmarks to a canvas:
217+
218+
``` javascript
219+
// adjust the landmark positions in case your displayed image has a different size then the original
220+
const landmarksForSize = landmarks.forSize(myImg.width, myImg.height))
221+
const canvas = document.getElementById('overlay')
222+
canvas.width = myImg.width
223+
canvas.height = myImg.height
224+
faceapi.drawLandmarks(canvas, landmarksForSize, { drawLines: true })
225+
```
226+
227+
Retrieve the face landmark positions
228+
229+
``` javascript
230+
const landmarkPositions = landmarks.getPositions()
231+
232+
// or get the positions of individual contours
233+
const jawOutline = landmarks.getJawOutline()
234+
const nose = landmarks.getNose()
235+
const mouth = landmarks.getMouth()
236+
const leftEye = landmarks.getLeftEye()
237+
const rightEye = landmarks.getRightEye()
238+
const leftEyeBbrow = landmarks.getLeftEyeBrow()
239+
const rightEyeBrow = landmarks.getRightEyeBrow()
240+
```
241+
242+
Compute the Face Landmarks for Detected Faces
243+
244+
``` javascript
245+
const detections = await detectionNet.locateFaces(input)
246+
247+
// get the face tensors from the image (have to be disposed manually)
248+
const faceTensors = await faceapi.extractFaceTensors(input, detections)
249+
const landmarksByFace = await Promise.all(faceTensors.map(t => landmarksNet.detectLandmarks(t)))
250+
251+
// free memory for face image tensors after we computed their descriptors
252+
faceTensors.forEach(t => t.dispose())
253+
```

0 commit comments

Comments
 (0)