face-api/src/classes/LabeledFaceDescriptors.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-08-18 13:54:53 +02:00
export class LabeledFaceDescriptors {
private _label: string
2020-12-23 17:26:55 +01:00
2020-08-18 13:54:53 +02:00
private _descriptors: Float32Array[]
constructor(label: string, descriptors: Float32Array[]) {
if (!(typeof label === 'string')) {
2020-12-23 17:26:55 +01:00
throw new Error('LabeledFaceDescriptors - constructor expected label to be a string');
2020-08-18 13:54:53 +02:00
}
2020-12-23 17:26:55 +01:00
if (!Array.isArray(descriptors) || descriptors.some((desc) => !(desc instanceof Float32Array))) {
throw new Error('LabeledFaceDescriptors - constructor expected descriptors to be an array of Float32Array');
2020-08-18 13:54:53 +02:00
}
2020-12-23 17:26:55 +01:00
this._label = label;
this._descriptors = descriptors;
2020-08-18 13:54:53 +02:00
}
2020-12-23 17:26:55 +01:00
public get label(): string { return this._label; }
public get descriptors(): Float32Array[] { return this._descriptors; }
2020-08-18 13:54:53 +02:00
public toJSON(): any {
return {
label: this.label,
2020-12-23 17:26:55 +01:00
descriptors: this.descriptors.map((d) => Array.from(d)),
2020-08-18 13:54:53 +02:00
};
}
public static fromJSON(json: any): LabeledFaceDescriptors {
2020-12-23 17:26:55 +01:00
const descriptors = json.descriptors.map((d: any) => new Float32Array(d));
2020-08-18 13:54:53 +02:00
return new LabeledFaceDescriptors(json.label, descriptors);
}
2020-12-23 17:26:55 +01:00
}