Skip to content

Commit

Permalink
Merge pull request justadudewhohacks#397 from jderrough/master
Browse files Browse the repository at this point in the history
Added FaceMatcher.toJSON()/.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.toJSON()/.fromJSON()/.fromPOJO()
  • Loading branch information
justadudewhohacks authored Aug 31, 2019
2 parents f9bc705 + b7f300d commit 0f2fda5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/classes/LabeledFaceDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ export class LabeledFaceDescriptors {

public get label(): string { return this._label }
public get descriptors(): Float32Array[] { return this._descriptors }

public toJSON(): any {
return {
label: this.label,
descriptors: this.descriptors.map((d) => Array.from(d))
};
}

public static fromJSON(json: any): LabeledFaceDescriptors {
const descriptors = json.descriptors.map((d: any) => {
return new Float32Array(d);
});
return new LabeledFaceDescriptors(json.label, descriptors);
}

}
13 changes: 13 additions & 0 deletions src/globalApi/FaceMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ export class FaceMatcher {
: new FaceMatch('unknown', bestMatch.distance)
}

public toJSON(): any {
return {
distanceThreshold: this.distanceThreshold,
labeledDescriptors: this.labeledDescriptors.map((ld) => ld.toJSON())
};
}

public static fromJSON(json: any): FaceMatcher {
const labeledDescriptors = json.labeledDescriptors
.map((ld: any) => LabeledFaceDescriptors.fromJSON(ld));
return new FaceMatcher(labeledDescriptors, json.distanceThreshold);
}

}
38 changes: 38 additions & 0 deletions test/tests/classes/LabeledFaceDescriptors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { LabeledFaceDescriptors } from '../../../src';

describe('globalApi', () => {

describe('LabeledFaceDescriptors', () => {

const json = '{"label":"foo","descriptors":[[1,2,3],[4,5,6]]}';
const l1 = 'foo';
const f1 = new Float32Array([1, 2, 3]);
const f2 = new Float32Array([4, 5, 6]);

it('JSON.stringify()', () => {
expect(JSON.stringify(new LabeledFaceDescriptors(l1, [f1,f2]))).toBe(json);
expect(JSON.stringify({ ld: new LabeledFaceDescriptors(l1, [f1,f2]) })).toBe(`{"ld":${json}}`);
expect(JSON.stringify([ new LabeledFaceDescriptors(l1, [f1,f2]) ])).toBe(`[${json}]`);
});

it('fromJSON()', () => {
const ld = LabeledFaceDescriptors.fromJSON(JSON.parse(json));

expect(ld.label).toBe(l1);
expect(ld.descriptors.length).toBe(2);
expect(ld.descriptors[0]).toEqual(f1);
expect(ld.descriptors[1]).toEqual(f2);
});

it('toJSON() => fromJSON()', () => {
const ld = LabeledFaceDescriptors.fromJSON(new LabeledFaceDescriptors(l1, [f1,f2]).toJSON());

expect(ld.label).toBe(l1);
expect(ld.descriptors.length).toBe(2);
expect(ld.descriptors[0]).toEqual(f1);
expect(ld.descriptors[1]).toEqual(f2);
});

});

});
59 changes: 59 additions & 0 deletions test/tests/globalApi/FaceMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { FaceMatcher } from '../../../src/globalApi/FaceMatcher';
import { LabeledFaceDescriptors } from '../../../src';

describe('globalApi', () => {

describe('FaceMatcher', () => {

const json = '{"distanceThreshold":123.321,"labeledDescriptors":[{"label":"foo","descriptors":[[1,2,3],[4,5,6]]},{"label":"bar","descriptors":[[7,8,9],[3,2,1]]}]}';
const dt = 123.321;
const l1 = 'foo';
const l2 = 'bar';
const f1 = new Float32Array([1, 2, 3]);
const f2 = new Float32Array([4, 5, 6]);
const f3 = new Float32Array([7, 8, 9]);
const f4 = new Float32Array([3, 2, 1]);
const lds = [
new LabeledFaceDescriptors(l1, [f1, f2]),
new LabeledFaceDescriptors(l2, [f3, f4])
];

it('JSON.stringify()', () => {
expect(JSON.stringify(new FaceMatcher(lds, dt))).toBe(json);
expect(JSON.stringify({ m: new FaceMatcher(lds, dt) })).toBe(`{"m":${json}}`);
expect(JSON.stringify([ new FaceMatcher(lds, dt) ])).toBe(`[${json}]`);
});

it('fromJSON()', () => {
const fm = FaceMatcher.fromJSON(JSON.parse(json));

expect(fm.distanceThreshold).toBe(dt);
expect(fm.labeledDescriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].label).toBe(l1);
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
expect(fm.labeledDescriptors[1].label).toBe(l2);
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
});

it('toJSON() => fromJSON()', () => {
const fm = FaceMatcher.fromJSON(new FaceMatcher(lds, dt).toJSON());

expect(fm.distanceThreshold).toBe(dt);
expect(fm.labeledDescriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].label).toBe(l1);
expect(fm.labeledDescriptors[0].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[0].descriptors[0]).toEqual(f1);
expect(fm.labeledDescriptors[0].descriptors[1]).toEqual(f2);
expect(fm.labeledDescriptors[1].label).toBe(l2);
expect(fm.labeledDescriptors[1].descriptors.length).toBe(2);
expect(fm.labeledDescriptors[1].descriptors[0]).toEqual(f3);
expect(fm.labeledDescriptors[1].descriptors[1]).toEqual(f4);
});

});

});

0 comments on commit 0f2fda5

Please sign in to comment.