Ubuntu20.04离线部署Opencv4.5.5

​ 出于公司项目需要,需要在Springboot中调用opencv.so文件,项目生产环境为ubuntu20.04,无法访问外网,需要离线编译安装opencv,因此搭建本地环境下载好需要的依赖包,通过U盘传输,进行离线包安装。本文前面部分介绍在线安装如何安装,离线部分操作步骤和在线基本一致,唯一的区别就是需要再下载依赖包时,修改apt源文件,使用本地源下载,后面会针对离线源的打包和替换部分进行介绍。

环境准备

  1. Java环境

    sudo apt-get install openjdk-8-jdk
    
  2. Ant环境

    sudo apt-get install ant
    
  3. 编译环境和组件

    sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev ffmpeg libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudi
    

编译安装OpenCV

  1. 下载源码opencv-4.5.5.zip ,地址:https://github.com/opencv/opencv/archive/4.5.5.zip

  2. 上传opencv-4.5.5.zip至服务器目录下,本文以/home/test为例

  3. 解压压缩包,创建build目录

    unzip opencv-4.5.5.zip 
    cd opencv-4.5.5
    rm -rf build
    mkdir build
    cd build
    
  4. 编译

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_FFMPEG=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D WITH_GSTREAMER=ON ..
    
    make -j$(nproc)
    
  5. 安装

    sudo make install
    

    接下来就可以在/usr/local中查看包的安装情况,进入/usr/local/share/java/opencv4,里面有安装生成的libopencv_java455.so

可能存在的问题

ippicv_2020_lnx_intel64_20191018_general.tgz下载不了

下载ippicv_2020_lnx_intel64_20191018_general.tgz

https://github.com/opencv/opencv_3rdparty/blob/ippicv/master_20191018/ippicv/ippicv_2020_lnx_intel64_20191018_general.tgz

上传至/home/test目录下

修改ippicv.cmake

 set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
  ocv_download(FILENAME ${OPENCV_ICV_NAME}
               HASH ${OPENCV_ICV_HASH}
               URL
                 "${OPENCV_IPPICV_URL}"
                 "$ENV{OPENCV_IPPICV_URL}"
                 "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
               DESTINATION_DIR "${THE_ROOT}"
               ID IPPICV
               STATUS res
               UNPACK RELATIVE_URL)

改为

 set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
  ocv_download(FILENAME ${OPENCV_ICV_NAME}
               HASH ${OPENCV_ICV_HASH}
               URL
                 "${OPENCV_IPPICV_URL}"
                 "$ENV{OPENCV_IPPICV_URL}"
                 "ippicv_2020_lnx_intel64_20191018_general.tgz:///home/test/ippicv_2020_lnx_intel64_20191018_general/"
               DESTINATION_DIR "${THE_ROOT}"
               ID IPPICV
               STATUS res
               UNPACK RELATIVE_URL)

ade-v0.1.1f.zip下载不了

下载

https://github.com/opencv/ade/archive/v0.1.1f.zip

上传至/home/test

修改/home/opencv-4.5.5/modules/gapi/cmake/DownloadADE.cmake

ocv_download(FILENAME ${ade_filename}
             HASH ${ade_md5}
             URL
               "${OPENCV_ADE_URL}"
               "$ENV{OPENCV_ADE_URL}"
               "https://github.com/opencv/ade/archive/"
             DESTINATION_DIR ${ade_src_dir}
             ID ADE
             STATUS res
             UNPACK RELATIVE_URL)

改为

ocv_download(FILENAME ${ade_filename}
             HASH ${ade_md5}
             URL
               "${OPENCV_ADE_URL}"
               "$ENV{OPENCV_ADE_URL}"
               "file:///home/test/ade-"
             DESTINATION_DIR ${ade_src_dir}
             ID ADE
             STATUS res
             UNPACK RELATIVE_URL)

离线安装

和在线安装的主要区别在于需要把用到的依赖包提前下好放到本地目录下,可以分为以下几个步骤,测试机环境打包,内网机器上传解压包后修改source.list。

  1. 测试机打包

    cd /var/cache/apt/archives/
    dpkg-scanpackages . | gzip -9c > Packages.gz
    
    tar -zcf packages.tar.gz ./*
    
  2. 内网机器上修改

    # 创建离线文件存放目录
    mkdir -p /offline/packages
    
    # 解压离线文件
    tar -zxvf packages.tar.gz -C /offline/packages/
    
    # 备份源文件
    mv /etc/apt/sources.list /etc/apt/sources.list.bak
    
    # 映射本地源,这里可以使用vi
    cat >>/etc/apt/sources.list <<EOF
    deb [trusted=yes] file:///offline/packages/ ./
    EOF
    
    # 更新
    apt-get update
    

热门相关:林深暗香来   亮剑之龙帅归来   我的绝色美女房客   超能狂兵   这个赘婿有点强