1
1
import * as faceapi from '../../src' ;
2
2
import { FaceDetection } from '../../src/faceDetectionNet/FaceDetection' ;
3
3
import { IRect } from '../../src/Rect' ;
4
+ import { expectMaxDelta } from '../utils' ;
4
5
5
- function expectFaceDetectionEquals ( result : FaceDetection , score : number , expectedBox : IRect ) {
6
- const { x, y, width, height } = result . getBox ( )
7
- expect ( result . getScore ( ) ) . toBeCloseTo ( score , 2 )
8
- expect ( Math . floor ( x ) ) . toEqual ( expectedBox . x )
9
- expect ( Math . floor ( y ) ) . toEqual ( expectedBox . y )
10
- expect ( Math . floor ( width ) ) . toEqual ( expectedBox . width )
11
- expect ( Math . floor ( height ) ) . toEqual ( expectedBox . height )
6
+ function expectRectClose (
7
+ result : IRect ,
8
+ expectedBox : IRect ,
9
+ maxDelta : number
10
+ ) {
11
+ const { x, y, width, height } = result
12
+ expectMaxDelta ( x , expectedBox . x , maxDelta )
13
+ expectMaxDelta ( y , expectedBox . y , maxDelta )
14
+ expectMaxDelta ( width , expectedBox . width , maxDelta )
15
+ expectMaxDelta ( height , expectedBox . height , maxDelta )
12
16
}
13
17
18
+ const expectedBoxes = [
19
+ { x : 48 , y : 253 , width : 104 , height : 129 } ,
20
+ { x : 260 , y : 227 , width : 76 , height : 117 } ,
21
+ { x : 466 , y : 165 , width : 88 , height : 130 } ,
22
+ { x : 234 , y : 36 , width : 84 , height : 119 } ,
23
+ { x : 577 , y : 65 , width : 84 , height : 105 } ,
24
+ { x : 84 , y : 14 , width : 79 , height : 132 }
25
+ ]
26
+
14
27
describe ( 'faceDetectionNet' , ( ) => {
15
28
16
- let faceDetectionNet : faceapi . FaceDetectionNet , imgEl : HTMLImageElement
29
+ let imgEl : HTMLImageElement
17
30
18
31
beforeAll ( async ( ) => {
19
- const res = await fetch ( 'base/weights/uncompressed/face_detection_model.weights' )
20
- const weights = new Float32Array ( await res . arrayBuffer ( ) )
21
- faceDetectionNet = faceapi . faceDetectionNet ( weights )
22
-
23
32
const img = await ( await fetch ( 'base/test/images/faces.jpg' ) ) . blob ( )
24
33
imgEl = await faceapi . bufferToImage ( img )
25
34
} )
26
35
27
- it ( 'scores > 0.8' , async ( ) => {
28
- const { width, height } = imgEl
36
+ describe ( 'uncompressed weights' , ( ) => {
37
+
38
+ let faceDetectionNet : faceapi . FaceDetectionNet
39
+
40
+ const expectedScores = [ 0.98 , 0.89 , 0.82 , 0.75 , 0.58 , 0.55 ]
41
+ const maxBoxDelta = 1
42
+
43
+ beforeAll ( async ( ) => {
44
+ const res = await fetch ( 'base/weights/uncompressed/face_detection_model.weights' )
45
+ const weights = new Float32Array ( await res . arrayBuffer ( ) )
46
+ faceDetectionNet = faceapi . faceDetectionNet ( weights )
47
+ } )
29
48
30
- const result = await faceDetectionNet . locateFaces ( imgEl ) as FaceDetection [ ]
31
- expect ( result . length ) . toEqual ( 3 )
49
+ it ( 'scores > 0.8' , async ( ) => {
50
+ const detections = await faceDetectionNet . locateFaces ( imgEl ) as FaceDetection [ ]
32
51
33
- result . forEach ( res => {
34
- expect ( res . getImageWidth ( ) ) . toEqual ( width )
35
- expect ( res . getImageHeight ( ) ) . toEqual ( height )
52
+ expect ( detections . length ) . toEqual ( 3 )
53
+ detections . forEach ( ( det , i ) => {
54
+ expect ( det . getImageWidth ( ) ) . toEqual ( imgEl . width )
55
+ expect ( det . getImageHeight ( ) ) . toEqual ( imgEl . height )
56
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
57
+ expectRectClose ( det . getBox ( ) , expectedBoxes [ i ] , maxBoxDelta )
58
+ } )
36
59
} )
37
- const [ d0 , d1 , d2 ] = result
38
- expectFaceDetectionEquals ( d0 , 0.98 , { x : 48 , y : 253 , width : 104 , height : 129 } )
39
- expectFaceDetectionEquals ( d1 , 0.89 , { x : 260 , y : 227 , width : 76 , height : 117 } )
40
- expectFaceDetectionEquals ( d2 , 0.82 , { x : 466 , y : 165 , width : 88 , height : 130 } )
60
+
61
+ it ( 'scores > 0.5' , async ( ) => {
62
+ const detections = await faceDetectionNet . locateFaces ( imgEl , 0.5 ) as FaceDetection [ ]
63
+
64
+ expect ( detections . length ) . toEqual ( 6 )
65
+ detections . forEach ( ( det , i ) => {
66
+ expect ( det . getImageWidth ( ) ) . toEqual ( imgEl . width )
67
+ expect ( det . getImageHeight ( ) ) . toEqual ( imgEl . height )
68
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
69
+ expectRectClose ( det . getBox ( ) , expectedBoxes [ i ] , maxBoxDelta )
70
+ } )
71
+ } )
72
+
41
73
} )
42
74
43
- it ( 'scores > 0.5' , async ( ) => {
44
- const { width, height } = imgEl
75
+ describe ( 'quantized weights' , ( ) => {
45
76
46
- const result = await faceDetectionNet . locateFaces ( imgEl , 0.5 ) as FaceDetection [ ]
47
- expect ( result . length ) . toEqual ( 6 )
77
+ let faceDetectionNet : faceapi . FaceDetectionNet
48
78
49
- result . forEach ( res => {
50
- expect ( res . getImageWidth ( ) ) . toEqual ( width )
51
- expect ( res . getImageHeight ( ) ) . toEqual ( height )
79
+ const expectedScores = [ 0.97 , 0.88 , 0.83 , 0.82 , 0.59 , 0.52 ]
80
+ const maxBoxDelta = 5
81
+
82
+ beforeAll ( async ( ) => {
83
+ faceDetectionNet = new faceapi . FaceDetectionNet ( )
84
+ await faceDetectionNet . load ( 'base/weights' )
52
85
} )
53
- const [ d0 , d1 , d2 , d3 , d4 , d5 ] = result
54
- expectFaceDetectionEquals ( d0 , 0.98 , { x : 48 , y : 253 , width : 104 , height : 129 } )
55
- expectFaceDetectionEquals ( d1 , 0.89 , { x : 260 , y : 227 , width : 76 , height : 117 } )
56
- expectFaceDetectionEquals ( d2 , 0.82 , { x : 466 , y : 165 , width : 88 , height : 130 } )
57
- expectFaceDetectionEquals ( d3 , 0.75 , { x : 234 , y : 36 , width : 84 , height : 119 } )
58
- expectFaceDetectionEquals ( d4 , 0.58 , { x : 577 , y : 65 , width : 84 , height : 105 } )
59
- expectFaceDetectionEquals ( d5 , 0.55 , { x : 84 , y : 14 , width : 79 , height : 132 } )
86
+
87
+ it ( 'scores > 0.8' , async ( ) => {
88
+ const detections = await faceDetectionNet . locateFaces ( imgEl ) as FaceDetection [ ]
89
+
90
+ expect ( detections . length ) . toEqual ( 4 )
91
+ detections . forEach ( ( det , i ) => {
92
+ expect ( det . getImageWidth ( ) ) . toEqual ( imgEl . width )
93
+ expect ( det . getImageHeight ( ) ) . toEqual ( imgEl . height )
94
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
95
+ expectRectClose ( det . getBox ( ) , expectedBoxes [ i ] , maxBoxDelta )
96
+ } )
97
+ } )
98
+
99
+ it ( 'scores > 0.5' , async ( ) => {
100
+ const detections = await faceDetectionNet . locateFaces ( imgEl , 0.5 ) as FaceDetection [ ]
101
+
102
+ expect ( detections . length ) . toEqual ( 6 )
103
+ detections . forEach ( ( det , i ) => {
104
+ expect ( det . getImageWidth ( ) ) . toEqual ( imgEl . width )
105
+ expect ( det . getImageHeight ( ) ) . toEqual ( imgEl . height )
106
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
107
+ expectRectClose ( det . getBox ( ) , expectedBoxes [ i ] , maxBoxDelta )
108
+ } )
109
+ } )
110
+
60
111
} )
112
+
61
113
} )
0 commit comments