import * as tf from '../../dist/tfjs.esm.js'; import { TNetInput } from '../dom'; 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 extends ComposableTask { constructor( protected parentTask: ComposableTask | Promise, protected input: TNetInput, protected extractedFaces?: Array ) { super() } } export class PredictAllFaceExpressionsTask< TSource extends WithFaceDetection<{}> > extends PredictFaceExpressionsTaskBase[], TSource[]> { public async run(): Promise[]> { const parentResults = await this.parentTask const faceExpressionsByFace = await extractAllFacesAndComputeResults( parentResults, this.input, async faces => await Promise.all(faces.map( face => nets.faceExpressionNet.predictExpressions(face) as Promise )), this.extractedFaces ) return parentResults.map( (parentResult, i) => extendWithFaceExpressions(parentResult, faceExpressionsByFace[i]) ) } withAgeAndGender() { return new PredictAllAgeAndGenderTask(this, this.input) } } export class PredictSingleFaceExpressionsTask< TSource extends WithFaceDetection<{}> > extends PredictFaceExpressionsTaskBase | undefined, TSource | undefined> { public async run(): Promise | undefined> { const parentResult = await this.parentTask if (!parentResult) { return } const faceExpressions = await extractSingleFaceAndComputeResult( parentResult, this.input, face => nets.faceExpressionNet.predictExpressions(face) as Promise, this.extractedFaces ) return extendWithFaceExpressions(parentResult, faceExpressions) } withAgeAndGender() { return new PredictSingleAgeAndGenderTask(this, this.input) } } export class PredictAllFaceExpressionsWithFaceAlignmentTask< TSource extends WithFaceLandmarks> > extends PredictAllFaceExpressionsTask { withAgeAndGender() { return new PredictAllAgeAndGenderWithFaceAlignmentTask(this, this.input) } withFaceDescriptors() { return new ComputeAllFaceDescriptorsTask(this, this.input) } } export class PredictSingleFaceExpressionsWithFaceAlignmentTask< TSource extends WithFaceLandmarks> > extends PredictSingleFaceExpressionsTask { withAgeAndGender() { return new PredictSingleAgeAndGenderWithFaceAlignmentTask(this, this.input) } withFaceDescriptor() { return new ComputeSingleFaceDescriptorTask(this, this.input) } }