rainyl / opencv_dart

OpenCV bindings for Dart language and Flutter.
https://pub.dev/packages/opencv_dart
Apache License 2.0
78 stars 10 forks source link
dart ffi flutter opencv opencv4

opencv_dart

OpenCV Bindings for Dart Language.

Star on Github https://pub.dev/packages/opencv_dart https://pub.dev/packages/opencv_dart License: Apache-2.0 Native Assets Build

[!IMPORTANT] For v1.0.6 and later, auto setup is supported, libs will be downloaded from Releases automatically.

  1. If you want to setup manually, please set OPENCV_DART_DISABLE_AUTO_BUILD environment variable, e.g., export OPENCV_DART_DISABLE_AUTO_BUILD=1(for Unix-like) or $env:OPENCV_DART_DISABLE_AUTO_BUILD=1(for Windows)
  2. iOS: support universal framework(os64, fat lib for x86_64 and arm64)
  3. Android: all supported ABIs (x86_64 arm64-v8a armeabi-v7a) will be built, if you want to reduce the app size, please add --split-per-abi to the flutter build command, e.g., flutter build apk --release --target-platform android-arm64,android-x64 --split-per-abi, otherwise, all .so will be packaged to app.

For v1.0.4 and below, make sure run the following setup commands before running your app:

  1. flutter pub add opencv_dart or dart pub add opencv_dart
  2. dart run opencv_dart:setup <platform> --arch <arch>
platform arch
android x86_64 arm64-v8a armeabi-v7a
linux x64
windows x64
macos x64 arm64
ios x64 arm64

[!NOTE] WIP, contributions are welcome!

Example

example

Supported Platforms

Platform Supported Tested Prebuilt Binaries
Android :white_check_mark: :ballot_box_with_check: x86_64, arm64-v8a, armeabi-v7a
iOS :white_check_mark: :ballot_box_with_check: arm64, x64(Simulator)
Linux :white_check_mark: :white_check_mark: x64
Windows :white_check_mark: :white_check_mark: x64
macOS :white_check_mark: :white_check_mark: x64, arm64

Status

Core Modules

module Binding status Test status description
core :white_check_mark: :white_check_mark: Core module
calib3d :white_check_mark: :white_check_mark: Calib3D module
dnn :white_check_mark: :white_check_mark: DNN module
features2d :white_check_mark: :white_check_mark: Features2D module
gapi :x: :x: GAPI module
highgui :white_check_mark: :white_check_mark: HighGUI module
imgcodecs :white_check_mark: :white_check_mark: ImageCodecs module
imgproc :white_check_mark: :white_check_mark: ImageProc module
ml :x: :x: ML module
objdetect :white_check_mark: :white_check_mark: Object Detection module
photo :white_check_mark: :white_check_mark: Photo module
stitching :ballot_box_with_check: :ballot_box_with_check: Stitching module
svd :white_check_mark: :white_check_mark: SVD module
video :white_check_mark: :white_check_mark: Video module
videoio :white_check_mark: :white_check_mark: VideoIO module

Contrib Modules

module Binding status Test status description
aruco :white_check_mark: :white_check_mark: ArUco module
img_hash :white_check_mark: :white_check_mark: Image hashing module
cuda :x: :x:
wechat_qrcode :white_check_mark: :white_check_mark:
bgsegm :x: :x:
superres :x: :x:
xfeatures2d :x: :x:
ximgproc :x: :x:
xobjdetect :x: :x:
xphoto :x: :x:

Usage

[!WARNING] Since v1.0.0, nearly ALL APIs were changed to compitable with opencv-python, for example:

// old API
void cvtColor(Mat src, Mat dst, int code);
// new API
Mat cvtColor(Mat src, int code, {Mat? dst});

// then usage will be changed from:
cvtColor(src, dst, cv.COLOR_BGR2GRAY);
// to:
final dst = cvtColor(src, cv.COLOR_BGR2GRAY);
// or:
cvtColor(src, cv.COLOR_BGR2GRAY, dst: dst);

If you really need updates for v0.6.x, open PRs and it will be merged to v0.6 branch.

Pure Dart

import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
  final img = cv.imread("test/images/lenna.png", flags: cv.IMREAD_COLOR);
  final gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY);
  print("${img.rows}, ${img.cols}");

  cv.imwrite("test_cvtcolor.png", gray);
}

Flutter

see example

More examples are on the way...see opencv_dart.examples and share yours

TODO

For Developers

[!NOTE] since v1.0.1, to speed up compile in CI, opencv is precompiled in opencv.full, and this repo will download the prebuilt static libraries from it's release, if you want to compile entirely by yourself, you can compile opencv and explicitly set -o opencv_dir=<path to opencv> for the below commands or set OpenCV_DIR environment variable.

How to compile

1. Install dependencies

2. clone this repo

git clone https://github.com/rainyl/opencv_dart.git
cd opencv_dart

3. compile

If you want to use your own NDK instead of conan maintained one, please set ANDROID_NDK_HOME or ANDROID_NDK_ROOT environment variable, e.g., export ANDROID_NDK_HOME=/opt/android-ndk-r26c

  conan build . -b missing -pr:h profiles/android-<arch> -c tools.android:ndk_path="<ABSOLUTE path for ndk>"

4. test

5. Cross-compile for linux aarch64

Compiling for linux aarch64 requires GCC 13 and newer, conan toolchain for linux arm is located in opencv.full, explore more there.

Contributors

rainyl
rainy liu
abdelaziz-mahdy
Abdelaziz Mahdy
Jiinwoo
JinWoo Jung
mdeleau
mdeleau
totemat
Matteo T.

Acknowledgement

Star History

Star History Chart

License

Apache-2.0 License