Home > Software design >  Is it possible for me to run Python OpenCV on Flutter
Is it possible for me to run Python OpenCV on Flutter

Time:12-15

I am currently developing an app using Flutter and would like to use OpenCV-related scripts to read (OMR).
Is it possible for me to run Python OpenCV (.py) in Flutter and by running multiple devices at the same time and is production server.

CodePudding user response:

Yes, You can use OPEN_CV library for android and iOS devices.

OR

You can use chaquopy for custom scripts in python for android only devices.

CodePudding user response:

yes there is an opencv library you can import https://pub.dev/packages/opencv

you access the opencv through platform channels https://docs.flutter.dev/development/platform-integration/platform-channels

try {
      platformVersion = await OpenCV.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

see on how to run the opencv native How to use OpenCV 4 in native C of Flutter (in 2021) (support Flutter 2.0)?

step by step instructions on how to implement edge detection using opencv in flutter

  1. Detection to happens in native C and you use Dart foreign function interface or dart:ffi to bridge to the c code running on your device

https://www.flutterclutter.dev/flutter/tutorials/implementing-edge-detection-in-flutter/2020/1509/

final processImage = nativeEdgeDetection
        .lookup<NativeFunction<ProcessImageFunction>>("process_image")
        .asFunction<ProcessImageNativeFunction>();

 you can access c   functions as pointers by dart
 https://stackoverflow.com/questions/68657817/flutter-get-image-as-data-back-from-c-opencv

 read by access opencv code native using dart.ffi
 https://docs.flutter.dev/development/platform-integration/c-interop

c code

#include <stdint.h>

extern "C" __attribute__((visibility("default"))) __attribute__((used))
int32_t native_add(int32_t x, int32_t y) {
    return x   y;
}

dart code (create the platform or bridge to the c/c code then lookup the code function)

final DynamicLibrary nativeAddLib = Platform.isAndroid
    ? DynamicLibrary.open('libnative_add.so')
    : DynamicLibrary.process();

final int Function(int x, int y) nativeAdd = nativeAddLib
    .lookup<NativeFunction<Int32 Function(Int32, Int32)>>('native_add')
    .asFunction();

dart communicates through channels with the os

https://www.raywenderlich.com/21512310-calling-native-libraries-in-flutter-with-dart-ffi

quote:

dart code can’t interoperate with platform code directly. The Dart compiler can’t use the same types, memory layouts, function signatures and other lower-level conventions your platform code uses.

You need a bridge between the two that takes code Dart understands and translates it to code that Java, Kotlin, Swift or Objective-C understand.

The Flutter authors addressed this with plugins and platform channels, which let messages pass between Dart and platform code.

dart.ffi quote

As mentioned earlier, FFI stands for Foreign Function Interface, a general term for any mechanism that lets developers write code in one programming language to invoke, or bridge, code written in another.

opencv.js

another impressive way to access opencv is by javascript try using opencv.js

https://docs.opencv.org/3.4/d5/d10/tutorial_js_root.html

step by step instructions to calling opencv.js in flutter

https://liewjuntung.medium.com/use-javascript-in-flutter-web-a6eed3efb9a0

  • Related