maxruby / OpenCV.jl

The OpenCV (C++) interface for Julia
Other
105 stars 28 forks source link

Libs are not properly detected #3

Closed NicolasCARPi closed 7 years ago

NicolasCARPi commented 9 years ago

Hello,

First I would like to thank you and congratulate you on your work :)

I've installed julia 0.4-dev, compiled and installed successfully OpenCV 3.0-BETA, as instructed. But when I do using OpenCV, it doesn't find the libs.

julia> using OpenCV;
libopencv_core.3.0.0 is not installed
libopencv_highgui.3.0.0 is not installed
libopencv_imgproc.3.0.0 is not installed
libopencv_video.3.0.0 is not installed
libopencv_videoio.3.0.0 is not installed
libopencv_calib3d.3.0.0 is not installed
libopencv_features2d.3.0.0 is not installed
libopencv_flann.3.0.0 is not installed
libopencv_imgcodecs.3.0.0 is not installed
libopencv_ml.3.0.0 is not installed
libopencv_objdetect.3.0.0 is not installed
libopencv_photo.3.0.0 is not installed
libopencv_shape.3.0.0 is not installed
libopencv_stitching.3.0.0 is not installed
libopencv_superres.3.0.0 is not installed
libopencv_videostab.3.0.0 is not installed
libopencv_viz.3.0.0 is not installed
ERROR: LoadError: No pre-installed libraries. Set path manually or install OpenCV.
 in include at ./boot.jl:249
 in include_from_node1 at ./loading.jl:128
 in reload_path at ./loading.jl:152
 in _require at ./loading.jl:67
 in require at ./loading.jl:52
 in print at ./string.jl
while loading /home/ktr/.julia/v0.4/OpenCV/src/OpenCV.jl, in expression starting on line 56

The libs are in /usr/local/lib (see screenshot : https://i.imgur.com/pIiWMWe.png).

How can I fix this ? I'd be OK to add the path manually (how do I do that btw?), but as it seems to be the default path, maybe there is a problem somewhere…

I'm running Archlinux. Tell me if you need more infos :)

Regards, ~Nico

maxruby commented 9 years ago

Sorry for the trouble - it will be easy to fix. You are the first one who asks about the installation for LInux and I never had a chance to test it, so its a good opportunity to fix this.

The "quick fix" is to go into src/OpenCV.jl and add the path manually in lines 52 and 53. However, the way I set it up, OpenCV.jl should find automatically the path for your libs based on the default path used by the OpenCV installation (which is OS-dependent).

Can you tell me what you get when you type the following in the Julia REPL?

(so,si,pr) = readandwrite(`pkg-config --libs opencv`)
output = readall(so)
close(so)

path = match(Regex("/usr/local/lib/"), output)
path != nothing ? println("Found a local OpenCV installation in $(path.match).") : nothing

If you get path =nothing, then there is problem with the search itself and I should definitely fix it so it works on all OS in general.

NicolasCARPi commented 9 years ago

Here is the result of what you asked :

julia> (so,si,pr) = readandwrite(`pkg-config --libs opencv`)
(Pipe(closed, 437 bytes waiting),Pipe(open, 0 bytes waiting),Process(`pkg-config --libs opencv`, ProcessExited(0)))

julia> output = readall(so)
"-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -ltbb /lib64/libXext.so /lib64/libX11.so /lib64/libICE.so /lib64/libSM.so /lib64/libGL.so /lib64/libGLU.so -lrt -lpthread -lm -ldl \n"

julia> close(so)

julia> path = match(Regex("/usr/local/lib/"), output)

julia> path != nothing ? println("Found a local OpenCV installation in $(path.match).") : nothing

julia> path

I then tried your quickfix, added directly the path to cvlibdir and cvheaderdir at indicated lines. But it throws the same error, except it's from the elseif !found loop. So I added it here also. And now I get an ArgumentError from the for i in opencv_libraries loop.

Do I need to recompile opencv with RTLD_GLOBAL (I have no idea what this is) ? Or maybe I'll just wait for a fix!

Thank you for your help :)

~Nico

maxruby commented 9 years ago

I will fix it when I get home. Sorry for the trouble.

NicolasCARPi commented 9 years ago

It's cool, no worries ;)

maxruby commented 9 years ago

I am working on this now. RTLD_GLOBAL is for Julia to use interdependent libraries where symbols must be made available for usage in other shared libraries - this is relevant for OpenCV libraries.

The issue here is that your Linux system is not detecting the location of the OpenCV shared libraries after searching in line 36 of src/OpenCV.jl:

path = match(Regex("/usr/local/lib/"), output)

This can be easily fixed by changing the Regex term to -libopencvsopath !=nothing.

I can see from your screenshot that your .so libraries are listed differently in Linux compared to OSX (/usr/local/lib/libopencv_calib3d.dylibin OSX vs -libopencv_calib3d.so in Linux).
What do you get when you type the following in the Julia REPL?

dlopen("/usr/local/lib/libopencv_calib3d.dylib")
NicolasCARPi commented 9 years ago

This is what I get : osef

maxruby commented 9 years ago

Sorry, I should correct this (it should be .so), what happens when you type. e.g.,

dlopen("/usr/local/lib/libopencv_calib3d.so")
NicolasCARPi commented 9 years ago

This works fine.

maxruby commented 9 years ago

So you got something like Ptr{Void} @0x00007fc03a6d1d20?

NicolasCARPi commented 9 years ago

Yeah, for some reason I can't copy/paste term output today… But this is what I get :)

maxruby commented 9 years ago

Before I commit changes and push to GitHub, can you check that your OpenCV header files are in

/usr/local/include/
NicolasCARPi commented 9 years ago

Yes, it looks so : https://i.imgur.com/f4aNLZv.png

maxruby commented 9 years ago

I hope this will be fixed with the changes I have made - I will submit it in the next 30 min.

However, one more thing to keep in mind is that if you need to include more advanced modules from opencv_contrib-master (e.g., bgsegm), currently the path must be manually set in src/OpenCV.jl (lines 101-105). I will try to make a specific module for the advanced algorithms and libraries, but for now I suggest that you add your own system-specific paths for that.

maxruby commented 9 years ago

For example, currently if you want to use the video module bgsegm functions from OpenCV 3.0.0, you have to

NicolasCARPi commented 9 years ago

I've git cloned again, but it still fails to load the libs :/ (same error as before). I'll try to find a solution myself later. In the meantime, I would strongly recommend diving into linux, have a look at docker, it's very cool to test out things :)

Regards, ~Nico

maxruby commented 9 years ago

I know the source of the problem. Its because the output from line 37 in src/OpenCV.jl which you get from the REPL is different in Linux from OSX - sorry I missed that.

Thanks for the link to docker - looks very cool.

maxruby commented 9 years ago

OK. Now I see exactly why you are having problems installing this on Linux and why you might have other problems than we did not anticipate . . .

The first thing to note is that your screenshot of the opencv libs directory does not fully match the listing of the corresponding libs in the output of the Julia REPL:

julia> output = readall(so) "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -ltbb /lib64/libXext.so /lib64/libX11.so /lib64/libICE.so /lib64/libSM.so /lib64/libGL.so /lib64/libGLU.so -lrt -lpthread -lm -ldl \n"

For example, the videoio module apparently in your /usr/local/lib directory is not listed in the output. Julia raised errors when first trying to check whether the libs are in your output.

I already fixed the code to make sure that it detects all your currently compiled libraries (according to your REPL output), but I am wondering whether you have successfully compiled all the necessary libs for OpenCV. It seems to me that e.g., you dont haveimgcodecs` listed in the output, but it is in your directory. Were there any errors during building of OpenCV? I do not understand why they are not listed in the output (this is not the case for OSX).

I just committed the changes to master.

NicolasCARPi commented 9 years ago

There were no errors during compilation of OpenCV as far as I know. It is true that the output of pkg-config doesn't match my libs in /usr/local/lib, which is weird.... I tried latest version, and it's better, because it cannot find the ones that are different from pkg-config output and ls /usr/local/lib. But after it fails at loading the ones that are not found. So maybe if you remove the ones not found in pkg-config from opencv_libraries, it might just work fine :) Here is the output : https://i.imgur.com/dbhZxaq.png

maxruby commented 9 years ago

I think the best compromise is not to throw an error (but inform) when it does not find the lib in another Linux or Windows (line 76 of src/OpenCV.jl).

Please try the latest commit.

NicolasCARPi commented 9 years ago

I want to try it, but it'll have to wait until monday as I can't access my work computer now :/ Have a nice weekend :)

NicolasCARPi commented 9 years ago

Hello Max, I tried to run your new code, but julia crashed. After that, I uninstalled opencv2, and the output was as before. After that, I uninstalled opencv3 also, and now I'm rebuilding/installing it differently, and I'll keep you updated on my endeavor :)

maxruby commented 9 years ago

OK. Julia crashing because of OpenCV.jl is rather odd. . . I am certain that we can figure this out. The main thing is to make sure that when you load each lib with dlopen_e the path to the lib has to be correct. If the latest code still does not work for you, I suggest you first try loading the libs with a simple iteration of dlopen_e calls. I confimed already that the lib path/names match your REPL output, so if this does not work, something else must be missing elsewhere that is Linux related.

NicolasCARPi commented 9 years ago

Hello Max. As I told you yesterday, I've reinstalled the 3.0.0-BETA with my package manager instead of compiling it myself. So as a result, libs are no more in /usr/local/lib but directly in /usr/lib.

If I change the lines 60 and 61 and remove the /local, it works.

Please have a look at this screenshot showing what is happening : https://i.imgur.com/PXxMkRA.png

To fix the loading of qt5, I changed line 21 of deps/Qt_support.jl and removed the 5, as on my machine, qt5 is just called qt. And line 22 (qtlibdir) is just /usr/lib. Line 25 is changed to :

 addHeaderDir(string(qtincdir, "/", "QtWidgets"), kind = C_System)

Now the Qt5 libs are loaded. Finally, look at the right upper part of this screenshot : https://i.imgur.com/kN7nvK2.png You can see that there is progress !

But there is this error about compiling with -fPIC or -fPIE, but that is qt related :)


As you can see, in GNU/Linux, libs can be in /usr/lib or /usr/local/lib. So I suggest your code looks at this two places. Also, qt5 can be called qt.

It looks like it works now (I can use opencv functions), will see if the qt5 error is relevant. \o/

maxruby commented 9 years ago

Great! It looks like there are some important differences in how Linux and OSX handle the paths to the libs. But the good news is that it is easy to fix. I will make the changes to the code based on your findings and wrapped them inside @linux_only. Hopefully it will make it easier for others to use :)

Regarding Qt paths, I am not surprised. The original code from Cxx.jl also had user-specific paths for loading the libs and although I tried to make it OSX-compatible, it is obviously not so for Linux or Windows. Having said that, Qt is not essential for most of the OpenCV.jl functionality. Its mainly used for a couple of convenient functions, e.g., imread() but otherwise not required. If however, you want to take advantage of Qt GUI support via OpenCV.jl, then it will be convenient to set up properly!

ibadami commented 9 years ago

@maxruby
I am working on Ubuntu 14.04 with master branch of julia and Opencv3 I installed Cxx.jl successfully. After cloning OpenCV.jl, I am getting the same set of errors as discussed above when I type using OpenCV . I looked inside the OpenCV.jl file, the path = nothing at line 50 All my libraries are found in pkg-config though in lines 51-55 At line 59 the check path !=nothing fails and try to find the preinstalled libraries in ./deps.

Since my opencv is installed in the same location as lines 60 and 61 are set. I changed line 59 to following if path != nothing to if path == nothing just to pass the check. then it trows error at line 76 , the variable RTLD_GLOBAL (I do not know what it does) is undefined. I removed the RTLD_GLOBAL variable to see what happens. It compiles and trows me this last error

ERROR: LoadError: LoadError: UndefVarError: QtWidgets not defined
 in include at ./boot.jl:261
 in include_from_node1 at ./loading.jl:304
 in include at ./boot.jl:261
 in include_from_node1 at ./loading.jl:304
 in require at ./loading.jl:243
while loading /home/badami/.julia/v0.5/OpenCV/./deps/Qt_support.jl, in expression starting on line 25
while loading /home/badami/.julia/v0.5/OpenCV/src/OpenCV.jl, in expression starting on line 134

So I commented the line 130 # Load Qt framework include(joinpath(Pkg.dir("OpenCV"), "./deps/Qt_support.jl")) to see what happens. It compiles without anymore errors. But then when I try to use cvPoint(10,10) I get

ERROR: Could not find Point in context cv
 in error at ./error.jl:21
 in lookup_name at /home/badami/.julia/v0.5/Cxx/src/typetranslation.jl:116

Can you please guide me here? Thank you.

maxruby commented 9 years ago

It looks like there are changes in Cxx/src/typetranslation.jl which cause the error in CvPoint. I am sorry to say that since I have not updated Cxx for a while, I have not encountered these errors. I will need to update julia and Cxx.jl in order to find out what is going on. It seems related to the handling of C++ types by Cxx, but it is just a guess.

ibadami commented 9 years ago

Do you think you will get a chance to update both Julia and Cxx soon?

maxruby commented 9 years ago

I will work on this tonight. I will let you know once I have checked the issue. Am 20.09.2015 17:19 schrieb "Ishrat" notifications@github.com:

Do you think you will get a chance to update both Julia and Cxx soon?

— Reply to this email directly or view it on GitHub https://github.com/maxruby/OpenCV.jl/issues/3#issuecomment-141796396.

ibadami commented 9 years ago

That is great! Thank you.

On Tue, Sep 22, 2015 at 7:37 AM, Maximiliano Suster < notifications@github.com> wrote:

I will work on this tonight. I will let you know once I have checked the issue. Am 20.09.2015 17:19 schrieb "Ishrat" notifications@github.com:

Do you think you will get a chance to update both Julia and Cxx soon?

— Reply to this email directly or view it on GitHub https://github.com/maxruby/OpenCV.jl/issues/3#issuecomment-141796396.

— Reply to this email directly or view it on GitHub https://github.com/maxruby/OpenCV.jl/issues/3#issuecomment-142185473.

Ishrat Badami Ph.D. Student Computer Vision Group UMIC Research Centre RWTH Aachen University Room No 129 Mies-van-der-Rohe Strasse 15 D-52074 Aachen

Germany

Tel: +49 241 80 20775 Fax: +49 241 80 22731

e-mail: badami@vision.rwth-aachen.de

URL: http://www.vision.rwth-aachen.de/people/ishrat-badami

maxruby commented 9 years ago

@ibadami.

Putting aside the issues of loading the opencv libs specifically in Ubuntu 14.04 with Libdl.dlopen and RTLD_GLOBAL, I can not reproduce your error. This is the output I get from opencv.jl in the console of my latest build from julia 0.5.0-dev+318 on OSX 10.10.5:

julia> cvPoint(10,10)
Cxx.CppValue{Cxx.CxxQualType{Cxx.CppTemplate{Cxx.CppBaseType{symbol("cv::Point_")},Tuple{Int32}},(false,false,false)},8}((0x0a,0x00,0x00,0x00,0x0a,0x00,0x00,0x00))
julia> cvRect(5,5,300,300)
Cxx.CppValue{Cxx.CxxQualType{Cxx.CppTemplate{Cxx.CppBaseType{symbol("cv::Rect_")},Tuple{Int32}},(false,false,false)},16}((0x05,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x2c,0x01,0x00,0x00,0x2c,0x01,0x00,0x00))

I am not sure why you are seeing this error, but I suspect it has to do with the implementation of lookup_name which relies on the Clang compiler. Maybe you should file the issue in Cxx.jl.

ibadami commented 9 years ago

Thank you for your feedback. One last question. Which version of OpenCV are you using?

maxruby commented 9 years ago

I compiled the latest master from the opencv 3.0 repository.

maxruby commented 9 years ago

@ibadami. Glad to hear it is working for you! We will need to have a more universal way to handle the Headers and libs. At this point, I dont have too much time for maintaing or extending opencv.jl.

ibadami commented 9 years ago

I understand. I am currently working actively with Julia and hence OpenCV.jl (Thanks to you!). If you wish I can keep writing the wrappers of all those functions/modules that are not yet available in OpenCV.jl, whenever I need to use them. Once I am done with one complete module, I can send you the pull request.

maxruby commented 9 years ago

Sounds good.