Commit 8127f9ec by vincent

added faceDetectionNet end to end test

parent cc63d787
......@@ -3,7 +3,21 @@ module.exports = function(config) {
frameworks: ['jasmine', 'karma-typescript'],
files: [
'src/**/*.ts',
'test/**/*.ts'
'test/**/*.ts',
{
pattern: 'test/images/*.jpg',
watched: false,
included: false,
served: true,
nocache: false
},
{
pattern: 'weights/*.weights',
watched: false,
included: false,
served: true,
nocache: false
}
],
preprocessors: {
'**/*.ts': ['karma-typescript']
......
......@@ -12,6 +12,15 @@
"seedrandom": "2.4.3"
}
},
"@types/axios": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz",
"integrity": "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=",
"dev": true,
"requires": {
"axios": "0.18.0"
}
},
"@types/estree": {
"version": "0.0.39",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz",
......@@ -368,25 +377,13 @@
"optional": true
},
"axios": {
"version": "0.15.3",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.15.3.tgz",
"integrity": "sha1-LJ1jiy4ZGgjqHWzJiOrda6W9wFM=",
"dev": true,
"optional": true,
"requires": {
"follow-redirects": "1.0.0"
},
"dependencies": {
"follow-redirects": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.0.0.tgz",
"integrity": "sha1-jjQpjL0uF28lTv/sdaHHjMhJ/Tc=",
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"dev": true,
"optional": true,
"requires": {
"debug": "2.6.9"
}
}
"follow-redirects": "1.5.0",
"is-buffer": "1.1.6"
}
},
"backo2": {
......@@ -3309,6 +3306,16 @@
"streamroller": "0.7.0"
},
"dependencies": {
"axios": {
"version": "0.15.3",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.15.3.tgz",
"integrity": "sha1-LJ1jiy4ZGgjqHWzJiOrda6W9wFM=",
"dev": true,
"optional": true,
"requires": {
"follow-redirects": "1.0.0"
}
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
......@@ -3317,6 +3324,28 @@
"requires": {
"ms": "2.0.0"
}
},
"follow-redirects": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.0.0.tgz",
"integrity": "sha1-jjQpjL0uF28lTv/sdaHHjMhJ/Tc=",
"dev": true,
"optional": true,
"requires": {
"debug": "2.6.9"
},
"dependencies": {
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dev": true,
"optional": true,
"requires": {
"ms": "2.0.0"
}
}
}
}
}
},
......
......@@ -23,8 +23,10 @@
"@tensorflow/tfjs-core": "^0.11.0"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/jasmine": "^2.8.8",
"@types/node": "^10.1.1",
"axios": "^0.18.0",
"jasmine-core": "^3.1.0",
"karma": "^2.0.3",
"karma-chrome-launcher": "^2.2.0",
......
export class Rect {
export interface IRect {
x: number
y: number
width: number
height: number
}
export class Rect implements IRect {
public x: number
public y: number
public width: number
......
......@@ -38,7 +38,7 @@ export function faceDetectionNet(weights: Float32Array) {
}
async function locateFaces(
input: tf.Tensor | NetInput,
input: tf.Tensor | NetInput | TNetInput,
minConfidence: number = 0.8,
maxResults: number = 100,
): Promise<FaceDetection[]> {
......
import axios from 'axios';
import * as faceapi from '../../src';
import { FaceDetection } from '../../src/faceDetectionNet/FaceDetection';
import { IRect } from '../../src/Rect';
function expectFaceDetectionEquals(result: FaceDetection, score: number, expectedBox: IRect) {
const { x, y, width, height } = result.getBox()
expect(result.getScore()).toBeCloseTo(score, 2)
expect(Math.floor(x)).toEqual(expectedBox.x)
expect(Math.floor(y)).toEqual(expectedBox.y)
expect(Math.floor(width)).toEqual(expectedBox.width)
expect(Math.floor(height)).toEqual(expectedBox.height)
}
describe('faceDetectionNet', () => {
let faceDetectionNet: any, imgEl: HTMLImageElement
beforeAll(async () => {
const res = await axios.get('base/weights/face_detection_model.weights', { responseType: 'arraybuffer' })
const weights = new Float32Array(res.data)
faceDetectionNet = faceapi.faceDetectionNet(weights)
const img = await axios.get('base/test/images/faces.jpg', { responseType: 'blob' })
imgEl = await faceapi.bufferToImage(img.data)
})
it('scores > 0.9', async () => {
const { width, height } = imgEl
const result = await faceDetectionNet.locateFaces(imgEl) as FaceDetection[]
expect(result.length).toEqual(3)
result.forEach(res => {
expect(res.getImageWidth()).toEqual(width)
expect(res.getImageHeight()).toEqual(height)
})
const [d0, d1, d2] = result
expectFaceDetectionEquals(d0, 0.98, { x: 48, y: 253, width: 104, height: 129 })
expectFaceDetectionEquals(d1, 0.89, { x: 260, y: 227, width: 76, height: 117 })
expectFaceDetectionEquals(d2, 0.82, { x: 466, y: 165, width: 88, height: 130 })
})
it('scores > 0.5', async () => {
const { width, height } = imgEl
const result = await faceDetectionNet.locateFaces(imgEl, 0.5) as FaceDetection[]
expect(result.length).toEqual(6)
result.forEach(res => {
expect(res.getImageWidth()).toEqual(width)
expect(res.getImageHeight()).toEqual(height)
})
const [d0, d1, d2, d3, d4, d5] = result
expectFaceDetectionEquals(d0, 0.98, { x: 48, y: 253, width: 104, height: 129 })
expectFaceDetectionEquals(d1, 0.89, { x: 260, y: 227, width: 76, height: 117 })
expectFaceDetectionEquals(d2, 0.82, { x: 466, y: 165, width: 88, height: 130 })
expectFaceDetectionEquals(d3, 0.75, { x: 234, y: 36, width: 84, height: 119 })
expectFaceDetectionEquals(d4, 0.58, { x: 577, y: 65, width: 84, height: 105 })
expectFaceDetectionEquals(d5, 0.55, { x: 84, y: 14, width: 79, height: 132 })
})
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment