From abae858f09b411af1ee840117531d1af08c1e237 Mon Sep 17 00:00:00 2001 From: chenjian Date: Tue, 14 Feb 2023 14:28:23 +0800 Subject: [PATCH] [C api] Add install command for c api and a demo in paddledection (#1289) * add c api demo and fix bugs * fix doc * add config.h.in * add config.h.in * add config.h.in --- CMakeLists.txt | 10 + FastDeploy.cmake.in | 3 + c_api/CMakeLists.txt | 1 + c_api/fastdeploy_capi/config.h | 22 ++ c_api/fastdeploy_capi/config.h.in | 22 ++ c_api/fastdeploy_capi/enum_variables.h | 71 ++++++ c_api/fastdeploy_capi/fd_common.h | 40 ---- c_api/fastdeploy_capi/fd_type.cc | 40 ++++ c_api/fastdeploy_capi/fd_type.h | 19 +- c_api/fastdeploy_capi/runtime_option.cc | 17 +- c_api/fastdeploy_capi/runtime_option.h | 17 +- c_api/fastdeploy_capi/vision.h | 26 +++ .../vision/classification/ppcls/model.cc | 7 +- .../vision/detection/ppdet/model.cc | 7 +- c_api/fastdeploy_capi/vision/result.cc | 6 +- c_api/fastdeploy_capi/vision/result.h | 16 -- c_api/fastdeploy_capi/vision/visualize.cc | 6 +- .../paddledetection/c/CMakeLists.txt | 13 ++ .../detection/paddledetection/c/README.md | 200 +++++++++++++++++ .../detection/paddledetection/c/README_CN.md | 204 ++++++++++++++++++ .../paddledetection/c/infer_ppyoloe.c | 124 +++++++++++ 21 files changed, 782 insertions(+), 89 deletions(-) create mode 100755 c_api/fastdeploy_capi/config.h create mode 100755 c_api/fastdeploy_capi/config.h.in create mode 100644 c_api/fastdeploy_capi/enum_variables.h create mode 100644 c_api/fastdeploy_capi/fd_type.cc create mode 100644 c_api/fastdeploy_capi/vision.h create mode 100644 examples/vision/detection/paddledetection/c/CMakeLists.txt create mode 100644 examples/vision/detection/paddledetection/c/README.md create mode 100644 examples/vision/detection/paddledetection/c/README_CN.md create mode 100644 examples/vision/detection/paddledetection/c/infer_ppyoloe.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 51ba10c83..4f85653f2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -715,6 +715,16 @@ if(WITH_ASCEND) ) endif() +if(WITH_CAPI) + install( + DIRECTORY ${PROJECT_SOURCE_DIR}/c_api/fastdeploy_capi + DESTINATION ${CMAKE_INSTALL_PREFIX}/include + FILES_MATCHING + PATTERN "*.h" + PATTERN "*/types_internal.h" EXCLUDE + ) +endif() + include(${PROJECT_SOURCE_DIR}/cmake/config_cpack.cmake) ############################### Building: FastDeploy Python Wheel ############################# diff --git a/FastDeploy.cmake.in b/FastDeploy.cmake.in index d622660f4..c79001c28 100644 --- a/FastDeploy.cmake.in +++ b/FastDeploy.cmake.in @@ -33,6 +33,7 @@ set(ORT_DIRECTORY "@ORT_DIRECTORY@") set(OPENVINO_DIRECTORY "@OPENVINO_DIRECTORY@") set(RKNN2_TARGET_SOC "@RKNN2_TARGET_SOC@") set(WITH_KUNLUNXIN @WITH_KUNLUNXIN@) +set(WITH_CAPI @WITH_CAPI@) # Whether to use FastDeploy static lib. The default # value for this option is determined by the SDK # build-time options. @@ -357,6 +358,7 @@ message(STATUS " CMAKE_INSTALL_PREFIX : ${CMAKE_INSTALL_PREFIX}") message(STATUS " CMAKE_MODULE_PATH : ${CMAKE_MODULE_PATH}") message(STATUS "") message(STATUS " WITH_GPU : ${WITH_GPU}") +message(STATUS " WITH_CAPI : ${WITH_CAPI}") message(STATUS " ENABLE_ORT_BACKEND : ${ENABLE_ORT_BACKEND}") message(STATUS " ENABLE_RKNPU2_BACKEND : ${ENABLE_RKNPU2_BACKEND}") message(STATUS " ENABLE_SOPHGO_BACKEND : ${ENABLE_SOPHGO_BACKEND}") @@ -365,6 +367,7 @@ message(STATUS " ENABLE_POROS_BACKEND : ${ENABLE_POROS_BACKEND}") message(STATUS " ENABLE_OPENVINO_BACKEND : ${ENABLE_OPENVINO_BACKEND}") message(STATUS " ENABLE_TRT_BACKEND : ${ENABLE_TRT_BACKEND}") message(STATUS " ENABLE_LITE_BACKEND : ${ENABLE_LITE_BACKEND}") + if(ENABLE_PADDLE_BACKEND) message(STATUS " Paddle Inference version : ${PADDLEINFERENCE_VERSION}") endif() diff --git a/c_api/CMakeLists.txt b/c_api/CMakeLists.txt index 7c7a16626..4f3934165 100644 --- a/c_api/CMakeLists.txt +++ b/c_api/CMakeLists.txt @@ -19,6 +19,7 @@ if(NOT WITH_CAPI) return() endif() +configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/c_api/fastdeploy_capi/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/c_api/fastdeploy_capi/config.h) file(GLOB_RECURSE DEPLOY_CAPI_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/c_api/fastdeploy_capi/*.cc) if(NOT ENABLE_VISION) file(GLOB_RECURSE DEPLOY_VISION_CAPI_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/c_api/fastdeploy_capi/vision/*.cc) diff --git a/c_api/fastdeploy_capi/config.h b/c_api/fastdeploy_capi/config.h new file mode 100755 index 000000000..73de04c44 --- /dev/null +++ b/c_api/fastdeploy_capi/config.h @@ -0,0 +1,22 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +#ifndef ENABLE_VISION +#define ENABLE_VISION +#endif + +#ifndef ENABLE_TEXT +/* #undef ENABLE_TEXT */ +#endif diff --git a/c_api/fastdeploy_capi/config.h.in b/c_api/fastdeploy_capi/config.h.in new file mode 100755 index 000000000..4b5b0137c --- /dev/null +++ b/c_api/fastdeploy_capi/config.h.in @@ -0,0 +1,22 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +#ifndef ENABLE_VISION +#cmakedefine ENABLE_VISION +#endif + +#ifndef ENABLE_TEXT +#cmakedefine ENABLE_TEXT +#endif diff --git a/c_api/fastdeploy_capi/enum_variables.h b/c_api/fastdeploy_capi/enum_variables.h new file mode 100644 index 000000000..05a3d4c9f --- /dev/null +++ b/c_api/fastdeploy_capi/enum_variables.h @@ -0,0 +1,71 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#define FD_ENUM(type) \ + typedef int32_t type; \ + enum + +FD_ENUM(FD_C_ModelFormat){ + AUTOREC, ///< Auto recognize the model format by model file name + PADDLE, ///< Model with paddlepaddle format + ONNX, ///< Model with ONNX format + RKNN, ///< Model with RKNN format + TORCHSCRIPT, ///< Model with TorchScript format + SOPHGO, ///< Model with SOPHGO format +}; + +FD_ENUM(FD_C_rknpu2_CpuName){ + RK356X = 0, /* run on RK356X. */ + RK3588 = 1, /* default,run on RK3588. */ + UNDEFINED, +}; + +FD_ENUM(FD_C_rknpu2_CoreMask){ + RKNN_NPU_CORE_AUTO = 0, //< default, run on NPU core randomly. + RKNN_NPU_CORE_0 = 1, //< run on NPU core 0. + RKNN_NPU_CORE_1 = 2, //< run on NPU core 1. + RKNN_NPU_CORE_2 = 4, //< run on NPU core 2. + RKNN_NPU_CORE_0_1 = RKNN_NPU_CORE_0 | + RKNN_NPU_CORE_1, //< run on NPU core 1 and core 2. + RKNN_NPU_CORE_0_1_2 = RKNN_NPU_CORE_0_1 | + RKNN_NPU_CORE_2, //< run on NPU core 1 and core 2. + RKNN_NPU_CORE_UNDEFINED, +}; + +FD_ENUM(FD_C_LitePowerMode){ + LITE_POWER_HIGH = 0, ///< Use Lite Backend with high power mode + LITE_POWER_LOW = 1, ///< Use Lite Backend with low power mode + LITE_POWER_FULL = 2, ///< Use Lite Backend with full power mode + LITE_POWER_NO_BIND = 3, ///< Use Lite Backend with no bind power mode + LITE_POWER_RAND_HIGH = 4, ///< Use Lite Backend with rand high mode + LITE_POWER_RAND_LOW = 5 ///< Use Lite Backend with rand low power mode +}; + +FD_ENUM(FD_C_ResultType){ + UNKNOWN_RESULT, + CLASSIFY, + DETECTION, + SEGMENTATION, + OCR, + MOT, + FACE_DETECTION, + FACE_ALIGNMENT, + FACE_RECOGNITION, + MATTING, + MASK, + KEYPOINT_DETECTION, + HEADPOSE, +}; diff --git a/c_api/fastdeploy_capi/fd_common.h b/c_api/fastdeploy_capi/fd_common.h index 6374cf9b5..18bfb3c91 100644 --- a/c_api/fastdeploy_capi/fd_common.h +++ b/c_api/fastdeploy_capi/fd_common.h @@ -58,43 +58,3 @@ typedef int8_t FD_C_Bool; #define TRUE 1 #define FALSE 0 - -#define FD_ENUM(type) \ - typedef int32_t type; \ - enum - -FD_ENUM(FD_C_ModelFormat){ - AUTOREC, ///< Auto recognize the model format by model file name - PADDLE, ///< Model with paddlepaddle format - ONNX, ///< Model with ONNX format - RKNN, ///< Model with RKNN format - TORCHSCRIPT, ///< Model with TorchScript format - SOPHGO, ///< Model with SOPHGO format -}; - -FD_ENUM(FD_C_rknpu2_CpuName){ - RK356X = 0, /* run on RK356X. */ - RK3588 = 1, /* default,run on RK3588. */ - UNDEFINED, -}; - -FD_ENUM(FD_C_rknpu2_CoreMask){ - RKNN_NPU_CORE_AUTO = 0, //< default, run on NPU core randomly. - RKNN_NPU_CORE_0 = 1, //< run on NPU core 0. - RKNN_NPU_CORE_1 = 2, //< run on NPU core 1. - RKNN_NPU_CORE_2 = 4, //< run on NPU core 2. - RKNN_NPU_CORE_0_1 = RKNN_NPU_CORE_0 | - RKNN_NPU_CORE_1, //< run on NPU core 1 and core 2. - RKNN_NPU_CORE_0_1_2 = RKNN_NPU_CORE_0_1 | - RKNN_NPU_CORE_2, //< run on NPU core 1 and core 2. - RKNN_NPU_CORE_UNDEFINED, -}; - -FD_ENUM(FD_C_LitePowerMode){ - LITE_POWER_HIGH = 0, ///< Use Lite Backend with high power mode - LITE_POWER_LOW = 1, ///< Use Lite Backend with low power mode - LITE_POWER_FULL = 2, ///< Use Lite Backend with full power mode - LITE_POWER_NO_BIND = 3, ///< Use Lite Backend with no bind power mode - LITE_POWER_RAND_HIGH = 4, ///< Use Lite Backend with rand high mode - LITE_POWER_RAND_LOW = 5 ///< Use Lite Backend with rand low power mode -}; diff --git a/c_api/fastdeploy_capi/fd_type.cc b/c_api/fastdeploy_capi/fd_type.cc new file mode 100644 index 000000000..34390888a --- /dev/null +++ b/c_api/fastdeploy_capi/fd_type.cc @@ -0,0 +1,40 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "fastdeploy_capi/fd_type.h" + +#include + +#include "fastdeploy_capi/fd_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +FD_C_Mat FD_C_Imread(const char* imgpath) { + cv::Mat image = cv::imread(imgpath); + return new cv::Mat(image); +} + +FD_C_Bool FD_C_Imwrite(const char* savepath, FD_C_Mat img) { + cv::Mat cv_img = *(reinterpret_cast(img)); + bool result = cv::imwrite(savepath, cv_img); + return result; +} + +void FD_C_DestroyMat(FD_C_Mat mat) { delete reinterpret_cast(mat); } + +#ifdef __cplusplus +} +#endif diff --git a/c_api/fastdeploy_capi/fd_type.h b/c_api/fastdeploy_capi/fd_type.h index 75daf9db6..4a3d8cadd 100644 --- a/c_api/fastdeploy_capi/fd_type.h +++ b/c_api/fastdeploy_capi/fd_type.h @@ -17,7 +17,8 @@ #include #include -#include "fastdeploy_capi/fd_common.h" // NOLINT +#include "fastdeploy_capi/enum_variables.h" +#include "fastdeploy_capi/fd_common.h" typedef struct FD_C_OneDimArrayUint8 { size_t size; @@ -65,3 +66,19 @@ typedef struct FD_C_TwoDimArrayFloat { } FD_C_TwoDimArrayFloat; // std::vector> typedef void* FD_C_Mat; + +#ifdef __cplusplus +extern "C" { +#endif + +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat +FD_C_Imread(const char* imgpath); + +FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_Imwrite(const char* savepath, + __fd_keep FD_C_Mat); + +FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyMat(__fd_take FD_C_Mat mat); + +#ifdef __cplusplus +} +#endif diff --git a/c_api/fastdeploy_capi/runtime_option.cc b/c_api/fastdeploy_capi/runtime_option.cc index 3c9b4022d..4683d468d 100644 --- a/c_api/fastdeploy_capi/runtime_option.cc +++ b/c_api/fastdeploy_capi/runtime_option.cc @@ -17,7 +17,9 @@ #include "fastdeploy/utils/utils.h" #include "fastdeploy_capi/types_internal.h" +#ifdef __cplusplus extern "C" { +#endif FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper() { FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper = @@ -28,7 +30,7 @@ FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper() { return fd_c_runtime_option_wrapper; } -void FD_C_DestroyRuntimeOption( +void FD_C_DestroyRuntimeOptionWrapper( __fd_take FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper) { delete fd_c_runtime_option_wrapper; } @@ -404,15 +406,6 @@ void FD_C_RuntimeOptionWrapperUseIpu( batches_per_step); } -void FD_C_RuntimeOptionWrapperSetIpuConfig( - __fd_keep FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, - FD_C_Bool enable_fp16, int replica_num, float available_memory_proportion, - FD_C_Bool enable_half_partial) { - auto& runtime_option = CHECK_AND_CONVERT_FD_TYPE(RuntimeOptionWrapper, - fd_c_runtime_option_wrapper); - runtime_option->SetIpuConfig(enable_fp16, replica_num, - available_memory_proportion, - enable_half_partial); +#ifdef __cplusplus } - -} // extern "C" +#endif diff --git a/c_api/fastdeploy_capi/runtime_option.h b/c_api/fastdeploy_capi/runtime_option.h index cfc087473..40d220bbe 100644 --- a/c_api/fastdeploy_capi/runtime_option.h +++ b/c_api/fastdeploy_capi/runtime_option.h @@ -14,9 +14,7 @@ #pragma once -#include - -#include "fastdeploy_capi/fd_common.h" +#include "fastdeploy_capi/fd_type.h" typedef struct FD_C_RuntimeOptionWrapper FD_C_RuntimeOptionWrapper; @@ -499,19 +497,6 @@ FASTDEPLOY_CAPI_EXPORT extern void FD_C_RuntimeOptionWrapperUseIpu( int device_num, int micro_batch_size, FD_C_Bool enable_pipelining, int batches_per_step); -/** \brief Set IPU config. - * - * \param[in] fd_c_runtime_option_wrapper pointer to FD_C_RuntimeOptionWrapper object - * \param[in] enable_fp16 enable fp16. - * \param[in] replica_num the number of graph replication. - * \param[in] available_memory_proportion the available memory proportion for matmul/conv. - * \param[in] enable_half_partial enable fp16 partial for matmul, only work with fp16. - */ -FASTDEPLOY_CAPI_EXPORT extern void FD_C_RuntimeOptionWrapperSetIpuConfig( - __fd_keep FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, - FD_C_Bool enable_fp16, int replica_num, float available_memory_proportion, - FD_C_Bool enable_half_partial); - #ifdef __cplusplus } // extern "C" #endif diff --git a/c_api/fastdeploy_capi/vision.h b/c_api/fastdeploy_capi/vision.h new file mode 100644 index 000000000..a27a6c8dd --- /dev/null +++ b/c_api/fastdeploy_capi/vision.h @@ -0,0 +1,26 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +#include "fastdeploy_capi/config.h" + +#ifdef ENABLE_VISION +#include "fastdeploy_capi/vision/classification/ppcls/model.h" +#include "fastdeploy_capi/vision/detection/ppdet/model.h" +#include "fastdeploy_capi/vision/result.h" +#include "fastdeploy_capi/vision/visualize.h" +#endif + +#include "fastdeploy_capi/fd_type.h" +#include "fastdeploy_capi/runtime_option.h" diff --git a/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc b/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc index 3ed62f26a..ad0028612 100644 --- a/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc +++ b/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc @@ -16,7 +16,9 @@ #include "fastdeploy_capi/types_internal.h" +#ifdef __cplusplus extern "C" { +#endif FD_C_PaddleClasModelWrapper* FD_C_CreatePaddleClasModelWrapper( const char* model_file, const char* params_file, const char* config_file, @@ -50,4 +52,7 @@ FD_C_Bool FD_C_PaddleClasModelWrapperPredict( ClassifyResultWrapper, fd_c_classify_result_wrapper); return paddleclas_model->Predict(im, classify_result.get()); } -} \ No newline at end of file + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc b/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc index 17a87ec8b..e7055185f 100644 --- a/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc +++ b/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc @@ -17,7 +17,9 @@ #include "fastdeploy_capi/types_internal.h" #include "fastdeploy_capi/vision/visualize.h" +#ifdef __cplusplus extern "C" { +#endif FD_C_PPYOLOEWrapper* FD_C_CreatesPPYOLOEWrapper( const char* model_file, const char* params_file, const char* config_file, @@ -50,4 +52,7 @@ FD_C_Bool FD_C_PPYOLOEWrapperPredict( DetectionResultWrapper, fd_c_detection_result_wrapper); return ppyoloe_model->Predict(im, detection_result.get()); } -} \ No newline at end of file + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/c_api/fastdeploy_capi/vision/result.cc b/c_api/fastdeploy_capi/vision/result.cc index abf52ba69..207ef3950 100644 --- a/c_api/fastdeploy_capi/vision/result.cc +++ b/c_api/fastdeploy_capi/vision/result.cc @@ -17,7 +17,9 @@ #include "fastdeploy/utils/utils.h" #include "fastdeploy_capi/types_internal.h" +#ifdef __cplusplus extern "C" { +#endif // Classification Results @@ -235,4 +237,6 @@ FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapperFromData( return fd_c_detection_result_wrapper; } -} \ No newline at end of file +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/c_api/fastdeploy_capi/vision/result.h b/c_api/fastdeploy_capi/vision/result.h index 9d32052d9..247ec50f6 100644 --- a/c_api/fastdeploy_capi/vision/result.h +++ b/c_api/fastdeploy_capi/vision/result.h @@ -24,22 +24,6 @@ typedef struct FD_C_DetectionResultWrapper FD_C_DetectionResultWrapper; extern "C" { #endif -FD_ENUM(FD_C_ResultType){ - UNKNOWN_RESULT, - CLASSIFY, - DETECTION, - SEGMENTATION, - OCR, - MOT, - FACE_DETECTION, - FACE_ALIGNMENT, - FACE_RECOGNITION, - MATTING, - MASK, - KEYPOINT_DETECTION, - HEADPOSE, -}; - typedef struct FD_C_ClassifyResult { FD_C_OneDimArrayInt32 label_ids; FD_C_OneDimArrayFloat scores; diff --git a/c_api/fastdeploy_capi/vision/visualize.cc b/c_api/fastdeploy_capi/vision/visualize.cc index 9132fe606..6045270bd 100644 --- a/c_api/fastdeploy_capi/vision/visualize.cc +++ b/c_api/fastdeploy_capi/vision/visualize.cc @@ -17,7 +17,9 @@ #include "fastdeploy/vision/visualize/visualize.h" #include "fastdeploy_capi/types_internal.h" +#ifdef __cplusplus extern "C" { +#endif FD_C_Mat FD_C_VisDetection(FD_C_Mat im, FD_C_DetectionResult* fd_c_detection_result, @@ -32,4 +34,6 @@ FD_C_Mat FD_C_VisDetection(FD_C_Mat im, line_size, font_size); return new cv::Mat(result); } -} \ No newline at end of file +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/examples/vision/detection/paddledetection/c/CMakeLists.txt b/examples/vision/detection/paddledetection/c/CMakeLists.txt new file mode 100644 index 000000000..12a8d9a2c --- /dev/null +++ b/examples/vision/detection/paddledetection/c/CMakeLists.txt @@ -0,0 +1,13 @@ +PROJECT(infer_demo C) +CMAKE_MINIMUM_REQUIRED (VERSION 3.10) + +# 指定下载解压后的fastdeploy库路径 +option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.") + +include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) + +# 添加FastDeploy依赖头文件 +include_directories(${FASTDEPLOY_INCS}) + +add_executable(infer_ppyoloe_demo ${PROJECT_SOURCE_DIR}/infer_ppyoloe.c) +target_link_libraries(infer_ppyoloe_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/detection/paddledetection/c/README.md b/examples/vision/detection/paddledetection/c/README.md new file mode 100644 index 000000000..79a33e51a --- /dev/null +++ b/examples/vision/detection/paddledetection/c/README.md @@ -0,0 +1,200 @@ +English | [简体中文](README_CN.md) +# PaddleDetection C Deployment Example + +This directory provides examples that `infer_xxx.c` fast finishes the deployment of PaddleDetection models, including PPYOLOE on CPU/GPU. + +Before deployment, two steps require confirmation + +- 1. Software and hardware should meet the requirements. Please refer to [FastDeploy Environment Requirements](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) +- 2. Download the precompiled deployment library and samples code according to your development environment. Refer to [FastDeploy Precompiled Library](../../../../../docs/en/build_and_install/download_prebuilt_libraries.md) + +Taking inference on Linux as an example, the compilation test can be completed by executing the following command in this directory. FastDeploy version 1.0.4 or above (x.x.x>=1.0.4) is required to support this model. + +```bash +ppyoloe is taken as an example for inference deployment + +mkdir build +cd build +# Download the FastDeploy precompiled library. Users can choose your appropriate version in the `FastDeploy Precompiled Library` mentioned above +wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz +tar xvf fastdeploy-linux-x64-x.x.x.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x +make -j + +# Download the PPYOLOE model file and test images +wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +tar xvf ppyoloe_crn_l_300e_coco.tgz + + +# CPU inference +./infer_ppyoloe_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 0 +# GPU inference +./infer_ppyoloe_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 1 +``` + +The above command works for Linux or MacOS. For SDK use-pattern in Windows, refer to: +- [How to use FastDeploy C++ SDK in Windows](../../../../../docs/en/faq/use_sdk_on_windows.md) + +## PaddleDetection C Interface + +### RuntimeOption + +```c +FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper() +``` + +> Create a RuntimeOption object, and return a pointer to manipulate it. +> +> **Return** +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): Pointer to manipulate RuntimeOption object. + + +```c +void FD_C_RuntimeOptionWrapperUseCpu( + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper) +``` + +> Enable Cpu inference. +> +> **Params** +> +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): Pointer to manipulate RuntimeOption object. + +```c +void FD_C_RuntimeOptionWrapperUseGpu( + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, + int gpu_id) +``` +> Enable Gpu inference. +> +> **Params** +> +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): Pointer to manipulate RuntimeOption object. +> * **gpu_id**(int): gpu id + + +### Model + +```c + +FD_C_PPYOLOEWrapper* FD_C_CreatesPPYOLOEWrapper( + const char* model_file, const char* params_file, const char* config_file, + FD_C_RuntimeOptionWrapper* runtime_option, + const FD_C_ModelFormat model_format) + +``` + +> Create a PPYOLOE model object, and return a pointer to manipulate it. +> +> **Params** +> +> * **model_file**(const char*): Model file path +> * **params_file**(const char*): Parameter file path +> * **config_file**(const char*): Configuration file path, which is the deployment yaml file exported by PaddleDetection +> * **runtime_option**(FD_C_RuntimeOptionWrapper*): Backend inference configuration. None by default, which is the default configuration +> * **model_format**(FD_C_ModelFormat): Model format. Paddle format by default +> +> **Return** +> * **fd_c_ppyoloe_wrapper**(FD_C_PPYOLOEWrapper*): Pointer to manipulate PPYOLOE object. + + +#### Read and write image + +```c +FD_C_Mat FD_C_Imread(const char* imgpath) +``` + +> Read an image, and return a pointer to cv::Mat. +> +> **Params** +> +> * **imgpath**(const char*): image path +> +> **Return** +> +> * **imgmat**(FD_C_Mat): pointer to cv::Mat object which holds the image. + + +```c +FD_C_Bool FD_C_Imwrite(const char* savepath, FD_C_Mat img); +``` + +> Write image to a file. +> +> **Params** +> +> * **savepath**(const char*): save path +> * **img**(FD_C_Mat): pointer to cv::Mat object +> +> **Return** +> +> * **result**(FD_C_Bool): bool to indicate success or failure + + +#### Prediction + +```c +FD_C_Bool FD_C_PPYOLOEWrapperPredict( + __fd_take FD_C_PPYOLOEWrapper* fd_c_ppyoloe_wrapper, FD_C_Mat img, + FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) +``` +> +> Predict an image, and generate detection result. +> +> **Params** +> * **fd_c_ppyoloe_wrapper**(FD_C_PPYOLOEWrapper*): pointer to manipulate PPYOLOE object +> * **img**(FD_C_Mat): pointer to cv::Mat object, which can be obained by FD_C_Imread interface +> * **result**(FD_C_DetectionResultWrapper*): Detection result, including detection box and confidence of each box. Refer to [Vision Model Prediction Result](../../../../../docs/api/vision_results/) for DetectionResult + + +#### Result + +```c +FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapper(); +``` +> +> Create a DetectionResult object to keep the detection result,return a pointer to manipulate it. +> +> **Return** +> * **fd_c_detection_result_wrapper**(FD_C_DetectionResultWrapper*): pointer to manipulate DetectionResult object + + + +```c +FD_C_DetectionResult* FD_C_DetectionResultWrapperGetData( + FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) +``` +> +> Get the C DetectionResult structure from FD_C_DetectionResultWrapper, which can access the fileds directly. +> +> **Params** +> * **fd_c_detection_result_wrapper**(FD_C_DetectionResultWrapper*): pointer to manipulate DetectionResult object +> +> **Return** +> * **fd_c_detection_result**(FD_C_DetectionResult*): pointer to C DetectionResult structure + + + +```c +FD_C_Mat FD_C_VisDetection(FD_C_Mat im, FD_C_DetectionResult* fd_detection_result, + float score_threshold, int line_size, float font_size); +``` +> +> Visualize detection results and return visualization image. +> +> **Params** +> * **im**(FD_C_Mat): pointer to input image +> * **fd_detection_result**(FD_C_DetectionResult*): pointer to C DetectionResult structure +> * **score_threshold**(float): score threshold +> * **line_size**(int): line size +> * **font_size**(float): font size +> +> **Return** +> * **vis_im**(FD_C_Mat): pointer to visualization image. + + +- [Model Description](../../) +- [Python Deployment](../python) +- [Vision Model prediction results](../../../../../docs/api/vision_results/) +- [How to switch the model inference backend engine](../../../../../docs/en/faq/how_to_change_backend.md) diff --git a/examples/vision/detection/paddledetection/c/README_CN.md b/examples/vision/detection/paddledetection/c/README_CN.md new file mode 100644 index 000000000..22cbd311c --- /dev/null +++ b/examples/vision/detection/paddledetection/c/README_CN.md @@ -0,0 +1,204 @@ +[English](README.md) | 简体中文 +# PaddleDetection C 部署示例 + +本目录下提供`infer_xxx.c`来调用C API快速完成PaddleDetection模型PPYOLOE在CPU/GPU上部署的示例。 + +在部署前,需确认以下两个步骤 + +- 1. 软硬件环境满足要求,参考[FastDeploy环境要求](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) +- 2. 根据开发环境,下载预编译部署库和samples代码,参考[FastDeploy预编译库](../../../../../docs/cn/build_and_install/download_prebuilt_libraries.md) + +以Linux上推理为例,在本目录执行如下命令即可完成编译测试,支持此模型需保证FastDeploy版本1.0.4以上(x.x.x>=1.0.4) + +```bash +以ppyoloe为例进行推理部署 + +mkdir build +cd build +# 下载FastDeploy预编译库,用户可在上文提到的`FastDeploy预编译库`中自行选择合适的版本使用 +wget https://bj.bcebos.com/fastdeploy/release/cpp/fastdeploy-linux-x64-x.x.x.tgz +tar xvf fastdeploy-linux-x64-x.x.x.tgz +cmake .. -DFASTDEPLOY_INSTALL_DIR=${PWD}/fastdeploy-linux-x64-x.x.x +make -j + +# 下载PPYOLOE模型文件和测试图片 +wget https://bj.bcebos.com/paddlehub/fastdeploy/ppyoloe_crn_l_300e_coco.tgz +wget https://gitee.com/paddlepaddle/PaddleDetection/raw/release/2.4/demo/000000014439.jpg +tar xvf ppyoloe_crn_l_300e_coco.tgz + + +# CPU推理 +./infer_ppyoloe_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 0 +# GPU推理 +./infer_ppyoloe_demo ./ppyoloe_crn_l_300e_coco 000000014439.jpg 1 +``` + +以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考: +- [如何在Windows中使用FastDeploy C++ SDK](../../../../../docs/cn/faq/use_sdk_on_windows.md) + +如果用户使用华为昇腾NPU部署, 请参考以下方式在部署前初始化部署环境: +- [如何使用华为昇腾NPU部署](../../../../../docs/cn/faq/use_sdk_on_ascend.md) + +## PaddleDetection C API接口 + +### 配置 + +```c +FD_C_RuntimeOptionWrapper* FD_C_CreateRuntimeOptionWrapper() +``` + +> 创建一个RuntimeOption的配置对象,并且返回操作它的指针。 +> +> **返回** +> +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针 + + +```c +void FD_C_RuntimeOptionWrapperUseCpu( + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper) +``` + +> 开启CPU推理 +> +> **参数** +> +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针 + +```c +void FD_C_RuntimeOptionWrapperUseGpu( + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, + int gpu_id) +``` +> 开启GPU推理 +> +> **参数** +> +> * **fd_c_runtime_option_wrapper**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption对象的指针 +> * **gpu_id**(int): 显卡号 + + +### 模型 + +```c + +FD_C_PPYOLOEWrapper* FD_C_CreatesPPYOLOEWrapper( + const char* model_file, const char* params_file, const char* config_file, + FD_C_RuntimeOptionWrapper* runtime_option, + const FD_C_ModelFormat model_format) + +``` + +> 创建一个PPYOLOE的模型,并且返回操作它的指针。 +> +> **参数** +> +> * **model_file**(const char*): 模型文件路径 +> * **params_file**(const char*): 参数文件路径 +> * **config_file**(const char*): 配置文件路径,即PaddleDetection导出的部署yaml文件 +> * **runtime_option**(FD_C_RuntimeOptionWrapper*): 指向RuntimeOption的指针,表示后端推理配置 +> * **model_format**(FD_C_ModelFormat): 模型格式 +> +> **返回** +> * **fd_c_ppyoloe_wrapper**(FD_C_PPYOLOEWrapper*): 指向PPYOLOE模型对象的指针 + + +#### 读写图像 + +```c +FD_C_Mat FD_C_Imread(const char* imgpath) +``` + +> 读取一个图像,并且返回cv::Mat的指针。 +> +> **参数** +> +> * **imgpath**(const char*): 图像文件路径 +> +> **返回** +> +> * **imgmat**(FD_C_Mat): 指向图像数据cv::Mat的指针。 + + +```c +FD_C_Bool FD_C_Imwrite(const char* savepath, FD_C_Mat img); +``` + +> 将图像写入文件中。 +> +> **参数** +> +> * **savepath**(const char*): 保存图像的路径 +> * **img**(FD_C_Mat): 指向图像数据的指针 +> +> **返回** +> +> * **result**(FD_C_Bool): 表示操作是否成功 + + +#### Predict函数 + +```c +FD_C_Bool FD_C_PPYOLOEWrapperPredict( + __fd_take FD_C_PPYOLOEWrapper* fd_c_ppyoloe_wrapper, FD_C_Mat img, + FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) +``` +> +> 模型预测接口,输入图像直接并生成检测结果。 +> +> **参数** +> * **fd_c_ppyoloe_wrapper**(FD_C_PPYOLOEWrapper*): 指向PPYOLOE模型的指针 +> * **img**(FD_C_Mat): 输入图像的指针,指向cv::Mat对象,可以调用FD_C_Imread读取图像获取 +> * **result**(FD_C_DetectionResultWrapper*): 指向检测结果的指针,检测结果包括检测框,各个框的置信度, DetectionResult说明参考[视觉模型预测结果](../../../../../docs/api/vision_results/) + + +#### Predict结果 + +```c +FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapper(); +``` +> +> 创建一个DetectionResult对象,用来保存推理的结果,并返回所创建的DetectionResult对象的指针。 +> +> **返回** +> * **fd_c_detection_result_wrapper**(FD_C_DetectionResultWrapper*): 指向DetectionResult对象的指针 + + + +```c +FD_C_DetectionResult* FD_C_DetectionResultWrapperGetData( + FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) +``` +> +> 从DetectionResult对象中提取纯C结构的DetectionResult结果,并返回结构指针,通过该指针可直接返回结构中的字段。 +> +> **参数** +> * **fd_c_detection_result_wrapper**(FD_C_DetectionResultWrapper*): 指向DetectionResult对象的指针 +> +> **返回** +> * **fd_c_detection_result**(FD_C_DetectionResult*): 指向纯C结构的DetectionResult的指针 + + + +```c +FD_C_Mat FD_C_VisDetection(FD_C_Mat im, FD_C_DetectionResult* fd_detection_result, + float score_threshold, int line_size, float font_size); +``` +> +> 对检测结果进行可视化,返回可视化的图像。 +> +> **参数** +> * **im**(FD_C_Mat): 指向输入图像的指针 +> * **fd_detection_result**(FD_C_DetectionResult*): 指向纯C结构DetectionResult的指针 +> * **score_threshold**(float): 检测阈值 +> * **line_size**(int): 检测框线大小 +> * **font_size**(float): 检测框字体大小 +> +> **返回** +> * **vis_im**(FD_C_Mat): 指向可视化图像的指针 + + +- [模型介绍](../../) +- [Python部署](../python) +- [视觉模型预测结果](../../../../../docs/api/vision_results/) +- [如何切换模型推理后端引擎](../../../../../docs/cn/faq/how_to_change_backend.md) diff --git a/examples/vision/detection/paddledetection/c/infer_ppyoloe.c b/examples/vision/detection/paddledetection/c/infer_ppyoloe.c new file mode 100644 index 000000000..6b9cc4369 --- /dev/null +++ b/examples/vision/detection/paddledetection/c/infer_ppyoloe.c @@ -0,0 +1,124 @@ +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "fastdeploy_capi/vision.h" + +#ifdef WIN32 +const char sep = '\\'; +#else +const char sep = '/'; +#endif + +void CpuInfer(const char* model_dir, const char* image_file) { + char model_file[100]; + char params_file[100]; + char config_file[100]; + int max_size = 99; + snprintf(model_file, max_size, "%s%c%s", model_dir, sep, "model.pdmodel"); + snprintf(params_file, max_size, "%s%c%s", model_dir, sep, "model.pdiparams"); + snprintf(config_file, max_size, "%s%c%s", model_dir, sep, "infer_cfg.yml"); + + FD_C_RuntimeOptionWrapper* option = FD_C_CreateRuntimeOptionWrapper(); + FD_C_RuntimeOptionWrapperUseCpu(option); + + FD_C_PPYOLOEWrapper* model = FD_C_CreatesPPYOLOEWrapper( + model_file, params_file, config_file, option, PADDLE); + + FD_C_Mat im = FD_C_Imread(image_file); + + FD_C_DetectionResultWrapper* result_wrapper = + FD_C_CreateDetectionResultWrapper(); + + if (!FD_C_PPYOLOEWrapperPredict(model, im, result_wrapper)) { + printf("Failed to predict.\n"); + return; + } + + FD_C_DetectionResult* result = + FD_C_DetectionResultWrapperGetData(result_wrapper); + FD_C_Mat vis_im = FD_C_VisDetection(im, result, 0.5, 1, 0.5); + + FD_C_Imwrite("vis_result.jpg", vis_im); + printf("Visualized result saved in ./vis_result.jpg\n"); + + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPPYOLOEWrapper(model); + FD_C_DestroyDetectionResultWrapper(result_wrapper); + FD_C_DestroyDetectionResult(result); + FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); +} + +void GpuInfer(const char* model_dir, const char* image_file) { + char model_file[100]; + char params_file[100]; + char config_file[100]; + int max_size = 99; + snprintf(model_file, max_size, "%s%c%s", model_dir, sep, "model.pdmodel"); + snprintf(params_file, max_size, "%s%c%s", model_dir, sep, "model.pdiparams"); + snprintf(config_file, max_size, "%s%c%s", model_dir, sep, "infer_cfg.yml"); + + FD_C_RuntimeOptionWrapper* option = FD_C_CreateRuntimeOptionWrapper(); + FD_C_RuntimeOptionWrapperUseGpu(option, 0); + + FD_C_PPYOLOEWrapper* model = FD_C_CreatesPPYOLOEWrapper( + model_file, params_file, config_file, option, PADDLE); + + FD_C_Mat im = FD_C_Imread(image_file); + + FD_C_DetectionResultWrapper* result_wrapper = + FD_C_CreateDetectionResultWrapper(); + + if (!FD_C_PPYOLOEWrapperPredict(model, im, result_wrapper)) { + printf("Failed to predict.\n"); + return; + } + + FD_C_DetectionResult* result = + FD_C_DetectionResultWrapperGetData(result_wrapper); + FD_C_Mat vis_im = FD_C_VisDetection(im, result, 0.5, 1, 0.5); + + FD_C_Imwrite("vis_result.jpg", vis_im); + printf("Visualized result saved in ./vis_result.jpg\n"); + + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPPYOLOEWrapper(model); + FD_C_DestroyDetectionResultWrapper(result_wrapper); + FD_C_DestroyDetectionResult(result); + FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); +} + +int main(int argc, char* argv[]) { + if (argc < 4) { + printf( + "Usage: infer_demo path/to/model_dir path/to/image run_option, " + "e.g ./infer_model ./ppyoloe_model_dir ./test.jpeg 0" + "\n"); + printf( + "The data type of run_option is int, 0: run with cpu; 1: run with gpu" + "\n"); + return -1; + } + + if (atoi(argv[3]) == 0) { + CpuInfer(argv[1], argv[2]); + } else if (atoi(argv[3]) == 1) { + GpuInfer(argv[1], argv[2]); + } + return 0; +}