Manav Rathi пре 1 година
родитељ
комит
074d315c9f
2 измењених фајлова са 64 додато и 4 уклоњено
  1. 53 0
      web/apps/photos/src/services/face/transform-box.ts
  2. 11 4
      web/docs/dependencies.md

+ 53 - 0
web/apps/photos/src/services/face/transform-box.ts

@@ -0,0 +1,53 @@
+import { Box, Point } from "services/face/geom";
+import type { FaceDetection } from "services/face/types";
+import {
+    Matrix,
+    applyToPoint,
+    compose,
+    scale,
+    translate,
+} from "transformation-matrix";
+
+/**
+ * Transform the given {@link faceDetections} from their coordinate system in
+ * which they were detected ({@link inBox}) back to the coordinate system of the
+ * original image ({@link toBox}).
+ */
+export const transformFaceDetections = (
+    faceDetections: FaceDetection[],
+    inBox: Box,
+    toBox: Box,
+): FaceDetection[] => {
+    const transform = boxTransformationMatrix(inBox, toBox);
+    return faceDetections.map((f) => ({
+        box: transformBox(f.box, transform),
+        landmarks: f.landmarks.map((p) => transformPoint(p, transform)),
+        probability: f.probability,
+    }));
+};
+
+const boxTransformationMatrix = (inBox: Box, toBox: Box): Matrix =>
+    compose(
+        translate(toBox.x, toBox.y),
+        scale(toBox.width / inBox.width, toBox.height / inBox.height),
+    );
+
+const transformPoint = (point: Point, transform: Matrix) => {
+    const txdPoint = applyToPoint(transform, point);
+    return new Point(txdPoint.x, txdPoint.y);
+};
+
+const transformBox = (box: Box, transform: Matrix) => {
+    const topLeft = transformPoint(new Point(box.x, box.y), transform);
+    const bottomRight = transformPoint(
+        new Point(box.x + box.width, box.y + box.height),
+        transform,
+    );
+
+    return new Box({
+        x: topLeft.x,
+        y: topLeft.y,
+        width: bottomRight.x - topLeft.x,
+        height: bottomRight.y - topLeft.y,
+    });
+};

+ 11 - 4
web/docs/dependencies.md

@@ -177,12 +177,19 @@ some cases.
 
 
 ## Face search
 ## Face search
 
 
--   [matrix](https://github.com/mljs/matrix) and
+-   [transformation-matrix](https://github.com/chrvadala/transformation-matrix)
+    is used for performing 2D affine transformations using transformation
+    matrices. It is used during face detection.
+
+-   [matrix](https://github.com/mljs/matrix) is mathematical matrix abstraction.
+    It is used alongwith
     [similarity-transformation](https://github.com/shaileshpandit/similarity-transformation-js)
     [similarity-transformation](https://github.com/shaileshpandit/similarity-transformation-js)
-    are used during face alignment.
+    during face alignment.
 
 
--   [transformation-matrix](https://github.com/chrvadala/transformation-matrix)
-    is used during face detection.
+    > Note that while both `transformation-matrix` and `matrix` are "matrix"
+    > libraries, they have different foci and purposes: `transformation-matrix`
+    > provides affine transforms, while `matrix` is for performing computations
+    > on matrices, say inverting them or performing their decomposition.
 
 
 -   [hdbscan](https://github.com/shaileshpandit/hdbscan-js) is used for face
 -   [hdbscan](https://github.com/shaileshpandit/hdbscan-js) is used for face
     clustering.
     clustering.