參考文章:How to Install OpenCV (3.4.0) on Jetson TX2html
與參考文章大部分都是類似的,若是不習慣看英文,能夠看看我下面的描述python
在咱們使用python3進行編程時,import cv2不起做用。因爲預先安裝的opencv是與python2.7綁定的,若是想在python3環境下使用,咱們須要從新編譯opencvlinux
已經在Jetson TX2上安裝了JetPack-3.3(或JetPack-3.2.1或JetPack-3.1)git
首先清理舊的opencv包,並構建opencv所需的依賴項github
1. 刪除全部由JetPack(或OpenCV4Tegra)安裝的舊opencv內容編程
sudo apt-get purge libopencv*
2.我更喜歡使用新版本的numpy(與pip一塊兒安裝),因此我也要刪除這個python-numpy apt包vim
sudo apt-get purge python-numpy
3. 刪除其餘未使用的apt包windows
sudo apt autoremove
4. 將全部已安裝的apt軟件包升級到最新版本(可選作)瀏覽器
sudo apt-get update sudo apt-get dist-upgrade
5. 將gcc apt包更新到最新版本(強烈推薦)網絡
sudo apt-get install --only-upgrade g++-5 cpp-5 gcc-5
6. 基於jetson安裝opencv依賴項
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安裝依賴項
sudo apt-get install libdc1394-22-dev libxine2-dev \ libgstreamer1.0-dev \ libgstreamer-plugins-base1.0-dev
8. 安裝其餘依賴項
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. 安裝QT5的依賴項
sudo apt-get install qt5-default
10. 安裝python3的依賴項
sudo apt-get install python3-dev python3-pip python3-tk sudo pip3 install numpy sudo pip3 install matplotlib
修改matplotlibrc的41行,'backend : TkAgg'
sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc
vim技巧 : 按Esc,set nu(顯示行號)
11. 安裝python2的依賴項
sudo apt-get install python-dev python-pip python-tk sudo pip2 install numpy sudo pip2 install matplotlib
修改matplotlibrc的41行,'backend : TkAgg'
sudo vim /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc
在下載和編譯opencv-3.4.0以前,須要先進行一些修改。具體點說是修改 /usr/local/cuda/include/cuda_gl_interop.h 並修復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
下面是cuda_gl_interop.h的62行到68行,修改後以下:
//#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
接下來,下載opencv-3.4.0源代碼,cmake並編譯。注意,opencv_contrib模塊(cnn/dnn之類的東西)會在pycaffe上引發問題,因此通過一些實驗以後,我決定徹底不包含這些模塊。
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
若是下載opencv源碼較慢,能夠在windows的瀏覽器中輸入網址 https://github.com/opencv/opencv/archive/3.4.0.zip 手動下載源碼包,以後將源碼包放入TX2
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
在Jetson TX2上正確安裝了opencv-3.4.0以後,咱們可使用python腳本捕獲和顯示來自Jetson板載攝像機、USB網絡攝像頭或IP攝像頭的實時視頻。跟隨這篇文章:在Jetson TX2上捕獲、顯示攝像頭視頻。