2020-08-20 02:05:34 +02:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.DetectSingleFaceTask = exports.DetectAllFacesTask = exports.DetectFacesTaskBase = void 0;
|
|
|
|
const WithFaceDetection_1 = require("../factories/WithFaceDetection");
|
|
|
|
const TinyFaceDetectorOptions_1 = require("../tinyFaceDetector/TinyFaceDetectorOptions");
|
|
|
|
const ComposableTask_1 = require("./ComposableTask");
|
|
|
|
const DetectFaceLandmarksTasks_1 = require("./DetectFaceLandmarksTasks");
|
|
|
|
const nets_1 = require("./nets");
|
|
|
|
const PredictAgeAndGenderTask_1 = require("./PredictAgeAndGenderTask");
|
|
|
|
const PredictFaceExpressionsTask_1 = require("./PredictFaceExpressionsTask");
|
|
|
|
class DetectFacesTaskBase extends ComposableTask_1.ComposableTask {
|
|
|
|
constructor(input, options = new TinyFaceDetectorOptions_1.TinyFaceDetectorOptions()) {
|
2020-08-18 14:04:33 +02:00
|
|
|
super();
|
|
|
|
this.input = input;
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 02:05:34 +02:00
|
|
|
exports.DetectFacesTaskBase = DetectFacesTaskBase;
|
|
|
|
class DetectAllFacesTask extends DetectFacesTaskBase {
|
2020-08-18 14:04:33 +02:00
|
|
|
async run() {
|
|
|
|
const { input, options } = this;
|
2020-08-20 02:05:34 +02:00
|
|
|
const faceDetectionFunction = options instanceof TinyFaceDetectorOptions_1.TinyFaceDetectorOptions
|
|
|
|
? (input) => nets_1.nets.tinyFaceDetector.locateFaces(input, options)
|
2020-08-18 14:04:33 +02:00
|
|
|
: null;
|
|
|
|
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:05:34 +02:00
|
|
|
return res(detections.map(detection => WithFaceDetection_1.extendWithFaceDetection({}, detection)));
|
2020-08-18 14:04:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
withFaceLandmarks(useTinyLandmarkNet = false) {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new DetectFaceLandmarksTasks_1.DetectAllFaceLandmarksTask(this.runAndExtendWithFaceDetections(), this.input, useTinyLandmarkNet);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
withFaceExpressions() {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new PredictFaceExpressionsTask_1.PredictAllFaceExpressionsTask(this.runAndExtendWithFaceDetections(), this.input);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
withAgeAndGender() {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new PredictAgeAndGenderTask_1.PredictAllAgeAndGenderTask(this.runAndExtendWithFaceDetections(), this.input);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 02:05:34 +02:00
|
|
|
exports.DetectAllFacesTask = DetectAllFacesTask;
|
|
|
|
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:05:34 +02:00
|
|
|
return res(detection ? WithFaceDetection_1.extendWithFaceDetection({}, detection) : undefined);
|
2020-08-18 14:04:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
withFaceLandmarks(useTinyLandmarkNet = false) {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new DetectFaceLandmarksTasks_1.DetectSingleFaceLandmarksTask(this.runAndExtendWithFaceDetection(), this.input, useTinyLandmarkNet);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
withFaceExpressions() {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new PredictFaceExpressionsTask_1.PredictSingleFaceExpressionsTask(this.runAndExtendWithFaceDetection(), this.input);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
withAgeAndGender() {
|
2020-08-20 02:05:34 +02:00
|
|
|
return new PredictAgeAndGenderTask_1.PredictSingleAgeAndGenderTask(this.runAndExtendWithFaceDetection(), this.input);
|
2020-08-18 14:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 02:05:34 +02:00
|
|
|
exports.DetectSingleFaceTask = DetectSingleFaceTask;
|
2020-08-18 14:04:33 +02:00
|
|
|
//# sourceMappingURL=DetectFacesTasks.js.map
|