Skip to content

Commit 729ea84

Browse files
adapated unit tests to inconsistent mtcnn box order between chrome and firefox
1 parent 6dc6223 commit 729ea84

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

test/tests/e2e/allFacesMtcnn.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as faceapi from '../../../src';
22
import { FaceLandmarks5 } from '../../../src/mtcnn/FaceLandmarks5';
3-
import { NetInput } from '../../../src/NetInput';
4-
import { describeWithNets, expectAllTensorsReleased } from '../../utils';
3+
import { describeWithNets, expectAllTensorsReleased, expectMaxDelta } from '../../utils';
54
import { expectMtcnnResults } from './expectedResults';
65

76
describe('allFacesMtcnn', () => {
@@ -29,9 +28,9 @@ describe('allFacesMtcnn', () => {
2928
faceDetection: res.detection,
3029
faceLandmarks: res.landmarks as FaceLandmarks5
3130
}))
32-
expectMtcnnResults(mtcnnResult, [0, 1, 2, 3, 4, 5], 1, 1)
31+
expectMtcnnResults(mtcnnResult, 5, 5)
3332
results.forEach(({ descriptor }, i) => {
34-
expect(descriptor).toEqual(new Float32Array(facesFaceDescriptors[i]))
33+
descriptor.forEach((val, j) => expectMaxDelta(val, facesFaceDescriptors[i][j], 0.05))
3534
})
3635
})
3736

test/tests/e2e/expectedResults.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as faceapi from '../../../src';
22
import { FaceLandmarks5 } from '../../../src/mtcnn/FaceLandmarks5';
33
import { Point } from '../../../src/Point';
4-
import { expectMaxDelta, expectPointClose, expectRectClose } from '../../utils';
4+
import { expectMaxDelta, expectPointClose, expectRectClose, rectDist } from '../../utils';
55

66
export const expectedSsdBoxes = [
77
{ x: 48, y: 253, width: 104, height: 129 },
@@ -30,13 +30,25 @@ export const expectedMtcnnFaceLandmarks = [
3030
[new Point(489, 224), new Point(534, 223), new Point(507, 250), new Point(493, 271), new Point(530, 270)]
3131
]
3232

33-
3433
export function expectMtcnnResults(
3534
results: { faceDetection: faceapi.FaceDetection, faceLandmarks: faceapi.FaceLandmarks5 }[],
36-
boxOrder: number[],
3735
maxBoxDelta: number,
3836
maxLandmarkPointsDelta: number
3937
) {
38+
39+
// find idx of closest box
40+
const boxOrder = results
41+
.map(({ faceDetection }) => faceDetection.getBox())
42+
.map((box) =>
43+
expectedMtcnnBoxes
44+
.map((box, idx) => ({ box, idx }))
45+
.reduce((closest, curr) => rectDist(box, closest.box) < rectDist(box, curr.box) ? closest : curr)
46+
.idx
47+
)
48+
49+
// expect there to be no duplicate indices
50+
expect(new Set(boxOrder).size).toEqual(results.length)
51+
4052
results.forEach((result, i) => {
4153
const { faceDetection, faceLandmarks } = result
4254
expect(faceDetection instanceof faceapi.FaceDetection).toBe(true)

test/tests/e2e/mtcnn.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('mtcnn', () => {
2222

2323
const results = await mtcnn.forward(imgEl, forwardParams)
2424
expect(results.length).toEqual(6)
25-
expectMtcnnResults(results, [0, 1, 2, 3, 4, 5], 1, 1)
25+
expectMtcnnResults(results, 5, 5)
2626
})
2727

2828
it('minFaceSize = 80, finds all faces', async () => {
@@ -33,7 +33,7 @@ describe('mtcnn', () => {
3333
const results = await mtcnn.forward(imgEl, forwardParams)
3434

3535
expect(results.length).toEqual(6)
36-
expectMtcnnResults(results, [0, 5, 3, 1, 2, 4], 12, 12)
36+
expectMtcnnResults(results, 12, 12)
3737
})
3838

3939
it('all optional params passed, finds all faces', async () => {
@@ -46,7 +46,7 @@ describe('mtcnn', () => {
4646

4747
const results = await mtcnn.forward(imgEl, forwardParams)
4848
expect(results.length).toEqual(6)
49-
expectMtcnnResults(results, [5, 1, 4, 3, 2, 0], 6, 10)
49+
expectMtcnnResults(results, 6, 10)
5050
})
5151

5252
it('scale steps passed, finds all faces', async () => {
@@ -56,7 +56,7 @@ describe('mtcnn', () => {
5656

5757
const results = await mtcnn.forward(imgEl, forwardParams)
5858
expect(results.length).toEqual(6)
59-
expectMtcnnResults(results, [5, 1, 3, 0, 2, 4], 7, 15)
59+
expectMtcnnResults(results, 7, 15)
6060
})
6161

6262
})

test/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { IPoint } from '../src/';
77
import { allFacesFactory, allFacesMtcnnFactory } from '../src/allFacesFactory';
88
import { allFacesMtcnnFunction, allFacesFunction } from '../src/globalApi';
99

10+
// increase default timeout for async hooks
11+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000
12+
1013
export function zeros(length: number): Float32Array {
1114
return new Float32Array(length)
1215
}
@@ -50,6 +53,13 @@ export function expectRectClose(
5053
expectMaxDelta(height, expectedBox.height, maxDelta)
5154
}
5255

56+
export function rectDist (r1: IRect, r2: IRect) {
57+
return Math.abs(r1.x - r2.x)
58+
+ Math.abs(r1.y - r2.y)
59+
+ Math.abs(r1.width - r2.width)
60+
+ Math.abs(r1.height - r2.height)
61+
}
62+
5363
export type WithNetOptions = {
5464
quantized?: boolean
5565
}

0 commit comments

Comments
 (0)