Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build only for python2 #6

Open
arielkantorovich opened this issue Mar 27, 2022 · 16 comments
Open

Build only for python2 #6

arielkantorovich opened this issue Mar 27, 2022 · 16 comments

Comments

@arielkantorovich
Copy link

Hi, I use ./OpenCV-4-5-1.sh and it's successful.
Unfortunately, it's built on my Jetson Nano only for python2 and I want to use python3 I don't understand why?
I would happy how can I build OpenCV with Cuda on python3?

@Qengineering
Copy link
Owner

Strange. OpenCV should be installed for both, Python 2 and 3.
Please check you installation with the following command in Python 2 (in your case)

$ python
>>> import cv2
>>> print( cv2.getBuildInformation() )

Somewhere at the bottom of the list you must see which python install is selected.
OpenCVbuild
As you can see, both python versions are available.
If you have the same output, most common cause are the locations of the install path.

@arielkantorovich
Copy link
Author

arielkantorovich commented Mar 27, 2022

When I use :

$ python
>> import cv2
>>> print( cv2.getBuildInformation())

I get:
`General configuration for OpenCV 4.5.1 =====================================
Version control: unknown

Extra modules:
Location (extra): /home/ubuntu/opencv_contrib/modules
Version control (extra): unknown

Platform:
Timestamp: 2022-03-27T07:10:03Z
Host: Linux 4.9.140-tegra aarch64
CMake: 3.10.2
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: RELEASE

CPU/HW features:
Baseline: NEON FP16
required: NEON

C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: /usr/bin/c++ (ver 7.5.0)
C++ flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--gc-sections -Wl,--as-needed
Linker flags (Debug): -Wl,--gc-sections -Wl,--as-needed
ccache: NO
Precompiled headers: NO
Extra dependencies: m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
3rdparty dependencies:

OpenCV modules:
To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: cnn_3dobj cvv java julia matlab ovis viz
Applications: perf_tests apps
Documentation: NO
Non-free algorithms: YES

GUI:
GTK+: YES (ver 3.22.30)
GThread : YES (ver 2.56.4)
GtkGlExt: NO
VTK support: NO

Media I/O:
ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
WEBP: /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020e)
PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: OpenJPEG (ver 2.3.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: YES (2.2.5)
FFMPEG: YES
avcodec: YES (57.107.100)
avformat: YES (57.83.100)
avutil: YES (55.78.100)
swscale: YES (4.8.100)
avresample: YES (3.7.0)
GStreamer: YES (1.14.5)
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: TBB (ver 2020.2 interface 11102)

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: YES (/usr/lib/aarch64-linux-gnu/liblapack.so /usr/lib/aarch64-linux-gnu/libcblas.so /usr/lib/aarch64-linux-gnu/libatlas.so)
Eigen: YES (ver 3.3.4)
Custom HAL: YES (carotene (ver 0.0.1))
Protobuf: build (3.5.1)

NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
NVIDIA GPU arch: 53
NVIDIA PTX archs:

cuDNN: YES (ver 8.0.0)

Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.17)
Libraries: /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
install path: lib/python2.7/dist-packages/cv2/python-2.7

Python 3:
Interpreter: /usr/bin/python3 (ver 3.6.9)
Libraries: /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
numpy: /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.19.4)
install path: lib/python3.6/dist-packages/cv2/python-3.6

Python (for build): /usr/bin/python2.7

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Install to: /usr

when I use:
$ python3
>>> import cv2
>>> print( cv2.getBuildInformation()

I get:`
General configuration for OpenCV 4.5.1 =====================================
Version control: 4.5.1-dirty

Platform:
Timestamp: 2021-01-02T12:47:39Z
Host: Linux 5.4.0-54-generic aarch64
CMake: 3.18.4
CMake generator: Unix Makefiles
CMake build tool: /bin/gmake
Configuration: Release

CPU/HW features:
Baseline: NEON FP16

C/C++:
Built as dynamic libs?: NO
C++ standard: 11
C++ Compiler: /opt/rh/devtoolset-9/root/usr/bin/c++ (ver 9.3.1)
C++ flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /opt/rh/devtoolset-9/root/usr/bin/cc
C flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed
Linker flags (Debug): -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed
ccache: YES
Precompiled headers: NO
Extra dependencies: ade Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libpng.so /lib64/libz.so dl m pthread rt
3rdparty dependencies: ittnotify libprotobuf libjpeg-turbo libwebp libtiff libopenjp2 IlmImf quirc tegra_hal

OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java python2 ts
Applications: -
Documentation: NO
Non-free algorithms: NO

GUI:
QT: YES (ver 5.15.0)
QT OpenGL support: NO
GTK+: NO
VTK support: NO

Media I/O:
ZLib: /lib64/libz.so (ver 1.2.7)
JPEG: libjpeg-turbo (ver 2.0.6-62)
WEBP: build (ver encoder: 0x020f)
PNG: /lib64/libpng.so (ver 1.5.13)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: build (ver 2.3.1)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.112.103)
avformat: YES (58.64.100)
avutil: YES (56.60.100)
swscale: YES (5.8.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: YES (carotene (ver 0.0.1))
Protobuf: build (3.5.1)

OpenCL: YES (no extra features)
Include path: /tmp/pip-req-build-zuuo394f/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 3:
Interpreter: /opt/python/cp36-cp36m/bin/python (ver 3.6.12)
Libraries: libpython3.6m.a (ver 3.6.12)
numpy: /tmp/pip-build-env-c03ewnak/overlay/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3)
install path: python

Python (for build): /bin/python2.7

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Install to: /tmp/pip-req-build-zuuo394f/_skbuild/linux-aarch64-3.6/cmake-install
-----------------------------------------------------------------`

@Qengineering
Copy link
Owner

Your original Python 3 OpenCV installation is overwritten by some other software package. That package uses also OpenCV and has the requirement in its wheel. You can see this in the last line, it is some pip-req-build, not something initiated by the script. Second, the locations are also different, as the OpenCV library itself (libpython3.6m.a)
Not sure what to do now. You should get rid of this OpenCV version.
First a reboot. See what's happening. After all you have several files located in the /tmp folder, which will be cleaned during reboot. You could also remove the files mentioned in the last part of the build info. If you have still the /opencv/build folder, rerun $ sudo make install.
If you know what is installed lately you could also tried this path. Otherwise, a fresh CD-image is one of the remaining options.

@arielkantorovich
Copy link
Author

I'm sorry but I did not quite understand your answer, I don't see the build folder.
Do I need to reinstall if yes what I need to do differently that this time is work with python3.
Thank you for your help.

@Qengineering
Copy link
Owner

First option.
If you don't have much software installed, consider starting all over again. In other words, flash a (new) SD card with the JetPack image, install it on your Jetson Nano and run the OpenCV script next. A clean operating system with a clean OpenCV.

Second option,
If you have already many software on your Jetson Nano, and you cannot easily start all over again, remove all files mentioned in the last part

Interpreter: /opt/python/cp36-cp36m/bin/python (ver 3.6.12)
Libraries: libpython3.6m.a (ver 3.6.12)
numpy: /tmp/pip-build-env-c03ewnak/overlay/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3)

Test is you can run OpenCV in python 3. If everything is removed, you should not. That's good.
Re-install the script. Hopefully you have a working python 3 version.

@arielkantorovich
Copy link
Author

I have too many software on my Nano so the first option its problem about the second option I try to remove the last part but I don't have this path on my Jetson for example if I use:
ls /opt
I don't see any folder with the name python

@arielkantorovich
Copy link
Author

maybe I can try to build again and specify the path for python3 in the .sh file?
I would happy if you can help me I am stuck and I don't know what can I do to solve this problem.
@Qengineering

@arielkantorovich
Copy link
Author

The weirdest thing I see, When I am in the directory /usr/lib/python3.6/dist-packages/cv2/python3.6 and I use cv2.getBuildInformation() I get everything fine

@Qengineering
Copy link
Owner

It has all to do with way Python loads its libraries. And in which order.
In your case you have two OpenCV installations at different locations. In Python 2 it selects the right one. Probably because there is only one version. In Python 3 initially it selected the wrong one. Except if your in the 'home' directory /usr/lib/python3.6/dist-packages/cv2/python3.6. The working directory is first scanned, and if something isn't found, it falls back to the other installation path.

You can run $ python3 -m sysconfig
You get all the information about the current python on your Jetson Nano.
At the top you see the 8 path python uses.

We must remove the link to the wrong OpenCV version.

$ python 3
>>> import cv2
>>> cv2.__file__

It gives you the OpenCV library python is currently using.
Try to find the 'wrong' OpenCV lib. I think its in your case libpython3.6m.a.
I would rename the file with some strange name like libmywrong3.6m.a' This way python will not recognize it and you can always undo your actions by restoring the original name. Probably you have to use sudo` to renaming.
See if once the wrong opencv isn't found, the correct on pops up.

@arielkantorovich
Copy link
Author

arielkantorovich commented Mar 28, 2022

Ok so I remove the wrong path but now I don't recognize OpenCV how I can make that is recognized OpenCV from the correct path? how can I link the correct path with OpenCV?

@Qengineering
Copy link
Owner

Try export PYTHONPATH="/usr/lib/python3.6/dist-packages/cv2/python3.6" before running python3.
If it works we can set this setting more permanently in ~/.bashrc

@arielkantorovich
Copy link
Author

arielkantorovich commented Mar 28, 2022

Unfortunately it does not work, still when I run python not from the path /usr/lib/python3.6/dist-packages/cv2/python3.6
I get this error:
>>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: libgdal.so.20: cannot open shared object file: No such file or directory
maybe I need to export the path such that pip3 recognizes this path?

@Qengineering
Copy link
Owner

There are still some residues left from your previous OpenCV installation.
If you know the 'wrong OpenCV' folder, see if you have a file called init.py. Rename that file so it will not be loaded.
Or $ sudo apt-get install libgdal-dev which will install libgdal on your system. This strategy may cause a chain of missing dependencies, you need to install.

@arielkantorovich
Copy link
Author

arielkantorovich commented Mar 28, 2022

Ok, I install libgdal-dev but still the same problem.
when I am in the correct path and I run the command: which pip3 I get the path /usr/bin/pip3 but when I am in another folder like home, I get /home/ubuntu/.local/bin/pip3 I think this is the problem.
how do I change the pip3 path? or maybe make intersection for the paths

@Qengineering
Copy link
Owner

Sorry, it becomes harder and harder to solve something like this on a distance.
At the bottom line, you have two installations running and you must remove all references to the wrong one first.
Somehow, somewhere there are some sticky links still active.

Most answers i found in this tutorial https://realpython.com/python-import/
See if it helps you.

@jonathanblade
Copy link

jonathanblade commented Jul 14, 2022

Hi everyone! I have the same problem that opencv 4.5.0 was proper installed only for python 2. How @Qengineering wrote, the reason of that is python not proper resolve order loads of library. In my case i fixed it like that:

sudo mv /usr/lib/python3/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so.backup
sudo ln -s /usr/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so

where:
/usr/lib/python3/dist-packages/cv2.cpython-36m-aarch64-linux-gnu.so - default preinstalled opencv
/usr/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so - manually installed opencv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants