Skip to content

rainyl/opencv_dart

Repository files navigation

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

Please use v0.3.0 and later version

For v1.0.5 and later, setup is no longer needed, the prebuilt libs will be downloaded from releases automatically, some notes:

  1. iOS: default target architecture is arm64, if you need to run on x64(ios simulator), please set OPENCV_DART_ARCH to x64, e.g., export OPENCV_DART_ARCH=x64
  2. Android: all supported ABIs (x86_64 arm64-v8a armeabi-v7a) will be downloaded, 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
  1. run your app as normal

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.

Note

WIP, contributions are welcome!

Example

example

Supported Platforms

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

Status

Core Modules

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

Contrib Modules

module Binding status Test status description
aruco ArUco module
img_hash Image hashing module
cuda
wechat_qrcode
bgsegm
superres
xfeatures2d
ximgproc
xobjdetect
xphoto
  • ❌ : not finished
  • ☑️ : partially supported
  • ✅ : finished
  • modules not in the above table are not considered, contributions are welcome
  • videoio: cv.VideoCapture from file is not supported yet supported now.

Usage

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...

TODO

  • compile libs for android, linux
  • support for iOS, macOS
  • add more examples
  • documentation
  • modify C wrapper to catch exceptions
  • Native Assets, see native-assets branch
  • async?
  • more/full test coverage
  • directly include opencv source code, refactor cmakelists.txt

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. prepare a compiler.

    windows: Install Visual Studio 2019 or Later

    ubuntu: reference opencv official build guide to install

    sudo apt-get install build-essential libgtk-3-dev ffmpeg libavcodec-dev cmake \
       ninja-build ccache nasm libavformat-dev libavutil-dev libswscale-dev \
       libgflags-dev python3 libjpeg-dev libpng-dev libtiff-dev python3-pip

    macos:

    brew install --force --overwrite ninja ccache ffmpeg nasm cmake

    from v0.6.4, build system has been migrated to conan

       python3 -m pip install conan
       conan profile detect -f
  2. clone this repo, git clone https://github.com/rainyl/opencv_dart.git

  3. cd opencv_dart

  4. compile

    for windows:

    conan build . -b missing -s compiler.cppstd=20

    for linux, macos:

    conan build . -b missing

    for android, you need to download android ndk and opencv for android sdk, extract opencv sdk and copy and rename OpenCV-android-sdk to build/opencv/android directory. NO need for opencv sdk now, will be compiled from source to enable contrib modules

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

    for ios:

    echo "tools.cmake.cmaketoolchain:user_toolchain=[\"`pwd`/profiles/ios.toolchain.cmake\"]" >> profiles/ios-<arch>
    conan build . -b missing -pr:h profiles/ios-<arch>
  5. If you want to test using vscode, add dynamic library path to "dart.env" in settings.json

Cross-compile for linux aarch64

With proper toolchain, cross-compiling is possible, compiling for linux aarch64 requires GCC 13 and newer, conan toolchain for linux arm is located in opencv.full, explore more there.

Acknowledgement

Star History

Star History Chart

License

Apache-2.0 License