# face-api.js **Repository Path**: iloseall/face-api.js ## Basic Information - **Project Name**: face-api.js - **Description**: JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js - **Primary Language**: TypeScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-06-21 - **Last Updated**: 2021-03-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # face-api.js [![Build Status](https://travis-ci.org/justadudewhohacks/face-api.js.svg?branch=master)](https://travis-ci.org/justadudewhohacks/face-api.js) [![Slack](https://slack.bri.im/badge.svg)](https://slack.bri.im) **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))** Table of Contents: * **[Resources](#resources)** * **[Live Demos](#live-demos)** * **[Tutorials](#tutorials)** * **[Examples](#examples)** * **[Running the Examples](#running-the-examples)** * **[Available Models](#models)** * **[Face Detection Models](#models-face-detection)** * **[68 Point Face Landmark Detection Models](#models-face-landmark-detection)** * **[Face Recognition Model](#models-face-recognition)** * **[Face Expression Recognition Model](#models-face-expression-recognition)** * **[Getting Started](#getting-started)** * **[face-api.js for the Browser](#getting-started-browser)** * **[face-api.js for Nodejs](#getting-started-nodejs)** * **[Usage](#usage)** * **[Loading the Models](#usage-loading-models)** * **[High Level API](#usage-high-level-api)** * **[Displaying Detection Results](#usage-displaying-detection-results)** * **[Face Detection Options](#usage-face-detection-options)** * **[Utility Classes](#usage-utility-classes)** * **[Other Useful Utility](#other-useful-utility)** * **[API Documentation](https://justadudewhohacks.github.io/face-api.js/docs/globals.html)** # Resources ## Live Demos **[Check out the live demos!](https://justadudewhohacks.github.io/face-api.js/)** ## Tutorials Check out my face-api.js tutorials: * **[face-api.js — JavaScript API for Face Recognition in the Browser with tensorflow.js](https://itnext.io/face-api-js-javascript-api-for-face-recognition-in-the-browser-with-tensorflow-js-bcc2a6c4cf07)** * **[Realtime JavaScript Face Tracking and Face Recognition using face-api.js’ MTCNN Face Detector](https://itnext.io/realtime-javascript-face-tracking-and-face-recognition-using-face-api-js-mtcnn-face-detector-d924dd8b5740)** # Examples ## Face Recognition ![preview_face-detection-and-recognition](https://user-images.githubusercontent.com/31125521/41526995-1a90e4e6-72e6-11e8-96d4-8b2ccdee5f79.gif) ![face-recognition-preview](https://user-images.githubusercontent.com/31125521/47384002-41e36f80-d706-11e8-8cd9-b3102c1bee67.png) ## Face Similarity ![preview_face-similarity](https://user-images.githubusercontent.com/31125521/40316573-0a1190c0-5d1f-11e8-8797-f6deaa344523.gif) ## Face Expression Recognition ![preview_face-expression-recognition](https://user-images.githubusercontent.com/31125521/50575270-f501d080-0dfb-11e9-9676-8f419efdade4.png) ## Face Landmark Detection ![face_landmarks_boxes_2](https://user-images.githubusercontent.com/31125521/46063404-00928b00-c16d-11e8-8f29-e9c50afd2bc8.jpg) ![preview_face_landmarks](https://user-images.githubusercontent.com/31125521/41507950-e121b05e-723c-11e8-89f2-d8f9348a8e86.png) ## Realtime Face Tracking ![output](https://user-images.githubusercontent.com/31125521/47383860-ea450400-d705-11e8-9880-d5d15d952661.gif) ## MTCNN ![mtcnn-preview](https://user-images.githubusercontent.com/31125521/42756818-0a41edaa-88fe-11e8-9033-8cd141b0fa09.gif) ## Running the Examples Clone the repository: ``` bash git clone https://github.com/justadudewhohacks/face-api.js.git ``` ### Running the Browser Examples ``` bash cd face-api.js/examples/examples-browser npm i npm start ``` Browse to http://localhost:3000/. ### Running the Nodejs Examples ``` bash cd face-api.js/examples/examples-nodejs npm i ``` Now run one of the examples using ts-node: ``` bash ts-node faceDetection.ts ``` Or simply compile and run them with node: ``` bash tsc faceDetection.ts node faceDetection.js ``` # Available Models ## Face Detection Models ### SSD Mobilenet V1 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. This face detector is aiming towards obtaining high accuracy in detecting face bounding boxes instead of low inference time. The size of the quantized model is about 5.4 MB (**ssd_mobilenetv1_model**). 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. ### Tiny Face Detector The Tiny Face Detector is a very performant, realtime face detector, which is much faster, smaller and less resource consuming compared to the SSD Mobilenet V1 face detector, in return it performs slightly less well on detecting small faces. This model is extremely mobile and web friendly, thus it should be your GO-TO face detector on mobile devices and resource limited clients. The size of the quantized model is only 190 KB (**tiny_face_detector_model**). The face detector has been trained on a custom dataset of ~14K images labeled with bounding boxes. Furthermore the model has been trained to predict bounding boxes, which entirely cover facial feature points, thus it in general produces better results in combination with subsequent face landmark detection than SSD Mobilenet V1. This model is basically an even tinier version of Tiny Yolo V2, replacing the regular convolutions of Yolo with depthwise separable convolutions. Yolo is fully convolutional, thus can easily adapt to different input image sizes to trade off accuracy for performance (inference time). ### MTCNN **Note, this model is mostly kept in this repo for experimental reasons. In general the other face detectors should perform better, but of course you are free to play around with MTCNN.** MTCNN (Multi-task Cascaded Convolutional Neural Networks) represents an alternative face detector to SSD Mobilenet v1 and Tiny Yolo v2, which offers much more room for configuration. By tuning the input parameters, MTCNN should be able to detect a wide range of face bounding box sizes. MTCNN is a 3 stage cascaded CNN, which simultaneously returns 5 face landmark points along with the bounding boxes and scores for each face. Additionally the model size is only 2MB. MTCNN has been presented in the paper [Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks](https://kpzhang93.github.io/MTCNN_face_detection_alignment/paper/spl.pdf) by Zhang et al. and the model weights are provided in the official [repo](https://github.com/kpzhang93/MTCNN_face_detection_alignment) of the MTCNN implementation. ## 68 Point Face Landmark Detection Models This package implements a very lightweight and fast, yet accurate 68 point face landmark detector. The default model has a size of only 350kb (**face_landmark_68_model**) and the tiny model is only 80kb (**face_landmark_68_tiny_model**). Both models employ the ideas of depthwise separable convolutions as well as densely connected blocks. The models have been trained on a dataset of ~35k face images labeled with 68 face landmark points. ## Face Recognition Model 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. 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. The size of the quantized model is roughly 6.2 MB (**face_recognition_model**). ## Face Expression Recognition Model The face expression recognition model is lightweight, fast and provides reasonable accuracy. The model has a size of roughly 310kb and it employs depthwise separable convolutions and densely connected blocks. It has been trained on a variety of images from publicly available datasets as well as images scraped from the web. Note, that wearing glasses might decrease the accuracy of the prediction results. # Getting Started ## face-api.js for the Browser Simply include the latest script from [dist/face-api.js](https://github.com/justadudewhohacks/face-api.js/tree/master/dist). Or install it via npm: ``` bash npm i face-api.js ``` ## face-api.js for Nodejs We can use the equivalent API in a nodejs environment by polyfilling some browser specifics, such as HTMLImageElement, HTMLCanvasElement and ImageData. The easiest way to do so is by installing the node-canvas package. Alternatively you can simply construct your own tensors from image data and pass tensors as inputs to the API. Furthermore you want to install @tensorflow/tfjs-node (not required, but highly recommended), which speeds things up drastically by compiling and binding to the native Tensorflow C++ library: ``` bash npm i face-api.js canvas @tensorflow/tfjs-node ``` Now we simply monkey patch the environment to use the polyfills: ``` javascript // import nodejs bindings to native tensorflow, // not required, but will speed up things drastically (python required) import '@tensorflow/tfjs-node'; // implements nodejs wrappers for HTMLCanvasElement, HTMLImageElement, ImageData import * as canvas from 'canvas'; import * as faceapi from 'face-api.js'; // patch nodejs environment, we need to provide an implementation of // HTMLCanvasElement and HTMLImageElement, additionally an implementation // of ImageData is required, in case you want to use the MTCNN const { Canvas, Image, ImageData } = canvas faceapi.env.monkeyPatch({ Canvas, Image, ImageData }) ``` # Usage ## Loading the Models To load a model, you have provide the corresponding manifest.json file as well as the model weight files (shards) as assets. Simply copy them to your public or assets folder. The manifest.json and shard files of a model have to be located in the same directory / accessible under the same route. Assuming the models reside in **public/models**: ``` javascript await faceapi.loadSsdMobilenetv1Model('/models') // accordingly for the other models: // await faceapi.loadTinyFaceDetectorModel('/models') // await faceapi.loadMtcnnModel('/models') // await faceapi.loadFaceLandmarkModel('/models') // await faceapi.loadFaceLandmarkTinyModel('/models') // await faceapi.loadFaceRecognitionModel('/models') // await faceapi.loadFaceExpressionModel('/models') ``` All global neural network instances are exported via faceapi.nets: ``` javascript console.log(faceapi.nets) ``` The following is equivalent to `await faceapi.loadSsdMobilenetv1Model('/models')`: ``` javascript await faceapi.nets.ssdMobilenetv1.loadFromUri('/models') ``` In a nodejs environment you can furthermore load the models directly from disk: ``` javascript await faceapi.nets.ssdMobilenetv1.loadFromDisk('./models') ``` You can also load the model from a tf.NamedTensorMap: ``` javascript await faceapi.nets.ssdMobilenetv1.loadFromWeightMap(weightMap) ``` Alternatively, you can also create own instances of the neural nets: ``` javascript const net = new faceapi.SsdMobilenetv1() await net.load('/models') ``` You can also load the weights as a Float32Array (in case you want to use the uncompressed models): ``` javascript // using fetch net.load(await faceapi.fetchNetWeights('/models/face_detection_model.weights')) // using axios const res = await axios.get('/models/face_detection_model.weights', { responseType: 'arraybuffer' }) const weights = new Float32Array(res.data) net.load(weights) ``` ## High Level API In the following **input** can be an HTML img, video or canvas element or the id of that element. ``` html