import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:sign_checker/domain/yolo_decoder.dart'; void main() { const decoder = YoloDecoder(); test('raw head keeps the stronger box and suppresses the overlap', () { const channels = 6; const anchors = 8; final data = Float32List(channels * anchors); void set(int channel, int anchor, double value) { data[channel * anchors + anchor] = value; } set(0, 3, 100); set(1, 3, 80); set(2, 3, 40); set(3, 3, 20); set(4, 3, 0.05); set(5, 3, 0.91); set(0, 4, 104); set(1, 4, 82); set(2, 4, 40); set(3, 4, 20); set(4, 4, 0.02); set(5, 4, 0.62); final boxes = decoder.decode( data: data, shape: const [1, channels, anchors], inputSize: 320, confidenceThreshold: 0.4, iouThreshold: 0.45, ); expect(boxes, hasLength(1)); expect(boxes.single.classIndex, 1); expect(boxes.single.score, closeTo(0.91, 1e-6)); expect(boxes.single.left, closeTo(80, 1e-6)); expect(boxes.single.top, closeTo(70, 1e-6)); expect(boxes.single.right, closeTo(120, 1e-6)); expect(boxes.single.bottom, closeTo(90, 1e-6)); }); test('nms head reads xyxy, score and class', () { final data = Float32List.fromList([ 10, 12, 40, 50, 0.8, 3, // 0, 0, 0, 0, 0.1, 0, ]); final boxes = decoder.decode( data: data, shape: const [1, 2, 6], inputSize: 320, confidenceThreshold: 0.4, iouThreshold: 0.45, ); expect(boxes, hasLength(1)); expect(boxes.single.classIndex, 3); expect(boxes.single.left, 10); expect(boxes.single.bottom, 50); }); test('normalized coordinates are scaled into the input square', () { const channels = 6; const anchors = 8; final data = Float32List(channels * anchors); data[0 * anchors + 1] = 0.5; data[1 * anchors + 1] = 0.5; data[2 * anchors + 1] = 0.25; data[3 * anchors + 1] = 0.25; data[5 * anchors + 1] = 0.95; final boxes = decoder.decode( data: data, shape: const [1, channels, anchors], inputSize: 320, confidenceThreshold: 0.4, iouThreshold: 0.45, ); expect(boxes.single.left, closeTo(120, 1e-6)); expect(boxes.single.width, closeTo(80, 1e-6)); }); }