Introduction to Face-API.js

Face-API.js is a JavaScript library that can detect and recognize faces in images and videos. It provides a set of APIs to perform various tasks related to face detection and recognition, such as detecting facial landmarks, predicting gender and age, and recognizing emotions. This library is built with TensorFlow.js, a machine learning library for JavaScript, and provides a high-level interface to easily integrate these capabilities into your web applications.

Installing Face-API.js

You can install Face-API.js by either downloading the code from the GitHub repository or importing it from npm. Here's how you can install it using npm:

npm install face-api.js

Using Face-API.js

Here are some examples of how to use Face-API.js to detect and recognize faces.

Face Detection

Face detection is the process of identifying faces in an image. Face-API.js provides a function that can detect faces in an image and return their position as a set of bounding boxes. Here's how you can use this function:

const image = document.getElementById('inputImage');
Promise.all([
  faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
  faceapi.detectAllFaces(image)
]).then((result) => {
  const faces = result[1];
  // Draw bounding boxes around the detected faces
  const canvas = document.getElementById('outputCanvas');
  faceapi.matchDimensions(canvas, image);
  const resizedFaces = faceapi.resizeResults(faces, image);
  resizedFaces.forEach((face) => {
    const box = face.detection.box;
    const drawBox = new faceapi.draw.DrawBox(box, { label: 'Face' });
    drawBox.draw(canvas);
  });
}).catch((error) => {
  console.log(error);
});

In this example, we first load the tinyFaceDetector model from the /models directory using the loadFromUri() function. We then call the detectAllFaces() function to detect faces in the input image. The Promise.all() function is used to wait for these asynchronous functions to complete.

Once the faces are detected, we draw bounding boxes around them using the DrawBox() function provided by Face-API.js.

Face Landmarks Detection

Face landmarks are key points on a face, such as the corners of the eyes, nose, and mouth. Face-API.js provides a function that can detect these landmarks and return their positions as a set of points. Here's how you can use this function:

const image = document.getElementById('inputImage');
Promise.all([
  faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
  faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
  faceapi.detectSingleFace(image)
]).then((result) => {
  const landmarks = result[2].landmarks;
  // Draw facial landmarks
  const canvas = document.getElementById('outputCanvas');
  faceapi.matchDimensions(canvas, image);
  const resizedLandmarks = faceapi.resizeResults(landmarks, image);
  const drawLandmarks = new faceapi.draw.DrawFaceLandmarks(resizedLandmarks);
  drawLandmarks.draw(canvas);
}).catch((error) => {
  console.log(error);
});

In this example, we first load the tinyFaceDetector and faceLandmark68Net models from the /models directory using the loadFromUri() function. We then call the detectSingleFace() function to detect a single face in the input image. The Promise.all() function is used to wait for these asynchronous functions to complete.

Once the face is detected, we detect its landmarks using the landmarks property of the result object. We then draw these landmarks using the DrawFaceLandmarks() function provided by Face-API.js.

Face Recognition

Face recognition is the process of identifying people from their facial features. Face-API.js provides a function that can recognize faces and return their identities. Here's how you can use this function:

const image = document.getElementById('inputImage');
Promise.all([
  faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
  faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
  faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
  faceapi.fetchImage('https://example.com/known_face.jpg')
]).then((result) => {
  const descriptors = result[3];
  const labeledDescriptors = [
    new faceapi.LabeledFaceDescriptors('Known', [descriptors])
  ];
  const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors);
  faceapi.detectAllFaces(image).then((faces) => {
    // Recognize faces and label them
    const canvas = document.getElementById('outputCanvas');
    faceapi.matchDimensions(canvas, image);
    const resizedFaces = faceapi.resizeResults(faces, image);
    const labeledFaces = resizedFaces.map((face) => faceMatcher.findBestMatch(face.descriptor));
    labeledFaces.forEach((labeledFace) => {
      const box = labeledFace.detection.box;
      const drawBox = new faceapi.draw.DrawBox(box, { label: labeledFace.label });
      drawBox.draw(canvas);
    });
  });
}).catch((error) => {
  console.log(error);
});

In this example, we first load the tinyFaceDetector, faceLandmark68Net, and faceRecognitionNet models from the /models directory using the loadFromUri() function. We also fetch an image of a known face from a URL and compute its face descriptor using the fetchImage() function.

We then create a FaceMatcher object using the descriptor of the known face. This object can match other faces with the known face and return their labels.

Once the faces are detected, we use the FaceMatcher object to recognize them and label them based on their similarity to the known face. We then draw bounding boxes around the detected faces and label them using the label property of the labeledFace object.

Conclusion

In this article, we've explored the capabilities of Face-API.js and how to use them to detect and recognize faces in images and videos. With these APIs, you can easily build applications that require facial analysis, such as image search, augmented reality, and security systems. Happy coding!

Related APIs