Skip to content

Commit c5cfd99

Browse files
added some notes for full face detection to face recognition pipeline in readme
1 parent ac739e3 commit c5cfd99

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* **[Face Detection](#usage-face-detection)**
1212
* **[Face Recognition](#usage-face-recognition)**
1313
* **[Face Landmark Detection](#usage-face-landmark-detection)**
14+
* **[Full Face Detection and Recognition Pipeline](#usage-full-face-detection-and-recognition-pipeline)**
1415

1516
## Examples
1617

@@ -175,19 +176,6 @@ Or simply obtain the tensor (tensor has to be disposed manually):
175176
const t = recognitionNet.forward('myImg')
176177
```
177178

178-
Compute the Face Descriptors for Detected Faces:
179-
180-
``` javascript
181-
const detections = await detectionNet.locateFaces(input)
182-
183-
// get the face tensors from the image (have to be disposed manually)
184-
const faceTensors = await faceapi.extractFaceTensors(input, detections)
185-
const descriptors = await Promise.all(faceTensors.map(t => recognitionNet.computeFaceDescriptor(t)))
186-
187-
// free memory for face image tensors after we computed their descriptors
188-
faceTensors.forEach(t => t.dispose())
189-
```
190-
191179
<a name="usage-face-landmark-detection"></a>
192180

193181
### Face Landmark Detection
@@ -247,3 +235,39 @@ const landmarksByFace = await Promise.all(faceTensors.map(t => faceLandmarkNet.d
247235
// free memory for face image tensors after we computed their descriptors
248236
faceTensors.forEach(t => t.dispose())
249237
```
238+
239+
<a name="usage-full-face-detection-and-recognition-pipeline"></a>
240+
241+
### Full Face Detection and Recognition Pipeline
242+
243+
After face detection has been performed, I would recommend to align the bounding boxes of the detected faces before passing them to the face recognition net, which will make the computed face descriptor much more accurate. You can easily align the faces from their face landmark positions as shown in the following example:
244+
245+
``` javascript
246+
// first detect the face locations
247+
const detections = await detectionNet.locateFaces(input)
248+
249+
// get the face tensors from the image (have to be disposed manually)
250+
const faceTensors = (await faceapi.extractFaceTensors(input, detections))
251+
252+
// detect landmarks and get the aligned face image bounding boxes
253+
const alignedFaceBoxes = await Promise.all(faceTensors.map(
254+
async (faceTensor, i) => {
255+
const faceLandmarks = await landmarkNet.detectLandmarks(faceTensor)
256+
return faceLandmarks.align(detections[i])
257+
}
258+
))
259+
260+
// free memory for face image tensors after we detected the face landmarks
261+
faceTensors.forEach(t => t.dispose())
262+
263+
// get the face tensors for the aligned face images from the image (have to be disposed manually)
264+
const alignedFaceTensors = (await faceapi.extractFaceTensors(input, alignedFaceBoxes))
265+
266+
// compute the face descriptors from the aligned face images
267+
const descriptors = await Promise.all(alignedFaceTensors.map(
268+
faceTensor => recognitionNet.computeFaceDescriptor(faceTensor)
269+
))
270+
271+
// free memory for face image tensors after we computed their descriptors
272+
alignedFaceTensors.forEach(t => t.dispose())
273+
```

examples/views/detectAndRecognizeFaces.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
}
106106
))
107107

108-
// free memory for face image tensors after we computed their descriptors
108+
// free memory for face image tensors after we detected the face landmarks
109109
faceTensors.forEach(t => t.dispose())
110110

111111
const alignedFaceTensors = (await faceapi.extractFaceTensors(input, alignedFaceBoxes))

0 commit comments

Comments
 (0)