optimization of the detection algorithem.
BOOK update.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/foundation.dart' show kReleaseMode, Uint8List;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'yolo_detector.dart';
|
||||
import 'preview_screen.dart';
|
||||
@@ -21,7 +23,8 @@ class CameraScreen extends StatefulWidget {
|
||||
class _CameraScreenState extends State<CameraScreen> {
|
||||
CameraController? _controller;
|
||||
Future<void>? _initializeControllerFuture;
|
||||
late YOLODetector _yoloDetector;
|
||||
YOLODetector? _yoloDetector; // null until the model has loaded
|
||||
bool _processing = false; // a photo is being analysed
|
||||
Color _bulletColor = Colors.blue; // default fallback
|
||||
|
||||
// The variable to hold the number of bullets entered by the user (default set to 5)
|
||||
@@ -44,7 +47,13 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||
_loadBulletColor(); // 👈
|
||||
_loadLanguage();
|
||||
_loadSelectedTargetColor();
|
||||
YOLODetector.create().then((detector) => _yoloDetector = detector);
|
||||
YOLODetector.create().then((detector) {
|
||||
if (mounted) {
|
||||
_yoloDetector = detector;
|
||||
} else {
|
||||
detector.close(); // screen closed while the model was loading
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String _t(String key) {
|
||||
@@ -149,45 +158,44 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||
@override
|
||||
void dispose() {
|
||||
_controller?.dispose();
|
||||
_yoloDetector.close();
|
||||
_yoloDetector?.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _takePicture() async {
|
||||
final detector = _yoloDetector;
|
||||
if (_processing || detector == null) return; // busy, or model still loading
|
||||
setState(() => _processing = true);
|
||||
try {
|
||||
await _initializeControllerFuture;
|
||||
final clock = Stopwatch()..start();
|
||||
final file = await _controller!.takePicture();
|
||||
final imageFile = File(file.path);
|
||||
final imageBytes = await imageFile.readAsBytes();
|
||||
final decodedImage = img.decodeImage(imageBytes)!;
|
||||
final shot = clock.elapsedMilliseconds;
|
||||
|
||||
// Crop center square from full image
|
||||
final width = decodedImage.width;
|
||||
final height = decodedImage.height;
|
||||
final size = width < height ? width : height;
|
||||
final offsetX = (width - size) ~/ 2;
|
||||
final offsetY = (height - size) ~/ 2;
|
||||
// Platform JPEG decoder (off the UI thread); the model input is built in
|
||||
// the background straight from the photo's centre square
|
||||
final bytes = await File(file.path).readAsBytes();
|
||||
final photo = await decodePhoto(bytes);
|
||||
final decoded = clock.elapsedMilliseconds;
|
||||
|
||||
final cropped = img.copyCrop(
|
||||
decodedImage,
|
||||
x: offsetX,
|
||||
y: offsetY,
|
||||
width: size,
|
||||
height: size,
|
||||
);
|
||||
|
||||
final croppedFile = File('${file.path}_square.jpg')
|
||||
..writeAsBytesSync(img.encodeJpg(cropped));
|
||||
|
||||
final detections = await _yoloDetector.detect(croppedFile);
|
||||
// Save the square for the preview while the model runs
|
||||
final squarePath = '${file.path}_square.jpg';
|
||||
final (_, detections) = await (
|
||||
saveSquareInBackground(photo, squarePath),
|
||||
detector.detectPhoto(photo),
|
||||
).wait;
|
||||
debugPrint('⏱ detection: photo $shot ms, decode ${decoded - shot} ms '
|
||||
'(${photo.channels == 4 ? 'platform' : 'Dart'}), '
|
||||
'model + save ${clock.elapsedMilliseconds - decoded} ms, '
|
||||
'total ${clock.elapsedMilliseconds} ms');
|
||||
if (!kReleaseMode) await _compareWithDartDecoder(detector, bytes, detections);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
Navigator.push(
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PreviewScreen(
|
||||
imagePath: croppedFile.path,
|
||||
imagePath: squarePath,
|
||||
detections: detections,
|
||||
bulletCount: _numberOfBullets, // Pass the bullet count
|
||||
),
|
||||
@@ -195,9 +203,34 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('Error taking picture: $e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _processing = false);
|
||||
}
|
||||
}
|
||||
|
||||
// Debug and profile builds only: detect again on the pure-Dart decode of the same photo
|
||||
// and log how far the results are apart (the platform decoder may round a
|
||||
// few pixel values differently).
|
||||
Future<void> _compareWithDartDecoder(YOLODetector detector, Uint8List bytes,
|
||||
List<DetectionResult> platform) async {
|
||||
final dart = await detector
|
||||
.detectPhoto(await Isolate.run(() => decodePhotoDart(bytes)));
|
||||
String count(List<DetectionResult> r, int c) =>
|
||||
'${r.where((d) => d.classId == c).length}';
|
||||
var maxShift = 0.0;
|
||||
for (final a in platform) {
|
||||
var nearest = double.infinity;
|
||||
for (final b in dart.where((b) => b.classId == a.classId)) {
|
||||
nearest = min(nearest,
|
||||
sqrt(pow((a.x1 + a.x2 - b.x1 - b.x2) / 2, 2) + pow((a.y1 + a.y2 - b.y1 - b.y2) / 2, 2)));
|
||||
}
|
||||
maxShift = max(maxShift, nearest);
|
||||
}
|
||||
debugPrint('🔍 decoder check: bullets ${count(platform, 0)} vs ${count(dart, 0)}, '
|
||||
'rings ${count(platform, 2)} vs ${count(dart, 2)} (platform vs Dart), '
|
||||
'largest position difference ${maxShift.isFinite ? maxShift.toStringAsFixed(1) : '–'} px');
|
||||
}
|
||||
|
||||
void _showBulletDialog() {
|
||||
showCameraSheet(
|
||||
context,
|
||||
@@ -411,8 +444,15 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
backgroundColor: Colors.white,
|
||||
onPressed: _takePicture,
|
||||
child: const Icon(Icons.camera_alt, color: Colors.black),
|
||||
onPressed: _processing ? null : _takePicture,
|
||||
child: _processing
|
||||
? const SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 3, color: Colors.black),
|
||||
)
|
||||
: const Icon(Icons.camera_alt, color: Colors.black),
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: _initializeControllerFuture,
|
||||
|
||||
Reference in New Issue
Block a user