ComputeFaceDescriptorsTasks.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { __awaiter, __extends, __generator } from "tslib";
  2. import { extendWithFaceDescriptor } from '../factories/WithFaceDescriptor';
  3. import { ComposableTask } from './ComposableTask';
  4. import { extractAllFacesAndComputeResults, extractSingleFaceAndComputeResult } from './extractFacesAndComputeResults';
  5. import { nets } from './nets';
  6. import { PredictAllAgeAndGenderWithFaceAlignmentTask, PredictSingleAgeAndGenderWithFaceAlignmentTask, } from './PredictAgeAndGenderTask';
  7. import { PredictAllFaceExpressionsWithFaceAlignmentTask, PredictSingleFaceExpressionsWithFaceAlignmentTask, } from './PredictFaceExpressionsTask';
  8. var ComputeFaceDescriptorsTaskBase = /** @class */ (function (_super) {
  9. __extends(ComputeFaceDescriptorsTaskBase, _super);
  10. function ComputeFaceDescriptorsTaskBase(parentTask, input) {
  11. var _this = _super.call(this) || this;
  12. _this.parentTask = parentTask;
  13. _this.input = input;
  14. return _this;
  15. }
  16. return ComputeFaceDescriptorsTaskBase;
  17. }(ComposableTask));
  18. export { ComputeFaceDescriptorsTaskBase };
  19. var ComputeAllFaceDescriptorsTask = /** @class */ (function (_super) {
  20. __extends(ComputeAllFaceDescriptorsTask, _super);
  21. function ComputeAllFaceDescriptorsTask() {
  22. return _super !== null && _super.apply(this, arguments) || this;
  23. }
  24. ComputeAllFaceDescriptorsTask.prototype.run = function () {
  25. return __awaiter(this, void 0, void 0, function () {
  26. var parentResults, descriptors;
  27. return __generator(this, function (_a) {
  28. switch (_a.label) {
  29. case 0: return [4 /*yield*/, this.parentTask];
  30. case 1:
  31. parentResults = _a.sent();
  32. return [4 /*yield*/, extractAllFacesAndComputeResults(parentResults, this.input, function (faces) { return Promise.all(faces.map(function (face) {
  33. return nets.faceRecognitionNet.computeFaceDescriptor(face);
  34. })); }, null, function (parentResult) { return parentResult.landmarks.align(null, { useDlibAlignment: true }); })];
  35. case 2:
  36. descriptors = _a.sent();
  37. return [2 /*return*/, descriptors.map(function (descriptor, i) { return extendWithFaceDescriptor(parentResults[i], descriptor); })];
  38. }
  39. });
  40. });
  41. };
  42. ComputeAllFaceDescriptorsTask.prototype.withFaceExpressions = function () {
  43. return new PredictAllFaceExpressionsWithFaceAlignmentTask(this, this.input);
  44. };
  45. ComputeAllFaceDescriptorsTask.prototype.withAgeAndGender = function () {
  46. return new PredictAllAgeAndGenderWithFaceAlignmentTask(this, this.input);
  47. };
  48. return ComputeAllFaceDescriptorsTask;
  49. }(ComputeFaceDescriptorsTaskBase));
  50. export { ComputeAllFaceDescriptorsTask };
  51. var ComputeSingleFaceDescriptorTask = /** @class */ (function (_super) {
  52. __extends(ComputeSingleFaceDescriptorTask, _super);
  53. function ComputeSingleFaceDescriptorTask() {
  54. return _super !== null && _super.apply(this, arguments) || this;
  55. }
  56. ComputeSingleFaceDescriptorTask.prototype.run = function () {
  57. return __awaiter(this, void 0, void 0, function () {
  58. var parentResult, descriptor;
  59. return __generator(this, function (_a) {
  60. switch (_a.label) {
  61. case 0: return [4 /*yield*/, this.parentTask];
  62. case 1:
  63. parentResult = _a.sent();
  64. if (!parentResult) {
  65. return [2 /*return*/];
  66. }
  67. return [4 /*yield*/, extractSingleFaceAndComputeResult(parentResult, this.input, function (face) { return nets.faceRecognitionNet.computeFaceDescriptor(face); }, null, function (parentResult) { return parentResult.landmarks.align(null, { useDlibAlignment: true }); })];
  68. case 2:
  69. descriptor = _a.sent();
  70. return [2 /*return*/, extendWithFaceDescriptor(parentResult, descriptor)];
  71. }
  72. });
  73. });
  74. };
  75. ComputeSingleFaceDescriptorTask.prototype.withFaceExpressions = function () {
  76. return new PredictSingleFaceExpressionsWithFaceAlignmentTask(this, this.input);
  77. };
  78. ComputeSingleFaceDescriptorTask.prototype.withAgeAndGender = function () {
  79. return new PredictSingleAgeAndGenderWithFaceAlignmentTask(this, this.input);
  80. };
  81. return ComputeSingleFaceDescriptorTask;
  82. }(ComputeFaceDescriptorsTaskBase));
  83. export { ComputeSingleFaceDescriptorTask };
  84. //# sourceMappingURL=ComputeFaceDescriptorsTasks.js.map