nonMaxSuppression method

List<ResultObjectDetection> nonMaxSuppression(
  1. List<ResultObjectDetection> boxes
)

Implementation

List<ResultObjectDetection> nonMaxSuppression(
    List<ResultObjectDetection> boxes) {
  // Sort the boxes based on the score in descending order
  boxes.sort((boxA, boxB) => boxB.score.compareTo(boxA.score));

  List<ResultObjectDetection> selected = [];
  List<bool> active = List<bool>.filled(boxes.length, true);
  int numActive = active.length;

  bool done = false;
  for (int i = 0; i < boxes.length && !done; i++) {
    if (active[i]) {
      ResultObjectDetection boxA = boxes[i];
      selected.add(boxA);
      if (selected.length >= nmsLimit) break;

      for (int j = i + 1; j < boxes.length; j++) {
        if (active[j]) {
          ResultObjectDetection boxB = boxes[j];
          if (iou(boxA.rect, boxB.rect) > IOUThreshold) {
            active[j] = false;
            numActive -= 1;
            if (numActive <= 0) {
              done = true;
              break;
            }
          }
        }
      }
    }
  }
  print("Result length after processing ${selected.length}");

  return selected;
}