Unverified Commit 0f2fda5e by Vincent Mühler Committed by GitHub

Merge pull request #397 from jderrough/master

Added FaceMatcher.toJSON()/.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.toJSON()/.fromJSON()/.fromPOJO()
parents f9bc7050 b7f300db
...@@ -17,4 +17,19 @@ export class LabeledFaceDescriptors { ...@@ -17,4 +17,19 @@ export class LabeledFaceDescriptors {
public get label(): string { return this._label } public get label(): string { return this._label }
public get descriptors(): Float32Array[] { return this._descriptors } 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);
}
} }
\ No newline at end of file
...@@ -67,4 +67,17 @@ export class FaceMatcher { ...@@ -67,4 +67,17 @@ export class FaceMatcher {
: new FaceMatch('unknown', bestMatch.distance) : 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);
}
} }
\ No newline at end of file
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);
});
});
});
\ No newline at end of file
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);
});
});
});
\ 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