Skip to content

Instantly share code, notes, and snippets.

@supachaic
Last active March 9, 2019 04:16
Show Gist options
  • Save supachaic/7f0ff690a22ea2af177c73daff2bc792 to your computer and use it in GitHub Desktop.
Save supachaic/7f0ff690a22ea2af177c73daff2bc792 to your computer and use it in GitHub Desktop.
src/api/face.js
import * as faceapi from 'face-api.js';
// Load models and weights
export async function loadModels() {
const MODEL_URL = process.env.PUBLIC_URL + '/models';
await faceapi.loadTinyFaceDetectorModel(MODEL_URL);
await faceapi.loadFaceLandmarkTinyModel(MODEL_URL);
await faceapi.loadFaceRecognitionModel(MODEL_URL);
}
export async function getFullFaceDescription(blob, inputSize = 512) {
// tiny_face_detector options
let scoreThreshold = 0.5;
const OPTION = new faceapi.TinyFaceDetectorOptions({
inputSize,
scoreThreshold
});
const useTinyModel = true;
// fetch image to api
let img = await faceapi.fetchImage(blob);
// detect all faces and generate full description from image
// including landmark and descriptor of each face
let fullDesc = await faceapi
.detectAllFaces(img, OPTION)
.withFaceLandmarks(useTinyModel)
.withFaceDescriptors();
return fullDesc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment