face-api/src/resizeResults.ts

35 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-08-18 13:54:53 +02:00
import { Dimensions, IDimensions } from './classes';
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-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02: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) {
throw new Error(`resizeResults - invalid dimensions: ${JSON.stringify({ width, height })}`)
}
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
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)) {
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-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (isWithFaceDetection(results)) {
return extendWithFaceDetection(results, results.detection.forSize(width, height))
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
if (results instanceof FaceLandmarks || results instanceof FaceDetection) {
return (results as any).forSize(width, height)
}
2020-08-26 00:24:48 +02:00
2020-08-18 13:54:53 +02:00
return results
}