|
2 | 2 |
|
3 | 3 | **face recognition API for the browser with tensorflow.js**
|
4 | 4 |
|
5 |
| -coming soon ... |
| 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. |
6 | 6 |
|
7 |
| - |
| 7 | +## Face Recognition |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Face Similarity |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +Get the latest build from dist/face-recognition.min.js and include the script: |
| 18 | + |
| 19 | +``` html |
| 20 | +<script src="face-recognition.min.js"></script> |
| 21 | +``` |
| 22 | + |
| 23 | +Download the weights file from your server and initialize the net (note, that your server has to host the *face_recognition_model.weights* file): |
| 24 | + |
| 25 | +``` javascript |
| 26 | +const res = await axios.get('face_recognition_model.weights', { responseType: 'arraybuffer' }) |
| 27 | +const weights = new Float32Array(res.data) |
| 28 | +const net = facerecognition.faceRecognitionNet(weights) |
| 29 | +``` |
| 30 | + |
| 31 | +Compute and compare two 150 x 150 sized face images: |
| 32 | + |
| 33 | +``` javascript |
| 34 | +// input can be an ImageData object obtained from a 150 x 150 canvas via ctx.getImageData(0, 0, 150, 150) |
| 35 | +// or a flat array with length 3*150*150 with pixel values in rgb order |
| 36 | +// also have a look at the examples how to get the image data from a base64 string, from an <img>, from a <canvas> ... |
| 37 | +const imgData1 = ... |
| 38 | +const imgData2 = ... |
| 39 | + |
| 40 | + |
| 41 | +const descriptor1 = await net.computeFaceDescriptor(imgData1) |
| 42 | +const descriptor2 = await net.computeFaceDescriptor(imgData2) |
| 43 | +const distance = facerecognition.computeFaceDescriptor(descriptor1, descriptor2) |
| 44 | + |
| 45 | +if (distance < 0.6) |
| 46 | + console.log('match') |
| 47 | +else |
| 48 | + console.log('no match') |
| 49 | +``` |
| 50 | + |
| 51 | +You can also get the face descriptor data synchronously: |
| 52 | + |
| 53 | +``` javascript |
| 54 | +const desc = net.computeFaceDescriptorSync(imgData1) |
| 55 | +``` |
| 56 | + |
| 57 | +Or simply obtain the tensor: |
| 58 | + |
| 59 | +``` javascript |
| 60 | +const t = net.forward(imgData1) |
| 61 | +``` |
8 | 62 |
|
9 | 63 | ## Running the examples
|
10 | 64 |
|
|
0 commit comments