Installation OpenCV (3.4.0) in the Jetson TX2

Reference article: How the I Built TensorFlow 1.8.0 ON Jetson TX2

Most of the reference articles are similar, if not used to look at the English, I can see description below

 

When we use python3 programming, import cv2 does not work. As the pre-installed opencv with python2.7 bound, if you want to use in python3 environment, we need to recompile opencv

 

prerequisites

Already installed JetPack-3.3 (or JetPack-3.2.1 or JetPack-3.1) in the Jetson TX2

 

installation steps

First, clean up the old opencv package, and build the dependencies required opencv

1. Remove all contents from the old opencv JetPack (or OpenCV4Tegra) installed

sudo apt-get purge libopencv*

2. I prefer to use the new version of numpy (installed with pip), so I have to delete the python-numpy apt package

sudo apt-get purge python-numpy

3. Delete unused other apt package

sudo apt autoremove

4. all installed packages apt upgrade to the latest version ( optional do )

sudo apt-get update
sudo apt-get dist-upgrade

5. gcc apt package to the latest version (highly recommended)

sudo apt-get install --only-upgrade g++-5 cpp-5 gcc-5

6. Installation opencv dependency based jetson

sudo apt-get install build-essential make cmake cmake-curses-gui \
                       g++ libavformat-dev libavutil-dev \
                       libswscale-dev libv4l-dev libeigen3-dev \
                       libglew-dev libgtk2.0-dev

7. gstreamer installation dependencies

sudo apt-get install libdc1394-22-dev libxine2-dev \
                       libgstreamer1.0-dev \
                       libgstreamer-plugins-base1.0-dev

8. Install the other dependencies

sudo apt-get install libjpeg8-dev libjpeg-turbo8-dev libtiff5-dev \
                       libjasper-dev libpng12-dev libavcodec-dev
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev \
                       libatlas-base-dev gfortran
sudo apt-get install libopenblas-dev liblapack-dev liblapacke-dev

9. The installation of dependencies QT5

sudo apt-get install qt5-default

10. Installation of dependencies python3

sudo apt-get install python3-dev python3-pip python3-tk
sudo pip3 install numpy
sudo pip3 install matplotlib

Matplotlibrc modify the line 41, 'backend: TkAgg'

sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc

vim tips: Press Esc, set nu (display line numbers)

11. Installation of dependencies python2

sudo apt-get install python-dev python-pip python-tk
sudo pip2 install numpy
sudo pip2 install matplotlib

Matplotlibrc modify the line 41, 'backend: TkAgg'

sudo vim /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc

Before downloading and compiling opencv-3.4.0, you need to make some changes. Specific point that is modified   /usr/local/cuda/include/cuda_gl_interop.h and fix the symbolic links libGL.so

sudo vim /usr/local/cuda/include/cuda_gl_interop.h
cd /usr/lib/aarch64-linux-gnu/
sudo ln -sf tegra/libGL.so libGL.so

The following is cuda_gl_interop.h line 62 to the line 68, modified as follows:

//#if defined(__arm__) || defined(__aarch64__)
//#ifndef GL_VERSION
//#error Please include the appropriate gl headers before including cuda_gl_interop.h
//#endif
//#else
 #include <GL/gl.h>
//#endif

 

Next, download opencv-3.4.0 source code, cmake and compile. Note, opencv_contrib module (cnn / dnn things like that) can cause problems on pycaffe, so after some experimentation, I decided to completely do not include these modules.

mkdir -p ~/src
cd ~/src
wget https://github.com/opencv/opencv/archive/3.4.0.zip \
       -O opencv-3.4.0.zip
unzip opencv-3.4.0.zip

2. Build opencv (CUDA_ARCH_BIN="6.2" for TX2, or "5.3" for TX1)
cd ~/src/opencv-3.4.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 WITH_QT=ON -D WITH_OPENGL=ON ..
make -j4
sudo make install

If the download opencv source code is slow, you can enter the URL in the browser windows  https://github.com/opencv/opencv/archive/3.4.0.zip  manually download the source package, then the source package into TX2

 

Verifying Successful Installation

ls /usr/local/lib/python3.5/dist-packages/cv2.*
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-aarch64-linux-gnu.so
ls /usr/local/lib/python2.7/dist-packages/cv2.*
/use/local/lib/python2.7/dist-packages/cv2.so
python3 -c 'import cv2; print(cv2.__version__)'
3.4.0
python2 -c 'import cv2; print(cv2.__version__)'
3.4.0

 

After the Jetson TX2 correctly opencv-3.4.0 installed, we can use the python script captures and displays real-time video USB webcam or IP camera from Jetson on-board cameras. Follow this article: how to capture and display video camera on the Jetson TX2 with Python.

 

Guess you like

Origin www.cnblogs.com/gezhuangzhuang/p/11621777.html