• ubuntu 20.04 + Anaconda + cuda-11.8 + opencv-4.8.0(cuda)


    环境:一键编译opencv-4.8.0(cuda),前提是已经安装好了cuda和cudnn

    Anaconda安装

    参考:

    https://blog.csdn.net/weixin_46947765/article/details/130980957

    opencv4.8.0编译安装


    一键编译shell脚本

    1. VERSION=4.8.0
    2. test -e ${VERSION}.zip || wget https://github.com/opencv/opencv/archive/refs/tags/${VERSION}.zip
    3. test -e opencv-${VERSION} || unzip ${VERSION}.zip
    4. test -e opencv_extra_${VERSION}.zip || wget -O opencv_extra_${VERSION}.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/${VERSION}.zip
    5. test -e opencv_contrib-${VERSION} || unzip opencv_extra_${VERSION}.zip
    6. cd opencv-${VERSION}
    7. mkdir build
    8. cd build
    9. cmake -D CMAKE_BUILD_TYPE=RELEASE \
    10. -D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.8.0 \
    11. -D WITH_TBB=ON \
    12. -D ENABLE_FAST_MATH=1 \
    13. -D CUDA_FAST_MATH=1 \
    14. -D WITH_CUBLAS=1 \
    15. -D WITH_CUDA=ON \
    16. -D BUILD_opencv_cudacodec=ON \
    17. -D WITH_CUDNN=ON \
    18. -D OPENCV_DNN_CUDA=ON \
    19. -D WITH_QT=OFF \
    20. -D WITH_OPENGL=ON \
    21. -D BUILD_opencv_apps=OFF \
    22. -D BUILD_opencv_python2=OFF \
    23. -D OPENCV_GENERATE_PKGCONFIG=ON \
    24. -D OPENCV_PC_FILE_NAME=opencv.pc \
    25. -D OPENCV_ENABLE_NONFREE=ON \
    26. -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${VERSION}/modules \
    27. -D INSTALL_PYTHON_EXAMPLES=OFF \
    28. -D INSTALL_C_EXAMPLES=OFF \
    29. -D BUILD_EXAMPLES=OFF \
    30. -D CUDA_ARCH_BIN=7.5 \
    31. -D WITH_FFMPEG=ON \
    32. -D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
    33. -D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
    34. ..
    35. make -j8
    36. sudo make -j8 install

    环境设置:

    1. 打开主目录下的 .bashrc文件添加如下路径,例如我的.bashrc文件在/home/lu/下。
    2. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-4.8.0/lib/pkgconfig
    3. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv-4.8.0/lib
    4. 终端运行:source ~/.bashrc

    如果报错:

    1. # 报错:
    2. terminate called after throwing an instance of 'cv::Exception'
    3. what(): OpenCV(4.8.0) /media/lu/workspace/WorkSpace/visual_studio/YOLOv8-TensorRT-CPP/libs/tensorrt-cpp-api/scripts/opencv_contrib-4.8.0/modules/cudev/include/opencv2/cudev/grid/detail/transform.hpp:264: error: (-217:Gpu API call) no kernel image is available for execution on the device in function 'call'
    4. 已放弃 (核心已转储)
    5. # 或者
    6. error: (-216:No CUDA support) OpenCV was not built to work with the selected device. Please check CUDA_ARCH_PTX or CUDA_ARCH_BIN in your build configuration. in function 'initCUDABackend'

    可能是编译opencv的时候指定的算力和显卡算力不一致导致的,可参考:

    Please check CUDA_ARCH_PTX or CUDA_ARCH_BIN in your build configuration错误解决-CSDN博客


     

    查看算力可参考下面文章:

    Pytorch查看torch版本,查看torchvision版本,查看CUDA版本,查看cudnn版本,查看pytorch可用性,查看cuda可用性,查看cudnn可用性,查看显卡,指定运算GPU_C系语言的博客-CSDN博客
     

    如果cmake生成配置文件时下载第三方库失败,具体日志参考build目录下CMakeDownloadLog.txt文件,可以先手动下载再编译,可以使用build目录下的download_with_wget.sh进行下载,如果报错解析主机失败连接不上,可以替换下载连接,将下载脚本中的raw.githubusercontent.com全部替换为aw.gitmirror.com,再次下载。

    1. lu@host:/home/lu/project/opencv-4.8.0/build$ cat download_with_wget.sh
    2. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/ippicv/43219bdc7e3805adcbe3a1e2f1f3ef3b-ippicv_2021.8_lnx_intel64_20230330_general.tgz) && wget -O "/home/lu/project/opencv-4.8.0/.cache/ippicv/43219bdc7e3805adcbe3a1e2f1f3ef3b-ippicv_2021.8_lnx_intel64_20230330_general.tgz" "https://raw.gitmirror.com/opencv/opencv_3rdparty/1224f78da6684df04397ac0f40c961ed37f79ccb/ippicv/ippicv_2021.8_lnx_intel64_20230330_general.tgz"
    3. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/238e2b2d6f3c18d6c3a30de0c31e23cf-detect.caffemodel) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/238e2b2d6f3c18d6c3a30de0c31e23cf-detect.caffemodel" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.caffemodel"
    4. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/6fb4976b32695f9f5c6305c19f12537d-detect.prototxt) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/6fb4976b32695f9f5c6305c19f12537d-detect.prototxt" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/detect.prototxt"
    5. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/cbfcd60361a73beb8c583eea7e8e6664-sr.caffemodel) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/cbfcd60361a73beb8c583eea7e8e6664-sr.caffemodel" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.caffemodel"
    6. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/69db99927a70df953b471daaba03fbef-sr.prototxt) && wget -O "/home/lu/project/opencv-4.8.0/.cache/wechat_qrcode/69db99927a70df953b471daaba03fbef-sr.prototxt" "https://raw.gitmirror.com/WeChatCV/opencv_3rdparty/a8b69ccc738421293254aec5ddb38bd523503252/sr.prototxt"
    7. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ea90e7a8f3f7876d450e4149c97c74f-boostdesc_bgm.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i"
    8. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/232c966b13651bd0e46a1497b0852191-boostdesc_bgm_bi.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i"
    9. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/324426a24fa56ad9c5b8e3e0b3e5303e-boostdesc_bgm_hd.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i"
    10. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/202e1b3e9fec871b04da31f7f016679f-boostdesc_binboost_064.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i"
    11. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/98ea99d399965c03d555cef3ea502a0b-boostdesc_binboost_128.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i"
    12. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/e6dcfa9f647779eb1ce446a8d759b6ea-boostdesc_binboost_256.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i"
    13. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/boostdesc/0ae0675534aa318d9668f2a179c2a052-boostdesc_lbgm.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i"
    14. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/e8d0dcd54d1bcfdc29203d011a797179-vgg_generated_48.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i"
    15. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7126a5d9a8884ebca5aea5d63d677225-vgg_generated_64.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i"
    16. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/7cd47228edec52b6d82f46511af325c5-vgg_generated_80.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i"
    17. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/151805e03568c9f490a5e3a872777b75-vgg_generated_120.i) && wget -O "/home/lu/project/opencv-4.8.0/.cache/xfeatures2d/vgg/151805e03568c9f490a5e3a872777b75-vgg_generated_120.i" "https://raw.gitmirror.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i"
    18. mkdir -p $(dirname /home/lu/project/opencv-4.8.0/.cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-face_landmark_model.dat) && wget -O "/home/lu/project/opencv-4.8.0/.cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-face_landmark_model.dat" "https://raw.gitmirror.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat"
    19. lu@host:/home/lu/project/opencv-4.8.0/build$

    如果还是不行可以考虑修改hosts文件

    解决办法

    进入 IPAddress.com
    输入 raw.githubusercontent.com 查询真实的IP地址
    现在查到的是 185.199.108.133

    在最后一行添加以下内容,保存完事

    1. $ cat /etc/hosts
    2. 127.0.0.1 localhost
    3. 127.0.1.1 host
    4. # The following lines are desirable for IPv6 capable hosts
    5. ::1 ip6-localhost ip6-loopback
    6. fe00::0 ip6-localnet
    7. ff00::0 ip6-mcastprefix
    8. ff02::1 ip6-allnodes
    9. ff02::2 ip6-allrouters
    10. # new add line
    11. 185.199.108.133 raw.githubusercontent.com

  • 相关阅读:
    Node学习笔记之fs模块
    遍历用for还是foreach?
    面试算法15:字符串中的所有变位词
    2024年申报国自然项目基金撰写及技巧
    CSS3 动画
    java ssm企业图书借阅职工书屋系统
    4.4 x64dbg 绕过反调试保护机制
    一文详述流媒体传输网络MediaUni
    小球垂直跳动,C语言模拟重力加速度
    一键自动化博客发布工具,用过的人都说好(阿里云篇)
  • 原文地址:https://blog.csdn.net/lu_linux/article/details/134194311