ubuntu20.04 版本下安装Openpose (包括Opencv,caffe,cuda,cudnn)的超详细笔记--更新于-4-14

时间:2024-02-17 11:57:23

在Ubuntu18.04 安装Openpose总是不成功。开源的东西就是这样,没有准头,版本之间的依赖关系非常复杂。于是重新安装了最新的Ubuntu20.04版本。现在已经安装好了,将中间所花的时间,所踩的坑,尽量记录下来,整理成文字,希望看到这篇博文的朋友能少走一些弯路。

本人也是在Openpose搭建过程中花费了大量的时间,反复都没能成功后,多次搭建,将成功的经验写出来,本博文持续更新,相信持续的更新,能让本帖真正解决openpose环境搭建的问题。

如有同行觉得有那些无法执行成功,可以在评论区提出,我们会验证并贴出实验结果。

 

主要参考的资料链接

https://qengineering.eu/install-caffe-on-ubuntu-20.04-with-opencv-4.4.html

这里主要包括对上面英文资料的翻译

Caffe是2013年开发的,当时OpenCV是2.4.13版本,python也只有python2。当前Ubuntu20.04默认python3.8。其他各种组件都在不断的更新,所以这里介绍Ubuntu20.04版本安装Caffe。

 

  1. 安装CUDA

    1. 删除以前的cuda

  1. sudo /usr/local/cuda-10.1/bin/cuda-uninstaller

  2. sudo apt --purge remove nvidia* #彻底卸载N卡驱动

 

在安装Cuda之前,必须选择Openpose已经测试过的版本组合:可以直接查询这个网址

https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/installation/1_prerequisites.md#ubuntu-prerequisites

Note: OpenPose has been tested extensively with CUDA 11.1.1 (cuDNN 8.0.5) for Ubuntu 20. Older OpenPose versions (v1.6.X and v1.5.X) were tested with CUDA 10.1 (cuDNN 7.5.1) for Ubuntu 18 and CUDA 8.0 (cuDNN 5.1)

推荐直接使用 ./scripts/ubuntu/install_cuda.sh 安装

所以,为了安装openpose,显卡如果不支持这两个版本,不知道换硬件可不可以。

 

 

参考:https://blog.csdn.net/qq_42341984/article/details/110326344

首先通过nvidia-smi命令显示当前nvidia的情况:

edward@Server-adiAi:~/software/openpose$ nvidia-smi

Sun Jan 10 09:47:09 2021

+-----------------------------------------------------------------------------+

| NVIDIA-SMI 455.45.01 Driver Version: 455.45.01 CUDA Version: 11.1 |

|-------------------------------+----------------------+----------------------+

| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |

| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |

| | | MIG M. |

|===============================+======================+======================|

| 0 GeForce RTX 206... Off | 00000000:01:00.0 On | N/A |

| 41% 19C P8 10W / 175W | 230MiB / 7979MiB | 4% Default |

| | | N/A |

+-------------------------------+----------------------+----------------------+

如果没有安装,则查询当前设备后安装驱动

 

sudo ubuntu-drivers devices
sudo apt install nvidia-driver-435

 

驱动安装之后,需要重新启动电脑。

 

为什么采用CUDA也是非常无奈的事情,需要根据所购买的卡来下载合适的cuda版本,可以到 https://developer.nvidia.com/cuda-gpus 这个地址去查。

这里严重声明一下:这个地址,如果是在中国下载,或对方检测到了是中国的IP地址,那么,下载会自动跳转,然后下载一个32B的无意义的东西,而且还不给半点提示,这个花费了我们大量的时间。明显就只针对中国人的一种歧视。不是正常人可以干出来的事情。几乎整个AI都被英伟达给绑架了,美国人早点暴露,或许我们会有更好的GPU。退一步讲,既然我们已经购买了英伟达的显卡,为何还对中国实行*就非常不道义。*是美国*的事情,既然已经把显卡买到了中国,不开放cuda的行为就像是收了钱还不给货,或者收了钱还把人家正常购买的东西变成废品。这完全不是一个负责人的公司干的事情。

解决办法就是到网上去寻找已经下载了的cuda安装程序。我也是在网上下载的程序,这是我使用的资源:

 

差一些的显卡是安装不了cuda的,需要性能大于3.0。

 

  1. 安装cudnn

 

sudo dpkg -i libcudnn8_8.0.3.33-1+cuda11.0_amd64.deb

sudo dpkg -i libcudnn8-dev_8.0.3.33-1+cuda11.0_amd64.deb

sudo dpkg -i libcudnn8-samples_8.0.3.33-1+cuda11.0_amd64.deb

检查cudnn的版本信息输入如下命令:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

如果出现如下信息,说明cuDNN已正确识别。

#define CUDNN_MAJOR 7

#define CUDNN_MINOR 6

#define CUDNN_PATCHLEVEL 5

--

#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

 

  1. cudnn8里面cudnn.h已经没有CUDNN_MAJOR这个信息。编译时,caffe.make 会检查CUDNN_VERSION,将#define CUDNN_VERSION 8005 加入到/usr/include/cudnn.h 所有头文件的前面

  2. 用cmake编译的时候,CUDA_VERSION 是根据 /usr/bin/nvcc --version 来寻找cuda版本的

 

  1. 测试cuDNN

官方说法:To verify that cuDNN is installed and is running properly, compile the mnistCUDNN sample located in the /usr/src/cudnn_samples_v8 directory in the debian file.

 

0. Copy the cuDNN sample to a writable path.

cp -r /usr/src/cudnn_samples_v8/ $HOME

Go to the writable path.

cd ~/cudnn_samples_v8/mnistCUDNN

这个最好是进去再make,不然在文件外面操作可能有权限的问题。

 

2. Compile the mnistCUDNN sample.

编译文件。

sudo make clean

sudo make

3. Run the mnistCUDNN sample.

运行样例程序。

sudo ./mnistCUDNN

4. If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:

如果成功运行,会显示下列信息:

Test passed!

---经过测试,这一步需要能够通过。

 

  1. 安装Caffe(直接在Openpose内的caffe编译,然后让opencv/openpose用这个目录下的caffe)

    1. 首先安装依赖:

$ sudo apt-get install cmake git unzip

$ sudo apt-get install libprotobuf-dev libleveldb-dev liblmdb-dev

$ sudo apt-get install libsnappy-dev libhdf5-serial-dev protobuf-compiler

$ sudo apt-get install --no-install-recommends libboost-all-dev

$ sudo apt-get install libatlas-base-dev libopenblas-dev

$ sudo apt-get install the python3-dev python3-skimage

$ sudo pip3 install pydot

$ sudo apt-get install graphviz

 

  1. 下载caffe

git clone https://github.com/BVLC/caffe.git

这里必须使用git

clone 之后,需要回退caffe的版本,这一步非常重要。新版本会有很多无法处理的问题,都不知道如何收集这些问题。详细的信息可以参考:

https://blog.csdn.net/weixin_34826139/article/details/112222956

git下载caffe

sudo git clone https://github.com/BVLC/caffe.git

更新版本

git checkout f019d0d

因为是需要编译openpose,所以将caffe拷贝到~/libs/openpose/3rdparty/caffe目录下。

 

Notes 01:注意,caffe的编译要根据Makefile.config来,不要采用cmake-gui来,因为cmake-gui是用来生成Make文件的,而caffe本身自带的就可以满足。直接在caffe的根目录下执行 sudo make && sudo make test && sudo runtest

Notes 02: f019d0d版本的caffe和下载的caffe model是兼容的,和最新的master的版本是不兼容的,所以不推荐用最新的版本。

 

  1. 在caffe目录下有2个文件:Makefile.config.example,在这个基础上进行修改

cd /pathto/caffe

mv Makefile.config.example Makefile.config

sudo vim Makefile.config

修改处:

#USE_CUDNN := 1

USE_CUDNN := 0 #不使用CUDNN

#OPENCV_VERSION := 3

OPENCV_VERSION := 3

#WITH_PYTHON_LAYER := 1

WITH_PYTHON_LAYER := 1 #使用python接口

 

#INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include

#LIBRARY_DIRS:= $(PYTHON_LIB) /usr/local/lib /usr/lib

#下面的修改需要根据实际情况修改

INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include /usr/include /usr/include/hdf5/serial /usr/local/include/opencv4

LIBRARY_DIRS:= $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/lib/opencv4/3rdparty

 

/usr/lib/x86_64-linux-gnu/hdf5/serial 部分x86_64-linux-gnu 需要通过

find / -name hdf5 来找到具体的位置, 在opencv4的环境下应该就/usr/local/lib/opencv4/3rdparty 这个位置。

 

  1. 修改Makefile

sudo Makefile

有些文章说需要修改NVCCFLAGS 加上 -D_FORCE_INLINES ,在Ubunt20.04环境下不能做这个修改,否则会出错。

#NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC$(COMMON_FLAGS)

NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

 

#LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

#LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5

 

在Makefile中,需要删除 D_FORCE_INLINES

NVCCFLAGS += D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

 

无法发现hdf5 应找到 hdf5的目录,修改

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/lib/opencv4/3rdparty

 

  1. 编译:

sudo make clean

sudo make

sudo make test

sudo make runtest

 

编译成功后,lib文件在./build/lib 下,include文件在

 

  1. find_packages 的知识

Module模式:搜索CMAKE_MODULE_PATH指定路径下的FindXXX.cmake文件,执行该文件从而找到XXX库。其中,具体查找库并给XXX_INCLUDE_DIRS和XXX_LIBRARIES两个变量赋值的操作由FindXXX.cmake模块完成(先搜索当前项目里面的Module文件夹里面提供的FindXXX.cmake,然后再搜索系统路径/usr/local/share/cmake-x.y/Modules/FindXXX.cmake)

 

  1. 如下是出现的问题及如何修改:

    1. Could NOT find NumPy

Could NOT find NumPy (missing: NUMPY_INCLUDE_DIR NUMPY_VERSION) (Required is at least version "1.7.1")

在caffe的FindNumPy.cmake ,2000多行,还没法正确获得一个地址....

通过Opencv的cmake获得了Numpy 的地址为:

/usr/local/lib/python3.8/dist-packages/numpy/core/include (ver 1.20.2)

FindNumpy错误的使用了python2.7的执行文件,我们在该文件中添加一行: "set(PYTHON_EXECUTABLE /usr/bin/python3.8)" in FindNumPy.make,then we can get the Numpy.

Found NumPy: /usr/lib/python3/dist-packages/numpy/core/include (found suitable version "1.17.4", minimum required is "1.7.1")

NumPy ver. 1.17.4 found (include: /usr/lib/python3/dist-packages/numpy/core/include)

 

  1. Could NOT find caffe/common.hpp

fatal error: caffe/common.hpp: No such file or directory

#include "caffe/common.hpp"

添加打印,发现caffe的include是:caffe_INCLUDE_DIRS PUBLIC;/usr/include;PUBLIC;/usr/include;PUBLIC;/usr/include;PUBLIC;/usr/include;PUBLIC;/usr/include/hdf5/serial;PUBLIC;/usr/include;PUBLIC;/usr/include;PRIVATE;/usr/include;PUBLIC;/usr/local/cuda/include;PUBLIC;/usr/local/include/opencv4;PUBLIC;/usr/include/x86_64-linux-gnu;/usr/include/x86_64-linux-gnu; PRIVATE;/usr/include/python3.8;/usr/lib/python3/dist-packages/numpy/core/include;PUBLIC;/usr/include

所以需要将这个文件的根目录:/home/edward/git/libs/openpose2021/3rdparty/caffe/include/

直接修改CMakeLists.txt

set(Caffe_INCLUDE_DIRS ${Caffe_INCLUDE_DIRS};${PROJECT_SOURCE_DIR}/include)

--看起来是有效的,需要generate

 

  1. cudnn的问题(去除cudnn)

1)home/edward/git/libs/openpose2021/3rdparty/caffe/src/caffe/layers/cudnn_conv_layer.cpp:136:7: error: ‘CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT’ was not declared in this scope

CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT,

suggested alternative: ‘CUDNN_CONVOLUTION_FWD_ALGO_DIRECT’

这个修改都按照最新cudnn的API来作适当的修改。

2)/home/edward/git/libs/openpose2021/3rdparty/caffe/src/caffe/layers/cudnn_conv_layer.cpp:131:17: error: there are no arguments to ‘cudnnGetConvolutionForwardAlgorithm’ that depend on a template parameter, so a declaration of ‘cudnnGetConvolutionForwardAlgorithm’ must be available [-fpermissive]

3)在Opencv3 cv::CV_LOAD_IMAGE_COLOR 已经找不到了,都用cv::IMREAD_COLOR替换

This means you need to use cv::CV_LOAD_IMAGE_COLOR when using OpenCv 3.0 instead of cv::IMREAD_COLOR

CV_LOAD_IMAGE_GRAYSCALE to cv::IMREAD_GRAYSCALE

The documentation for OpenCV 3.0 can be found here: http://docs.opencv.org/3.0.0/d4/da8/group__imgcodecs.html

The current enum responsible for imread is:

enum cv::ImreadModes {

cv::IMREAD_UNCHANGED = -1,

cv::IMREAD_GRAYSCALE = 0,

cv::IMREAD_COLOR = 1,

cv::IMREAD_ANYDEPTH = 2,

cv::IMREAD_ANYCOLOR = 4,

cv::IMREAD_LOAD_GDAL = 8

}

4)向_v7转变主要涉及到conv_layer,deconv_layer等几个文件,都在layers目录下。

.

其他的一些问题:

7.1、编译得好好的之后(删除掉了use_Cudnn)莫名其妙出现

edward@Server-adiAi:~/software/openpose/3rdparty/caffe$ sudo make clean

Package opencv was not found in the pkg-config search path.

Perhaps you should add the directory containing `opencv.pc\'

to the PKG_CONFIG_PATH environment variable

No package \'opencv\' found

 

7.2执行如下语句时出现:pkg-config --cflags opencv

出现找不到opencv的错误

解决方法

cd /usr/local/lib

sudo mkdir pkgconfig

cd pkgconfig

sudo touch opencv.pc

---在文件中加入

prefix=/usr/local

exec_prefix=${prefix}

includedir=${prefix}/include

libdir=${exec_prefix}/lib

 

Name: opencv

Description: The opencv library

Version:4.0.1

Cflags: -I${includedir}/opencv4

Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

 

7.3 设定PKG_CONFIG_PATH

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

可以写入到/etc/profile.d/cuda.sh,在Ubuntu系统中,/etc/profile.d/下面的sh文件都会在开机时启动。所以创建cuda.sh,在这个sh里面配置系统变量。

vim /etc/profile.d/cuda.sh

chmod a+x /etc/profile.d/cuda.sh

文件中添加:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

检测结果可以执行:pkg-config --cflags --libs opencv

 

7.4. 编译Openpose的错误

fatal error: caffe/proto/caffe.pb.h: No such file or directory

解决:

打开终端,进入caffe所在路径

执行以下指令

mkdir include/caffe/proto

protoc ./src/caffe/proto/caffe.proto --cpp_out=.

mv ./src/caffe/proto/caffe.pb.h ./include/caffe/proto/

 

  1. 附录:caffe中Makefile.config详解

https://blog.csdn.net/jiaxinhong/article/details/82826391

#提前下好model减少配置时间sudo -- 可以直接通过 ./getModles.sh来操作

- [BODY_25 model](http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/body_25/pose_iter_584000.caffemodel): download in `models/pose/body_25/`.

- [COCO model](http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/coco/pose_iter_440000.caffemodel): download in `models/pose/coco/`.

- [MPI model](http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/mpi/pose_iter_160000.caffemodel): download in `models/pose/mpi/`.

- [Face model](http://posefs1.perception.cs.cmu.edu/OpenPose/models/face/pose_iter_116000.caffemodel): download in `models/face/`.

- [Hands model](http://posefs1.perception.cs.cmu.edu/OpenPose/models/hand/pose_iter_102000.caffemodel): download in `models/hand/`.

将下好的model和caffe分别放入/openpose/models和/3rdparty/caffe

2、下载Models

cd models

./getModels.sh

cd ..

3、使用cmake-gui编译安装

使用cmake-gui进行配置检查opencv、cuda路径(配置步骤如下)

 

这里因为已经将caffe编译好了,所以可以直接编译openpose。去掉BUILD_CAFFE选项。BUID_PYTHON选项需要在仔细观察以下。

出现:

 

cmake-gui .. 出现

CMake Error at python/openpose/CMakeLists.txt:6 (pybind11_add_module): Unknown CMake command "pybind11_add_module".异常

解决办法:在3rdparty目录下执行:

git clone https://github.com/pybind/pybind11.git

 

在openpose目录下创建build文件夹:

sudo mkdir build

cd build

sudo /pathto/cmake-gui ..

sudo make

sudo make install

 

 

  1. OpenCV的安装

    1. 安装OpenCV的依赖:

#启动

$ sudo apt-get update

$ sudo apt-get upgrade

# 安装依赖

$ sudo apt-get install build-essential cmake git unzip pkg-config

$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*

$ sudo apt-get install python3-dev python3-numpy python3-pip

$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev

$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev

$ sudo apt-get install libv4l-dev v4l-utils

$ sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

$ sudo apt-get install libavresample-dev libvorbis-dev libxine2-dev

$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev

$ sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev

$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev

$ sudo apt-get install liblapack-dev libeigen3-dev gfortran

$ sudo apt-get install libhdf5-dev protobuf-compiler

$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev

# 创建 symlink to videodev.h

$ cd /usr/include/linux

$ sudo ln -s -f ../libv4l1-videodev.h videodev.h

$ cd ~

 

  1. 下载OpenCV

需要下载两个:opencv.zip 和 opencv_contrib.zip。也可到 https://opencv.org/releases/下载最新版本

git下载,并指定版本:

sudo git clone https://github.com/opencv/opencv.git

sudo git clone https://github.com/opencv/opencv_contrib.git

cd opencv

sudo git checkout -f 4.4.0

cd ../opencv_contrib

sudo git checkout -f 4.4.0

===

$ cd ~

$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.4.0.zip

$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.4.0.zip

$ unzip opencv.zip

$ unzip opencv_contrib.zip

$ mv opencv-4.4.0 opencv

$ mv opencv_contrib-4.4.0 opencv_contrib

$ cd opencv

$ mkdir build

$ cd build

$ sudo cmake-gui ..

 

  1. 这里推荐采用cmake-gui进行安装。

首先安装cmake-gui

然后再opencv下创建一个build, 进入build目录后,执行 /pathTo/cmake-gui ..

点击configure,然后再图形上进行配置,完成配置后,点generate。

在使用cmake-gui的过程中,最下面的框是运行的结果,这里面展示的所有红色部分都需要解决。但上面的配置窗口上也会被标记为红色,这些红色条目仅仅提示我们那些配置可以调整,所以不要被上面框内的红色吓到了。

然后Make OpenCV

在opencv目录下

sudo make

sudo make install

 

安装是否完成,可以通过进入到python3, 输入 Import cv2

如果没有异常就标识Opencv 安装成功。

 

  1. 配置OpenCV动态库环境/(程序执行时加载动态库*.so的路径)

新编译出来的opencv4.pc 会安装在/usr/local/lib/pkgconfig/opencv.pc

OpenCV的库 /usr/local/opencv4/lib

pkg-config --modversion opencv4 这个命令会在/usr/bin/pkgconfig/下先搜索

然后再在/usr/local/bin/pkgconfig下搜索。如果出现版本不对的情况,把第一个目录下的删除掉即可。

 

 

sudo vim /etc/bash.bashrc

 

 

 

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export PKG_CONFIG_PATH

 

验证是否配置完成:

 

 

pkg-config --libs opencv4

 

-L/usr/local/opencv4/lib -lopencv_world

 

  1. 错误及解决办法

    1. No OpenGL support

出现如下错误:

error: (-218:No OpenGL support) Library was built without OpenGL support in function ‘cvNamedWindow’

解决办法:在cmake-gui上的确勾选了,但依旧要在CMakeList.txt中修改

打开CMakeList.txt文件,搜索WITH_OPENGL 然后修改(WITH_OPENGL “Include OpenGL support” OFF 为(WITH_OPENGL “Include OpenGL support” ON

 

原来是机器里装了gtk3,所以

-D WITH_GTK_2_X=ON

 

opencv默认安装是不支持opengl的,机器里恰巧又没有QT,所以没有去检查OpenGL项目。

看来是gtk3与gtkglext不兼容,为了支持opengl,只能使用gtk2了。在cmake的时候,再增加一个选项

HAVE_GTKGLEXT==FALSE

 

就OK了。

如果采用cmake-gui , 只需要将WITH_GTK_2_X上勾选就可以。

 

 

 

  1. 测试Opencv

在~/git/libs/opencv2021/opencv/samples/cpp/example_cmake

内分别执行

cmake .

make

./opencv_example

就会显示Hello world.

 

 

OPENCV_EXTRA_MODULES_PATH 设置为 /pathto/opencv_contrib/modules

我的是/home/edward/git/opencv/opencv_contrib/modules

如果出现无法下载xfeatures2d的情况,就多按configure按钮,一般就能完成下载。

CUDA_TOOLKIT_ROOT_DIR , 需要指定为cuda的位置 /usr/local/cuda

然后执行

sudo make时可能出现 nvcc fatal : unsupported gpu architecture \'compute_30\' 错误

在cmake-gui界面,设置 CUDA_GENERATION=Kepler,本人环境是设置为auto,Turing

 

4.1)不同NVIDIA显卡对应的SM架构(CUDA arch and CUDA gencode)

Turing (CUDA 10及以上版本) SM75 or SM_75, compute_75 – GTX Turing – GTX 1660 Ti,RTX 2060, RTX 2070, RTX 2080,Titan RTX,Quadro RTX 4000, Quadro RTX 5000, Quadro RTX 6000, Quadro RTX 8000

 

5)在之前安装OK,后面安装其他库的是否出现如下异常:

nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified

CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:220 (message):

Error generating

/home/edward/git/libs/opencv2021/opencv/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o

--不知道是什么异常....网上也找不到解决的办法...网上说是某个cmake文件中 - 给写成了中文的-。

 

解决办法:(在编译其他文件的时候,可能会用新版本的gcc,所以需要切换一下)

切换到gcc5

/usr/bin/gcc-5

/usr/bin/g++-5

export CC=/usr/bin/gcc-5

export CXX=/usr/bin/g++-5

cmake /path/to/your/project

 

5.1) home/edward/git/libs/opencv2021/opencv/modules/core/src/cuda/gpu_mat.cu:43:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory

#include "opencv2/opencv_modules.hpp"

解决办法:把/usr/local/include/opencv4/opencv2内的内容拷贝到/usr/local/include/opencv2内。这样就可以找到了。

 

5.2 /usr/local/include/opencv2/core/cvdef.h:73:10: fatal error: cvconfig.h: No such file or directory #include "cvconfig.h"

将#include "cvconfig.h" 改为 "opencv2/cvconfig.h"

5.3 usr/local/include/opencv2/core/cv_cpu_dispatch.h:7:10: fatal error: cv_cpu_config.h: No such file or directory

这里cv_cpu_config.h是在build目录下的一个文件,不知如何处理。

this might not work. you\'re supposed to build the apps along with the opencv libs from src, by enabling

BUILD_opencv_apps=ON in cmake

修改后也还是出现这个问题

 

7) 5.3这个问题无法被解决,... 终究还是cuda的问题。opencv不用显卡编译,去掉use_cuda,use_cudnn这两个选项。(不就是一个显卡厂商吗,拽什么拽?尽量不用怎样?)

 

6)在出现不支持compute_20等错误的是否,很有可能是这个CUDA_NVCC_FLAGS参数需要修改,可以直接在makefile中删除compute_20等

set(CUDA_NVCC_FLAGS -gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-Xcudafe;--diag_suppress=cc_clobber_ignored;-Xcudafe;--diag_suppress=integer_sign_change;-Xcudafe;--diag_suppress=useless_using_declaration;-Xcudafe;--diag_suppress=set_but_not_used;-DUSE_CUDNN;-Xcompiler;-fPIC ;; )

 

将opencv-4.5.0改成opencv, opencv_contrib-4.5.0 改成opencv_contrib 这样方便后续的操作。 这里的build目录是建立在opencv子目录下的。

在 /path/to/opencv/build/unix-install 的目录下,有一个opencv4.pc的文件,这个文件就是在配置pkg_config时所用的.pc文件。可以将这个文件放在系统目录/usr/lib/pkgconfig目录下,就可以使用meson来配置opencv 的环境了。

 

"用pkg-config --modversion opencv命令查看opencv版本"

 

  1. 如果没有采用cmake-gui .. 编译,则采用如下OpenCV cmake的指令:红色部分需要做改动

这个是直接在opencv目录下执行。

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=/usr/local \

-D OPENCV_EXTRA_MODULES_PATH=/home/edward/software/opencv/opencv_contrib/modules \

-D BUILD_TIFF=ON \

-D WITH_FFMPEG=ON \

-D WITH_GSTREAMER=ON \

-D WITH_TBB=ON \

-D BUILD_TBB=ON \

-D WITH_EIGEN=ON \

-D WITH_V4L=ON \

-D WITH_LIBV4L=ON \

-D WITH_VTK=OFF \

-D WITH_QT=OFF \

-D WITH_OPENGL=ON \

-D OPENCV_ENABLE_NONFREE=ON \

-D INSTALL_C_EXAMPLES=OFF \

-D INSTALL_PYTHON_EXAMPLES=OFF \

-D BUILD_NEW_PYTHON_SUPPORT=ON \

-D OPENCV_GENERATE_PKGCONFIG=ON \

-D BUILD_TESTS=OFF \

-D OPENCV_DNN_CUDA=ON \

-D ENABLE_FAST_MATH=ON \

-D CUDA_FAST_MATH=ON \

-D CUDA_ARCH_BIN=8.0 \

-D WITH_CUBLAS=ON \

-D WITH_CUDNN=ON \

-D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.5 \

-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \

-D BUILD_EXAMPLES=OFF ..

没有cuda可以用下面的cmake

$ cmake -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=/usr/local \

-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

-D BUILD_TIFF=ON \

-D WITH_FFMPEG=ON \

-D WITH_GSTREAMER=ON \

-D WITH_TBB=ON \

-D BUILD_TBB=ON \

-D WITH_EIGEN=OFF \

-D WITH_V4L=ON \

-D WITH_LIBV4L=ON \

-D WITH_VTK=OFF \

-D WITH_QT=OFF \

-D WITH_OPENGL=ON \

-D OPENCV_ENABLE_NONFREE=ON \

-D INSTALL_C_EXAMPLES=OFF \

-D INSTALL_PYTHON_EXAMPLES=OFF \

-D BUILD_NEW_PYTHON_SUPPORT=ON \

-D OPENCV_GENERATE_PKGCONFIG=ON \

-D BUILD_TESTS=OFF \

-D BUILD_EXAMPLES=OFF ..

 

  1. 编译安装Openpose

推荐参考:https://blog.csdn.net/ookamiww/article/details/105795786

及:https://www.it610.com/article/1279878419298926592.htm

 

CMakeList.txt 有1千多行,真没办法一个一个来看... CMakeList.txt为什么要写那么长???

 

  1. 下载版本

git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose.git

下载openpose对应的caffe并且拷入openpose中的caffe目录下

 

#安装caffe依赖

sudo apt-get install --no-install-recommends libboost-all-dev

sudo apt-get install libgflags-dev libgoogle-glog-dev

sudo apt-get install libprotobuf-dev protobuf-compiler

sudo apt-get install libhdf5-serial-dev libleveldb-dev libsnappy-dev libatlas-base-dev

#下载caffe源码 https://github.com/BVLC/caffe

 

cd build

sudo cmake-gui ..

配置好caffe,opencv,然后执行:

sudo make

sudo make install

  1. 错误及解决办法

    1. a target must be either all-keyword or all-plain.

CMake Error at src/openpose/calibration/CMakeLists.txt:18 (target_link_libraries):

The keyword signature for target_link_libraries has already been used with

the target "openpose_calibration". All uses of target_link_libraries with

a target must be either all-keyword or all-plain.

类似的错误可以通过在

pathto/openpose/src/openpose/calibration/CMakeLists.txt:18

如下语句添加:PUBLIC

target_link_libraries(openpose_calibration PUBLIC openpose_core)

 
  1. .cu找不到头文件???

edward@Server-adiAi:~/git/libs/openpose2021/build$ sudo make

[ 1%] Building NVCC (Device) object src/openpose/CMakeFiles/openpose.dir/tracking/openpose_generated_pyramidalLK.cu.o

/home/edward/git/libs/openpose2021/src/openpose/tracking/pyramidalLK.cu:1:10: fatal error: openpose_private/tracking/pyramidalLK.hpp: No such file or directory

#include <openpose_private/tracking/pyramidalLK.hpp>

通过在CmakeList.txt下添加:

get_property(dirs DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)

message(">>> , include_dirs=${dirs}")

打印出:

>>> include_dirs=/home/edward/git/libs/openpose2021/include;/usr/include;/usr/include;/usr/include;/usr/local/include/opencv4;/home/edward/git/libs/openpose2021/3rdparty/caffe/include/caffe;/home/edward/git/libs/openpose2021/PUBLIC;/usr/local/cuda/include;/home/edward/git/libs/openpose2021/PUBLIC;/usr/include;/usr/local/cuda/include

目录中:/home/edward/git/libs/openpose2021/include/openpose_private/tracking/pyramidalLK.hpp

所以怎么会找不到这个文件呢?一时也不知道如何修改,所以直接修改代码,用绝对路径:

#include </home/edward/software/openpose/include/openpose_private/tracking/pyramidalLK.hpp>

其他类似问题也依靠这个来解决。---有没有大牛可以帮忙解答一下?

几乎所有的都和.cu文件相关。--

 

  1. 找不到头文件caffe/blob.hpp:

home/edward/git/libs/openpose2021/src/openpose/core/arrayCpuGpu.cpp:3:14: fatal error: caffe/blob.hpp:

这个是在 Caffe_INCLUDE_DIRS 配置上需要修改:

/home/edward/git/libs/openpose2021/3rdparty/caffe/include

 

  1. 找不到头文件caffe/proto/caffe.pb.h:

In file included from /home/edward/git/libs/openpose2021/src/openpose/core/arrayCpuGpu.cpp:3:

/home/edward/git/libs/openpose2021/3rdparty/caffe/include/caffe/blob.hpp:9:10: fatal error: caffe/proto/caffe.pb.h: No such file or directory

#include "caffe/proto/caffe.pb.h"

这个问题和以前的问题还不一样,这些都是build的时候出现的头文件。这个文件在:

/home/edward/git/libs/openpose2021/3rdparty/caffe/build/include/caffe/proto/caffe.pb.h

也先直接用绝对路径来解决。

也可以在cmake-gui 中, Caffe_INCLUDE_DIRS 配置为

/home/edward/git/libs/openpose2021/3rdparty/caffe/include;/home/edward/git/libs/openpose2021/3rdparty/caffe/build/include

 

  1. 连接错误undefined reference to symbol \'cudaFree@@libcudart.so.11.0\'

这个是在:

[ 97%] Building CXX object examples/tests/CMakeFiles/resizeTest.bin.dir/resizeTest.cpp.o

也就是一个测试例子,看看能不能删除这个例子。

/usr/bin/ld: CMakeFiles/resizeTest.bin.dir/resizeTest.cpp.o: undefined reference to symbol \'cudaFree@@libcudart.so.11.0\'

/usr/bin/ld: /usr/local/cuda/lib64/libcudart.so.11.0: error adding symbols: DSO missing from command line

collect2: error: ld returned 1 exit status

cudaFree symbol is defined in libcudart.so. It should be located in /usr/local/cuda/lib with a default install.(DSO is a dynamic shared object or a shared library)

解决办法:直接把这个例子全部注释掉,幸好只有一个例子受到影响

 

  1. 执行文件时出现No OpenGL support:

./build/examples/openpose/openpose.bin --video examples/media/video.avi

当cmake和make编译均通过之后,执行文件时出现该错误

OpenCV Error: No OpenGL support (Library was built without OpenGL support) in CvWindow,

file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window_QT.cpp, line 1642

terminate called after throwing an instance of \'cv::Exception\'

what(): /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/highgui/src/window_QT.cpp:1642:

error: (-218) Library was built without OpenGL support in function CvWindow

 

出现这个问题的原因在于寻找到的 OpenCV模块是不支持 OpenGL的,于是需要在OpenCV模块下的CMakeLists文件中打开对OpenGL的支持。具体操作请参考链接:https://blog.csdn.net/weixin_40198941/article/details/97523485

--需要重新编译Opencv,带OpenGL. 可是编译时带了这个选项的。。。。

 

  1. 测试结果

Scanning dependencies of target pyopenpose

[100%] Building CXX object python/openpose/CMakeFiles/pyopenpose.dir/openpose_python.cpp.o

[100%] Linking CXX shared module pyopenpose.cpython-38-x86_64-linux-gnu.so

[100%] Built target pyopenpose

执行make install 后,我们可以看到有那些.so文件被生成,放在什么目录

/usr/local/lib/libopenpose_calibration.so

 

4、测试

视频:

./build/examples/openpose/openpose.bin --video examples/media/video.avi # With face and hands ./build/examples/openpose/openpose.bin --video examples/media/video.avi --face --hand

图像:

./build/examples/openpose/openpose.bin --image_dir examples/media/

# With face and hands ./build/examples/openpose/openpose.bin --image_dir examples/media/ --face --hand

 

# Running on Video

# Ubuntu and Mac

./build/examples/openpose/openpose.bin --video examples/media/video.avi

# With face and hands

./build/examples/openpose/openpose.bin --video examples/media/video.avi --face --hand

# Running on Webcam

# Ubuntu and Mac

./build/examples/openpose/openpose.bin

# With face and hands

./build/examples/openpose/openpose.bin --face --hand

# Running on Images

# Ubuntu and Mac

./build/examples/openpose/openpose.bin --image_dir examples/media/

# With face and hands

./build/examples/openpose/openpose.bin --image_dir examples/media/ --face --hand

 

 

其他参考链接

https://blog.csdn.net/weixin_42770591/article/details/81869688

 

 

 

 

  1. 采用QT实现Openpose的编程

单纯测试是无法满足开发Openpose应用的需求,这里需要将Openpose能单独合入到QT的编译器里面。我们也可以使用CmakeList.txt来实现编译,总的来看,还是QT比较合适(提供了很多c++库)。奉劝大家不要用windows的库,毕竟是微软不是吃素的。

本人环境中使用的.pro文件,里面写了注释,也是根据我有限的水平写的,希望能帮助到看这篇文章的人:

QT += core gui

greaterThan(QT_MAJOR_VERSION, 3): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use

# any Qt feature that has been marked deprecated (the exact warnings

# depend on your compiler). Please consult the documentation of the

# deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

 

# for OpenCV 这些是从opencv.pc里面抄写出来的列表。根据自己的opencv的安全情况,找到对应的opencv.pc

LIBS += -L/usr/local/lib -lopencv_gapi -lopencv_stitching \

-lopencv_alphamat -lopencv_aruco -lopencv_bgsegm \

-lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm \

-lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo \

-lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm \

-lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy \

-lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform \

-lopencv_line_descriptor -lopencv_quality -lopencv_rapid -lopencv_reg \

-lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light \

-lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching \

-lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab \

-lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping \

-lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect \

-lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann \

-lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc \

-lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev

 

#这个也来自Opencv.pc的列表。

INCLUDEPATH += /usr/local/include/opencv4/

 

#OpenPose 的配置

#INCLUDEPATH += /usr/include #这个需要注释掉,否则会出现 "can\'t find stdlib.h"

INCLUDEPATH += /usr/local/include

INCLUDEPATH += /usr/local/cuda-11.0/include

INCLUDEPATH += /home/edward/software/openpose/include

INCLUDEPATH += /home/edward/software/openpose/3rdparty/caffe/include

 

LIBS += -L/usr/lib/

LIBS += -L/usr/local/lib/

#下面这个也需要注释掉,因为这个会使用老版本的qt

#LIBS += -L/usr/lib/x86_64-linux-gnu/

# 这个是本人自己编译安装的QT,

LIBS += -L/home/edward/software/qt5.13.2/5.13.2/gcc_64/lib

LIBS += -L/usr/local/cuda-11.0/lib64/

LIBS += -L/usr/local/cuda/lib64/

#下面这个是从晚上抄下来的,不知道有什么用。先放着。现在开始不喜欢Nvidia了,什么是否有机会,采用华为的GPU。希望有这个机会

LIBS += -L/usr/lib/nvidia/

 

 

#LIBS += -lopenpose_unity -lopenpose_producer -lopenpose -lopenpose_tracking -lopenpose_wrapper -lpenpose_thread -lopenpose_gui -lopenpose_filestream -lopenpose_gpu -lopenpose_hand -lopenpose_3d -lopenpose_utilities -lopenpose_pose -lopenpose_core -lopenpose_calibration -lopenpose_face -lopenpose_net

 

LIBS += -lopenpose

LIBS += -lcurand -lcublas -lcudnn -lcudart_static

LIBS += -lcudnn -lglog -lgflags -lboost_system -lboost_filesystem -lm -lboost_thread

LIBS += -pthread -fPIC -std=c++11 -fopenmp

 

 

# You can also make your code fail to compile if it uses deprecated APIs.

# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

 

SOURCES += \

main.cpp \

mainwindow.cpp

 

HEADERS += \

mainwindow.h

 

FORMS += \

mainwindow.ui

 

# Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin

else: unix:!android: target.path = /opt/$${TARGET}/bin

!isEmpty(target.path): INSTALLS += target