2020-12-23 17:26:55 +01:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2020-12-19 17:46:41 +01:00
|
|
|
import { TNetInput } from '../dom/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { extendWithFaceDescriptor, WithFaceDescriptor } from '../factories/WithFaceDescriptor';
|
|
|
|
import { WithFaceDetection } from '../factories/WithFaceDetection';
|
|
|
|
import { WithFaceLandmarks } from '../factories/WithFaceLandmarks';
|
|
|
|
import { ComposableTask } from './ComposableTask';
|
|
|
|
import { extractAllFacesAndComputeResults, extractSingleFaceAndComputeResult } from './extractFacesAndComputeResults';
|
|
|
|
import { nets } from './nets';
|
|
|
|
import {
|
|
|
|
PredictAllAgeAndGenderWithFaceAlignmentTask,
|
|
|
|
PredictSingleAgeAndGenderWithFaceAlignmentTask,
|
|
|
|
} from './PredictAgeAndGenderTask';
|
|
|
|
import {
|
|
|
|
PredictAllFaceExpressionsWithFaceAlignmentTask,
|
|
|
|
PredictSingleFaceExpressionsWithFaceAlignmentTask,
|
|
|
|
} from './PredictFaceExpressionsTask';
|
|
|
|
|
|
|
|
export class ComputeFaceDescriptorsTaskBase<TReturn, TParentReturn> 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 parentTask: ComposableTask<TParentReturn> | Promise<TParentReturn>,
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
protected input: TNetInput,
|
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 ComputeAllFaceDescriptorsTask<
|
|
|
|
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
|
|
|
|
> extends ComputeFaceDescriptorsTaskBase<WithFaceDescriptor<TSource>[], TSource[]> {
|
|
|
|
public async run(): Promise<WithFaceDescriptor<TSource>[]> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const parentResults = await this.parentTask;
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
const descriptors = await extractAllFacesAndComputeResults<TSource, Float32Array[]>(
|
|
|
|
parentResults,
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
(faces) => Promise.all(faces.map((face) => nets.faceRecognitionNet.computeFaceDescriptor(face) as Promise<Float32Array>)),
|
2020-08-18 13:54:53 +02:00
|
|
|
null,
|
2020-12-23 17:26:55 +01:00
|
|
|
(parentResult) => parentResult.landmarks.align(null, { useDlibAlignment: true }),
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return descriptors.map((descriptor, i) => extendWithFaceDescriptor<TSource>(parentResults[i], descriptor));
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceExpressions() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictAllFaceExpressionsWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictAllAgeAndGenderWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ComputeSingleFaceDescriptorTask<
|
|
|
|
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
|
|
|
|
> extends ComputeFaceDescriptorsTaskBase<WithFaceDescriptor<TSource> | undefined, TSource | undefined> {
|
|
|
|
public async run(): Promise<WithFaceDescriptor<TSource> | undefined> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const parentResult = await this.parentTask;
|
2020-08-18 13:54:53 +02:00
|
|
|
if (!parentResult) {
|
2020-12-19 17:46:41 +01:00
|
|
|
return undefined;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
const descriptor = await extractSingleFaceAndComputeResult<TSource, Float32Array>(
|
|
|
|
parentResult,
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
(face) => nets.faceRecognitionNet.computeFaceDescriptor(face) as Promise<Float32Array>,
|
2020-08-18 13:54:53 +02:00
|
|
|
null,
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line no-shadow
|
|
|
|
(parentResult) => parentResult.landmarks.align(null, { useDlibAlignment: true }),
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return extendWithFaceDescriptor(parentResult, descriptor);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceExpressions() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictSingleFaceExpressionsWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictSingleAgeAndGenderWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
2020-12-23 17:26:55 +01:00
|
|
|
}
|