/node-facenet

A TensorFlow backed FaceNet implementation for Node.js.

Primary LanguageTypeScriptApache License 2.0Apache-2.0

FaceNet

Build Status NPM Version Downloads Join the chat at https://gitter.im/node-facenet/Lobby node TypeScript

node-facenet is a TensorFlow backed FaceNet implementation for Node.js.

Google Facenet

FaceNet is a deep convolutional network designed by Google, trained to solve face verification, recognition and clustering problem with efficiently at scale.

  1. directly learns a mapping from face images to a compact Euclidean space where distances directly correspond to a measure of face similarity.
  2. optimize the embedding face recognition performance using only 128-bytes per face.
  3. achieves accuracy of 99.63% on Labeled Faces in the Wild (LFW) dataset, and 95.12% on YouTube Faces DB.

Example

The follow examples will give you some intuitions for using the code.

  1. visualize example will calculate the similarity between faces and draw them on the photo.
  2. demo exmaple will show you how to do align for face alignment and embedding to get face feature vector.

1. Visualize for Intuition

FaceNet Visualization

  1. Face is in the green rectangle.
  2. Similarity(distance) between faces showed as a number in the middle of the line.
  3. To identify if two faces belong to the same person, we could use an experiential threshold of distance: 0.75.
$ git clone git@github.com:zixia/node-facenet.git
$ cd facenet
$ npm install
$ npm run example:visualize

01:15:43 INFO CLI Visualized image saved to:  facenet-visulized.jpg

2. Demo for API Usage

TL;DR: Talk is cheap, show me the code!

import { Facenet } from 'facenet'

const facenet = new Facenet()

// Do Face Alignment, return faces
const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)

for (const face of faceList) {
  console.log('bounding box:',  face.boundingBox)
  console.log('landmarks:',     face.facialLandmark)

  // Calculate Face Embedding, return feature vector
  const embedding = await facenet.embedding(face)
  console.log('embedding:', embedding)
}

Full source code can be found at here: https://github.com/zixia/node-facenet/blob/master/examples/demo.ts

Try it by run:

$ git clone git@github.com:zixia/node-facenet.git
$ cd facenet
$ npm install
$ npm run example:demo

image file: /home/zixia/git/facenet/examples/../tests/fixtures/two-faces.jpg
face file: 1-1.jpg
bounding box: {
  p1: { x: 360, y: 95 }, 
  p2: { x: 589, y: 324 } 
}
landmarks: { 
  leftEye:  { x: 441, y: 181 },
  rightEye: { x: 515, y: 208 },
  nose:     { x: 459, y: 239 },
  leftMouthCorner:  { x: 417, y: 262 },
  rightMouthCorner: { x: 482, y: 285 } 
}
embedding: array([ 0.02453, 0.03973, 0.05397, ..., 0.10603, 0.15305,-0.07288])

face file: 1-2.jpg
bounding box: { 
  p1: { x: 142, y: 87 }, 
  p2: { x: 395, y: 340 } 
}
landmarks: { 
  leftEye:  { x: 230, y: 186 },
  rightEye: { x: 316, y: 197 },
  nose:     { x: 269, y: 257 },
  leftMouthCorner:  { x: 223, y: 273 },
  rightMouthCorner: { x: 303, y: 281 } 
}
embedding: array([ 0.03241, -0.0737,  0.0475, ..., 0.07235, 0.12581,-0.00817])

FACENET MANAGER

UNDER HEAVY DEVELOPMENT NOW

Roadmap: release facenet-manager on version 0.4

asciicast

The above ascii recording is just for demo purpose. Will replace it with facenet-manager later.

Install & Requirement

$ npm install facenet

OS

Supported:

  • Linux
  • Mac
  • Windows

Dependency

  1. Node.js >= 7 (8 is recommend)
  2. Tensorflow >= 1.2
  3. Python3 >=3.5 (3.6 is recommend)

Ram

Neural Network Model Task Ram
MTCNN Facenet#align() 100MB
Facenet Facenet#embedding() 2GB

If you are dealing with very large images(like 3000x3000 pixels), there will need additional 1GB of memory.

So I believe that Facenet will need at least 2GB memory, and >=4GB is recommended.

API

Neural Network alone is not enough. It's Neural Network married with pre-trained model, married with easy to use APIs, that yield us the result that makes our APP sing.

Facenet is designed for bring the state-of-art neural network with bleeding-edge technology to full stack developers.

Facenet

import { Facenet } from 'facenet'

const facenet = new Facenet()
facenet.quit()

1. Facenet#align(filename: string): Promise<Face[]>

Do face alignment for the image, return a list of faces.

2. Facenet#embedding(face: Face): Promise<FaceEmbedding>

Get the embedding for a face.

Face

Get the 128 dim embedding vector for this face.(After alignment)

import { Face } from 'facenet'

console.log('bounding box:',  face.boundingBox)
console.log('landmarks:',     face.facialLandmark)
console.log('embedding:',     face.embedding)

Environment Variables

FACENET_MODEL

FaceNet neural network model files, set to other version of model as you like.

Default is set to models/ directory inside project directory. The pre-trained models is come from 20170512-110547, 0.992, MS-Celeb-1M, Inception ResNet v1, which will be download & save automatically by postinstall script.

$ pwd
/home/zixia/git/node-facenet

$ ls models/
20170512-110547.pb
model-20170512-110547.ckpt-250000.index
model-20170512-110547.ckpt-250000.data-00000-of-00001
model-20170512-110547.meta

Docker

Docker Pulls Docker Stars Docker Layers

Develop

Issue Stats Issue Stats Coverage Status Greenkeeper badge

$ git clone git@github.com:zixia/node-facenet.git
$ cd facenet
$ npm install
$ npm test

Command Line Interface

align

Draw a rectangle with five landmarks on all faces in the input_image, save it to output_image.

$ ./node_modules/.bin/ts-node bin/align.ts input_image output_image

embedding

Output the 128 dim embedding vector of the face image.

$ ./node_modules/.bin/ts-node bin/embedding.ts face_image

Resources

Machine Learning

Python3

1. Typing

1. NumJS

Dataset

  1. LFW - Labeled Faces in the Wild

Todo

  • NPM Module: facenet
  • Docker Image: zixia/facenet
  • Examples
    • API Usage Demo
    • Triple Distance Visulization Demo
    • Performance Test(Align/Embedding/Batch)
    • Validation Test(LFW Accuracy)
  • Neural Network Models
  • Python3 async & await
  • Divide Different Neural Network to seprate class files(e.g. Facenet/Mtcnn/ChineseWhisper)
  • TensorFlow Sereving
  • OpenAPI Specification(Swagger)

Inspiration

This repository is heavily inspired by the following implementations:

Credits

  1. Face alignment using MTCNN: Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks
  2. Face embedding using FaceNet: FaceNet: A Unified Embedding for Face Recognition and Clustering
  3. TensorFlow implementation of the face recognizer: Face recognition using Tensorflow

Contribute

FaceNet Badge

Powered by FaceNet

[![Powered by FaceNet](https://img.shields.io/badge/Powered%20By-FaceNet-green.svg)](https://github.com/zixia/node-facenet)

Changelog

v0.3 / Master

  1. Added three cache classes: AlignmentCache & EmbeddingCache & FaceCache.
  2. Added cache manager utilities: embedding-cache-manager & alignment-cache-manager & face-cache-manager
  3. Added Dataset manager utility: lfw-manager (should be dataset-manager in future)

v0.2 Aug 2017 (BREAKING CHANGES)

  1. Facenet#align() now accept a filename string as parameter.
  2. BREAKING CHANGE: FaceImage class had been removed.
  3. BREAKING CHANGE: Face class refactoring.

v0.1 Jul 2017

  1. npm run demo to visuliaze the face alignment and distance(embedding) in a three people photo.
  2. Facenet.align() to do face alignment
  3. Facenet.embedding() to calculate the 128 dim feature vector of face
  4. Initial workable version

FAQ

  1. facenet-manager display not right under Windows

See: Running Terminal Dashboards on Windows

Author

Huan LI <zixia@zixia.net> (http://linkedin.com/in/zixia)

profile for zixia at Stack Overflow, Q&A for professional and enthusiast programmers

Copyright & License

  • Code & Docs © 2017 Huan LI <zixia@zixia.net>
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons