2022-05-19 09:02:40 +00:00
|
|
|
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm';
|
2022-09-27 07:10:28 +00:00
|
|
|
import { Config, FaceResult, Human } from '@vladmandic/human';
|
2022-05-19 09:02:40 +00:00
|
|
|
|
2022-06-10 12:19:18 +00:00
|
|
|
import { DETECTION_TYPES, FACE_DETECTION_SCORE_THRESHOLD, FACE_EXPRESSIONS_NAMING_MAPPING } from './constants';
|
2022-09-14 12:42:46 +00:00
|
|
|
import { DetectInput, DetectOutput, FaceBox, InitInput } from './types';
|
2022-05-19 09:02:40 +00:00
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
export interface IFaceLandmarksHelper {
|
2022-09-08 09:52:36 +00:00
|
|
|
detect: ({ image, threshold }: DetectInput) => Promise<DetectOutput>;
|
|
|
|
getDetectionInProgress: () => boolean;
|
|
|
|
getDetections: (image: ImageBitmap | ImageData) => Promise<Array<FaceResult>>;
|
|
|
|
getFaceBox: (detections: Array<FaceResult>, threshold: number) => FaceBox | undefined;
|
|
|
|
getFaceCount: (detections: Array<FaceResult>) => number;
|
|
|
|
getFaceExpression: (detections: Array<FaceResult>) => string | undefined;
|
|
|
|
init: () => Promise<void>;
|
2022-05-19 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-11 12:30:37 +00:00
|
|
|
* Helper class for human library.
|
2022-05-19 09:02:40 +00:00
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
export class HumanHelper implements IFaceLandmarksHelper {
|
2022-05-19 09:02:40 +00:00
|
|
|
protected human: Human | undefined;
|
|
|
|
protected faceDetectionTypes: string[];
|
|
|
|
protected baseUrl: string;
|
|
|
|
private detectionInProgress = false;
|
|
|
|
private lastValidFaceBox: FaceBox | undefined;
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
/**
|
|
|
|
* Configuration for human.
|
|
|
|
*/
|
|
|
|
private config: Partial<Config> = {
|
|
|
|
backend: 'humangl',
|
|
|
|
async: true,
|
|
|
|
warmup: 'none',
|
|
|
|
cacheModels: true,
|
|
|
|
cacheSensitivity: 0,
|
|
|
|
debug: false,
|
|
|
|
deallocate: true,
|
|
|
|
filter: { enabled: false },
|
|
|
|
face: {
|
2022-06-08 17:28:41 +00:00
|
|
|
enabled: false,
|
2022-05-19 09:02:40 +00:00
|
|
|
detector: {
|
|
|
|
enabled: false,
|
|
|
|
rotation: false,
|
2022-06-08 17:28:41 +00:00
|
|
|
modelPath: 'blazeface-front.json',
|
2022-06-16 11:50:31 +00:00
|
|
|
maxDetected: 20
|
2022-05-19 09:02:40 +00:00
|
|
|
},
|
|
|
|
mesh: { enabled: false },
|
|
|
|
iris: { enabled: false },
|
2022-07-11 12:30:37 +00:00
|
|
|
emotion: {
|
2022-05-19 09:02:40 +00:00
|
|
|
enabled: false,
|
|
|
|
modelPath: 'emotion.json'
|
|
|
|
},
|
|
|
|
description: { enabled: false }
|
|
|
|
},
|
|
|
|
hand: { enabled: false },
|
|
|
|
gesture: { enabled: false },
|
|
|
|
body: { enabled: false },
|
|
|
|
segmentation: { enabled: false }
|
|
|
|
};
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Constructor function for the helper which initialize the helper.
|
|
|
|
*
|
|
|
|
* @param {InitInput} input - The input for the helper.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-06-16 11:50:31 +00:00
|
|
|
constructor({ baseUrl, detectionTypes }: InitInput) {
|
2022-05-19 09:02:40 +00:00
|
|
|
this.faceDetectionTypes = detectionTypes;
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Initializes the human helper with the available tfjs backend for the given detection types.
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-05-19 09:02:40 +00:00
|
|
|
async init(): Promise<void> {
|
|
|
|
|
|
|
|
if (!this.human) {
|
|
|
|
this.config.modelBasePath = this.baseUrl;
|
|
|
|
if (!self.OffscreenCanvas) {
|
|
|
|
this.config.backend = 'wasm';
|
|
|
|
this.config.wasmPath = this.baseUrl;
|
|
|
|
setWasmPaths(this.baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.faceDetectionTypes.length > 0 && this.config.face) {
|
2022-07-11 12:30:37 +00:00
|
|
|
this.config.face.enabled = true;
|
2022-06-08 17:28:41 +00:00
|
|
|
}
|
2022-05-19 09:02:40 +00:00
|
|
|
|
|
|
|
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_BOX) && this.config.face?.detector) {
|
|
|
|
this.config.face.detector.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_EXPRESSIONS) && this.config.face?.emotion) {
|
|
|
|
this.config.face.emotion.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialHuman = new Human(this.config);
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
try {
|
|
|
|
await initialHuman.load();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
this.human = initialHuman;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the face box from the detections, if there is no valid detections it will return undefined..
|
|
|
|
*
|
|
|
|
* @param {Array<FaceResult>} detections - The array with the detections.
|
|
|
|
* @param {number} threshold - Face box position change threshold.
|
|
|
|
* @returns {FaceBox | undefined}
|
|
|
|
*/
|
2022-06-16 11:50:31 +00:00
|
|
|
getFaceBox(detections: Array<FaceResult>, threshold: number): FaceBox | undefined {
|
|
|
|
if (this.getFaceCount(detections) !== 1) {
|
2022-05-19 09:02:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-10 12:19:18 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
const faceBox: FaceBox = {
|
|
|
|
// normalize to percentage based
|
2022-06-16 11:50:31 +00:00
|
|
|
left: Math.round(detections[0].boxRaw[0] * 100),
|
|
|
|
right: Math.round((detections[0].boxRaw[0] + detections[0].boxRaw[2]) * 100)
|
2022-05-19 09:02:40 +00:00
|
|
|
};
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
faceBox.width = Math.round(faceBox.right - faceBox.left);
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
if (this.lastValidFaceBox && threshold && Math.abs(this.lastValidFaceBox.left - faceBox.left) < threshold) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
this.lastValidFaceBox = faceBox;
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
return faceBox;
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the face expression from the detections, if there is no valid detections it will return undefined.
|
|
|
|
*
|
|
|
|
* @param {Array<FaceResult>} detections - The array with the detections.
|
|
|
|
* @returns {string | undefined}
|
|
|
|
*/
|
2022-06-16 11:50:31 +00:00
|
|
|
getFaceExpression(detections: Array<FaceResult>): string | undefined {
|
|
|
|
if (this.getFaceCount(detections) !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (detections[0].emotion) {
|
|
|
|
return FACE_EXPRESSIONS_NAMING_MAPPING[detections[0].emotion[0].emotion];
|
2022-05-19 09:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the face count from the detections, which is the number of detections.
|
|
|
|
*
|
|
|
|
* @param {Array<FaceResult>} detections - The array with the detections.
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2022-06-16 11:50:31 +00:00
|
|
|
getFaceCount(detections: Array<FaceResult> | undefined): number {
|
|
|
|
if (detections) {
|
|
|
|
return detections.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the detections from the image captured from the track.
|
|
|
|
*
|
|
|
|
* @param {ImageBitmap | ImageData} image - The image captured from the track,
|
|
|
|
* if OffscreenCanvas available it will be ImageBitmap, otherwise it will be ImageData.
|
|
|
|
* @returns {Promise<Array<FaceResult>>}
|
|
|
|
*/
|
2022-06-16 11:50:31 +00:00
|
|
|
async getDetections(image: ImageBitmap | ImageData): Promise<Array<FaceResult>> {
|
|
|
|
if (!this.human || !this.faceDetectionTypes.length) {
|
|
|
|
return [];
|
2022-06-10 12:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.human.tf.engine().startScope();
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-06-10 12:19:18 +00:00
|
|
|
const imageTensor = this.human.tf.browser.fromPixels(image);
|
|
|
|
const { face: detections } = await this.human.detect(imageTensor, this.config);
|
|
|
|
|
|
|
|
this.human.tf.engine().endScope();
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-06-10 12:19:18 +00:00
|
|
|
return detections.filter(detection => detection.score > FACE_DETECTION_SCORE_THRESHOLD);
|
2022-07-11 12:30:37 +00:00
|
|
|
}
|
2022-06-10 12:19:18 +00:00
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Gathers together all the data from the detections, it's the function that will be called in the worker.
|
|
|
|
*
|
|
|
|
* @param {DetectInput} input - The input for the detections.
|
|
|
|
* @returns {Promise<DetectOutput>}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
public async detect({ image, threshold }: DetectInput): Promise<DetectOutput> {
|
2022-05-19 09:02:40 +00:00
|
|
|
let faceExpression;
|
|
|
|
let faceBox;
|
|
|
|
|
|
|
|
this.detectionInProgress = true;
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
const detections = await this.getDetections(image);
|
2022-05-19 09:02:40 +00:00
|
|
|
|
2022-06-16 11:50:31 +00:00
|
|
|
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_EXPRESSIONS)) {
|
|
|
|
faceExpression = this.getFaceExpression(detections);
|
2022-05-19 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_BOX)) {
|
2022-07-11 12:30:37 +00:00
|
|
|
// if more than one face is detected the face centering will be disabled.
|
|
|
|
if (this.getFaceCount(detections) > 1) {
|
2022-06-16 11:50:31 +00:00
|
|
|
this.faceDetectionTypes.splice(this.faceDetectionTypes.indexOf(DETECTION_TYPES.FACE_BOX), 1);
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
// face-box for re-centering
|
2022-06-16 11:50:31 +00:00
|
|
|
faceBox = {
|
|
|
|
left: 0,
|
|
|
|
right: 100,
|
2022-07-11 12:30:37 +00:00
|
|
|
width: 100
|
2022-06-16 11:50:31 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
faceBox = this.getFaceBox(detections, threshold);
|
2022-05-19 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-16 11:50:31 +00:00
|
|
|
|
2022-05-19 09:02:40 +00:00
|
|
|
this.detectionInProgress = false;
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
return {
|
|
|
|
faceExpression,
|
2022-06-16 11:50:31 +00:00
|
|
|
faceBox,
|
|
|
|
faceCount: this.getFaceCount(detections)
|
2022-07-11 12:30:37 +00:00
|
|
|
};
|
2022-05-19 09:02:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
/**
|
|
|
|
* Returns the detection state.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-05-19 09:02:40 +00:00
|
|
|
public getDetectionInProgress(): boolean {
|
|
|
|
return this.detectionInProgress;
|
|
|
|
}
|
2022-07-11 12:30:37 +00:00
|
|
|
}
|