Ubuntu20.04離線部署Opencv4.5.5

陌客丁發表於2024-05-28

​ 出於公司專案需要,需要在Springboot中呼叫opencv.so檔案,專案生產環境為ubuntu20.04,無法訪問外網,需要離線編譯安裝opencv,因此搭建本地環境下載好需要的依賴包,透過隨身碟傳輸,進行離線包安裝。本文前面部分介紹線上安裝如何安裝,離線部分操作步驟和線上基本一致,唯一的區別就是需要再下載依賴包時,修改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
    

相關文章