Files
observer/flutter_app/lib/detection/nms.dart
T

22 lines
771 B
Dart

import 'detection_result.dart';
double iou(DetectionResult a, DetectionResult b) {
final x0 = a.left > b.left ? a.left : b.left;
final y0 = a.top > b.top ? a.top : b.top;
final x1 = a.right < b.right ? a.right : b.right;
final y1 = a.bottom < b.bottom ? a.bottom : b.bottom;
if (x1 <= x0 || y1 <= y0) return 0;
final inter = (x1 - x0) * (y1 - y0);
final union = a.width * a.height + b.width * b.height - inter;
return union <= 0 ? 0 : inter / union;
}
List<DetectionResult> nms(List<DetectionResult> boxes, double iouThreshold) {
final sorted = [...boxes]..sort((a, b) => b.score.compareTo(a.score));
final kept = <DetectionResult>[];
for (final b in sorted) {
if (!kept.any((k) => iou(b, k) > iouThreshold)) kept.add(b);
}
return kept;
}