native_vision.dart 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'dart:ffi';
  2. import 'dart:io';
  3. import 'package:ffi/ffi.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:path_provider/path_provider.dart';
  7. import 'dynamic_lib_singleton.dart';
  8. class DocCleanFilter {
  9. static final DynamicLibrary _nativeVision = DynamicLibSingleton().dl;
  10. // static final int Function(Pointer<Uint8> src, Pointer<Pointer<Uint8>> dst,
  11. // int width, int height) _nativeAdd =
  12. // _nativeVision
  13. // .lookup<
  14. // NativeFunction<
  15. // Int32 Function(Pointer<Uint8>, Pointer<Pointer<Uint8>>, Int32,
  16. // Int32)>>('native_add')
  17. // .asFunction();
  18. static final int Function(Pointer<Utf8> modelPath) _nativeInit = _nativeVision
  19. .lookup<NativeFunction<IntPtr Function(Pointer<Utf8>)>>('init')
  20. .asFunction();
  21. static final void Function(int docFilterPtr) _nativeRelease = _nativeVision
  22. .lookup<NativeFunction<Void Function(IntPtr)>>('release')
  23. .asFunction();
  24. static final int Function(
  25. Pointer<Utf8> imgPath, Pointer<Utf8> dstImgPath, int docFilterAdr)
  26. _nativeProc = _nativeVision
  27. .lookup<
  28. NativeFunction<
  29. Int32 Function(
  30. Pointer<Utf8>, Pointer<Utf8>, IntPtr)>>('process')
  31. .asFunction();
  32. static Future<String> _getModelPath() async {
  33. const modelName = "ssim_0.85_psnr_22.12_model_4.tflite";
  34. // const modelName = "ssim_0.87_psnr_23.88_model_50.tflite";
  35. final tempDir = await getTemporaryDirectory();
  36. final modelCachePath = "${tempDir.path}/$modelName";
  37. final file = File(modelCachePath);
  38. final exists = await file.exists();
  39. if (!exists) {
  40. final modelData =
  41. await rootBundle.load("packages/native_vision/assets/$modelName");
  42. final buffer = modelData.buffer;
  43. final result = await file.writeAsBytes(
  44. buffer.asUint8List(modelData.offsetInBytes, modelData.lengthInBytes));
  45. }
  46. return file.path;
  47. }
  48. static int _initDocModel(String modelPath) {
  49. final modelPathPtr = modelPath.toNativeUtf8();
  50. final docfilterPtr = _nativeInit(modelPathPtr);
  51. malloc.free(modelPathPtr);
  52. return docfilterPtr;
  53. }
  54. static Future<int> init() async {
  55. final modelPath = await _getModelPath();
  56. final docfilterPtr = await compute(_initDocModel, modelPath);
  57. return docfilterPtr;
  58. }
  59. static String _processAsync(args) {
  60. String origImgPath = args["origImgPath"];
  61. int docFilterPtr = args["docFilterPtr"];
  62. final origImgPathPtr = origImgPath.toNativeUtf8();
  63. final dstImgPath =
  64. "${File(origImgPath).parent.path}/processed_${DateTime.now().millisecondsSinceEpoch}.jpg";
  65. final dstImgPathPtr = dstImgPath.toNativeUtf8();
  66. final succ = _nativeProc(origImgPathPtr, dstImgPathPtr, docFilterPtr);
  67. malloc.free(origImgPathPtr);
  68. malloc.free(dstImgPathPtr);
  69. return succ == 1 ? dstImgPath : "";
  70. }
  71. static Future<String> process(String origImgPath, int docFilterPtr) async {
  72. Map args = {"docFilterPtr": docFilterPtr, "origImgPath": origImgPath};
  73. return await compute(_processAsync, args);
  74. }
  75. static Future<void> _release(int docFilterPtr) async {
  76. _nativeRelease(docFilterPtr);
  77. }
  78. static Future<void> release(int docFilterPtr) async {
  79. return await compute(_release, docFilterPtr);
  80. }
  81. }