optimization of the detection algorithem.

BOOK update.
This commit is contained in:
Jan Pleterski
2026-11-27 09:43:25 +01:00
parent b22d7987e3
commit 5f9e2a3b86
11 changed files with 1209 additions and 542 deletions
+46
View File
@@ -0,0 +1,46 @@
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:sd_kovinar/utils/target_scoring.dart';
void main() {
group('TargetScoring', () {
test('a hand-entered shot scores what was entered, in every direction', () {
for (int score = 0; score <= 10; score++) {
for (int deg = 0; deg < 360; deg += 5) {
final p = TargetScoring.positionFor(score, deg * pi / 180);
expect(TargetScoring.scoreAt(p), score,
reason: 'score $score placed at $deg°');
}
}
});
test('hand-entered shots are not placed on a ring line', () {
// Moving a dot by a couple of pixels (rounding, editing) must not
// change its score
for (int score = 0; score <= 10; score++) {
final p = TargetScoring.positionFor(score, 0.7);
for (final d in [const Offset(2, 0), const Offset(-2, 0), const Offset(0, 2), const Offset(0, -2)]) {
expect(TargetScoring.scoreAt(p + d), score, reason: 'score $score nudged by $d');
}
}
});
test('ring edges count the bullet radius', () {
const c = TargetScoring.center;
// Bullet edge exactly on the outer edge of ring 10
expect(TargetScoring.scoreAt(c + const Offset(14 + 25, 0)), 10);
expect(TargetScoring.scoreAt(c + const Offset(14 + 25 + 0.5, 0)), 9);
// Outside the last ring
expect(TargetScoring.scoreAt(c + const Offset(510 + 25 + 0.5, 0)), 0);
});
test('hand-entered shots stay on the 1200x1200 target', () {
for (int deg = 0; deg < 360; deg += 15) {
final p = TargetScoring.positionFor(0, deg * pi / 180);
expect(p.dx, inInclusiveRange(0, 1200));
expect(p.dy, inInclusiveRange(0, 1200));
}
});
});
}
+107
View File
@@ -0,0 +1,107 @@
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:image/image.dart' as img;
import 'package:sd_kovinar/screens/yolo_detector.dart';
/// The model input as it was built before the speed-up: decode, crop the
/// centre square, resize (nearest), then pixel by pixel.
Float32List oldInput(Uint8List jpeg, int width, int height) {
final decoded = img.decodeImage(jpeg)!;
final size = min(decoded.width, decoded.height);
final square = img.copyCrop(decoded,
x: (decoded.width - size) ~/ 2,
y: (decoded.height - size) ~/ 2,
width: size,
height: size);
final resized = img.copyResize(square, width: width, height: height);
final input = Float32List(width * height * 3);
int i = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final p = resized.getPixel(x, y);
input[i++] = p.r / 255.0;
input[i++] = p.g / 255.0;
input[i++] = p.b / 255.0;
}
}
return input;
}
/// A photo-like JPEG: smooth shapes plus sensor noise, so it compresses like
/// a camera picture rather than pure noise.
Uint8List cameraLikeJpeg(int width, int height, {int seed = 1, int? orientation}) {
final rng = Random(seed);
final image = img.Image(width: width, height: height);
for (final p in image) {
final d = sqrt(pow(p.x - width / 2, 2) + pow(p.y - height / 2, 2));
final ring = (d ~/ 40).isEven ? 230 : 40;
p.setRgb((ring + rng.nextInt(20)).clamp(0, 255),
(ring * 0.9 + rng.nextInt(20)).clamp(0, 255), (p.x * 255 ~/ width));
}
img.fillRect(image, x1: 0, y1: 0, x2: 80, y2: 40, color: img.ColorRgb8(255, 0, 0));
if (orientation != null) image.exif.imageIfd.orientation = orientation;
return img.encodeJpg(image, quality: 92);
}
void main() {
group('model input with the Dart decoder is bit-identical to before', () {
for (final (w, h) in [(1280, 720), (720, 1280), (720, 720), (640, 640), (1000, 700)]) {
test('${w}x$h photo', () {
final jpeg = cameraLikeJpeg(w, h);
final now = inputFromPhoto(decodePhotoDart(jpeg), 640, 640);
expect(now, orderedEquals(oldInput(jpeg, 640, 640)));
});
}
});
test('saved square is the same as cropping the decoded photo', () {
final jpeg = cameraLikeJpeg(1280, 720);
final decoded = img.decodeImage(jpeg)!;
final expected = img.copyCrop(decoded, x: 280, y: 0, width: 720, height: 720);
final square = squareOf(decodePhotoDart(jpeg));
expect(square.width, 720);
expect(square.height, 720);
for (final (x, y) in [(0, 0), (359, 359), (719, 719), (100, 600)]) {
final a = square.getPixel(x, y), b = expected.getPixel(x, y);
expect([a.r, a.g, a.b], [b.r, b.g, b.b], reason: 'pixel $x,$y');
}
});
group('platform decoder', () {
testWidgets('applies the EXIF rotation like the Dart decoder', (tester) async {
// Stored 1280x720 with "rotate 90°": both decoders must give 720x1280
// with the red corner in the same place
final jpeg = cameraLikeJpeg(1280, 720, orientation: 6);
final dart = decodePhotoDart(jpeg);
final native = (await tester.runAsync(() => decodePhotoNative(jpeg)))!;
expect([native.width, native.height], [dart.width, dart.height]);
expect([native.width, native.height], [720, 1280]);
int red(Photo p, int x, int y) => p.pixels[(y * p.width + x) * p.channels];
// Red block was top-left before rotating 90° clockwise: now top-right
expect(red(native, native.width - 10, 10), greaterThan(200));
expect(red(dart, dart.width - 10, 10), greaterThan(200));
});
testWidgets('gives practically the same model input', (tester) async {
final jpeg = cameraLikeJpeg(1280, 720, seed: 7);
final dart = inputFromPhoto(decodePhotoDart(jpeg), 640, 640);
final decoded = await tester.runAsync(() => decodePhotoNative(jpeg));
final native = inputFromPhoto(decoded!, 640, 640);
var maxDiff = 0.0, sum = 0.0, differing = 0;
for (int i = 0; i < dart.length; i++) {
final d = (dart[i] - native[i]).abs();
if (d > 0) differing++;
sum += d;
maxDiff = max(maxDiff, d);
}
// ignore: avoid_print
print('platform vs Dart decoder: ${(differing / dart.length * 100).toStringAsFixed(1)}% '
'of values differ, mean ${(sum / dart.length * 255).toStringAsFixed(3)} / 255, '
'max ${(maxDiff * 255).round()} / 255');
// JPEG decoders may round a few values differently; nothing visible
expect(sum / dart.length * 255, lessThan(1.0));
});
});
}