face-api/src/resizeResults.ts

35 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-12-19 17:46:41 +01:00
import { Dimensions, IDimensions } from './classes/index';
2020-08-18 13:54:53 +02:00
import { FaceDetection } from './classes/FaceDetection';
import { FaceLandmarks } from './classes/FaceLandmarks';
import { extendWithFaceDetection, isWithFaceDetection } from './factories/WithFaceDetection';
import { extendWithFaceLandmarks, isWithFaceLandmarks } from './factories/WithFaceLandmarks';
export function resizeResults<T>(results: T, dimensions: IDimensions): T {
2020-12-27 00:35:17 +01:00
const { width, height } = new Dimensions(dimensions.width, dimensions.height);
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (width <= 0 || height <= 0) {
2020-12-27 00:35:17 +01:00
throw new Error(`resizeResults - invalid dimensions: ${JSON.stringify({ width, height })}`);
2020-08-18 13:54:53 +02:00
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (Array.isArray(results)) {
// return results.map(obj => resizeResults(obj, { width, height })) as any as T
2020-12-27 00:35:17 +01:00
return (results as Array<any>).map((obj) => resizeResults(obj, { width, height } as IDimensions)) as any as T;
2020-08-18 13:54:53 +02:00
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (isWithFaceLandmarks(results)) {
2020-12-27 00:35:17 +01:00
const resizedDetection = results.detection.forSize(width, height);
const resizedLandmarks = results.unshiftedLandmarks.forSize(resizedDetection.box.width, resizedDetection.box.height);
return extendWithFaceLandmarks(extendWithFaceDetection(results, resizedDetection), resizedLandmarks);
2020-08-18 13:54:53 +02:00
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (isWithFaceDetection(results)) {
2020-12-27 00:35:17 +01:00
return extendWithFaceDetection(results, results.detection.forSize(width, height));
2020-08-18 13:54:53 +02:00
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (results instanceof FaceLandmarks || results instanceof FaceDetection) {
2020-12-27 00:35:17 +01:00
return (results as any).forSize(width, height);
2020-08-18 13:54:53 +02:00
}
2020-08-26 00:24:48 +02:00
2020-12-27 00:35:17 +01:00
return results;
}