Skip to content

Commit f7e839a

Browse files
committed
Added FaceMatcher.toJSON() & FaceMatcher.fromJSON(), with unit tests
1 parent 514c0ec commit f7e839a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/globalApi/FaceMatcher.ts

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

70+
public static toJSON(matcher: FaceMatcher, pretty = false): string {
71+
return JSON.stringify(matcher, null, pretty ? 2 : undefined);
72+
}
73+
74+
public static fromJSON(jsonString: string): FaceMatcher {
75+
const poco: any = JSON.parse(jsonString);
76+
const labeledDescriptors = poco._labeledDescriptors
77+
.map(({ _label, _descriptors }: { _label: string, _descriptors: any }) => {
78+
return new LabeledFaceDescriptors(_label, _descriptors.map((d: any) => {
79+
return new Float32Array(Object.values(d));
80+
}));
81+
});
82+
return new FaceMatcher(labeledDescriptors, poco._distanceThreshold);
83+
}
84+
7085
}
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":[{"0":1,"1":2,"2":3},{"0":4,"1":5,"2":6}]},{"_label":"bar","_descriptors":[{"0":7,"1":8,"2":9},{"0":3,"1":2,"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('toJSON()', () => {
22+
const fm = new FaceMatcher(lds, dt);
23+
24+
expect(FaceMatcher.toJSON(fm)).toBe(json);
25+
});
26+
27+
it('fromJSON()', () => {
28+
const fm = FaceMatcher.fromJSON(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(FaceMatcher.toJSON(new FaceMatcher(lds, dt)));
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)