2021-05-12 14:32:11
OpenCV教學之使用cmake生成MakeFile時下載檔案
在編譯OpenCV以及其附加模組時,有時會需要一些第三方的庫,如果本地沒有,會自動下載,下載地址一般為GitHub,結果當然就是卡死在那裡,根本無法下載,下面教大家如何解決這種問題。
問題重現
比如我在使用cmake設定的過程中出現以下情況:
Call Stack (most recent call first):
/home/ryan/build/opencv_contrib-master/modules/dnn/cmake/download_protobuf.cmake:23 (ocv_download)
/home/ryan/build/opencv_contrib-master/modules/dnn/cmake/download_protobuf.cmake:51 (ocv_protobuf_download)
/home/ryan/build/opencv_contrib-master/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:30 (include)
/home/ryan/build/opencv_contrib-master/modules/dnn/CMakeLists.txt:38 (include)
-- Downloading v3.1.0/protobuf-cpp-3.1.0.tar.gz...
解決思路
從上面的紀錄檔中可以得到以下資訊:
- 需要下載protobuf-cpp-3.1.0.tar.gz這個檔案
- 最先報錯的是download_protobuf.cmake這個檔案
一般情況下,最先報錯的那個檔案能獲得一些與錯誤相關的資訊,於是,我們開啟這個檔案,如下
set(PROTOBUF_CPP_NAME "libprotobuf")
set(PROTOBUF_CPP_DOWNLOAD_HASH "bd5e3eed635a8d32e2b99658633815ef")
set(PROTOBUF_CPP_PATH "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/protobuf/sources") # /protobuf-3.1.0 subdirectory
set(OPENCV_PROTOBUF_CPP_DOWNLOAD_URL ${OPENCV_PROTOBUF_URL};$ENV{OPENCV_PROTOBUF_URL};https://github.com/google/protobuf/releases/download/)
function(ocv_protobuf_download file ID)
if(DEFINED ${ID}_DOWNLOADED_HASH
AND ${ID}_DOWNLOADED_HASH STREQUAL ${ID}_DOWNLOAD_HASH
AND EXISTS ${${ID}_PATH})
# Files have been downloaded and checked by the previous CMake run
return()
else()
if(EXISTS ${${ID}_PATH})
message(STATUS "${${ID}_NAME}: Removing previous unpacked files: ${${ID}_PATH}")
file(REMOVE_RECURSE ${${ID}_PATH})
endif()
endif()
unset(${ID}_DOWNLOADED_HASH CACHE)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.download)
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/.download/.gitignore" "*n")
ocv_download(PACKAGE ${file}
HASH ${${ID}_DOWNLOAD_HASH}
URL ${OPENCV_${ID}_DOWNLOAD_URL}
DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/.download)
set(${ID}_ARCHIVE "${DOWNLOAD_PACKAGE_LOCATION}")
ocv_assert(EXISTS "${${ID}_ARCHIVE}")
ocv_assert(NOT EXISTS "${${ID}_PATH}")
file(MAKE_DIRECTORY ${${ID}_PATH})
ocv_assert(EXISTS "${${ID}_PATH}")
file(WRITE "${${ID}_PATH}/.gitignore" "*n")
message(STATUS "${${ID}_NAME}: Unpacking ${file} to ${${ID}_PATH}...")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xz "${${ID}_ARCHIVE}"
WORKING_DIRECTORY "${${ID}_PATH}"
RESULT_VARIABLE __result)
if(NOT __result EQUAL 0)
message(FATAL_ERROR "${${ID}_NAME}: Failed to unpack ${ID} archive from ${${ID}_ARCHIVE} to ${${ID}_PATH} with error ${__result}")
endif()
ocv_assert(EXISTS "${${ID}_PATH}")
set(${ID}_DOWNLOADED_HASH "${${ID}_DOWNLOAD_HASH}" CACHE INTERNAL "${ID} hash")
#message(STATUS "${${ID}_NAME}: Successfully downloaded")
endfunction()
ocv_protobuf_download(v3.1.0/protobuf-cpp-3.1.0.tar.gz PROTOBUF_CPP)
可以看出,他需要libprotobuf這個庫,並且需要下載到 ${CMAKE_CURRENT_BINARY_DIR}/3rdparty/protobuf/sources
目錄下,而下載地址就是 https://github.com/google/protobuf/releases/download/
,於是我們只要手動下載,然後把檔案放到這個目錄下即可。
OpenCV官方教學中文版(For Python) PDF http://www.linuxidc.com/Linux/2015-08/121400.htm
Ubuntu 14.04安裝OpenCV2.4.9 http://www.linuxidc.com/Linux/2016-07/132884.htm
Ubuntu 16.04上用CMake圖形介面交叉編譯樹莓派的OpenCV3.0 http://www.linuxidc.com/Linux/2016-10/135914.htm
Ubuntu 16.04中安裝OpenCV 2.4.11 http://www.linuxidc.com/Linux/2016-07/132882.htm
Ubuntu 16.04下Matlab2014a+Anaconda2+OpenCV3.1+Caffe安裝 http://www.linuxidc.com/Linux/2016-07/132860.htm
Ubuntu 16.04下TensorFlow+Caffe+OpenCV3.1+Theano部署 http://www.linuxidc.com/Linux/2017-01/139503.htm
Ubuntu 16.04 編譯安裝OpenCV 3.1及OpenCV多版本切換 http://www.linuxidc.com/Linux/2017-01/139325.htm
相關文章