你好,游客 登录 注册 搜索
背景:
阅读新闻

OpenCV教程之使用cmake生成MakeFile时下载文件

[日期:2017-02-28] 来源:Linux社区  作者:wr132 [字体: ]

在编译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...

解决思路

从上面的日志中可以得到以下信息:

  1. 需要下载protobuf-cpp-3.1.0.tar.gz这个文件
  2. 最先报错的是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

[翻译]Ubuntu 14.04, 13.10 下安装 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2014-12/110045.htm

Ubuntu 安装 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2016-12/138293.htm

OpenCV的详细介绍请点这里
OpenCV的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-02/141158.htm

linux
相关资讯       makefile  CMake 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款