2020-12-23 17:26:55 +01:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2020-12-23 18:58:47 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm';
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-19 17:46:41 +01:00
|
|
|
import { TNetInput } from '../dom/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { FaceExpressions } from '../faceExpressionNet/FaceExpressions';
|
|
|
|
import { WithFaceDetection } from '../factories/WithFaceDetection';
|
|
|
|
import { extendWithFaceExpressions, WithFaceExpressions } from '../factories/WithFaceExpressions';
|
|
|
|
import { WithFaceLandmarks } from '../factories/WithFaceLandmarks';
|
|
|
|
import { ComposableTask } from './ComposableTask';
|
|
|
|
import { ComputeAllFaceDescriptorsTask, ComputeSingleFaceDescriptorTask } from './ComputeFaceDescriptorsTasks';
|
|
|
|
import { extractAllFacesAndComputeResults, extractSingleFaceAndComputeResult } from './extractFacesAndComputeResults';
|
|
|
|
import { nets } from './nets';
|
|
|
|
import {
|
|
|
|
PredictAllAgeAndGenderTask,
|
|
|
|
PredictAllAgeAndGenderWithFaceAlignmentTask,
|
|
|
|
PredictSingleAgeAndGenderTask,
|
|
|
|
PredictSingleAgeAndGenderWithFaceAlignmentTask,
|
|
|
|
} from './PredictAgeAndGenderTask';
|
|
|
|
|
|
|
|
export class PredictFaceExpressionsTaskBase<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
|
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 extractedFaces?: Array<HTMLCanvasElement | tf.Tensor3D>,
|
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 PredictAllFaceExpressionsTask<
|
|
|
|
TSource extends WithFaceDetection<{}>
|
|
|
|
> extends PredictFaceExpressionsTaskBase<WithFaceExpressions<TSource>[], TSource[]> {
|
|
|
|
public async run(): Promise<WithFaceExpressions<TSource>[]> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const parentResults = await this.parentTask;
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
const faceExpressionsByFace = await extractAllFacesAndComputeResults<TSource, FaceExpressions[]>(
|
|
|
|
parentResults,
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
async (faces) => Promise.all(faces.map(
|
|
|
|
(face) => nets.faceExpressionNet.predictExpressions(face) as Promise<FaceExpressions>,
|
2020-08-18 13:54:53 +02:00
|
|
|
)),
|
2020-12-23 17:26:55 +01:00
|
|
|
this.extractedFaces,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
return parentResults.map(
|
2020-12-23 17:26:55 +01:00
|
|
|
(parentResult, i) => extendWithFaceExpressions<TSource>(parentResult, faceExpressionsByFace[i]),
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictAllAgeAndGenderTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PredictSingleFaceExpressionsTask<
|
|
|
|
TSource extends WithFaceDetection<{}>
|
2020-12-23 17:26:55 +01:00
|
|
|
> extends PredictFaceExpressionsTaskBase<WithFaceExpressions<TSource> | undefined, TSource | undefined> {
|
2020-08-18 13:54:53 +02:00
|
|
|
public async run(): Promise<WithFaceExpressions<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 faceExpressions = await extractSingleFaceAndComputeResult<TSource, FaceExpressions>(
|
|
|
|
parentResult,
|
|
|
|
this.input,
|
2020-12-23 17:26:55 +01:00
|
|
|
(face) => nets.faceExpressionNet.predictExpressions(face) as Promise<FaceExpressions>,
|
|
|
|
this.extractedFaces,
|
|
|
|
);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return extendWithFaceExpressions(parentResult, faceExpressions);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictSingleAgeAndGenderTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PredictAllFaceExpressionsWithFaceAlignmentTask<
|
|
|
|
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
|
|
|
|
> extends PredictAllFaceExpressionsTask<TSource> {
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictAllAgeAndGenderWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceDescriptors() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new ComputeAllFaceDescriptorsTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PredictSingleFaceExpressionsWithFaceAlignmentTask<
|
|
|
|
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
|
|
|
|
> extends PredictSingleFaceExpressionsTask<TSource> {
|
|
|
|
withAgeAndGender() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new PredictSingleAgeAndGenderWithFaceAlignmentTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
withFaceDescriptor() {
|
2020-12-23 17:26:55 +01:00
|
|
|
return new ComputeSingleFaceDescriptorTask(this, this.input);
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
2020-12-23 17:26:55 +01:00
|
|
|
}
|