tflite_service.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'dart:ui';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:image/image.dart' as img;
  7. import 'package:image_picker/image_picker.dart';
  8. import 'package:tflite_flutter/tflite_flutter.dart';
  9. import 'package:camera/camera.dart';
  10. /// A detection result parsed from the model's end-to-end output.
  11. class DetectionResult {
  12. final String className;
  13. final int classIndex;
  14. final double confidence;
  15. /// Normalized bounding box (0.0 - 1.0)
  16. final Rect normalizedBox;
  17. const DetectionResult({
  18. required this.className,
  19. required this.classIndex,
  20. required this.confidence,
  21. required this.normalizedBox,
  22. });
  23. Color getStatusColor() {
  24. if (className == 'Empty_Bunch' || className == 'Abnormal') return const Color(0xFFF44336); // Colors.red
  25. if (className == 'Ripe' || className == 'Overripe') return const Color(0xFF4CAF50); // Colors.green
  26. return const Color(0xFFFF9800); // Colors.orange
  27. }
  28. }
  29. /// Custom TFLite inference service that correctly decodes the end-to-end
  30. /// YOLO model output format [1, N, 6] = [batch, detections, (x1,y1,x2,y2,conf,class_id)].
  31. class TfliteService {
  32. static const _modelAsset = 'best.tflite';
  33. static const _labelsAsset = 'labels.txt';
  34. static const int _inputSize = 640;
  35. static const double _confidenceThreshold = 0.25;
  36. Interpreter? _interpreter;
  37. List<String> _labels = [];
  38. final ImagePicker _picker = ImagePicker();
  39. bool _isInitialized = false;
  40. bool get isInitialized => _isInitialized;
  41. Future<void> initModel() async {
  42. try {
  43. // Load labels
  44. final labelData = await rootBundle.loadString('assets/$_labelsAsset');
  45. _labels = labelData.split('\n').where((l) => l.trim().isNotEmpty).map((l) => l.trim()).toList();
  46. // Load model
  47. final interpreterOptions = InterpreterOptions()..threads = 4;
  48. _interpreter = await Interpreter.fromAsset(
  49. 'assets/$_modelAsset',
  50. options: interpreterOptions,
  51. );
  52. _isInitialized = true;
  53. print('TfliteService: Model loaded. Labels: $_labels');
  54. print('TfliteService: Input: ${_interpreter!.getInputTensors().map((t) => t.shape)}');
  55. print('TfliteService: Output: ${_interpreter!.getOutputTensors().map((t) => t.shape)}');
  56. } catch (e) {
  57. print('TfliteService init error: $e');
  58. rethrow;
  59. }
  60. }
  61. Future<XFile?> pickImage() async {
  62. return await _picker.pickImage(
  63. source: ImageSource.gallery,
  64. maxWidth: _inputSize.toDouble(),
  65. maxHeight: _inputSize.toDouble(),
  66. );
  67. }
  68. /// Run inference on the image at [imagePath].
  69. /// Returns a list of [DetectionResult] sorted by confidence descending.
  70. /// Offloaded to a background isolate to keep UI smooth.
  71. Future<List<DetectionResult>> runInference(String imagePath) async {
  72. if (!_isInitialized) await initModel();
  73. final imageBytes = await File(imagePath).readAsBytes();
  74. // We pass the raw bytes and asset paths to the isolate.
  75. // The isolate will handle decoding, resizing, and inference.
  76. return await _runInferenceInIsolate(imageBytes);
  77. }
  78. /// Run inference on a [CameraImage] from the stream.
  79. /// Throttled by the caller.
  80. Future<List<DetectionResult>> runInferenceOnStream(CameraImage image) async {
  81. if (!_isInitialized) await initModel();
  82. // We pass the CameraImage planes to the isolate for conversion and inference.
  83. return await compute(_inferenceStreamTaskWrapper, {
  84. 'planes': image.planes.map((p) => {
  85. 'bytes': p.bytes,
  86. 'bytesPerRow': p.bytesPerRow,
  87. 'bytesPerPixel': p.bytesPerPixel,
  88. }).toList(),
  89. 'width': image.width,
  90. 'height': image.height,
  91. 'format': image.format.group,
  92. 'modelBytes': (await rootBundle.load('assets/$_modelAsset')).buffer.asUint8List(),
  93. 'labelData': await rootBundle.loadString('assets/$_labelsAsset'),
  94. });
  95. }
  96. static List<DetectionResult> _inferenceStreamTaskWrapper(Map<String, dynamic> args) {
  97. // This is a simplified wrapper for stream inference in isolate
  98. final modelBytes = args['modelBytes'] as Uint8List;
  99. final labelData = args['labelData'] as String;
  100. final planes = args['planes'] as List<dynamic>;
  101. final width = args['width'] as int;
  102. final height = args['height'] as int;
  103. final interpreter = Interpreter.fromBuffer(modelBytes);
  104. final labels = labelData.split('\n').where((l) => l.trim().isNotEmpty).map((l) => l.trim()).toList();
  105. try {
  106. // Manual YUV to RGB conversion if needed, or use image package if possible
  107. // For speed in stream, we might want a more optimized conversion.
  108. // But for now, let's use a basic one or the image package.
  109. img.Image? image;
  110. if (args['format'] == ImageFormatGroup.yuv420) {
  111. // Simple YUV420 to RGB (this is slow in Dart, but better in isolate)
  112. image = _convertYUV420ToImage(planes, width, height);
  113. } else if (args['format'] == ImageFormatGroup.bgra8888) {
  114. image = img.Image.fromBytes(
  115. width: width,
  116. height: height,
  117. bytes: planes[0]['bytes'].buffer,
  118. format: img.Format.uint8,
  119. numChannels: 4,
  120. order: img.ChannelOrder.bgra,
  121. );
  122. }
  123. if (image == null) return [];
  124. // Resize and Run
  125. final resized = img.copyResize(image, width: _inputSize, height: _inputSize);
  126. final inputTensor = List.generate(1, (_) =>
  127. List.generate(_inputSize, (y) =>
  128. List.generate(_inputSize, (x) {
  129. final pixel = resized.getPixel(x, y);
  130. return [pixel.r / 255.0, pixel.g / 255.0, pixel.b / 255.0];
  131. })
  132. )
  133. );
  134. final outputShape = interpreter.getOutputTensors()[0].shape;
  135. final outputTensor = List.generate(1, (_) =>
  136. List.generate(outputShape[1], (_) =>
  137. List<double>.filled(outputShape[2], 0.0)
  138. )
  139. );
  140. interpreter.run(inputTensor, outputTensor);
  141. return _decodeDetections(outputTensor[0], labels);
  142. } finally {
  143. interpreter.close();
  144. }
  145. }
  146. static img.Image _convertYUV420ToImage(List<dynamic> planes, int width, int height) {
  147. final yPlane = planes[0];
  148. final uPlane = planes[1];
  149. final vPlane = planes[2];
  150. final yBytes = yPlane['bytes'] as Uint8List;
  151. final uBytes = uPlane['bytes'] as Uint8List;
  152. final vBytes = vPlane['bytes'] as Uint8List;
  153. final yRowStride = yPlane['bytesPerRow'] as int;
  154. final uvRowStride = uPlane['bytesPerRow'] as int;
  155. final uvPixelStride = uPlane['bytesPerPixel'] as int;
  156. final image = img.Image(width: width, height: height);
  157. for (int y = 0; y < height; y++) {
  158. for (int x = 0; x < width; x++) {
  159. final int uvIndex = (uvRowStride * (y / 2).floor()) + (uvPixelStride * (x / 2).floor());
  160. final int yIndex = (y * yRowStride) + x;
  161. final int yp = yBytes[yIndex];
  162. final int up = uBytes[uvIndex];
  163. final int vp = vBytes[uvIndex];
  164. // Standard YUV to RGB conversion
  165. int r = (yp + (1.370705 * (vp - 128))).toInt().clamp(0, 255);
  166. int g = (yp - (0.337633 * (up - 128)) - (0.698001 * (vp - 128))).toInt().clamp(0, 255);
  167. int b = (yp + (1.732446 * (up - 128))).toInt().clamp(0, 255);
  168. image.setPixelRgb(x, y, r, g, b);
  169. }
  170. }
  171. return image;
  172. }
  173. static List<DetectionResult> _decodeDetections(List<List<double>> rawDetections, List<String> labels) {
  174. final detections = <DetectionResult>[];
  175. for (final det in rawDetections) {
  176. if (det.length < 6) continue;
  177. final conf = det[4];
  178. if (conf < _confidenceThreshold) continue;
  179. final x1 = det[0].clamp(0.0, 1.0);
  180. final y1 = det[1].clamp(0.0, 1.0);
  181. final x2 = det[2].clamp(0.0, 1.0);
  182. final y2 = det[3].clamp(0.0, 1.0);
  183. final classId = det[5].round();
  184. if (x2 <= x1 || y2 <= y1) continue;
  185. final label = (classId >= 0 && classId < labels.length) ? labels[classId] : 'Unknown';
  186. detections.add(DetectionResult(
  187. className: label,
  188. classIndex: classId,
  189. confidence: conf,
  190. normalizedBox: Rect.fromLTRB(x1, y1, x2, y2),
  191. ));
  192. }
  193. detections.sort((a, b) => b.confidence.compareTo(a.confidence));
  194. return detections;
  195. }
  196. Future<List<DetectionResult>> _runInferenceInIsolate(Uint8List imageBytes) async {
  197. // We need the model and labels passed as data
  198. final modelData = await rootBundle.load('assets/$_modelAsset');
  199. final labelData = await rootBundle.loadString('assets/$_labelsAsset');
  200. // Use compute to run in a real isolate
  201. return await compute(_inferenceTaskWrapper, {
  202. 'imageBytes': imageBytes,
  203. 'modelBytes': modelData.buffer.asUint8List(),
  204. 'labelData': labelData,
  205. });
  206. }
  207. static List<DetectionResult> _inferenceTaskWrapper(Map<String, dynamic> args) {
  208. return _inferenceTask(
  209. args['imageBytes'] as Uint8List,
  210. args['modelBytes'] as Uint8List,
  211. args['labelData'] as String,
  212. );
  213. }
  214. /// The static task that runs in the background isolate
  215. static List<DetectionResult> _inferenceTask(Uint8List imageBytes, Uint8List modelBytes, String labelData) {
  216. // 1. Initialize Interpreter inside the isolate
  217. final interpreter = Interpreter.fromBuffer(modelBytes);
  218. final labels = labelData.split('\n').where((l) => l.trim().isNotEmpty).map((l) => l.trim()).toList();
  219. try {
  220. // 2. Preprocess image
  221. final decoded = img.decodeImage(imageBytes);
  222. if (decoded == null) throw Exception('Could not decode image');
  223. final resized = img.copyResize(decoded, width: _inputSize, height: _inputSize, interpolation: img.Interpolation.linear);
  224. final inputTensor = List.generate(1, (_) =>
  225. List.generate(_inputSize, (y) =>
  226. List.generate(_inputSize, (x) {
  227. final pixel = resized.getPixel(x, y);
  228. return [pixel.r / 255.0, pixel.g / 255.0, pixel.b / 255.0];
  229. })
  230. )
  231. );
  232. // 3. Prepare output
  233. final outputShape = interpreter.getOutputTensors()[0].shape;
  234. final numDetections = outputShape[1];
  235. final numFields = outputShape[2];
  236. final outputTensor = List.generate(1, (_) =>
  237. List.generate(numDetections, (_) =>
  238. List<double>.filled(numFields, 0.0)
  239. )
  240. );
  241. // 4. Run
  242. interpreter.run(inputTensor, outputTensor);
  243. return _decodeDetections(outputTensor[0], labels);
  244. } finally {
  245. interpreter.close();
  246. }
  247. }
  248. void dispose() {
  249. _interpreter?.close();
  250. _interpreter = null;
  251. _isInitialized = false;
  252. }
  253. }