FastDeploy之hello world(C++)

环境准备

  • CUDA >= 11.2
  • cuDNN >= 8.0
  • python >= 3.6
  • Linux(x64)

下载FastDeploy C++ SDK

wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-gpu-1.0.6.tgz
tar -xzvf fastdeploy-linux-x64-gpu-1.0.6.tgz

获取模型和测试图像

wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz
wget https://bj.bcebos.com/fastdeploy/tests/test_det.jpg
tar xvf ppyoloe_crn_l_300e_coco.tgz

准备CMakeList.txt

cmake_minimum_required(VERSION 3.0.0)
project(ppyoloe VERSION 0.1.0)

include(CTest)
enable_testing()

include(/mnt/c/yp/lib/fastdeploy_cpp_sdk/FastDeploy.cmake)
# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})

# opencv
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(ppyoloe main.cpp)

target_link_libraries(ppyoloe ${FASTDEPLOY_LIBS} ${OpenCV_LIBS})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

准备C++推理代码

main.cpp

#include <stdio.h>
#include <iostream>
#include <string>

#include "fastdeploy/vision.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main() {
    
    
  std::string model_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/model.pdmodel";
  std::string params_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/model.pdiparams";
  std::string infer_cfg_file = "/mnt/c/yp/fastDeploy/ppyoloe/ppyoloe_crn_l_300e_coco/infer_cfg.yml";
  // 模型推理的配置信息
  fastdeploy::RuntimeOption option;
  auto model = fastdeploy::vision::detection::PPYOLOE(model_file, params_file, infer_cfg_file, option);

  assert(model.Initialized()); // 判断模型是否初始化成功

  cv::Mat im = cv::imread("/mnt/c/yp/fastDeploy/ppyoloe/test_det.jpg");
  fastdeploy::vision::DetectionResult result;
  
  assert(model.Predict(&im, &result)); // 判断是否预测成功

  std::cout << result.Str() << std::endl;

  cv::Mat vis_im = fastdeploy::vision::Visualize::VisDetection(im, result, 0.5);
  // 可视化结果保存到本地
  cv::imwrite("vis_result.jpg", vis_im);
  std::cout << "Visualized result save in vis_result.jpg" << std::endl;
  return 0;
}

编译可执行程序

mkdir build && cd build && cmake .. && make -j8

运行可执行程序

./ppyoloe 

PS:执行时如提示error while loading shared libraries: libxxx.so: cannot open shared object file: No such file…,说明程序执行时没有找到FastDeploy的库路径,可通过执行如下命令,将FastDeploy的库路径添加到环境变量之后,重新执行二进制程序。

source /Path/to/fastdeploy_cpp_sdk/fastdeploy_init.sh

在这里插入图片描述
在这里插入图片描述

可能遇到的问题

/usr/bin/ld: warning: libcudnn.so.8, needed by
/mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so,
not found (try using -rpath or -rpath-link)
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]
/usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]' /usr/bin/ld: /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib/libnvinfer_plugin.so: undefined reference to [email protected]

解决方法
查找libcudnn.so.8 find / -name libcudnn.so.8
拷贝到/mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib中去
cp /xxx/ libcudnn.so.8 /mnt/c/yp/lib/fastdeploy_cpp_sdk/third_libs/install/tensorrt/lib

参考

https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/cn/quick_start/models/cpp.md

猜你喜欢

转载自blog.csdn.net/weixin_42990464/article/details/130724432