/opencv_dart

OpenCV bindings for Dart language and Flutter. Support Asynchronous Now!

Primary LanguageDartApache License 2.0Apache-2.0

opencv_dart

OpenCV Bindings for Dart Language. Support both asynchronous and synchronous!

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

Important

For v1.0.6 and later, 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)

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 os64 (universal framework)

Note

WIP, APIs may change in the future, 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

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 awesome-opencv_dart and share yours

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

  • Windows: Install Visual Studio 2019 or Later, install Conan

     python3 -m pip install conan
     conan profile detect -f

    If you are usin Scoop:

    scoop install conan
  • Linux: Ubuntu as example, note opencv.full is built on Ubuntu 22.04 with ffmpeg 4.4

    sudo apt-get install build-essential libgtk-3-dev ffmpeg libavcodec-dev cmake \
      ninja-build libavformat-dev libavutil-dev libswscale-dev \
      libgflags-dev python3 libjpeg-dev libpng-dev libtiff-dev python3-pip
    
    python3 -m pip install conan
    conan profile detect -f
  • macOS: XCode is required

    brew install --force --overwrite ninja ffmpeg@6 conan
    brew link --overwrite ffmpeg@6
    conan profile detect -f

2. clone this repo

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

3. compile

  • Windows:

    conan build . -b missing -s compiler.cppstd=20
  • Linux, macos:

    conan build . -b missing
  • android

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>"
  • ios:

    conan build . -b missing -pr:h profiles/ios-<arch>

4. test

  • Set OPENCV_DART_LIB_PATH environment variable to the path of the compiled dynamic library, e.g., export OPENCV_DART_LIB_PATH=pwd/linux/libopencv_dart.so or $ENV:OPENCV_DART_LIB_PATH=$PWD\windows\opencv_dart.dll
  • or append the lib path to the library search path of your system
  • If you want to test using vscode, add above variable to "dart.env" in settings.json

Contributors

rainyl
rainy liu
abdelaziz-mahdy
Abdelaziz Mahdy
einsitang
爱因斯唐
Jiinwoo
JinWoo Jung
mdeleau
mdeleau
totemat
Matteo T.

Acknowledgement

Star History

Star History Chart

License

Apache-2.0 License