英伟达开发板是arm64,所以换源就要换成是支持arm64的国内源(不做详细介绍,但是挺重要)
1.默认你的jetson nano已经安装镜像并启动。打开控制终端,输入如下命令安装依赖库
sudo apt-get update sudo apt-get upgrade sudo apt-get install build-essential python3 python3-dev python3-pip python3-pandas python3-opencv python3-h5py libhdf5-serial-dev hdf5-tools nano ntp
2.安装opencv
- 构建OpenCV的第一步是在Jetson Nano上定义交换空间。
- Jetson Nano具有4GB RAM。这不足以从源代码构建OpenCV。因此,我们需要在Nano上定义交换空间以防止内存崩溃
-
pip3 install virtualenv python3 -m virtualenv -p python3 env echo "source env/bin/activate" >> ~/.bashrc source ~/.bashrc
# Turn off swap sudo swapoff /var/swapfile # Allocates 4G of additional swap space at /var/swapfile sudo fallocate -l 4G /var/swapfile # Permissions sudo chmod 600 /var/swapfile # Make swap space sudo mkswap /var/swapfile # Turn on swap sudo swapon /var/swapfile # Automount swap space on reboot sudo bash -c 'echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab' # Reboot sudo reboot
安装opencv依赖项,用aptitude来进行操作:
# Update sudo apt-get update sudo apt-get upgrade # Pre-requisites sudo aptitude install build-essential cmake unzip pkg-config sudo aptitude install libjpeg-dev libpng-dev libtiff-dev sudo aptitude install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo aptitude install libxvidcore-dev libx264-dev #下面这个会告诉你有冲突项,你第一次选择n,第二次之后选择y即可 sudo aptitude install libgtk-3-dev sudo aptitude install libatlas-base-dev gfortran sudo aptitude install python3-dev
下载opencv4.1源代码(可以自己下载好再上传上去linux中,也就是用xshell6的rz上传文件)
# Create a directory for opencv mkdir -p projects/cv2 cd projects/cv2 # Download sources wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip # Unzip unzip opencv.zip unzip opencv_contrib.zip # Rename mv opencv-4.1.0 opencv mv opencv_contrib-4.1.0 opencv_contrib
进入虚拟环境
source ~/env/bin/activate # Install Numpy pip install numpy
创建工作文件
# Create a build directory cd projects/cv2/opencv mkdir build cd build
以下代码都是在build路径中进行操作
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON # Contrib path -D OPENCV_EXTRA_MODULES_PATH=~/projects/cv2/opencv_contrib/modules # Your virtual environment's Python executable # You need to specify the result of echo $(which python) -D PYTHON_EXECUTABLE=~/env/bin/python -D BUILD_EXAMPLES=ON ..
上传缺少的文件(可能缺少):
https://files.cnblogs.com/files/ikic/boostdesc_bgm.i,vgg_generated_48.i%E7%AD%89.rar
上面下载好之后,将里面所有的文件放去 opencv_contrib/modules/xfeatures2d/src/
cd projects/cv2/opencv_contrib/modules/xfeatures2d/src/
然后用xshell的rz上传里面所有文件
有的话会说上传错误,一个一个上传。
上传成功之后回到build的工作目录中
cd projects/cv2/opencv/build
make -j2
在编译过程中,会遇到一些问题:
引用最爱铅笔字的博客(当时没有截屏):
这次跑到73%的时候又出现错误了fatal error: features2d/test/
: 没有那个文件或目录
#include "features2d/test/test_detectors_regression.impl.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
遇到这种问题,
是头文件有点问题,把下面文件夹里的两个文件拷贝到opencv_contrib/modules/xfeatures2d/test
在打开这个文件,修改下头文件。
复制粘贴之后就修改报错的那个文件
将下面这句话改成
#include "features2d/test/test_detectors_regression.impl.hpp"
然后以此类推,哪里报错就修改那里的文件的头include,哪个文件缺失就从opencv那移过来
这些错误修改之后就运行(可重复运行)
make -j2
直到后面全部运行成功
在build中安装opencv
# Install OpenCV sudo make install sudo ldconfig
将构建的OpenCV库链接到虚拟环境virtualenv
# Go to the folder where OpenCV's native library is built cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6 ls # Rename(xxx是可替代的,看ls之后的结果来进行下一步修改) mv cv2.cpython-36m-xxx-linux-gnu.so cv2.so # Go to your virtual environments site-packages folder cd ~/env/lib/python3.6/site-packages/ # Symlink the native library ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so
这样就完成了