目录
安装PyQt5
安装OpenCV
编译报错:nvcc fatal : Unsupported gpu architecture 'compute_87
导入报错: Illegal instruction (core dumped)
方法一
方法二
注意目前似乎只支持Python3.6!!!
sudo apt install pyqt5* -y
sudo apt-get install python3-pyqt5 -y
pip install pyqt5-sip
sudo ln -s /usr/lib/python3/dist-packages/PyQt5/ /home/sxf/archiconda3/envs/py36/lib/python3.6/site-packages
sudo ln -s /usr/lib/python3/dist-packages/sip* /home/sxf/archiconda3/envs/py36/lib/python3.6/site-packages
可以先尝试这个两个方法,如果不能用,再往下看编译方法:
sudo apt-get install python3-opencv
pip install opencv-python
可参考:OpenCV with CUDA for Jetson Nano | NVIDIA Developer # 检查你的总内存(RAM + swap),以便快速构建。至少需要: # OpenCV 4.8.0 -> 8.5 GB! # OpenCV 4.7.0 -> 8.5 GB! # OpenCV 4.6.0 -> 8.5 GB! # OpenCV 4.5.5 -> 8.5 GB! # OpenCV 4.5.4 -> 8.5 GB! # OpenCV 4.5.3 -> 8.5 GB! # OpenCV 4.5.2 -> 8.5 GB! # OpenCV 4.5.1 -> 6.5 GB # OpenCV 4.5.0 -> 6.5 GB
由于编译需要较多的内存,因此推荐设置至少6G的swap,当然编译安装完可以再取消这么高的swap。
新建6G的swap空间:
sudo fallocate -l 6G /swapfile
ls -lh /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
或者,调整现有的swap空间:
# 如果你已经有一个交换文件,你需要先禁用它:
sudo swapoff -a
# 选择一个合适的交换文件大小,并创建一个新的交换文件:
sudo fallocate -l 6G /swapfile
# 如果 fallocate 不可用,你可以使用 dd 命令:
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
# 确保交换文件的权限正确,以防止其他用户读取或写入该文件:
sudo chmod 600 /swapfile
# 使用 mkswap 命令将文件设置为交换空间:
sudo mkswap /swapfile
# 启用新的交换文件:
sudo swapon /swapfile
# 再次检查交换空间配置以确保新的交换文件已启用:
free -h
# 将交换文件添加到 /etc/fstab 以便在系统启动时自动启用:
sudo bash -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'
自动化编译安装OpenCV:
# pip方法装的会报错
# pip install opencv-python
wget https://raw.githubusercontent.com/mdegans/nano_build_opencv/master/build_opencv.sh
sudo chmod build_opencv.sh
./build_opencv.sh
# 如果需要特定版本的opencv:
# ./build_opencv.sh 4.4.0
注意,这个sh脚本会去下载OpenCV的仓库,国内网络可以考虑替换为以下地址: git clone --depth 1 --branch "$1" https://gitclone.com/github.com/opencv/opencv.git git clone --depth 1 --branch "$1" https://gitclone.com/github.com/opencv/opencv_contrib.git
推荐还是上个魔法,因为过程中可能还会自动去github下载其他仓库。
然后就是漫长等待:
最后安装python绑定:
cd /tmp/build_opencv/opencv/build/python_loader
python setup.py install
# 或者
pip3 install .
CUDA问题,不知道怎么修复。不过反正我并不需要cuda版本的OpenCV,我只要能装上OpenCV就行,所以我修改了前面的build_opencv.sh脚本,把make时cuda相关的配置都OFF了。更改后的:
#!/usr/bin/env bash
# 2019 Michael de Gans
set -e
# change default constants here:
readonly PREFIX=/usr/local # install prefix, (can be ~/.local for a user install)
readonly DEFAULT_VERSION=4.4.0 # controls the default version (gets reset by the first argument)
readonly CPUS=$(nproc) # controls the number of jobs
# better board detection. if it has 6 or more cpus, it probably has a ton of ram too
if [[ $CPUS -gt 5 ]]; then
# something with a ton of ram
JOBS=$CPUS
else
JOBS=2 # you can set this to 4 if you have a swap file
# otherwise a Nano will choke towards the end of the build
fi
cleanup () {
# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script
while true ; do
echo "Do you wish to remove temporary build files in /tmp/build_opencv ? "
if ! [[ "$1" -eq "--test-warning" ]] ; then
echo "(Doing so may make running tests on the build later impossible)"
fi
read -p "Y/N " yn
case ${yn} in
[Yy]* ) rm -rf /tmp/build_opencv ; break;;
[Nn]* ) exit ;;
* ) echo "Please answer yes or no." ;;
esac
done
}
setup () {
cd /tmp
if [[ -d "build_opencv" ]] ; then
echo "It appears an existing build exists in /tmp/build_opencv"
cleanup
fi
mkdir build_opencv
cd build_opencv
}
git_source () {
echo "Getting version '$1' of OpenCV"
git clone --depth 1 --branch "$1" https://gitclone.com/github.com/opencv/opencv.git
git clone --depth 1 --branch "$1" https://gitclone.com/github.com/opencv/opencv_contrib.git
}
install_dependencies () {
# open-cv has a lot of dependencies, but most can be found in the default
# package repository or should already be installed (eg. CUDA).
echo "Installing build dependencies."
sudo apt-get update
sudo apt-get dist-upgrade -y --autoremove
sudo apt-get install -y \
build-essential \
cmake \
git \
gfortran \
libatlas-base-dev \
libavcodec-dev \
libavformat-dev \
libavresample-dev \
libcanberra-gtk3-module \
libdc1394-22-dev \
libeigen3-dev \
libglew-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libgstreamer1.0-dev \
libgtk-3-dev \
libjpeg-dev \
libjpeg8-dev \
libjpeg-turbo8-dev \
liblapack-dev \
liblapacke-dev \
libopenblas-dev \
libpng-dev \
libpostproc-dev \
libswscale-dev \
libtbb-dev \
libtbb2 \
libtesseract-dev \
libtiff-dev \
libv4l-dev \
libxine2-dev \
libxvidcore-dev \
libx264-dev \
pkg-config \
python-dev \
python-numpy \
python3-dev \
python3-numpy \
python3-matplotlib \
qv4l2 \
v4l-utils \
zlib1g-dev
}
configure () {
local CMAKEFLAGS="
-D BUILD_EXAMPLES=OFF
-D BUILD_opencv_python2=OFF
-D BUILD_opencv_python3=ON
-D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=${PREFIX}
-D CUDA_ARCH_BIN=5.3,6.2,7.2,8.7
-D CUDA_ARCH_PTX=
-D CUDA_FAST_MATH=ON
-D CUDNN_VERSION='10.2'
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3
-D ENABLE_NEON=ON
-D OPENCV_DNN_CUDA=OFF
-D OPENCV_ENABLE_NONFREE=ON
-D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules
-D OPENCV_GENERATE_PKGCONFIG=ON
-D WITH_CUBLAS=OFF
-D WITH_CUDA=OFF
-D WITH_CUDNN=OFF
-D WITH_GSTREAMER=ON
-D WITH_LIBV4L=ON
-D WITH_OPENGL=ON"
if [[ "$1" != "test" ]] ; then
CMAKEFLAGS="
${CMAKEFLAGS}
-D BUILD_PERF_TESTS=OFF
-D BUILD_TESTS=OFF"
fi
echo "cmake flags: ${CMAKEFLAGS}"
cd opencv
mkdir build
cd build
cmake ${CMAKEFLAGS} .. 2>&1 | tee -a configure.log
}
main () {
local VER=${DEFAULT_VERSION}
# parse arguments
if [[ "$#" -gt 0 ]] ; then
VER="$1" # override the version
fi
if [[ "$#" -gt 1 ]] && [[ "$2" == "test" ]] ; then
DO_TEST=1
fi
# prepare for the build:
setup
install_dependencies
git_source ${VER}
if [[ ${DO_TEST} ]] ; then
configure test
else
configure
fi
# start the build
make -j${JOBS} 2>&1 | tee -a build.log
if [[ ${DO_TEST} ]] ; then
make test 2>&1 | tee -a test.log
fi
# avoid a sudo make install (and root owned files in ~) if $PREFIX is writable
if [[ -w ${PREFIX} ]] ; then
make install 2>&1 | tee -a install.log
else
sudo make install 2>&1 | tee -a install.log
fi
cleanup --test-warning
}
main "$@"
降低numpy版本:
pip install opencv-python==4.5.3.56
pip install numpy==1.19.4
就在启动python之前导出OPENBLAS_CORETYPE = ARMV8(或任何实际的硬件)应该可以解决这个问题:
export OPENBLAS_CORETYPE=ARMV8