extractFaceTensors.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var tf = require("@tensorflow/tfjs-core");
  5. var FaceDetection_1 = require("../classes/FaceDetection");
  6. var utils_1 = require("../utils");
  7. /**
  8. * Extracts the tensors of the image regions containing the detected faces.
  9. * Useful if you want to compute the face descriptors for the face images.
  10. * Using this method is faster then extracting a canvas for each face and
  11. * converting them to tensors individually.
  12. *
  13. * @param imageTensor The image tensor that face detection has been performed on.
  14. * @param detections The face detection results or face bounding boxes for that image.
  15. * @returns Tensors of the corresponding image region for each detected face.
  16. */
  17. function extractFaceTensors(imageTensor, detections) {
  18. return tslib_1.__awaiter(this, void 0, void 0, function () {
  19. return tslib_1.__generator(this, function (_a) {
  20. if (!utils_1.isTensor3D(imageTensor) && !utils_1.isTensor4D(imageTensor)) {
  21. throw new Error('extractFaceTensors - expected image tensor to be 3D or 4D');
  22. }
  23. if (utils_1.isTensor4D(imageTensor) && imageTensor.shape[0] > 1) {
  24. throw new Error('extractFaceTensors - batchSize > 1 not supported');
  25. }
  26. return [2 /*return*/, tf.tidy(function () {
  27. var _a = imageTensor.shape.slice(utils_1.isTensor4D(imageTensor) ? 1 : 0), imgHeight = _a[0], imgWidth = _a[1], numChannels = _a[2];
  28. var boxes = detections.map(function (det) { return det instanceof FaceDetection_1.FaceDetection
  29. ? det.forSize(imgWidth, imgHeight).box
  30. : det; })
  31. .map(function (box) { return box.clipAtImageBorders(imgWidth, imgHeight); });
  32. var faceTensors = boxes.map(function (_a) {
  33. var x = _a.x, y = _a.y, width = _a.width, height = _a.height;
  34. return tf.slice3d(imageTensor.as3D(imgHeight, imgWidth, numChannels), [y, x, 0], [height, width, numChannels]);
  35. });
  36. return faceTensors;
  37. })];
  38. });
  39. });
  40. }
  41. exports.extractFaceTensors = extractFaceTensors;
  42. //# sourceMappingURL=extractFaceTensors.js.map