face-api/src/globalApi/PredictAgeAndGenderTask.ts

107 lines
3.9 KiB
TypeScript
Raw Normal View History

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
import { AgeAndGenderPrediction } from '../ageGenderNet/types';
2020-12-19 17:46:41 +01:00
import { TNetInput } from '../dom/index';
2020-08-18 13:54:53 +02:00
import { extendWithAge, WithAge } from '../factories/WithAge';
import { WithFaceDetection } from '../factories/WithFaceDetection';
import { WithFaceLandmarks } from '../factories/WithFaceLandmarks';
import { extendWithGender, WithGender } from '../factories/WithGender';
import { ComposableTask } from './ComposableTask';
import { ComputeAllFaceDescriptorsTask, ComputeSingleFaceDescriptorTask } from './ComputeFaceDescriptorsTasks';
import { extractAllFacesAndComputeResults, extractSingleFaceAndComputeResult } from './extractFacesAndComputeResults';
import { nets } from './nets';
import {
PredictAllFaceExpressionsTask,
PredictAllFaceExpressionsWithFaceAlignmentTask,
PredictSingleFaceExpressionsTask,
PredictSingleFaceExpressionsWithFaceAlignmentTask,
} from './PredictFaceExpressionsTask';
export class PredictAgeAndGenderTaskBase<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 PredictAllAgeAndGenderTask<
TSource extends WithFaceDetection<{}>
> extends PredictAgeAndGenderTaskBase<WithAge<WithGender<TSource>>[], TSource[]> {
public async run(): Promise<WithAge<WithGender<TSource>>[]> {
2020-12-23 17:26:55 +01:00
const parentResults = await this.parentTask;
2020-08-18 13:54:53 +02:00
const ageAndGenderByFace = await extractAllFacesAndComputeResults<TSource, AgeAndGenderPrediction[]>(
parentResults,
this.input,
2020-12-23 17:26:55 +01:00
async (faces) => Promise.all(faces.map(
(face) => nets.ageGenderNet.predictAgeAndGender(face) as Promise<AgeAndGenderPrediction>,
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((parentResult, i) => {
2020-12-23 17:26:55 +01:00
const { age, gender, genderProbability } = ageAndGenderByFace[i];
return extendWithAge(extendWithGender(parentResult, gender, genderProbability), age);
});
2020-08-18 13:54:53 +02:00
}
withFaceExpressions() {
2020-12-23 17:26:55 +01:00
return new PredictAllFaceExpressionsTask(this, this.input);
2020-08-18 13:54:53 +02:00
}
}
export class PredictSingleAgeAndGenderTask<
TSource extends WithFaceDetection<{}>
2020-12-23 17:26:55 +01:00
> extends PredictAgeAndGenderTaskBase<WithAge<WithGender<TSource>> | undefined, TSource | undefined> {
2020-08-18 13:54:53 +02:00
public async run(): Promise<WithAge<WithGender<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 { age, gender, genderProbability } = await extractSingleFaceAndComputeResult<TSource, AgeAndGenderPrediction>(
parentResult,
this.input,
2020-12-23 17:26:55 +01:00
(face) => nets.ageGenderNet.predictAgeAndGender(face) as Promise<AgeAndGenderPrediction>,
this.extractedFaces,
);
2020-08-18 13:54:53 +02:00
2020-12-23 17:26:55 +01:00
return extendWithAge(extendWithGender(parentResult, gender, genderProbability), age);
2020-08-18 13:54:53 +02:00
}
withFaceExpressions() {
2020-12-23 17:26:55 +01:00
return new PredictSingleFaceExpressionsTask(this, this.input);
2020-08-18 13:54:53 +02:00
}
}
export class PredictAllAgeAndGenderWithFaceAlignmentTask<
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
> extends PredictAllAgeAndGenderTask<TSource> {
withFaceExpressions() {
2020-12-23 17:26:55 +01:00
return new PredictAllFaceExpressionsWithFaceAlignmentTask(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 PredictSingleAgeAndGenderWithFaceAlignmentTask<
TSource extends WithFaceLandmarks<WithFaceDetection<{}>>
> extends PredictSingleAgeAndGenderTask<TSource> {
withFaceExpressions() {
2020-12-23 17:26:55 +01:00
return new PredictSingleFaceExpressionsWithFaceAlignmentTask(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
}