FaceMatcher.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { FaceMatch } from '../classes/FaceMatch';
  2. import { LabeledFaceDescriptors } from '../classes/LabeledFaceDescriptors';
  3. import { euclideanDistance } from '../euclideanDistance';
  4. var FaceMatcher = /** @class */ (function () {
  5. function FaceMatcher(inputs, distanceThreshold) {
  6. if (distanceThreshold === void 0) { distanceThreshold = 0.6; }
  7. this._distanceThreshold = distanceThreshold;
  8. var inputArray = Array.isArray(inputs) ? inputs : [inputs];
  9. if (!inputArray.length) {
  10. throw new Error("FaceRecognizer.constructor - expected atleast one input");
  11. }
  12. var count = 1;
  13. var createUniqueLabel = function () { return "person " + count++; };
  14. this._labeledDescriptors = inputArray.map(function (desc) {
  15. if (desc instanceof LabeledFaceDescriptors) {
  16. return desc;
  17. }
  18. if (desc instanceof Float32Array) {
  19. return new LabeledFaceDescriptors(createUniqueLabel(), [desc]);
  20. }
  21. if (desc.descriptor && desc.descriptor instanceof Float32Array) {
  22. return new LabeledFaceDescriptors(createUniqueLabel(), [desc.descriptor]);
  23. }
  24. throw new Error("FaceRecognizer.constructor - expected inputs to be of type LabeledFaceDescriptors | WithFaceDescriptor<any> | Float32Array | Array<LabeledFaceDescriptors | WithFaceDescriptor<any> | Float32Array>");
  25. });
  26. }
  27. Object.defineProperty(FaceMatcher.prototype, "labeledDescriptors", {
  28. get: function () { return this._labeledDescriptors; },
  29. enumerable: true,
  30. configurable: true
  31. });
  32. Object.defineProperty(FaceMatcher.prototype, "distanceThreshold", {
  33. get: function () { return this._distanceThreshold; },
  34. enumerable: true,
  35. configurable: true
  36. });
  37. FaceMatcher.prototype.computeMeanDistance = function (queryDescriptor, descriptors) {
  38. return descriptors
  39. .map(function (d) { return euclideanDistance(d, queryDescriptor); })
  40. .reduce(function (d1, d2) { return d1 + d2; }, 0)
  41. / (descriptors.length || 1);
  42. };
  43. FaceMatcher.prototype.matchDescriptor = function (queryDescriptor) {
  44. var _this = this;
  45. return this.labeledDescriptors
  46. .map(function (_a) {
  47. var descriptors = _a.descriptors, label = _a.label;
  48. return new FaceMatch(label, _this.computeMeanDistance(queryDescriptor, descriptors));
  49. })
  50. .reduce(function (best, curr) { return best.distance < curr.distance ? best : curr; });
  51. };
  52. FaceMatcher.prototype.findBestMatch = function (queryDescriptor) {
  53. var bestMatch = this.matchDescriptor(queryDescriptor);
  54. return bestMatch.distance < this.distanceThreshold
  55. ? bestMatch
  56. : new FaceMatch('unknown', bestMatch.distance);
  57. };
  58. FaceMatcher.prototype.toJSON = function () {
  59. return {
  60. distanceThreshold: this.distanceThreshold,
  61. labeledDescriptors: this.labeledDescriptors.map(function (ld) { return ld.toJSON(); })
  62. };
  63. };
  64. FaceMatcher.fromJSON = function (json) {
  65. var labeledDescriptors = json.labeledDescriptors
  66. .map(function (ld) { return LabeledFaceDescriptors.fromJSON(ld); });
  67. return new FaceMatcher(labeledDescriptors, json.distanceThreshold);
  68. };
  69. return FaceMatcher;
  70. }());
  71. export { FaceMatcher };
  72. //# sourceMappingURL=FaceMatcher.js.map