DetectFaceLandmarksTasks.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { __awaiter, __extends, __generator } from "tslib";
  2. import * as tf from '@tensorflow/tfjs-core';
  3. import { extractFaces, extractFaceTensors } from '../dom';
  4. import { extendWithFaceLandmarks } from '../factories/WithFaceLandmarks';
  5. import { ComposableTask } from './ComposableTask';
  6. import { ComputeAllFaceDescriptorsTask, ComputeSingleFaceDescriptorTask } from './ComputeFaceDescriptorsTasks';
  7. import { nets } from './nets';
  8. import { PredictAllAgeAndGenderWithFaceAlignmentTask, PredictSingleAgeAndGenderWithFaceAlignmentTask, } from './PredictAgeAndGenderTask';
  9. import { PredictAllFaceExpressionsWithFaceAlignmentTask, PredictSingleFaceExpressionsWithFaceAlignmentTask, } from './PredictFaceExpressionsTask';
  10. var DetectFaceLandmarksTaskBase = /** @class */ (function (_super) {
  11. __extends(DetectFaceLandmarksTaskBase, _super);
  12. function DetectFaceLandmarksTaskBase(parentTask, input, useTinyLandmarkNet) {
  13. var _this = _super.call(this) || this;
  14. _this.parentTask = parentTask;
  15. _this.input = input;
  16. _this.useTinyLandmarkNet = useTinyLandmarkNet;
  17. return _this;
  18. }
  19. Object.defineProperty(DetectFaceLandmarksTaskBase.prototype, "landmarkNet", {
  20. get: function () {
  21. return this.useTinyLandmarkNet
  22. ? nets.faceLandmark68TinyNet
  23. : nets.faceLandmark68Net;
  24. },
  25. enumerable: true,
  26. configurable: true
  27. });
  28. return DetectFaceLandmarksTaskBase;
  29. }(ComposableTask));
  30. export { DetectFaceLandmarksTaskBase };
  31. var DetectAllFaceLandmarksTask = /** @class */ (function (_super) {
  32. __extends(DetectAllFaceLandmarksTask, _super);
  33. function DetectAllFaceLandmarksTask() {
  34. return _super !== null && _super.apply(this, arguments) || this;
  35. }
  36. DetectAllFaceLandmarksTask.prototype.run = function () {
  37. return __awaiter(this, void 0, void 0, function () {
  38. var parentResults, detections, faces, _a, faceLandmarksByFace;
  39. var _this = this;
  40. return __generator(this, function (_b) {
  41. switch (_b.label) {
  42. case 0: return [4 /*yield*/, this.parentTask];
  43. case 1:
  44. parentResults = _b.sent();
  45. detections = parentResults.map(function (res) { return res.detection; });
  46. if (!(this.input instanceof tf.Tensor)) return [3 /*break*/, 3];
  47. return [4 /*yield*/, extractFaceTensors(this.input, detections)];
  48. case 2:
  49. _a = _b.sent();
  50. return [3 /*break*/, 5];
  51. case 3: return [4 /*yield*/, extractFaces(this.input, detections)];
  52. case 4:
  53. _a = _b.sent();
  54. _b.label = 5;
  55. case 5:
  56. faces = _a;
  57. return [4 /*yield*/, Promise.all(faces.map(function (face) { return _this.landmarkNet.detectLandmarks(face); }))];
  58. case 6:
  59. faceLandmarksByFace = _b.sent();
  60. faces.forEach(function (f) { return f instanceof tf.Tensor && f.dispose(); });
  61. return [2 /*return*/, parentResults.map(function (parentResult, i) {
  62. return extendWithFaceLandmarks(parentResult, faceLandmarksByFace[i]);
  63. })];
  64. }
  65. });
  66. });
  67. };
  68. DetectAllFaceLandmarksTask.prototype.withFaceExpressions = function () {
  69. return new PredictAllFaceExpressionsWithFaceAlignmentTask(this, this.input);
  70. };
  71. DetectAllFaceLandmarksTask.prototype.withAgeAndGender = function () {
  72. return new PredictAllAgeAndGenderWithFaceAlignmentTask(this, this.input);
  73. };
  74. DetectAllFaceLandmarksTask.prototype.withFaceDescriptors = function () {
  75. return new ComputeAllFaceDescriptorsTask(this, this.input);
  76. };
  77. return DetectAllFaceLandmarksTask;
  78. }(DetectFaceLandmarksTaskBase));
  79. export { DetectAllFaceLandmarksTask };
  80. var DetectSingleFaceLandmarksTask = /** @class */ (function (_super) {
  81. __extends(DetectSingleFaceLandmarksTask, _super);
  82. function DetectSingleFaceLandmarksTask() {
  83. return _super !== null && _super.apply(this, arguments) || this;
  84. }
  85. DetectSingleFaceLandmarksTask.prototype.run = function () {
  86. return __awaiter(this, void 0, void 0, function () {
  87. var parentResult, detection, faces, _a, landmarks;
  88. return __generator(this, function (_b) {
  89. switch (_b.label) {
  90. case 0: return [4 /*yield*/, this.parentTask];
  91. case 1:
  92. parentResult = _b.sent();
  93. if (!parentResult) {
  94. return [2 /*return*/];
  95. }
  96. detection = parentResult.detection;
  97. if (!(this.input instanceof tf.Tensor)) return [3 /*break*/, 3];
  98. return [4 /*yield*/, extractFaceTensors(this.input, [detection])];
  99. case 2:
  100. _a = _b.sent();
  101. return [3 /*break*/, 5];
  102. case 3: return [4 /*yield*/, extractFaces(this.input, [detection])];
  103. case 4:
  104. _a = _b.sent();
  105. _b.label = 5;
  106. case 5:
  107. faces = _a;
  108. return [4 /*yield*/, this.landmarkNet.detectLandmarks(faces[0])];
  109. case 6:
  110. landmarks = _b.sent();
  111. faces.forEach(function (f) { return f instanceof tf.Tensor && f.dispose(); });
  112. return [2 /*return*/, extendWithFaceLandmarks(parentResult, landmarks)];
  113. }
  114. });
  115. });
  116. };
  117. DetectSingleFaceLandmarksTask.prototype.withFaceExpressions = function () {
  118. return new PredictSingleFaceExpressionsWithFaceAlignmentTask(this, this.input);
  119. };
  120. DetectSingleFaceLandmarksTask.prototype.withAgeAndGender = function () {
  121. return new PredictSingleAgeAndGenderWithFaceAlignmentTask(this, this.input);
  122. };
  123. DetectSingleFaceLandmarksTask.prototype.withFaceDescriptor = function () {
  124. return new ComputeSingleFaceDescriptorTask(this, this.input);
  125. };
  126. return DetectSingleFaceLandmarksTask;
  127. }(DetectFaceLandmarksTaskBase));
  128. export { DetectSingleFaceLandmarksTask };
  129. //# sourceMappingURL=DetectFaceLandmarksTasks.js.map