赞
踩
最近,打算在Companion Computer上一些目标识别的算法,最流行、最基础的可能就是OpenCV了。
首先,将算法在x86的laptop上跑起来,将目标识别程序运行起来,基本的功能实现。
然后,基于Linux系统移植到Companion Computer上就会简单很多。
准备以下验证环境:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
$ tree -L 1
.
├── opencv
├── opencv_contrib
├── opencv-4.9.0-py3
├── opencv-4.9.0.zip
└── opencv_contrib-4.9.0.zip
目前,最新的OpenCV发行版本: 4.9.0
### Define OpenCV Version to install
cvVersion="4.9.0"
### Save current working directory
SNAPDRAGONFLY_CURRENT_DIR_PATH=$(pwd)
SNAPDRAGONFLY_CURRENT_OPENCV=opencv
SNAPDRAGONFLY_CURRENT_OPENCV_CONTRIB=opencv_contrib
SNAPDRAGONFLY_CURRENT_OPENCV_ZIP=opencv-$cvVersion.zip
SNAPDRAGONFLY_CURRENT_OPENCV_CONTRIB_ZIP=opencv_contrib-$cvVersion.zip
除了必备的组件外,更新了一些常用的工具,比如:tree, aptitude 。
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu jammy-security main" -y sudo apt -y update sudo apt -y upgrade ### Install dependencies sudo apt -y install unzip locate tree aptitude sudo apt -y install build-essential checkinstall cmake pkg-config yasm sudo apt -y install git gfortran sudo apt -y install libjpeg8-dev libpng-dev libjpeg-dev libgtk-3-dev sudo apt -y install software-properties-common sudo apt -y install libtiff-dev sudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-dev sudo apt -y install libxine2-dev sudo apt -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev sudo apt -y install libgtk2.0-dev libtbb-dev libtbb2 sudo apt -y install openexr libopenexr-dev python3-numpy ### qt5-default dependencies sudo apt -y install libatlas-base-dev sudo apt -y install libfaac-dev libmp3lame-dev libtheora-dev sudo apt -y install libvorbis-dev libxvidcore-dev sudo apt -y install libopencore-amrnb-dev libopencore-amrwb-dev libavro-dev sudo apt -y install x264 libx264-dev v4l-utils sudo apt -y install qtcreator qtbase5-dev qt5-qmake cmake ### Optional dependencies sudo apt -y install libprotobuf-dev protobuf-compiler sudo apt -y install libgoogle-glog-dev libgflags-dev sudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen ### Special dependencies sudo apt -y install libv4l-dev cd /usr/include/linux sudo ln -s -f ../libv4l1-videodev.h videodev.h
注:本次系统采用的Ubuntu 22.04.3 LTS,所以使用了jammy-security源,请大家根据实际情况调整。
使用Python虚拟环境,方便切换(笔者这里也有一些本地环境,为了避免相互干扰,虚拟Python环境会更好些)。
sudo apt -y install python3-dev python3-pip sudo apt -y install python3-testresources sudo apt -y install python3.10-venv ### create virtual environment python3 -m venv opencv-"$cvVersion"-py3 echo "# Virtual Environment Wrapper" >> ~/.bashrc echo "alias workoncv-$cvVersion=\"source $SNAPDRAGONFLY_CURRENT_DIR_PATH/opencv-$cvVersion-py3/bin/activate\"" >> ~/.bashrc ### enter virtual environment source "$SNAPDRAGONFLY_CURRENT_DIR_PATH"/opencv-"$cvVersion"-py3/bin/activate ### install python libraries within this virtual environment pip install -r requirements.txt ### quit virtual environment deactivate
这里的命令含有部分脚本变量。如果大家copy下来,实际上是可以直接脚本运行的。
但是,国内环境复杂多变,如果顺序执行的脚本搞不定,还请一行一行手动执行。
注:后续有时间,会整理一些基本安装脚本,方便大家学习使用。
# https://github.com/opencv/opencv.git wget https://github.com/opencv/opencv/archive/refs/tags/$cvVersion.zip mv $cvVersion.zip $SNAPDRAGONFLY_CURRENT_OPENCV_ZIP unzip $SNAPDRAGONFLY_CURRENT_OPENCV_ZIP mv opencv-$cvVersion opencv # https://github.com/opencv/opencv_contrib.git wget https://github.com/opencv/opencv_contrib/archive/refs/tags/$cvVersion.zip mv $cvVersion.zip $SNAPDRAGONFLY_CURRENT_OPENCV_CONTRIB_ZIP unzip $SNAPDRAGONFLY_CURRENT_OPENCV_CONTRIB_ZIP mv opencv_contrib-$cvVersion opencv_contrib ### Clean build directories rm -rf opencv/build rm -rf opencv_contrib/build ### Build opencv cd opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D WITH_TBB=ON \ -D BUILD_TBB=OFF \ -D WITH_V4L=ON \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=$SNAPDRAGONFLY_CURRENT_DIR_PATH/opencv-$cvVersion-py3/lib/python3.10/site-packages \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D PYTHON_EXECUTABLE=../../opencv-$cvVersion-py3/bin/python3 \ -D BUILD_EXAMPLES=ON .. make -j$(nproc) make install
$ cp opencv/build/lib/python3/cv2.cpython-310-x86_64-linux-gnu.so opencv-4.9.0-py3/lib/python3.10/site-packages/cv2/cv2.so
大致看到下面输出,系统里面应该基本就安装好了。
$ pkg-config --modversion opencv4
4.9.0
$ workoncv-4.9.0
(opencv-4.9.0-py3) daniel@daniel-ThinkPad-SL410:~/OpenCV$ ipython
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.21.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import cv2
In [2]: print(cv2.__version__)
4.9.0
In [3]: quit
(opencv-4.9.0-py3) daniel@daniel-ThinkPad-SL410:~/OpenCV$ deactivate
笔者整理这份资料是搞了好几天的,遇到不少坑。
主要就是折腾在OpenCV开发环境不熟悉,网络复杂多变等问题上。希望整理出来,方便交流学习!
注:感兴趣的朋友可以关注:SnapLearnOpenCV 和ubuntu22.04@laptop OpenCV Get Started。
补充:3.3章需要可能需要代理,具体情况请根据实际网络进行配置。
$ git config --global https.proxy http://192.168.1.13:808
$ export https_proxy=http://192.168.1.13:808
【1】ubuntu22.04@laptop 常用基础环境安装
【2】ubuntu22.04@laptop安装&配置VNC服务端
【3】ArduPilot开源代码之CompanionComputer上天计划
之前发现python3环境下,cv2输出4.5.4,没有理解。后面发现该4.5.4是ubuntu deb安装包的版本。
$ python3 -c "import cv2; print(cv2.__version__)"
4.5.4
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。