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 { FaceLandmarks68 } from '../classes/FaceLandmarks68';
|
2020-12-19 17:46:41 +01:00
|
|
|
import { extractFaces, extractFaceTensors, TNetInput } from '../dom/index';
|
2020-08-18 13:54:53 +02:00
|
|
|
import { FaceLandmark68Net } from '../faceLandmarkNet/FaceLandmark68Net';
|
|
|
|
import { FaceLandmark68TinyNet } from '../faceLandmarkNet/FaceLandmark68TinyNet';
|
|
|
|
import { WithFaceDetection } from '../factories/WithFaceDetection';
|
|
|
|
import { extendWithFaceLandmarks, WithFaceLandmarks } from '../factories/WithFaceLandmarks';
|
|
|
|
import { ComposableTask } from './ComposableTask';
|
|
|
|
import { ComputeAllFaceDescriptorsTask, ComputeSingleFaceDescriptorTask } from './ComputeFaceDescriptorsTasks';
|
|
|
|
import { nets } from './nets';
|
2021-03-19 23:46:36 +01:00
|
|
|
import { PredictAllAgeAndGenderWithFaceAlignmentTask, PredictSingleAgeAndGenderWithFaceAlignmentTask } from './PredictAgeAndGenderTask';
|
|
|
|
import { PredictAllFaceExpressionsWithFaceAlignmentTask, PredictSingleFaceExpressionsWithFaceAlignmentTask } from './PredictFaceExpressionsTask';
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
export class DetectFaceLandmarksTaskBase<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 useTinyLandmarkNet: boolean,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
protected get landmarkNet(): FaceLandmark68Net | FaceLandmark68TinyNet {
|
|
|
|
return this.useTinyLandmarkNet
|
|
|
|
? nets.faceLandmark68TinyNet
|
2020-12-23 17:26:55 +01:00
|
|
|
: nets.faceLandmark68Net;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DetectAllFaceLandmarksTask<
|
|
|
|
TSource extends WithFaceDetection<{}>
|
|
|
|
> extends DetectFaceLandmarksTaskBase<WithFaceLandmarks<TSource>[], TSource[]> {
|
|
|
|
public async run(): Promise<WithFaceLandmarks<TSource>[]> {
|
2020-12-23 17:26:55 +01:00
|
|
|
const parentResults = await this.parentTask;
|
|
|
|
const detections = parentResults.map((res) => res.detection);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
const faces: Array<HTMLCanvasElement | tf.Tensor3D> = this.input instanceof tf.Tensor
|
|
|
|
? await extractFaceTensors(this.input, detections)
|
2020-12-23 17:26:55 +01:00
|
|
|
: await extractFaces(this.input, detections);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
const faceLandmarksByFace = await Promise.all(faces.map(
|
2020-12-23 17:26:55 +01:00
|
|
|
(face) => this.landmarkNet.detectLandmarks(face),
|
|
|
|
)) as FaceLandmarks68[];
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
faces.forEach((f) => f instanceof tf.Tensor && f.dispose());
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return parentResults.map((parentResult, i) => extendWithFaceLandmarks<TSource>(parentResult, faceLandmarksByFace[i]));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 DetectSingleFaceLandmarksTask<
|
|
|
|
TSource extends WithFaceDetection<{}>
|
2020-12-23 17:26:55 +01:00
|
|
|
> extends DetectFaceLandmarksTaskBase<WithFaceLandmarks<TSource> | undefined, TSource | undefined> {
|
2020-08-18 13:54:53 +02:00
|
|
|
public async run(): Promise<WithFaceLandmarks<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
|
|
|
}
|
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
const { detection } = parentResult;
|
2020-08-18 13:54:53 +02:00
|
|
|
const faces: Array<HTMLCanvasElement | tf.Tensor3D> = this.input instanceof tf.Tensor
|
|
|
|
? await extractFaceTensors(this.input, [detection])
|
2020-12-23 17:26:55 +01:00
|
|
|
: await extractFaces(this.input, [detection]);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
const landmarks = await this.landmarkNet.detectLandmarks(faces[0]) as FaceLandmarks68;
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
faces.forEach((f) => f instanceof tf.Tensor && f.dispose());
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return extendWithFaceLandmarks<TSource>(parentResult, landmarks);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|