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
import { Box, IBoundingBox, IRect } from '../classes/index';
import { FaceDetection } from '../classes/FaceDetection';
import { isWithFaceDetection, WithFaceDetection } from '../factories/WithFaceDetection';
import { round } from '../utils/index';
import { DrawBox } from './DrawBox';
 
export type TDrawDetectionsInput = IRect | IBoundingBox | FaceDetection | WithFaceDetection<{}>
 
export function drawDetections(
  canvasArg: string | HTMLCanvasElement,
  detections: TDrawDetectionsInput | Array<TDrawDetectionsInput>,
) {
  const detectionsArray = Array.isArray(detections) ? detections : [detections];
 
  detectionsArray.forEach((det) => {
    // eslint-disable-next-line no-nested-ternary
    const score = det instanceof FaceDetection
      ? det.score
      : (isWithFaceDetection(det) ? det.detection.score : undefined);
 
    // eslint-disable-next-line no-nested-ternary
    const box = det instanceof FaceDetection
      ? det.box
      : (isWithFaceDetection(det) ? det.detection.box : new Box(det));
 
    const label = score ? `${round(score)}` : undefined;
    new DrawBox(box, { label }).draw(canvasArg);
  });
}