From 162d760b8034f0b8d05002e66f6f9a9d0f3f2bcf Mon Sep 17 00:00:00 2001 From: Shailesh Pandit Date: Wed, 29 Dec 2021 10:32:01 +0530 Subject: [PATCH] Use transformation-matrix package for transforms --- package.json | 1 + src/utils/machineLearning/transform.ts | 53 ++++++-------------------- yarn.lock | 12 +++--- 3 files changed, 18 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 13936aada..4780aa703 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "react-window-infinite-loader": "^1.0.5", "scrypt-js": "^3.0.1", "styled-components": "^5.2.0", + "transformation-matrix": "^2.10.0", "tsne-js": "^1.0.3", "workbox-precaching": "^6.1.5", "workbox-recipes": "^6.1.5", diff --git a/src/utils/machineLearning/transform.ts b/src/utils/machineLearning/transform.ts index ccefe7f4f..cd5ff4877 100644 --- a/src/utils/machineLearning/transform.ts +++ b/src/utils/machineLearning/transform.ts @@ -1,55 +1,24 @@ import { Box, Point } from '../../../thirdparty/face-api/classes'; -import { Matrix } from 'ml-matrix'; import { newBoxFromPoints } from '.'; -export function translation(x: number, y: number) { - return new Matrix([ - [1, 0, x], - [0, 1, y], - [0, 0, 1], - ]); -} - -export function scale(sx: number, sy: number) { - return new Matrix([ - [sx, 0, 0], - [0, sy, 0], - [0, 0, 1], - ]); -} - -export function rotation(angle: number) { - const cosa = Math.cos(angle); - const sina = Math.sin(angle); - return new Matrix([ - [cosa, -sina, 0], - [sina, cosa, 0], - [0, 0, 1], - ]); -} +import { + scale, + translate, + compose, + Matrix, + applyToPoint, +} from 'transformation-matrix'; export function computeTransformToBox(inBox: Box, toBox: Box): Matrix { - return translation(toBox.x, toBox.y).mmul( + return compose( + translate(toBox.x, toBox.y), scale(toBox.width / inBox.width, toBox.height / inBox.height) ); } -export function pointToArray(point: Point) { - return [point.x, point.y]; -} - -export function transformPointVec(point: number[], transform: Matrix) { - point[2] = 1; - const mat = new Matrix([point]).transpose(); - const mulmat = new Matrix(transform).mmul(mat).to1DArray(); - // console.log({point, mat, mulmat}); - - return mulmat; -} - export function transformPoint(point: Point, transform: Matrix) { - const pointVec = transformPointVec(pointToArray(point), transform); - return new Point(pointVec[0], pointVec[1]); + const txdPoint = applyToPoint(transform, point); + return new Point(txdPoint.x, txdPoint.y); } export function transformPoints(points: Point[], transform: Matrix) { diff --git a/yarn.lock b/yarn.lock index baa3f4398..bcdd5694d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3634,12 +3634,7 @@ eve@~0.5.1: resolved "https://registry.npmjs.org/eve/-/eve-0.5.4.tgz" integrity sha1-Z9CAuXJSkdfjieNMJoYN2X8d66o= -eventemitter3@^4.0.0: - version "4.0.7" - resolved "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz" - integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== - -eventemitter3@^4.0.7: +eventemitter3@^4.0.0, eventemitter3@^4.0.7: version "4.0.7" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== @@ -7113,6 +7108,11 @@ tr46@~0.0.3: resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o= +transformation-matrix@^2.10.0: + version "2.10.0" + resolved "https://registry.yarnpkg.com/transformation-matrix/-/transformation-matrix-2.10.0.tgz#a135638fdbf91da2e75316cf68648e2436c60a1d" + integrity sha512-KH+vJ3eW9rZQMJY2nbhtX9ogUSb4vlolFaVJwe0J1nChBoE/G5+EVqwMgd8kxxL0pdb5PIeRypf3RjzgyDC+0A== + tryer@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz"