extractFacesAndComputeResults.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { __awaiter, __generator } from "tslib";
  2. import * as tf from '@tensorflow/tfjs-core';
  3. import { extractFaces, extractFaceTensors } from '../dom';
  4. import { isWithFaceLandmarks } from '../factories/WithFaceLandmarks';
  5. export function extractAllFacesAndComputeResults(parentResults, input, computeResults, extractedFaces, getRectForAlignment) {
  6. if (getRectForAlignment === void 0) { getRectForAlignment = function (_a) {
  7. var alignedRect = _a.alignedRect;
  8. return alignedRect;
  9. }; }
  10. return __awaiter(this, void 0, void 0, function () {
  11. var faceBoxes, faces, _a, _b, results;
  12. return __generator(this, function (_c) {
  13. switch (_c.label) {
  14. case 0:
  15. faceBoxes = parentResults.map(function (parentResult) {
  16. return isWithFaceLandmarks(parentResult)
  17. ? getRectForAlignment(parentResult)
  18. : parentResult.detection;
  19. });
  20. _a = extractedFaces;
  21. if (_a) return [3 /*break*/, 5];
  22. if (!(input instanceof tf.Tensor)) return [3 /*break*/, 2];
  23. return [4 /*yield*/, extractFaceTensors(input, faceBoxes)];
  24. case 1:
  25. _b = _c.sent();
  26. return [3 /*break*/, 4];
  27. case 2: return [4 /*yield*/, extractFaces(input, faceBoxes)];
  28. case 3:
  29. _b = _c.sent();
  30. _c.label = 4;
  31. case 4:
  32. _a = (_b);
  33. _c.label = 5;
  34. case 5:
  35. faces = _a;
  36. return [4 /*yield*/, computeResults(faces)];
  37. case 6:
  38. results = _c.sent();
  39. faces.forEach(function (f) { return f instanceof tf.Tensor && f.dispose(); });
  40. return [2 /*return*/, results];
  41. }
  42. });
  43. });
  44. }
  45. export function extractSingleFaceAndComputeResult(parentResult, input, computeResult, extractedFaces, getRectForAlignment) {
  46. return __awaiter(this, void 0, void 0, function () {
  47. var _this = this;
  48. return __generator(this, function (_a) {
  49. return [2 /*return*/, extractAllFacesAndComputeResults([parentResult], input, function (faces) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
  50. return [2 /*return*/, computeResult(faces[0])];
  51. }); }); }, extractedFaces, getRectForAlignment)];
  52. });
  53. });
  54. }
  55. //# sourceMappingURL=extractFacesAndComputeResults.js.map