@@ -5,8 +5,9 @@ import { extractFaces, extractFaceTensors } from '../dom';
5
5
import { FaceExpressions } from '../faceExpressionNet/FaceExpressions' ;
6
6
import { WithFaceDetection } from '../factories/WithFaceDetection' ;
7
7
import { extendWithFaceExpressions , WithFaceExpressions } from '../factories/WithFaceExpressions' ;
8
+ import { isWithFaceLandmarks , WithFaceLandmarks } from '../factories/WithFaceLandmarks' ;
8
9
import { ComposableTask } from './ComposableTask' ;
9
- import { DetectAllFaceLandmarksTask , DetectSingleFaceLandmarksTask } from './DetectFaceLandmarksTasks ' ;
10
+ import { ComputeAllFaceDescriptorsTask , ComputeSingleFaceDescriptorTask } from './ComputeFaceDescriptorsTasks ' ;
10
11
import { nets } from './nets' ;
11
12
12
13
export class PredictFaceExpressionsTaskBase < TReturn , TParentReturn > extends ComposableTask < TReturn > {
@@ -26,10 +27,12 @@ export class PredictAllFaceExpressionsTask<
26
27
27
28
const parentResults = await this . parentTask
28
29
29
- const detections = parentResults . map ( parentResult => parentResult . detection )
30
+ const faceBoxes = parentResults . map (
31
+ parentResult => isWithFaceLandmarks ( parentResult ) ? parentResult . alignedRect : parentResult . detection
32
+ )
30
33
const faces : Array < HTMLCanvasElement | tf . Tensor3D > = this . input instanceof tf . Tensor
31
- ? await extractFaceTensors ( this . input , detections )
32
- : await extractFaces ( this . input , detections )
34
+ ? await extractFaceTensors ( this . input , faceBoxes )
35
+ : await extractFaces ( this . input , faceBoxes )
33
36
34
37
const faceExpressionsByFace = await Promise . all ( faces . map (
35
38
face => nets . faceExpressionNet . predictExpressions ( face )
@@ -41,13 +44,9 @@ export class PredictAllFaceExpressionsTask<
41
44
( parentResult , i ) => extendWithFaceExpressions < TSource > ( parentResult , faceExpressionsByFace [ i ] )
42
45
)
43
46
}
44
-
45
- withFaceLandmarks ( ) : DetectAllFaceLandmarksTask < WithFaceExpressions < TSource > > {
46
- return new DetectAllFaceLandmarksTask ( this , this . input , false )
47
- }
48
47
}
49
48
50
- export class PredictSingleFaceExpressionTask <
49
+ export class PredictSingleFaceExpressionsTask <
51
50
TSource extends WithFaceDetection < { } >
52
51
> extends PredictFaceExpressionsTaskBase < WithFaceExpressions < TSource > | undefined , TSource | undefined > {
53
52
@@ -58,19 +57,33 @@ export class PredictSingleFaceExpressionTask<
58
57
return
59
58
}
60
59
61
- const { detection } = parentResult
60
+ const faceBox = isWithFaceLandmarks ( parentResult ) ? parentResult . alignedRect : parentResult . detection
62
61
const faces : Array < HTMLCanvasElement | tf . Tensor3D > = this . input instanceof tf . Tensor
63
- ? await extractFaceTensors ( this . input , [ detection ] )
64
- : await extractFaces ( this . input , [ detection ] )
62
+ ? await extractFaceTensors ( this . input , [ faceBox ] )
63
+ : await extractFaces ( this . input , [ faceBox ] )
65
64
66
65
const faceExpressions = await nets . faceExpressionNet . predictExpressions ( faces [ 0 ] ) as FaceExpressions
67
66
68
67
faces . forEach ( f => f instanceof tf . Tensor && f . dispose ( ) )
69
68
70
69
return extendWithFaceExpressions ( parentResult , faceExpressions )
71
70
}
71
+ }
72
+
73
+ export class PredictAllFaceExpressionsWithFaceAlignmentTask <
74
+ TSource extends WithFaceLandmarks < WithFaceDetection < { } > >
75
+ > extends PredictAllFaceExpressionsTask < TSource > {
76
+
77
+ withFaceDescriptors ( ) : ComputeAllFaceDescriptorsTask < WithFaceLandmarks < TSource > > {
78
+ return new ComputeAllFaceDescriptorsTask < WithFaceLandmarks < TSource > > ( this , this . input )
79
+ }
80
+ }
81
+
82
+ export class PredictSingleFaceExpressionsWithFaceAlignmentTask <
83
+ TSource extends WithFaceLandmarks < WithFaceDetection < { } > >
84
+ > extends PredictSingleFaceExpressionsTask < TSource > {
72
85
73
- withFaceLandmarks ( ) : DetectSingleFaceLandmarksTask < WithFaceExpressions < TSource > > {
74
- return new DetectSingleFaceLandmarksTask ( this , this . input , false )
86
+ withFaceDescriptor ( ) : ComputeSingleFaceDescriptorTask < WithFaceLandmarks < TSource > > {
87
+ return new ComputeSingleFaceDescriptorTask < WithFaceLandmarks < TSource > > ( this , this . input )
75
88
}
76
89
}
0 commit comments