DetectFacesTasks.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var WithFaceDetection_1 = require("../factories/WithFaceDetection");
  5. var MtcnnOptions_1 = require("../mtcnn/MtcnnOptions");
  6. var SsdMobilenetv1Options_1 = require("../ssdMobilenetv1/SsdMobilenetv1Options");
  7. var TinyFaceDetectorOptions_1 = require("../tinyFaceDetector/TinyFaceDetectorOptions");
  8. var tinyYolov2_1 = require("../tinyYolov2");
  9. var ComposableTask_1 = require("./ComposableTask");
  10. var DetectFaceLandmarksTasks_1 = require("./DetectFaceLandmarksTasks");
  11. var nets_1 = require("./nets");
  12. var PredictAgeAndGenderTask_1 = require("./PredictAgeAndGenderTask");
  13. var PredictFaceExpressionsTask_1 = require("./PredictFaceExpressionsTask");
  14. var DetectFacesTaskBase = /** @class */ (function (_super) {
  15. tslib_1.__extends(DetectFacesTaskBase, _super);
  16. function DetectFacesTaskBase(input, options) {
  17. if (options === void 0) { options = new SsdMobilenetv1Options_1.SsdMobilenetv1Options(); }
  18. var _this = _super.call(this) || this;
  19. _this.input = input;
  20. _this.options = options;
  21. return _this;
  22. }
  23. return DetectFacesTaskBase;
  24. }(ComposableTask_1.ComposableTask));
  25. exports.DetectFacesTaskBase = DetectFacesTaskBase;
  26. var DetectAllFacesTask = /** @class */ (function (_super) {
  27. tslib_1.__extends(DetectAllFacesTask, _super);
  28. function DetectAllFacesTask() {
  29. return _super !== null && _super.apply(this, arguments) || this;
  30. }
  31. DetectAllFacesTask.prototype.run = function () {
  32. return tslib_1.__awaiter(this, void 0, void 0, function () {
  33. var _a, input, options, faceDetectionFunction;
  34. return tslib_1.__generator(this, function (_b) {
  35. switch (_b.label) {
  36. case 0:
  37. _a = this, input = _a.input, options = _a.options;
  38. if (!(options instanceof MtcnnOptions_1.MtcnnOptions)) return [3 /*break*/, 2];
  39. return [4 /*yield*/, nets_1.nets.mtcnn.forward(input, options)];
  40. case 1: return [2 /*return*/, (_b.sent())
  41. .map(function (result) { return result.detection; })];
  42. case 2:
  43. faceDetectionFunction = options instanceof TinyFaceDetectorOptions_1.TinyFaceDetectorOptions
  44. ? function (input) { return nets_1.nets.tinyFaceDetector.locateFaces(input, options); }
  45. : (options instanceof SsdMobilenetv1Options_1.SsdMobilenetv1Options
  46. ? function (input) { return nets_1.nets.ssdMobilenetv1.locateFaces(input, options); }
  47. : (options instanceof tinyYolov2_1.TinyYolov2Options
  48. ? function (input) { return nets_1.nets.tinyYolov2.locateFaces(input, options); }
  49. : null));
  50. if (!faceDetectionFunction) {
  51. throw new Error('detectFaces - expected options to be instance of TinyFaceDetectorOptions | SsdMobilenetv1Options | MtcnnOptions | TinyYolov2Options');
  52. }
  53. return [2 /*return*/, faceDetectionFunction(input)];
  54. }
  55. });
  56. });
  57. };
  58. DetectAllFacesTask.prototype.runAndExtendWithFaceDetections = function () {
  59. var _this = this;
  60. return new Promise(function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
  61. var detections;
  62. return tslib_1.__generator(this, function (_a) {
  63. switch (_a.label) {
  64. case 0: return [4 /*yield*/, this.run()];
  65. case 1:
  66. detections = _a.sent();
  67. return [2 /*return*/, res(detections.map(function (detection) { return WithFaceDetection_1.extendWithFaceDetection({}, detection); }))];
  68. }
  69. });
  70. }); });
  71. };
  72. DetectAllFacesTask.prototype.withFaceLandmarks = function (useTinyLandmarkNet) {
  73. if (useTinyLandmarkNet === void 0) { useTinyLandmarkNet = false; }
  74. return new DetectFaceLandmarksTasks_1.DetectAllFaceLandmarksTask(this.runAndExtendWithFaceDetections(), this.input, useTinyLandmarkNet);
  75. };
  76. DetectAllFacesTask.prototype.withFaceExpressions = function () {
  77. return new PredictFaceExpressionsTask_1.PredictAllFaceExpressionsTask(this.runAndExtendWithFaceDetections(), this.input);
  78. };
  79. DetectAllFacesTask.prototype.withAgeAndGender = function () {
  80. return new PredictAgeAndGenderTask_1.PredictAllAgeAndGenderTask(this.runAndExtendWithFaceDetections(), this.input);
  81. };
  82. return DetectAllFacesTask;
  83. }(DetectFacesTaskBase));
  84. exports.DetectAllFacesTask = DetectAllFacesTask;
  85. var DetectSingleFaceTask = /** @class */ (function (_super) {
  86. tslib_1.__extends(DetectSingleFaceTask, _super);
  87. function DetectSingleFaceTask() {
  88. return _super !== null && _super.apply(this, arguments) || this;
  89. }
  90. DetectSingleFaceTask.prototype.run = function () {
  91. return tslib_1.__awaiter(this, void 0, void 0, function () {
  92. var faceDetections, faceDetectionWithHighestScore;
  93. return tslib_1.__generator(this, function (_a) {
  94. switch (_a.label) {
  95. case 0: return [4 /*yield*/, new DetectAllFacesTask(this.input, this.options)];
  96. case 1:
  97. faceDetections = _a.sent();
  98. faceDetectionWithHighestScore = faceDetections[0];
  99. faceDetections.forEach(function (faceDetection) {
  100. if (faceDetection.score > faceDetectionWithHighestScore.score) {
  101. faceDetectionWithHighestScore = faceDetection;
  102. }
  103. });
  104. return [2 /*return*/, faceDetectionWithHighestScore];
  105. }
  106. });
  107. });
  108. };
  109. DetectSingleFaceTask.prototype.runAndExtendWithFaceDetection = function () {
  110. var _this = this;
  111. return new Promise(function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
  112. var detection;
  113. return tslib_1.__generator(this, function (_a) {
  114. switch (_a.label) {
  115. case 0: return [4 /*yield*/, this.run()];
  116. case 1:
  117. detection = _a.sent();
  118. return [2 /*return*/, res(detection ? WithFaceDetection_1.extendWithFaceDetection({}, detection) : undefined)];
  119. }
  120. });
  121. }); });
  122. };
  123. DetectSingleFaceTask.prototype.withFaceLandmarks = function (useTinyLandmarkNet) {
  124. if (useTinyLandmarkNet === void 0) { useTinyLandmarkNet = false; }
  125. return new DetectFaceLandmarksTasks_1.DetectSingleFaceLandmarksTask(this.runAndExtendWithFaceDetection(), this.input, useTinyLandmarkNet);
  126. };
  127. DetectSingleFaceTask.prototype.withFaceExpressions = function () {
  128. return new PredictFaceExpressionsTask_1.PredictSingleFaceExpressionsTask(this.runAndExtendWithFaceDetection(), this.input);
  129. };
  130. DetectSingleFaceTask.prototype.withAgeAndGender = function () {
  131. return new PredictAgeAndGenderTask_1.PredictSingleAgeAndGenderTask(this.runAndExtendWithFaceDetection(), this.input);
  132. };
  133. return DetectSingleFaceTask;
  134. }(DetectFacesTaskBase));
  135. exports.DetectSingleFaceTask = DetectSingleFaceTask;
  136. //# sourceMappingURL=DetectFacesTasks.js.map