DetectFacesTasks.js 7.0 KB

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