gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* eslint-disable max-classes-per-file */
import { FaceDetection } from '../classes/FaceDetection';
import { TNetInput } from '../dom/index';
import { extendWithFaceDetection, WithFaceDetection } from '../factories/WithFaceDetection';
import { SsdMobilenetv1Options } from '../ssdMobilenetv1/SsdMobilenetv1Options';
import { TinyFaceDetectorOptions } from '../tinyFaceDetector/TinyFaceDetectorOptions';
import { TinyYolov2Options } from '../tinyYolov2/index';
import { ComposableTask } from './ComposableTask';
import { DetectAllFaceLandmarksTask, DetectSingleFaceLandmarksTask } from './DetectFaceLandmarksTasks';
import { nets } from './nets';
import { PredictAllAgeAndGenderTask, PredictSingleAgeAndGenderTask } from './PredictAgeAndGenderTask';
import { PredictAllFaceExpressionsTask, PredictSingleFaceExpressionsTask } from './PredictFaceExpressionsTask';
import { FaceDetectionOptions } from './types';
 
export class DetectFacesTaskBase<TReturn> extends ComposableTask<TReturn> {
  // eslint-disable-next-line no-unused-vars
  constructor(protected input: TNetInput, protected options: FaceDetectionOptions = new SsdMobilenetv1Options()) {
    super();
  }
}
 
export class DetectAllFacesTask extends DetectFacesTaskBase<FaceDetection[]> {
  public override async run(): Promise<FaceDetection[]> {
    const { input, options } = this;
    let result;
    if (options instanceof TinyFaceDetectorOptions) result = nets.tinyFaceDetector.locateFaces(input, options);
    else if (options instanceof SsdMobilenetv1Options) result = nets.ssdMobilenetv1.locateFaces(input, options);
    else if (options instanceof TinyYolov2Options) result = nets.tinyYolov2.locateFaces(input, options);
    else throw new Error('detectFaces - expected options to be instance of TinyFaceDetectorOptions | SsdMobilenetv1Options | TinyYolov2Options');
    return result;
  }
 
  private runAndExtendWithFaceDetections(): Promise<WithFaceDetection<{}>[]> {
    return new Promise<WithFaceDetection<{}>[]>((resolve, reject) => {
      this.run()
        .then((detections) => resolve(detections.map((detection) => extendWithFaceDetection({}, detection))))
        .catch((err) => reject(err));
    });
  }
 
  withFaceLandmarks(useTinyLandmarkNet = false) {
    return new DetectAllFaceLandmarksTask(
      this.runAndExtendWithFaceDetections(),
      this.input,
      useTinyLandmarkNet,
    );
  }
 
  withFaceExpressions() {
    return new PredictAllFaceExpressionsTask(
      this.runAndExtendWithFaceDetections(),
      this.input,
    );
  }
 
  withAgeAndGender() {
    return new PredictAllAgeAndGenderTask(
      this.runAndExtendWithFaceDetections(),
      this.input,
    );
  }
}
 
export class DetectSingleFaceTask extends DetectFacesTaskBase<FaceDetection | undefined> {
  public override async run(): Promise<FaceDetection | undefined> {
    const faceDetections = await new DetectAllFacesTask(this.input, this.options);
    let faceDetectionWithHighestScore = faceDetections[0];
    faceDetections.forEach((faceDetection) => {
      if (faceDetection.score > faceDetectionWithHighestScore.score) faceDetectionWithHighestScore = faceDetection;
    });
    return faceDetectionWithHighestScore;
  }
 
  private runAndExtendWithFaceDetection(): Promise<WithFaceDetection<{}> | undefined> {
    // eslint-disable-next-line no-async-promise-executor
    return new Promise<WithFaceDetection<{}> | undefined>(async (resolve) => {
      const detection = await this.run();
      resolve(detection ? extendWithFaceDetection<{}>({}, detection) : undefined);
    });
  }
 
  withFaceLandmarks(useTinyLandmarkNet = false) {
    return new DetectSingleFaceLandmarksTask(
      this.runAndExtendWithFaceDetection(),
      this.input,
      useTinyLandmarkNet,
    );
  }
 
  withFaceExpressions() {
    return new PredictSingleFaceExpressionsTask(
      this.runAndExtendWithFaceDetection(),
      this.input,
    );
  }
 
  withAgeAndGender() {
    return new PredictSingleAgeAndGenderTask(
      this.runAndExtendWithFaceDetection(),
      this.input,
    );
  }
}