Skip to content

Commit 0f2fda5

Browse files
Merge pull request justadudewhohacks#397 from jderrough/master
Added FaceMatcher.toJSON()/.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.toJSON()/.fromJSON()/.fromPOJO()
2 parents f9bc705 + b7f300d commit 0f2fda5

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

src/classes/LabeledFaceDescriptors.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,19 @@ export class LabeledFaceDescriptors {
1717

1818
public get label(): string { return this._label }
1919
public get descriptors(): Float32Array[] { return this._descriptors }
20+
21+
public toJSON(): any {
22+
return {
23+
label: this.label,
24+
descriptors: this.descriptors.map((d) => Array.from(d))
25+
};
26+
}
27+
28+
public static fromJSON(json: any): LabeledFaceDescriptors {
29+
const descriptors = json.descriptors.map((d: any) => {
30+
return new Float32Array(d);
31+
});
32+
return new LabeledFaceDescriptors(json.label, descriptors);
33+
}
34+
2035
}

src/globalApi/FaceMatcher.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,17 @@ export class FaceMatcher {
6767
: new FaceMatch('unknown', bestMatch.distance)
6868
}
6969

70+
public toJSON(): any {
71+
return {
72+
distanceThreshold: this.distanceThreshold,
73+
labeledDescriptors: this.labeledDescriptors.map((ld) => ld.toJSON())
74+
};
75+
}
76+
77+
public static fromJSON(json: any): FaceMatcher {
78+
const labeledDescriptors = json.labeledDescriptors
79+
.map((ld: any) => LabeledFaceDescriptors.fromJSON(ld));
80+
return new FaceMatcher(labeledDescriptors, json.distanceThreshold);
81+
}
82+
7083
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { LabeledFaceDescriptors } from '../../../src';
2+
3+
describe('globalApi', () => {
4+
5+
describe('LabeledFaceDescriptors', () => {
6+
7+
const json = '{"label":"foo","descriptors":[[1,2,3],[4,5,6]]}';
8+
const l1 = 'foo';
9+
const f1 = new Float32Array([1, 2, 3]);
10+
const f2 = new Float32Array([4, 5, 6]);
11+
12+
it('JSON.stringify()', () => {
13+
expect(JSON.stringify(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
14+
expect(JSON.stringify({ ld: new LabeledFaceDescriptors(l1, [f1,f2]) })).toBe(`{"ld":${json}}`);
15+
expect(JSON.stringify([ new LabeledFaceDescriptors(l1, [f1,f2]) ])).toBe(`[${json}]`);
16+
});
17+
18+
it('fromJSON()', () => {
19+
const ld = LabeledFaceDescriptors.fromJSON(JSON.parse(json));
20+
21+
expect(ld.label).toBe(l1);
22+
expect(ld.descriptors.length).toBe(2);
23+
expect(ld.descriptors[0]).toEqual(f1);
24+
expect(ld.descriptors[1]).toEqual(f2);
25+
});
26+
27+
it('toJSON() => fromJSON()', () => {
28+
const ld = LabeledFaceDescriptors.fromJSON(new LabeledFaceDescriptors(l1, [f1,f2]).toJSON());
29+
30+
expect(ld.label).toBe(l1);
31+
expect(ld.descriptors.length).toBe(2);
32+
expect(ld.descriptors[0]).toEqual(f1);
33+
expect(ld.descriptors[1]).toEqual(f2);
34+
});
35+
36+
});
37+
38+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { FaceMatcher } from '../../../src/globalApi/FaceMatcher';
2+
import { LabeledFaceDescriptors } from '../../../src';
3+
4+
describe('globalApi', () => {
5+
6+
describe('FaceMatcher', () => {
7+
8+
const json = '{"distanceThreshold":123.321,"labeledDescriptors":[{"label":"foo","descriptors":[[1,2,3],[4,5,6]]},{"label":"bar","descriptors":[[7,8,9],[3,2,1]]}]}';
9+
const dt = 123.321;
10+
const l1 = 'foo';
11+
const l2 = 'bar';
12+
const f1 = new Float32Array([1, 2, 3]);
13+
const f2 = new Float32Array([4, 5, 6]);
14+
const f3 = new Float32Array([7, 8, 9]);
15+
const f4 = new Float32Array([3, 2, 1]);
16+
const lds = [
17+
new LabeledFaceDescriptors(l1, [f1, f2]),
18+
new LabeledFaceDescriptors(l2, [f3, f4])
19+
];
20+
21+
it('JSON.stringify()', () => {
22+
expect(JSON.stringify(new FaceMatcher(lds, dt))).toBe(json);
23+
expect(JSON.stringify({ m: new FaceMatcher(lds, dt) })).toBe(`{"m":${json}}`);
24+
expect(JSON.stringify([ new FaceMatcher(lds, dt) ])).toBe(`[${json}]`);
25+
});
26+
27+
it('fromJSON()', () => {
28+
const fm = FaceMatcher.fromJSON(JSON.parse(json));
29+
30+
expect(fm.distanceThreshold).toBe(dt);
31+
expect(fm.labeledDescriptors.length).toBe(2);
32+
expect(fm.labeledDescriptors[0].label).toBe(l1);
33+
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
34+
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
35+
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
36+
expect(fm.labeledDescriptors[1].label).toBe(l2);
37+
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
38+
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
39+
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
40+
});
41+
42+
it('toJSON() => fromJSON()', () => {
43+
const fm = FaceMatcher.fromJSON(new FaceMatcher(lds, dt).toJSON());
44+
45+
expect(fm.distanceThreshold).toBe(dt);
46+
expect(fm.labeledDescriptors.length).toBe(2);
47+
expect(fm.labeledDescriptors[0].label).toBe(l1);
48+
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
49+
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
50+
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
51+
expect(fm.labeledDescriptors[1].label).toBe(l2);
52+
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
53+
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
54+
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
55+
});
56+
57+
});
58+
59+
});

0 commit comments

Comments
 (0)