rainyl / opencv_dart

OpenCV bindings for Dart language and Flutter. Support Asynchronous Now!
https://pub.dev/packages/opencv_dart
Apache License 2.0
137 stars 18 forks source link

Failed to load dynamic library 'libopencv_dart.so': libavcodec.so.58: Can't open #113

Closed YancyHsu closed 5 months ago

YancyHsu commented 5 months ago

https://github.com/rainyl/opencv_dart/blob/b16eb1bab15cdbee39282431da71d81313d1a431/lib/src/core/base.dart#L46

On Linux error: ArgumentError (Invalid argument(s): Failed to load dynamic library 'libopencv_dart.so' Unbuntu Ubuntu 24.04 LTS

i have check the libPath here:

final libPath =
Platform.environment["OPENCV_DART_LIB_PATH"] ?? defaultLibPath;
print(File(libPath).absolute.existsSync());

got:

flutter: true

i just put libopencv_dart.so in example folder, and check its existence, the code above

rainyl commented 5 months ago

No need to manually copy libopencv_dart.so, add -v to see whether the libs were downloaded correctly, if correct, there should be a linux/flutter/ephemeral/.plugin_symlinks/opencv_dart/linux/libopencv_dart-linux-x64.tar.gz in your project dir and the extracted .so file is there, if not you can delete the libopencv_dart-linux-x64.tar.gz and try again, if your network is unstable, the libopencv_dart-linux-x64.tar.gz may be broken.

I have tested it on WSL 2 with Ubuntu 22.04 and it worked as expected. image

rainyl commented 5 months ago

Another way is to setup it manually or copy the .so to /linux/flutter/ephemeral/.plugin_symlinks/opencv_dart/linux/

And, if you really can't make work, just set the OPENCV_DART_LIB_PATH env variable to the ABSOLUTE path of libopencv_dart.so (you can download it from releases) and it should work. sorry I forgot that this var was designed only for debug and tests, not for users.

YancyHsu commented 5 months ago

the way you said above I tried, and .so exists as the libPath def line, as print out 'true', so i dont know why.

Maybe wsl and real linux is different in building .so?

I will try to build .so in my ubuntu system later

rainyl commented 5 months ago

Allright, then please add -v to see whether some errors exists, and .so was built by github action with Ubuntu 22.04 too, maybe some dependencies were not satisfied, I suggest you to use v2.x version if you use Flutter with Native Assets supported, or try to build it yourself.

image

YancyHsu commented 5 months ago

ArgumentError (Invalid argument(s): Failed to load dynamic library 'libopencv_dart.so': libavcodec.so.58: 无法打开共享对象文件: 没有那个文件或目录)

Maybe libavcodec.so.58 cause it?

I use as README said:

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

and

ldconfig -p

only find libavcodec.so.60, libavcodec.so.0 ,libavcodec.so, is it the reason?

rainyl commented 5 months ago

Yes, opencv used by this project was linked with ffmpeg 5.8 on ubuntu 22.04, but it seems newer ubuntu installs ffmpeg 6.0 (libavcodec.so.60), so either downgrade ffmpeg or use opencv_dart 2.x version, or you can build it by yourself.

YancyHsu commented 5 months ago

ok, i will try

YancyHsu commented 5 months ago

I try to build opencv.full by myself, but failed to get .so. Then I tried directly in opencv source code root folder: bash.sh:

rm -rf build_dart
mkdir build_dart && cd build_dart
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_opencv_dart=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j$(nproc)
make install

it success, and I use the .so, it's only 37mb, yours are 60mb, I dont know why. Then I use the 37mb one to try, still fail.

YancyHsu commented 5 months ago

Funny and finally, I tried this: add lines in ~/.profile:

export LD_LIBRARY_PATH=/opt/kingsoft/wps-office/office6
export PATH=$LD_LIBRARY_PATH:$PATH

because WPS has this libavcodec.so.58, not like ubuntu is self

then the example runs!!! success!!!

rainyl commented 5 months ago

I try to build opencv.full by myself, but failed to get .so.

I do not use Ubuntu 24.04 so not tested, but theoretically it should works, just install conan and other deps and follow the commands in workflow. But remember to firstly compile opencv.full and then compile opencv_dart, you need to pass -o opencv_dir=<compiled opencv dir includes cmake files> and then use conan to compile opencv_dart

because WPS has this libavcodec.so.58, not like ubuntu is self

Well, it looks like to be the problem of ffmpeg version as I said above, so you can find those .so.58 from ubuntu pkg website and extract them, then it should work.

But the best solution should be compile it with 6.0, I will add a new workflow when having some free time.

YancyHsu commented 5 months ago

I try to build opencv.full by myself, but failed to get .so.

I do not use Ubuntu 24.04 so not tested, but theoretically it should works, just install conan and other deps and follow the commands in workflow. But remember to firstly compile opencv.full and then compile opencv_dart, you need to pass -o opencv_dir=<compiled opencv dir includes cmake files> and then use conan to compile opencv_dart

because WPS has this libavcodec.so.58, not like ubuntu is self

Well, it looks like to be the problem of ffmpeg version as I said above, so you can find those .so.58 from ubuntu pkg website and extract them, then it should work.

But the best solution should be compile it with 6.0, I will add a new workflow when having some free time.

Just as you said, I tried -o opencv_dir, then it compiled successfully, andr everything is ok. I can not remember what I did before, maybe the network was bad then.

YancyHsu commented 5 months ago

To summarize, fixed as author said, build by yourself, use this command: https://github.com/rainyl/opencv_dart/blob/b16eb1bab15cdbee39282431da71d81313d1a431/.github/workflows/build_test_release.yaml#L102 use the .so in /home/yancy/Projects/opencv_dart/example/linux/flutter/ephemeral/.plugin_symlinks/opencv_dart

YancyHsu commented 5 months ago

Oh! I find it, if complie failed, write this in ~/.profile:

export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu
export PATH=$LD_LIBRARY_PATH:$PATH

Then conan can compile libavcodec.so.58 and other .so in x86_64-linux-gnu

rainyl commented 5 months ago

Great! Thanks for sharing your solutions, which will help others a lot.