When I started programming with python3 on Jetson TX2, I found that “import cv2” did not work. It appeared that the pre-installed OpenCV4Tegra (126.96.36.199) only provided bindings for python2.7. After some research, I found the recommended solution to this problem was to re-compile OpenCV.
I was kind of reluctant to remove OpenCV4Tegra and re-build OpenCV by myself. But anyway I finally decided to do it. I documented the procedure below.
- Complete installation of JetPack-3.1 on the target Jetson TX2.
Note that opencv_contrib modules (cnn/dnn stuffs) would cause problem on python caffe. After some experiment I decided not to include those modules at all.
### Remove OpenCV4Tegra $ sudo apt-get purge libopencv4tegra-python libopencv4tegra-dev libopencv4tegra $ sudo apt-get purge libopencv4tegra-repo ### Install dependencies based on the Jetson Installing OpenCV Guide $ sudo apt-get -y install build-essential make cmake cmake-curses-gui g++ libavformat-dev libavutil-dev libswscale-dev libv4l-dev libeigen3-dev libglew1.6-dev libgtk2.0-dev ### Install dependencies for gstreamer stuffs $ sudo apt-get install libdc1394-22-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev ### Install additional dependencies according to the pyimageresearch article $ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev $ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev gfortran ### Install dependencies for python3 $ sudo apt-get install python3-dev python3-pip $ sudo pip3 install numpy ### Download opencv-3.3.0 and opencv_contrib-3.3.0 source code $ mkdir -p ~/src $ cd ~/src $ wget https://github.com/opencv/opencv/archive/3.3.0.zip -O opencv-3.3.0.zip $ unzip opencv-3.3.0.zip ### Build opencv (if for TX1, set CUDA_ARCH_BIN="5.3" instead) $ cd opencv-3.3.0 $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_CUDA=ON -D CUDA_ARCH_BIN="6.2" -D CUDA_ARCH_PTX="" \ -D WITH_CUBLAS=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON \ -D ENABLE_NEON=ON -D WITH_LIBV4L=ON -D BUILD_TESTS=OFF \ -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF \ -D BUILD_opencv_python2=OFF .. $ make -j4 $ sudo make install
To verify the installation:
$ ls -l /usr/local/lib/pyhton3.5/dist-packages/cv2.* $ python3 >>> import numpy as np >>> import cv2 >>> cv2.__version__