Qengineering / Install-OpenCV-Jetson-Nano

OpenCV installation script with CUDA and cuDNN support
https://qengineering.eu/install-opencv-on-jetson-nano.html
BSD 3-Clause "New" or "Revised" License
137 stars 47 forks source link

Not able to get python3 as interpretor for Jetson Xavier. #10

Closed abhisheeekkk closed 1 year ago

abhisheeekkk commented 1 year ago

Hi there, I have downloaded the Jetpack 4.6.3, in my xavier NX, now when I try to run the script that you provided in your Article, OpenCV-4-6-0.sh After successful building and making by the script, I was not able to import python in python3, but was able to import it in python2.x

In the below logs you'll see that python3 is not in the interpreter but python2 is available. refer below image

Screenshot from 2023-01-24 18-02-52

Terminal Logs

General configuration for OpenCV 4.6.0 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/jetson/opencv_contrib/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2023-01-24T12:22:43Z
--     Host:                        Linux 4.9.299-tegra aarch64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make 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 -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-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 -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-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 -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-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 -Wl,--no-undefined  
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          m pthread cudart_static dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda-10.2/lib64 -L/usr/lib/aarch64-linux-gnu
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco barcode 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 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cvv java julia matlab ovis python3 viz
--     Applications:                perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI:                           GTK3
--     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:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        build (ver 42 - 4.2.0)
--     JPEG 2000:                   build (ver 2.4.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.19.1)
-- 
--   NVIDIA CUDA:                   YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             53
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.2.1)
-- 
--   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 (for build):            /usr/bin/python2.7
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr
-- -----------------------------------------------------------------

Can you please suggest what should I do?, I have followed all the steps as you mentioned. Thanks

Qengineering commented 1 year ago

@abhisheeekkk ,

That's strange. Somehow OpenCV couldn't find your Python3. When you run $ cmake -D CMAKE_BUILD_TYPE=RELEASE ...(etc) it ends with an overview like the one below. image Here you see the necessary Python3 folders. If the cmake command doesn't come with this information, it will not generate a Python3 version. Second, less common cause, the installation is done in the /usr/lib/python3/site-packages folder instead of the /usr/lib/python3/dist-packages. Hence the -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages option

abhisheeekkk commented 1 year ago

Hi @Qengineering,

I found the root cause of the issue, which was causing this strange behavior, So yesterday I wasn't focusing much on logs, because every line was showing SUCCESS, but today I thoroughly inspected the logs and found that, after these below exact lines in the Terminal,


-- ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default.
-- Detected processor: aarch64
-- Found PythonInterp: /usr/bin/python2.7 (found suitable version "2.7.17", minimum required is "2.7") 
-- Found PythonLibs: /usr/lib/aarch64-linux-gnu/libpython2.7.so (found suitable exact version "2.7.17") 
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.7.5", minimum required is "3.2") 
-- Found PythonLibs: /usr/lib/aarch64-linux-gnu/libpython3.7m.so (found suitable exact version "3.7.5") 

I was getting this error, but compilation didn't stop and moved so fast that it was easy to miss in a blink.

line 16, in <module>
from . import multiarray
ImportError: cannot import name multiarray

During handling of the above exception, another exception occurred:
........................

So, it was numpy all along, Even I was not able to uninstall and reinstall using pip3, [Error was - Not uninstalling numpy at /usr/lib/python3/dist-packages, outside environment /usr ] So I searched regarding this issue online and found some stackoverflow and askubuntu articles regarding this, particularly these one two helped me.

commands that I executed to solve the above error:


sudo apt remove python-numpy
sudo pip uninstall numpy --isolated

sudo -H python3.7 -m pip uninstall numpy
sudo apt purge python3-numpy
sudo -H python3.7 -m pip install --upgrade pip
sudo -H python3.7 -m pip install numpy

After verifying that numpy has been installed successfully, I tried to import it in the python3 script and it exported successfully, I cleaned the components which I installed using your script, like OpenCV, opencv_contrib folders, corrasponding zip files and shell file (OpenCV-4-6-0.sh)

Then I ran these commands according to your article and provided swap size.

# a fresh start, so check for updates
$ sudo apt-get update
$ sudo apt-get upgrade
# install nano
$ sudo apt-get install nano
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# enlarge the boundary (4.5.2 and higher)
$ sudo nano /sbin/dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot

after these commands I ran commands till here, because I had to make one change in the script because I am using Jetson Xavier not nano,


# check your memory first
$ free -m
# you need at least a total of 8.5 GB!
# if not, enlarge your swap space as explained in the guide
$ wget https://github.com/Qengineering/Install-OpenCV-Jetson-Nano/raw/main/OpenCV-4-6-0.sh
$ sudo chmod 755 ./OpenCV-4-6-0.sh

After running commands till here, I opened the script in the text editor and changed this line -D CUDA_ARCH_BIN=5.3 to -D CUDA_ARCH_BIN=7.2 Reference(https://developer.nvidia.com/cuda-gpus)

After the above change, I ran these commands according to your article:


$ ./OpenCV-4-6-0.sh
# once the installation is done...
$ rm OpenCV-4-6-0.sh
# remove the dphys-swapfile now
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# free the allocated memory
$ sudo rm /var/swap
# just a tip to save an additional 275 MB
$ sudo rm -rf ~/opencv
$ sudo rm -rf ~/opencv_contrib

To verify now I tried to import OpenCV for both python2.x.x. and python3.x.x and it was now not throwing errors.

success

Thank you @Qengineering, for your wonderful article and support!

Qengineering commented 1 year ago

@abhisheeekkk

Great!