face-api/build/globalApi/DetectFacesTasks.js

75 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-08-20 02:10:42 +02:00
import { extendWithFaceDetection } from '../factories/WithFaceDetection';
2020-08-26 00:24:48 +02:00
import { SsdMobilenetv1Options } from '../ssdMobilenetv1/SsdMobilenetv1Options';
2020-08-20 02:10:42 +02:00
import { TinyFaceDetectorOptions } from '../tinyFaceDetector/TinyFaceDetectorOptions';
2020-08-26 00:24:48 +02:00
import { TinyYolov2Options } from '../tinyYolov2';
2020-08-20 02:10:42 +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';
export class DetectFacesTaskBase extends ComposableTask {
2020-08-26 00:24:48 +02:00
constructor(input, options = new SsdMobilenetv1Options()) {
2020-08-18 14:04:33 +02:00
super();
this.input = input;
this.options = options;
}
}
2020-08-20 02:10:42 +02:00
export class DetectAllFacesTask extends DetectFacesTaskBase {
2020-08-18 14:04:33 +02:00
async run() {
const { input, options } = this;
2020-08-20 02:10:42 +02:00
const faceDetectionFunction = options instanceof TinyFaceDetectorOptions
? (input) => nets.tinyFaceDetector.locateFaces(input, options)
2020-08-26 00:24:48 +02:00
: (options instanceof SsdMobilenetv1Options
? (input) => nets.ssdMobilenetv1.locateFaces(input, options)
: (options instanceof TinyYolov2Options
? (input) => nets.tinyYolov2.locateFaces(input, options)
: null));
2020-08-18 14:04:33 +02:00
if (!faceDetectionFunction) {
throw new Error('detectFaces - expected options to be instance of TinyFaceDetectorOptions | SsdMobilenetv1Options | MtcnnOptions | TinyYolov2Options');
}
return faceDetectionFunction(input);
}
runAndExtendWithFaceDetections() {
return new Promise(async (res) => {
const detections = await this.run();
2020-08-20 02:10:42 +02:00
return res(detections.map(detection => extendWithFaceDetection({}, detection)));
2020-08-18 14:04:33 +02:00
});
}
withFaceLandmarks(useTinyLandmarkNet = false) {
2020-08-20 02:10:42 +02:00
return new DetectAllFaceLandmarksTask(this.runAndExtendWithFaceDetections(), this.input, useTinyLandmarkNet);
2020-08-18 14:04:33 +02:00
}
withFaceExpressions() {
2020-08-20 02:10:42 +02:00
return new PredictAllFaceExpressionsTask(this.runAndExtendWithFaceDetections(), this.input);
2020-08-18 14:04:33 +02:00
}
withAgeAndGender() {
2020-08-20 02:10:42 +02:00
return new PredictAllAgeAndGenderTask(this.runAndExtendWithFaceDetections(), this.input);
2020-08-18 14:04:33 +02:00
}
}
2020-08-20 02:10:42 +02:00
export class DetectSingleFaceTask extends DetectFacesTaskBase {
2020-08-18 14:04:33 +02:00
async run() {
const faceDetections = await new DetectAllFacesTask(this.input, this.options);
let faceDetectionWithHighestScore = faceDetections[0];
faceDetections.forEach(faceDetection => {
if (faceDetection.score > faceDetectionWithHighestScore.score) {
faceDetectionWithHighestScore = faceDetection;
}
});
return faceDetectionWithHighestScore;
}
runAndExtendWithFaceDetection() {
return new Promise(async (res) => {
const detection = await this.run();
2020-08-20 02:10:42 +02:00
return res(detection ? extendWithFaceDetection({}, detection) : undefined);
2020-08-18 14:04:33 +02:00
});
}
withFaceLandmarks(useTinyLandmarkNet = false) {
2020-08-20 02:10:42 +02:00
return new DetectSingleFaceLandmarksTask(this.runAndExtendWithFaceDetection(), this.input, useTinyLandmarkNet);
2020-08-18 14:04:33 +02:00
}
withFaceExpressions() {
2020-08-20 02:10:42 +02:00
return new PredictSingleFaceExpressionsTask(this.runAndExtendWithFaceDetection(), this.input);
2020-08-18 14:04:33 +02:00
}
withAgeAndGender() {
2020-08-20 02:10:42 +02:00
return new PredictSingleAgeAndGenderTask(this.runAndExtendWithFaceDetection(), this.input);
2020-08-18 14:04:33 +02:00
}
}
//# sourceMappingURL=DetectFacesTasks.js.map