isolate_utils.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'dart:isolate';
  2. import "dart:typed_data";
  3. import 'package:image/image.dart' as imgLib;
  4. import "package:photos/services/object_detection/tflite/classifier.dart";
  5. import 'package:tflite_flutter/tflite_flutter.dart';
  6. /// Manages separate Isolate instance for inference
  7. class IsolateUtils {
  8. static const String debugName = "InferenceIsolate";
  9. late SendPort _sendPort;
  10. final _receivePort = ReceivePort();
  11. SendPort get sendPort => _sendPort;
  12. Future<void> start() async {
  13. await Isolate.spawn<SendPort>(
  14. entryPoint,
  15. _receivePort.sendPort,
  16. debugName: debugName,
  17. );
  18. _sendPort = await _receivePort.first;
  19. }
  20. static void entryPoint(SendPort sendPort) async {
  21. final port = ReceivePort();
  22. sendPort.send(port.sendPort);
  23. await for (final IsolateData isolateData in port) {
  24. final classifier = ObjectClassifier(
  25. interpreter: Interpreter.fromAddress(isolateData.interpreterAddress),
  26. labels: isolateData.labels,
  27. );
  28. final image = imgLib.decodeImage(isolateData.input);
  29. final results = classifier.predict(image!);
  30. isolateData.responsePort.send(results);
  31. }
  32. }
  33. }
  34. /// Bundles data to pass between Isolate
  35. class IsolateData {
  36. Uint8List input;
  37. int interpreterAddress;
  38. List<String> labels;
  39. late SendPort responsePort;
  40. IsolateData(
  41. this.input,
  42. this.interpreterAddress,
  43. this.labels,
  44. );
  45. }