face-api.js is a robust JavaScript API designed for performing real-time face detection, face recognition, face landmark detection, face expression recognition, and age and gender estimation. It is built on top of the TensorFlow.js core library, enabling these advanced computer vision capabilities directly within web browsers and Node.js environments. The current stable version is 0.22.2. While a strict release cadence isn't explicitly stated, the project appears actively maintained with regular updates and tutorials, often aligning with TensorFlow.js advancements. Its key differentiator is providing a high-level, easy-to-use API over complex TensorFlow.js operations, simplifying the integration of sophisticated facial analysis features into JavaScript applications without requiring deep machine learning expertise. It also provides pre-trained models for various tasks, abstracting away the complexities of model management.
npm install face-api.jsVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes all necessary face-api.js models, starts a webcam stream, and continuously performs real-time face detection, landmark identification, expression recognition, and age/gender estimation, drawing the results onto an HTML canvas overlaid on the video feed.
Ensure the 'models' directory is served statically by your web server, and the `.load()` methods point to the correct URL (e.g., `faceapi.nets.modelName.load('/models')`).For browsers, ensure `@tensorflow/tfjs-backend-webgl` is installed and initialized. For Node.js, use `@tensorflow/tfjs-node` or `@tensorflow/tfjs-node-gpu`. Opt for `TinyFaceDetectorOptions` for faster, albeit slightly less accurate, detections.
At the top of your Node.js entry file, include `import '@tensorflow/tfjs-node';` or `require('@tensorflow/tfjs-node');` to activate the backend.Always prefer ES Module `import * as faceapi from 'face-api.js'` for consistency and future compatibility, especially when using bundlers like Webpack or Vite.
Ensure `@tensorflow/tfjs-backend-webgl` is installed and imported in your project (e.g., `import '@tensorflow/tfjs-backend-webgl';`). If running in Node.js, import `@tensorflow/tfjs-node` instead.
Verify that the 'models' directory is correctly placed in your public assets folder and served by your web server. The path provided to `load()` methods (e.g., `/models`) must match the actual URL where the model files are hosted.
Ensure you are using the correct `import * as faceapi from 'face-api.js'` syntax and that the import statement is executed before any `faceapi` calls. For CommonJS, use `const faceapi = require('face-api.js');` for older versions.Ensure you are targeting an HTMLCanvasElement for drawing operations (e.g., `canvas.getContext('2d')`) and not an HTMLVideoElement.