Code Monkey home page Code Monkey logo

face-recognition.js's Introduction

face-recognition.js

Build Status Build status

Simple Node.js API for robust face detection and face recognition. This a Node.js wrapper library for the face detection and face recognition tools implemented in dlib.

rec

Examples

Face Detection

face-got faces

Face Recognition

rec

Face Landmarks

landmark5 landmark68

Install

Linux and OSX

  • cmake
  • libx11 (XQuartz on OSX) for the dlib GUI (sudo apt-get install libx11-dev)
  • libpng for reading images (sudo apt-get install libpng-dev)

Windows

Auto build

Installing the package will build dlib for you and download the models. Note, this might take some time.

npm install face-recognition

Manual build

If you want to use an own build of dlib:

  • set DLIB_INCLUDE_DIR to the source directory of dlib
  • set DLIB_LIB_DIR to the file path to dlib.lib | dlib.so | dlib.dylib

If you set these environment variables, the package will use your own build instead of compiling dlib:

npm install face-recognition

How to use

const fr = require('face-recognition')

Loading images from disk

const image1 = fr.loadImage('path/to/image1.png')
const image2 = fr.loadImage('path/to/image2.jpg')

Displaying Images

const win = new fr.ImageWindow()

// display image
win.setImage(image)

// drawing the rectangle into the displayed image
win.addOverlay(rectangle)

// pause program until key pressed
fr.hitEnterToContinue()

Face Detection

const detector = fr.FaceDetector()

Detect all faces in the image and return the bounding rectangles:

const faceRectangles = detector.locateFaces(image)

Detect all faces and return them as seperate images:

const faceImages = detector.detectFaces(image)

You can also specify the output size of the face images (default is 150 e.g. 150x150):

const targetSize = 200
const faceImages = detector.detectFaces(image, targetSize)

Face Recognition

const recognizer = fr.FaceRecognizer()

Train the recognizer with face images of atleast two different persons:

// arrays of face images, (use FaceDetector to detect and extract faces)
const sheldonFaces = [ ... ]
const rajFaces = [ ... ]
const howardFaces = [ ... ]

recognizer.addFaces(sheldonFaces, 'sheldon')
recognizer.addFaces(rajFaces, 'raj')
recognizer.addFaces(howardFaces, 'howard')

You can also jitter the training data, which will apply transformations such as rotation, scaling and mirroring to create different versions of each input face. Increasing the number of jittered version may increase prediction accuracy but also increases training time:

const numJitters = 15
recognizer.addFaces(sheldonFaces, 'sheldon', numJitters)
recognizer.addFaces(rajFaces, 'raj', numJitters)
recognizer.addFaces(howardFaces, 'howard', numJitters)

Get the distances to each class:

const predictions = recognizer.predict(sheldonFaceImage)
console.log(predictions)

example output (the lower the distance, the higher the similarity):

[
  {
    className: 'sheldon',
    distance: 0.5
  },
  {
    className: 'raj',
    distance: 0.8
  },
  {
    className: 'howard',
    distance: 0.7
  }
]

Or immediately get the best result:

const bestPrediction = recognizer.predict(sheldonFaceImage)
console.log(bestPrediction)

example output:

{
  className: 'sheldon',
  distance: 0.5
}

Save a trained model to json file:

const fs = require('fs')
const modelState = recognizer.serialize()
fs.writeFileSync('model.json', JSON.stringify(modelState))

Load a trained model from json file:

const modelState = require('model.json')
recognizer.load(modelState)

Face Landmarks

This time using the FrontalFaceDetector (you can also use FaceDetector):

const detector = new fr.FrontalFaceDetector()

Use 5 point landmarks predictor:

const predictor = fr.FaceLandmark5Predictor()

Or 68 point landmarks predictor:

const predictor = fr.FaceLandmark68Predictor()

First get the bounding rectangles of the faces:

const img = fr.loadImage('image.png')
const faceRects = detector.detect(img)

Find the face landmarks:

const shapes = faceRects.map(rect => predictor.predict(img, rect))

Display the face landmarks:

const win = new fr.ImageWindow()
win.setImage(img)
win.renderFaceDetections(shapes)
fr.hitEnterToContinue()

face-recognition.js's People

Contributors

justadudewhohacks avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.