We need both
This commit is contained in:
parent
b8734fcc6c
commit
074d315c9f
2 changed files with 65 additions and 5 deletions
web
53
web/apps/photos/src/services/face/transform-box.ts
Normal file
53
web/apps/photos/src/services/face/transform-box.ts
Normal file
|
@ -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,
|
||||
});
|
||||
};
|
|
@ -177,12 +177,19 @@ some cases.
|
|||
|
||||
## Face search
|
||||
|
||||
- [matrix](https://github.com/mljs/matrix) and
|
||||
[similarity-transformation](https://github.com/shaileshpandit/similarity-transformation-js)
|
||||
are used during face alignment.
|
||||
|
||||
- [transformation-matrix](https://github.com/chrvadala/transformation-matrix)
|
||||
is used during face detection.
|
||||
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)
|
||||
during face alignment.
|
||||
|
||||
> 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
|
||||
clustering.
|
||||
|
|
Loading…
Add table
Reference in a new issue