import 'package:flutter_test/flutter_test.dart'; import 'package:observer/detection/detection_result.dart'; import 'package:observer/detection/nms.dart'; DetectionResult box(double l, double t, double r, double b, double score, {String label = 'x'}) => DetectionResult( label: label, score: score, left: l, top: t, right: r, bottom: b); void main() { test('overlappingBoxes_keepHighestScore', () { final a = box(0.1, 0.1, 0.5, 0.5, 0.8); final b = box(0.12, 0.12, 0.52, 0.52, 0.6); final result = nms([a, b], 0.45); expect(result.length, 1); expect(result[0].score, closeTo(0.8, 1e-6)); }); test('separateBoxes_bothKept', () { final a = box(0.1, 0.1, 0.3, 0.3, 0.8); final b = box(0.7, 0.7, 0.9, 0.9, 0.6); expect(nms([a, b], 0.45).length, 2); }); test('lowScoreBox_suppressedByHigherScore', () { final a = box(0.1, 0.1, 0.5, 0.5, 0.9); final b = box(0.1, 0.1, 0.5, 0.5, 0.5); final result = nms([b, a], 0.45); expect(result.length, 1); expect(result[0].score, greaterThan(0.5)); }); test('bigBox_containsSmallBox_deduped', () { // 同目标一大一小两框:标准 IoU = 0.04/0.13 ≈ 0.31 < 0.45 会漏, // minIoU = inter/小框 = 1 必须去重(2026-09-01 用户实测:单模型也有重叠框) final a = box(0.3, 0.3, 0.5, 0.5, 0.8); // 小框 0.2x0.2 final b = box(0.25, 0.25, 0.55, 0.55, 0.6); // 大框 0.3x0.3 套住小框 final result = nms([a, b], 0.45); expect(result.length, 1); expect(result[0].score, 0.8); }); test('partiallyOverlapping_sameTarget_deduped', () { // 标准 IoU = 0.075/0.195 ≈ 0.385 < 0.45 会漏;minIoU = 0.075/0.09 ≈ 0.83 final a = box(0.1, 0.1, 0.4, 0.4, 0.9); // 0.3x0.3 final b = box(0.15, 0.1, 0.55, 0.4, 0.5); // 0.4x0.3,与 a 重叠 0.25x0.3 final result = nms([a, b], 0.45); expect(result.length, 1); expect(result[0].score, 0.9); }); test('iou_nonOverlapping_isZero', () { final a = box(0.0, 0.0, 0.2, 0.2, 1); final b = box(0.8, 0.8, 1.0, 1.0, 1); expect(iou(a, b), closeTo(0, 1e-6)); }); test('iou_identicalBoxes_isOne', () { final a = box(0.1, 0.1, 0.5, 0.5, 1); expect(iou(a, a), closeTo(1, 1e-6)); }); }