nonMaximumSuppression function

List<Detection> nonMaximumSuppression(
  1. List<Detection> detections,
  2. double threshold
)

Implementation

List<Detection> nonMaximumSuppression(
  List<Detection> detections,
  double threshold,
) {
  if (detections.isEmpty) return [];
  var x1 = <double>[];
  var x2 = <double>[];
  var y1 = <double>[];
  var y2 = <double>[];
  var s = <double>[];

  detections.forEach((detection) {
    x1.add(detection.xMin);
    x2.add(detection.xMin + detection.width);
    y1.add(detection.yMin);
    y2.add(detection.yMin + detection.height);
    s.add(detection.score);
  });

  var _x1 = Array(x1);
  var _x2 = Array(x2);
  var _y1 = Array(y1);
  var _y2 = Array(y2);

  var area = (_x2 - _x1) * (_y2 - _y1);
  var I = _quickSort(s);

  var positions = <int>[];
  I.forEach((element) {
    positions.add(s.indexOf(element));
  });

  var pick = <int>[];
  while (I.isNotEmpty) {
    var ind0 = positions.sublist(positions.length - 1, positions.length);
    var ind1 = positions.sublist(0, positions.length - 1);

    var xx1 = _maximum(_itemIndex(_x1, ind0)[0], _itemIndex(_x1, ind1));
    var yy1 = _maximum(_itemIndex(_y1, ind0)[0], _itemIndex(_y1, ind1));
    var xx2 = _minimum(_itemIndex(_x2, ind0)[0], _itemIndex(_x2, ind1));
    var yy2 = _minimum(_itemIndex(_y2, ind0)[0], _itemIndex(_y2, ind1));
    var w = _maximum(0.0, xx2 - xx1);
    var h = _maximum(0.0, yy2 - yy1);
    var inter = w * h;
    var o = inter /
        (_sum(_itemIndex(area, ind0)[0], _itemIndex(area, ind1)) - inter);

    pick.add(ind0[0]);
    I = o.where((element) => element <= threshold).toList();
  }
  return [detections[pick[0]]];
}