gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Dimensions, IDimensions } from './classes/index';
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 {
  const { width, height } = new Dimensions(dimensions.width, dimensions.height);
 
  if (width <= 0 || height <= 0) {
    throw new Error(`resizeResults - invalid dimensions: ${JSON.stringify({ width, height })}`);
  }
 
  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;
  }
 
  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);
  }
 
  if (isWithFaceDetection(results)) {
    return extendWithFaceDetection(results, results.detection.forSize(width, height));
  }
 
  if (results instanceof FaceLandmarks || results instanceof FaceDetection) {
    return (results as any).forSize(width, height);
  }
 
  return results;
}