node-facenet

Facenet v0.3.19 Documentation

Classes

Facenet
Face

Typedefs

FaceJsonObject

FaceJsonObject Type

Point

Point Type

Facenet

Kind: global class

new Facenet()

Facenet is designed for bring the state-of-art neural network with bleeding-edge technology to full stack developers Neural Network && pre-trained model && easy to use APIs

facenet.init() ⇒ Promise.<void>

Init facenet

Kind: instance method of Facenet

facenet.quit() ⇒ Promise.<void>

Quit facenet

Kind: instance method of Facenet

facenet.align(imageData) ⇒ Promise.<Array.<Face>>

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

Kind: instance method of Facenet
Returns: Promise.<Array.<Face>> - - a list of faces

Param Type
imageData ImageData | string

Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
console.info(faceList)
// Output
// [ Face {
//     id: 0,
//     imageData: ImageData { data: [Object] },
//     confidence: 0.9999634027481079,
//     landmark:
//      { leftEye: [Object],
//        rightEye: [Object],
//        nose: [Object],
//        leftMouthCorner: [Object],
//        rightMouthCorner: [Object] },
//      location: { x: 360, y: 94, w: 230, h: 230 },
//      md5: '003c926dd9d2368a86e41a2938aacc98' },
//   Face {
//     id: 1,
//     imageData: ImageData { data: [Object] },
//     confidence: 0.9998626708984375,
//     landmark:
//      { leftEye: [Object],
//        rightEye: [Object],
//        nose: [Object],
//        leftMouthCorner: [Object],
//        rightMouthCorner: [Object] },
//     location: { x: 141, y: 87, w: 253, h: 253 },
//     md5: '0451a0737dd9e4315a21594c38bce485' } ]
// leftEye: [Object],rightEye: [Object],nose: [Object],leftMouthCorner: [Object],rightMouthCorner: [Object] -- Object is Point, something like { x: 441, y: 181 }
// imageData: ImageData { data: [Object] } -- Object is Uint8ClampedArray

facenet.embedding(face) ⇒ Promise.<FaceEmbedding>

Calculate Face Embedding, get the 128 dims embeding from image(s)

Kind: instance method of Facenet
Returns: Promise.<FaceEmbedding> - - return feature vector

Param Type
face Face

Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
for (const face of faceList) {
  face.embedding = await facenet.embedding(face)
}
// Output, there are two faces in the picture, so return two 128 dims array
// array([ 0.03132, 0.05678, 0.06192, ..., 0.08909, 0.16793,-0.05703])
// array([ 0.03422,-0.08358, 0.03549, ..., 0.07108, 0.14013,-0.01417])

facenet.distance(face, faceList) ⇒ Array.<number>

Get distance between a face an each face in the faceList.

Kind: instance method of Facenet

Param Type
face Face
faceList Array.<Face>

Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
for (const face of faceList) {
face.embedding = await facenet.embedding(face)
}
const faceInFaceList = faceList[0]
const distance = facenet.distance(faceInFaceList, faceList)
console.info('distance:', distance)
// Output:
// distance: [ 0, 1.2971515811057608 ]
// The first face comes from the imageFile, the exactly same face, so the first result is 0.

Face

Kind: global class

new Face([imageData])

Creates an instance of Face.

Param Type
[imageData] ImageData

face.embedding : FaceEmbedding | undefined

Embedding the face, FaceEmbedding is 128 dim

Kind: instance property of Face

face.embedding

Set embedding for a face

Kind: instance property of Face

face.center : Point

Get center point for the location

Kind: instance property of Face
Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
console.info('face center : ', faceList[0].center)
// Output: center:  { x: 475, y: 209 }

face.width : number

Get width for the imageData

Kind: instance property of Face
Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
console.info('face width : ', faceList[0].width)
// Output: width:  230

face.height : number

Get height for the imageData

Kind: instance property of Face
Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
console.info('face height : ', faceList[0].height)
// Output: height:  230

face.depth : number

Get depth for the imageData: length/width/height

Kind: instance property of Face

face.init([options]) ⇒ Promise.<this>

Init a face

Kind: instance method of Face

Param Type Default
[options] FaceOptions {}

face.toJSON() ⇒ FaceJsonObject

Get Face Json format data

Kind: instance method of Face

face.distance(face) ⇒ number

Get the two face’s distance, the smaller the number is, the similar of the two face

Kind: instance method of Face

Param Type
face Face

Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
faceList[0].embedding = await facenet.embedding(faceList[0])
faceList[1].embedding = await facenet.embedding(faceList[1])
console.info('distance between the different face: ', faceList[0].distance(faceList[1]))
console.info('distance between the same face:      ', faceList[0].distance(faceList[0]))
// Output
// distance between the different face:  1.2971515811057608
// distance between the same face:       0
// faceList[0] is totally the same with faceList[0], so the number is 0
// faceList[1] is different with faceList[1], so the number is big.
// If the number is smaller than 0.75, maybe they are the same person.

face.save(file) ⇒ Promise.<void>

Save the face to the file

Kind: instance method of Face

Param Type
file string

Example

const imageFile = `${__dirname}/../tests/fixtures/two-faces.jpg`
const faceList = await facenet.align(imageFile)
faceList[0].save('womenFace.jpg')
// You can see it save the women face from `two-faces` pic to `womenFace.jpg`

Face.fromJSON(obj) ⇒ Face

Kind: static method of Face

Param Type
obj FaceJsonObject | string

FaceJsonObject

FaceJsonObject Type

Kind: global typedef
Properties

Name Type Description
confidence number The confidence to confirm is face
embedding Array.<number>  
imageData string Base64 of Buffer
landmark FacialLandmark Face landmark
location Rectangle Face location
md5 string Face md5

Point

Point Type

Kind: global typedef
Properties

Name Type
x number
y number