2020-12-23 17:26:55 +01:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2020-08-18 13:54:53 +02:00
|
|
|
import { FaceDetection } from '../classes/FaceDetection';
|
2020-12-19 17:46:41 +01:00
|
|
|
import { TNetInput } from '../dom/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { extendWithFaceDetection, WithFaceDetection } from '../factories/WithFaceDetection';
|
2020-08-26 00:24:48 +02:00
|
|
|
import { SsdMobilenetv1Options } from '../ssdMobilenetv1/SsdMobilenetv1Options';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { TinyFaceDetectorOptions } from '../tinyFaceDetector/TinyFaceDetectorOptions';
|
2020-12-19 17:46:41 +01:00
|
|
|
import { TinyYolov2Options } from '../tinyYolov2/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { ComposableTask } from './ComposableTask';
|
|
|
|
import { DetectAllFaceLandmarksTask, DetectSingleFaceLandmarksTask } from './DetectFaceLandmarksTasks';
|
|
|
|
import { nets } from './nets';
|
|
|
|
import { PredictAllAgeAndGenderTask, PredictSingleAgeAndGenderTask } from './PredictAgeAndGenderTask';
|
|
|
|
import { PredictAllFaceExpressionsTask, PredictSingleFaceExpressionsTask } from './PredictFaceExpressionsTask';
|
|
|
|
import { FaceDetectionOptions } from './types';
|
|
|
|
|
|
|
|
export class DetectFacesTaskBase<TReturn> extends ComposableTask<TReturn> {
|
|
|
|
constructor(
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-08-18 13:54:53 +02:00
|
|
|
protected input: TNetInput,
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
protected options: FaceDetectionOptions = new SsdMobilenetv1Options(),
|
2020-08-18 13:54:53 +02:00
|
|
|
) {
|
2020-12-23 17:26:55 +01:00
|
|
|
super();
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DetectAllFacesTask extends DetectFacesTaskBase<FaceDetection[]> {
|
|
|
|
public async run(): Promise<FaceDetection[]> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const { input, options } = this;
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-nested-ternary
|
2020-08-18 13:54:53 +02:00
|
|
|
const faceDetectionFunction = options instanceof TinyFaceDetectorOptions
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-shadow
|
2020-08-18 13:54:53 +02:00
|
|
|
? (input: TNetInput) => nets.tinyFaceDetector.locateFaces(input, options)
|
2020-08-26 00:24:48 +02:00
|
|
|
: (
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-nested-ternary
|
2020-08-26 00:24:48 +02:00
|
|
|
options instanceof SsdMobilenetv1Options
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-shadow
|
2020-08-26 00:24:48 +02:00
|
|
|
? (input: TNetInput) => nets.ssdMobilenetv1.locateFaces(input, options)
|
|
|
|
: (
|
|
|
|
options instanceof TinyYolov2Options
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-shadow
|
2020-08-26 00:24:48 +02:00
|
|
|
? (input: TNetInput) => nets.tinyYolov2.locateFaces(input, options)
|
|
|
|
: null
|
|
|
|
)
|
2020-12-23 17:26:55 +01:00
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
if (!faceDetectionFunction) {
|
2020-12-23 17:26:55 +01:00
|
|
|
throw new Error('detectFaces - expected options to be instance of TinyFaceDetectorOptions | SsdMobilenetv1Options | MtcnnOptions | TinyYolov2Options');
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return faceDetectionFunction(input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private runAndExtendWithFaceDetections(): Promise<WithFaceDetection<{}>[]> {
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
|
|
|
return new Promise<WithFaceDetection<{}>[]>(async (resolve) => {
|
|
|
|
const detections = await this.run();
|
|
|
|
resolve(detections.map((detection) => extendWithFaceDetection({}, detection)));
|
|
|
|
});
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceLandmarks(useTinyLandmarkNet: boolean = false) {
|
|
|
|
return new DetectAllFaceLandmarksTask(
|
|
|
|
this.runAndExtendWithFaceDetections(),
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
useTinyLandmarkNet,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceExpressions() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictAllFaceExpressionsTask(
|
2020-08-18 13:54:53 +02:00
|
|
|
this.runAndExtendWithFaceDetections(),
|
2020-12-23 17:26:55 +01:00
|
|
|
this.input,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
|
|
|
return new PredictAllAgeAndGenderTask(
|
|
|
|
this.runAndExtendWithFaceDetections(),
|
2020-12-23 17:26:55 +01:00
|
|
|
this.input,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DetectSingleFaceTask extends DetectFacesTaskBase<FaceDetection | undefined> {
|
|
|
|
public async run(): Promise<FaceDetection | undefined> {
|
|
|
|
const faceDetections = await new DetectAllFacesTask(this.input, this.options);
|
|
|
|
let faceDetectionWithHighestScore = faceDetections[0];
|
2020-12-23 17:26:55 +01:00
|
|
|
faceDetections.forEach((faceDetection) => {
|
2020-08-18 13:54:53 +02:00
|
|
|
if (faceDetection.score > faceDetectionWithHighestScore.score) {
|
|
|
|
faceDetectionWithHighestScore = faceDetection;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return faceDetectionWithHighestScore;
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:45:03 +02:00
|
|
|
private runAndExtendWithFaceDetection(): Promise<WithFaceDetection<{}> | undefined> {
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
|
|
|
return new Promise<WithFaceDetection<{}> | undefined>(async (resolve) => {
|
|
|
|
const detection = await this.run();
|
|
|
|
resolve(detection ? extendWithFaceDetection<{}>({}, detection) : undefined);
|
|
|
|
});
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceLandmarks(useTinyLandmarkNet: boolean = false) {
|
|
|
|
return new DetectSingleFaceLandmarksTask(
|
|
|
|
this.runAndExtendWithFaceDetection(),
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
useTinyLandmarkNet,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceExpressions() {
|
|
|
|
return new PredictSingleFaceExpressionsTask(
|
|
|
|
this.runAndExtendWithFaceDetection(),
|
2020-12-23 17:26:55 +01:00
|
|
|
this.input,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
|
|
|
return new PredictSingleAgeAndGenderTask(
|
|
|
|
this.runAndExtendWithFaceDetection(),
|
2020-12-23 17:26:55 +01:00
|
|
|
this.input,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
2020-12-23 17:26:55 +01:00
|
|
|
}
|