2020-11-18 14:26:28 +01:00
|
|
|
import * as tf from '../../dist/tfjs.esm.js';
|
2021-11-05 16:28:06 +01:00
|
|
|
import type { Point } from '../result';
|
2020-10-12 01:22:43 +02:00
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function getBoxSize(box) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return [
|
|
|
|
Math.abs(box.endPoint[0] - box.startPoint[0]),
|
|
|
|
Math.abs(box.endPoint[1] - box.startPoint[1]),
|
|
|
|
];
|
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function getBoxCenter(box) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return [
|
|
|
|
box.startPoint[0] + (box.endPoint[0] - box.startPoint[0]) / 2,
|
|
|
|
box.startPoint[1] + (box.endPoint[1] - box.startPoint[1]) / 2,
|
|
|
|
];
|
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function cutBoxFromImageAndResize(box, image, cropSize) {
|
2020-10-12 01:22:43 +02:00
|
|
|
const h = image.shape[1];
|
|
|
|
const w = image.shape[2];
|
|
|
|
const boxes = [[
|
2020-11-04 07:11:24 +01:00
|
|
|
box.startPoint[1] / h,
|
|
|
|
box.startPoint[0] / w,
|
|
|
|
box.endPoint[1] / h,
|
2020-10-12 01:22:43 +02:00
|
|
|
box.endPoint[0] / w,
|
|
|
|
]];
|
|
|
|
return tf.image.cropAndResize(image, boxes, [0], cropSize);
|
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function scaleBoxCoordinates(box, factor) {
|
2021-11-05 16:28:06 +01:00
|
|
|
const startPoint = [box.startPoint[0] * factor[0], box.startPoint[1] * factor[1]] as Point;
|
|
|
|
const endPoint = [box.endPoint[0] * factor[0], box.endPoint[1] * factor[1]] as Point;
|
2020-11-04 07:11:24 +01:00
|
|
|
const palmLandmarks = box.palmLandmarks.map((coord) => {
|
|
|
|
const scaledCoord = [coord[0] * factor[0], coord[1] * factor[1]];
|
|
|
|
return scaledCoord;
|
|
|
|
});
|
2020-11-08 15:56:02 +01:00
|
|
|
return { startPoint, endPoint, palmLandmarks, confidence: box.confidence };
|
2020-10-12 01:22:43 +02:00
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function enlargeBox(box, factor = 1.5) {
|
2020-10-12 01:22:43 +02:00
|
|
|
const center = getBoxCenter(box);
|
|
|
|
const size = getBoxSize(box);
|
|
|
|
const newHalfSize = [factor * size[0] / 2, factor * size[1] / 2];
|
2021-11-05 16:28:06 +01:00
|
|
|
const startPoint = [center[0] - newHalfSize[0], center[1] - newHalfSize[1]] as Point;
|
|
|
|
const endPoint = [center[0] + newHalfSize[0], center[1] + newHalfSize[1]] as Point;
|
2020-10-12 01:22:43 +02:00
|
|
|
return { startPoint, endPoint, palmLandmarks: box.palmLandmarks };
|
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function squarifyBox(box) {
|
2020-10-12 01:22:43 +02:00
|
|
|
const centers = getBoxCenter(box);
|
|
|
|
const size = getBoxSize(box);
|
|
|
|
const maxEdge = Math.max(...size);
|
|
|
|
const halfSize = maxEdge / 2;
|
2021-11-05 16:28:06 +01:00
|
|
|
const startPoint = [centers[0] - halfSize, centers[1] - halfSize] as Point;
|
|
|
|
const endPoint = [centers[0] + halfSize, centers[1] + halfSize] as Point;
|
2020-10-12 01:22:43 +02:00
|
|
|
return { startPoint, endPoint, palmLandmarks: box.palmLandmarks };
|
|
|
|
}
|
2021-02-08 17:39:09 +01:00
|
|
|
|
|
|
|
export function shiftBox(box, shiftFactor) {
|
2020-10-12 01:22:43 +02:00
|
|
|
const boxSize = [
|
2020-11-04 07:11:24 +01:00
|
|
|
box.endPoint[0] - box.startPoint[0],
|
|
|
|
box.endPoint[1] - box.startPoint[1],
|
2020-10-12 01:22:43 +02:00
|
|
|
];
|
|
|
|
const shiftVector = [boxSize[0] * shiftFactor[0], boxSize[1] * shiftFactor[1]];
|
2021-11-05 16:28:06 +01:00
|
|
|
const startPoint = [box.startPoint[0] + shiftVector[0], box.startPoint[1] + shiftVector[1]] as Point;
|
|
|
|
const endPoint = [box.endPoint[0] + shiftVector[0], box.endPoint[1] + shiftVector[1]] as Point;
|
2020-10-12 01:22:43 +02:00
|
|
|
return { startPoint, endPoint, palmLandmarks: box.palmLandmarks };
|
|
|
|
}
|
2021-10-20 15:10:57 +02:00
|
|
|
|
|
|
|
export function normalizeRadians(angle) {
|
|
|
|
return angle - 2 * Math.PI * Math.floor((angle + Math.PI) / (2 * Math.PI));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function computeRotation(point1, point2) {
|
|
|
|
const radians = Math.PI / 2 - Math.atan2(-(point2[1] - point1[1]), point2[0] - point1[0]);
|
|
|
|
return normalizeRadians(radians);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const buildTranslationMatrix = (x, y) => [[1, 0, x], [0, 1, y], [0, 0, 1]];
|
|
|
|
|
|
|
|
export function dot(v1, v2) {
|
|
|
|
let product = 0;
|
|
|
|
for (let i = 0; i < v1.length; i++) {
|
|
|
|
product += v1[i] * v2[i];
|
|
|
|
}
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getColumnFrom2DArr(arr, columnIndex) {
|
|
|
|
const column: Array<number> = [];
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
column.push(arr[i][columnIndex]);
|
|
|
|
}
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function multiplyTransformMatrices(mat1, mat2) {
|
|
|
|
const product: Array<number[]> = [];
|
|
|
|
const size = mat1.length;
|
|
|
|
for (let row = 0; row < size; row++) {
|
|
|
|
product.push([]);
|
|
|
|
for (let col = 0; col < size; col++) {
|
|
|
|
product[row].push(dot(mat1[row], getColumnFrom2DArr(mat2, col)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildRotationMatrix(rotation, center) {
|
|
|
|
const cosA = Math.cos(rotation);
|
|
|
|
const sinA = Math.sin(rotation);
|
|
|
|
const rotationMatrix = [[cosA, -sinA, 0], [sinA, cosA, 0], [0, 0, 1]];
|
|
|
|
const translationMatrix = buildTranslationMatrix(center[0], center[1]);
|
|
|
|
const translationTimesRotation = multiplyTransformMatrices(translationMatrix, rotationMatrix);
|
|
|
|
const negativeTranslationMatrix = buildTranslationMatrix(-center[0], -center[1]);
|
|
|
|
return multiplyTransformMatrices(translationTimesRotation, negativeTranslationMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function invertTransformMatrix(matrix) {
|
|
|
|
const rotationComponent = [[matrix[0][0], matrix[1][0]], [matrix[0][1], matrix[1][1]]];
|
|
|
|
const translationComponent = [matrix[0][2], matrix[1][2]];
|
|
|
|
const invertedTranslation = [
|
|
|
|
-dot(rotationComponent[0], translationComponent),
|
|
|
|
-dot(rotationComponent[1], translationComponent),
|
|
|
|
];
|
|
|
|
return [
|
|
|
|
rotationComponent[0].concat(invertedTranslation[0]),
|
|
|
|
rotationComponent[1].concat(invertedTranslation[1]),
|
|
|
|
[0, 0, 1],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function rotatePoint(homogeneousCoordinate, rotationMatrix) {
|
|
|
|
return [
|
|
|
|
dot(homogeneousCoordinate, rotationMatrix[0]),
|
|
|
|
dot(homogeneousCoordinate, rotationMatrix[1]),
|
|
|
|
];
|
|
|
|
}
|