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
35
36
37
38
39
import { Box } from '../classes/Box';
import { iou } from './iou';
 
export function nonMaxSuppression(
  boxes: Box[],
  scores: number[],
  iouThreshold: number,
  isIOU = true,
): number[] {
  let indicesSortedByScore = scores
    .map((score, boxIndex) => ({ score, boxIndex }))
    .sort((c1, c2) => c1.score - c2.score)
    .map((c) => c.boxIndex);
 
  const pick: number[] = [];
 
  while (indicesSortedByScore.length > 0) {
    const curr = indicesSortedByScore.pop() as number;
    pick.push(curr);
 
    const indices = indicesSortedByScore;
 
    const outputs: number[] = [];
    for (let i = 0; i < indices.length; i++) {
      const idx = indices[i];
 
      const currBox = boxes[curr];
      const idxBox = boxes[idx];
 
      outputs.push(iou(currBox, idxBox, isIOU));
    }
 
    indicesSortedByScore = indicesSortedByScore.filter(
      (_, j) => outputs[j] <= iouThreshold,
    );
  }
 
  return pick;
}