Skip to content

Commit

Permalink
[C API] add ppseg c api (PaddlePaddle#1384)
Browse files Browse the repository at this point in the history
* add ppseg c api

* fix bug

* fix interface
  • Loading branch information
rainyfly authored Feb 27, 2023
1 parent 017f305 commit b6e8773
Show file tree
Hide file tree
Showing 21 changed files with 779 additions and 344 deletions.
22 changes: 0 additions & 22 deletions c_api/fastdeploy_capi/config.h

This file was deleted.

9 changes: 9 additions & 0 deletions c_api/fastdeploy_capi/types_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(
DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(OCRResult,
fd_ocr_result_wrapper,
ocr_result)
// SegmentationResult
DECL_AND_IMPLEMENT_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(
SegmentationResult, fd_segmentation_result_wrapper, segmentation_result)

// Models:

Expand Down Expand Up @@ -147,6 +150,12 @@ DECL_AND_IMPLEMENT_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
DECL_AND_IMPLEMENT_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PPOCRv3, fd_ppocrv3_wrapper, ppocrv3_model);

// Segmentation models

// PaddleSegModel
DECL_AND_IMPLEMENT_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PaddleSegModel, fd_paddleseg_model_wrapper, segmentation_model);

#endif

std::unique_ptr<fastdeploy::RuntimeOption>&
Expand Down
33 changes: 33 additions & 0 deletions c_api/fastdeploy_capi/types_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "fastdeploy/vision/ocr/ppocr/recognizer.h"
#include "fastdeploy/vision/ocr/ppocr/ppocr_v2.h"
#include "fastdeploy/vision/ocr/ppocr/ppocr_v3.h"
#include "fastdeploy/vision/segmentation/ppseg/model.h"

#define DEFINE_RESULT_WRAPPER_STRUCT(typename, varname) typedef struct FD_C_##typename##Wrapper { \
std::unique_ptr<fastdeploy::vision::typename> varname; \
Expand All @@ -49,6 +50,10 @@
std::unique_ptr<fastdeploy::pipeline::typename> varname; \
} FD_C_##typename##Wrapper

#define DEFINE_SEGMENTATION_MODEL_WRAPPER_STRUCT(typename, varname) typedef struct FD_C_##typename##Wrapper { \
std::unique_ptr<fastdeploy::vision::segmentation::typename> varname; \
} FD_C_##typename##Wrapper

// ------------- belows are wrapper struct define --------------------- //

// Results:
Expand All @@ -63,6 +68,8 @@ DEFINE_RESULT_WRAPPER_STRUCT(DetectionResult, detection_result);
// OCRResult
DEFINE_RESULT_WRAPPER_STRUCT(OCRResult, ocr_result);

// Segmentation Result
DEFINE_RESULT_WRAPPER_STRUCT(SegmentationResult, segmentation_result);

// Models:

Expand Down Expand Up @@ -153,6 +160,10 @@ DEFINE_PIPELINE_MODEL_WRAPPER_STRUCT(PPOCRv2, ppocrv2_model);
// PPOCRv3
DEFINE_PIPELINE_MODEL_WRAPPER_STRUCT(PPOCRv3, ppocrv3_model);

// Segmentation models

// PaddleSegModel
DEFINE_SEGMENTATION_MODEL_WRAPPER_STRUCT(PaddleSegModel, segmentation_model);

// ------------- belows are function declaration for get ptr from wrapper --------------------- //

Expand All @@ -177,6 +188,10 @@ FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* varname)

#define DECLARE_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(typename, varname) std::unique_ptr<fastdeploy::vision::segmentation::typename>& \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* varname)


namespace fastdeploy {

Expand All @@ -194,6 +209,10 @@ DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(DetectionResult,
DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(OCRResult,
fd_ocr_result_wrapper);

// SegementationResult
DECLARE_RESULT_FUNC_FOR_GET_PTR_FROM_WRAPPER(SegmentationResult,
fd_segmentation_result_wrapper);


// Models:

Expand Down Expand Up @@ -324,6 +343,12 @@ DECLARE_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(PPOCRv2, fd_ppocrv2_wrapper
// PPOCRv3
DECLARE_PIPELINE_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(PPOCRv3, fd_ppocrv3_wrapper);

// Segmentation models

// PaddleSegModel
DECLARE_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(
PaddleSegModel, fd_paddleseg_model_wrapper);

} // namespace fastdeploy

#endif
Expand Down Expand Up @@ -383,3 +408,11 @@ FD_C_CheckAndConvert##typename##Wrapper( \
"The pointer of " #var_wrapper_name " shouldn't be nullptr."); \
return var_wrapper_name->var_ptr_name; \
}

#define DECL_AND_IMPLEMENT_SEGMENTATION_MODEL_FUNC_FOR_GET_PTR_FROM_WRAPPER(typename, var_wrapper_name, var_ptr_name) std::unique_ptr<fastdeploy::vision::segmentation::typename>& \
FD_C_CheckAndConvert##typename##Wrapper( \
FD_C_##typename##Wrapper* var_wrapper_name) { \
FDASSERT(var_wrapper_name != nullptr, \
"The pointer of " #var_wrapper_name " shouldn't be nullptr."); \
return var_wrapper_name->var_ptr_name; \
}
1 change: 1 addition & 0 deletions c_api/fastdeploy_capi/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "fastdeploy_capi/vision/classification/ppcls/model.h"
#include "fastdeploy_capi/vision/detection/ppdet/model.h"
#include "fastdeploy_capi/vision/ocr/ppocr/model.h"
#include "fastdeploy_capi/vision/segmentation/ppseg/model.h"
#include "fastdeploy_capi/vision/result.h"
#include "fastdeploy_capi/vision/visualize.h"
#endif
Expand Down
42 changes: 15 additions & 27 deletions c_api/fastdeploy_capi/vision/classification/ppcls/model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ FD_C_Bool FD_C_PaddleClasModelWrapperPredict(

bool successful = paddleclas_model->Predict(im, classify_result.get());
if (successful) {
FD_C_ClassifyResult* res =
FD_C_ClassifyResultWrapperGetData(fd_c_classify_result_wrapper);
*fd_c_classify_result = *res;
FD_C_ClassifyResultWrapperToCResult(fd_c_classify_result_wrapper,
fd_c_classify_result);
}
FD_C_DestroyClassifyResultWrapper(fd_c_classify_result_wrapper);
return successful;
}

Expand All @@ -69,36 +69,17 @@ FD_C_Bool FD_C_PaddleClasModelWrapperInitialized(
return paddleclas_model->Initialized();
}

FD_C_ClassifyResult* FD_C_ClassifyResultToC(
fastdeploy::vision::ClassifyResult* classify_result) {
// Internal use, transfer fastdeploy::vision::ClassifyResult to
// FD_C_ClassifyResult
FD_C_ClassifyResult* fd_c_classify_result_data = new FD_C_ClassifyResult();
// copy label_ids
fd_c_classify_result_data->label_ids.size = classify_result->label_ids.size();
fd_c_classify_result_data->label_ids.data =
new int32_t[fd_c_classify_result_data->label_ids.size];
memcpy(fd_c_classify_result_data->label_ids.data,
classify_result->label_ids.data(),
sizeof(int32_t) * fd_c_classify_result_data->label_ids.size);
// copy scores
fd_c_classify_result_data->scores.size = classify_result->scores.size();
fd_c_classify_result_data->scores.data =
new float[fd_c_classify_result_data->scores.size];
memcpy(fd_c_classify_result_data->scores.data, classify_result->scores.data(),
sizeof(float) * fd_c_classify_result_data->scores.size);
fd_c_classify_result_data->type =
static_cast<FD_C_ResultType>(classify_result->type);
return fd_c_classify_result_data;
}

FD_C_Bool FD_C_PaddleClasModelWrapperBatchPredict(
FD_C_PaddleClasModelWrapper* fd_c_paddleclas_model_wrapper,
FD_C_OneDimMat imgs, FD_C_OneDimClassifyResult* results) {
std::vector<cv::Mat> imgs_vec;
std::vector<FD_C_ClassifyResultWrapper*> results_wrapper_out;
std::vector<fastdeploy::vision::ClassifyResult> results_out;
for (int i = 0; i < imgs.size; i++) {
imgs_vec.push_back(*(reinterpret_cast<cv::Mat*>(imgs.data[i])));
FD_C_ClassifyResultWrapper* fd_classify_result_wrapper =
FD_C_CreateClassifyResultWrapper();
results_wrapper_out.push_back(fd_classify_result_wrapper);
}
auto& paddleclas_model = CHECK_AND_CONVERT_FD_TYPE(
PaddleClasModelWrapper, fd_c_paddleclas_model_wrapper);
Expand All @@ -108,9 +89,16 @@ FD_C_Bool FD_C_PaddleClasModelWrapperBatchPredict(
results->size = results_out.size();
results->data = new FD_C_ClassifyResult[results->size];
for (int i = 0; i < results_out.size(); i++) {
results->data[i] = *FD_C_ClassifyResultToC(&results_out[i]);
(*CHECK_AND_CONVERT_FD_TYPE(ClassifyResultWrapper,
results_wrapper_out[i])) =
std::move(results_out[i]);
FD_C_ClassifyResultWrapperToCResult(results_wrapper_out[i],
&results->data[i]);
}
}
for (int i = 0; i < results_out.size(); i++) {
FD_C_DestroyClassifyResultWrapper(results_wrapper_out[i]);
}
return successful;
}

Expand Down
19 changes: 13 additions & 6 deletions c_api/fastdeploy_capi/vision/detection/ppdet/base_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,22 @@ FD_C_Destroy##model_type##Wrapper(__fd_take FD_C_##model_type##Wrapper* wrapper_
DetectionResultWrapper, fd_c_detection_result_wrapper); \
bool successful = model->Predict(im, detection_result.get()); \
if (successful) { \
FD_C_DetectionResult* res = \
FD_C_DetectionResultWrapperGetData(fd_c_detection_result_wrapper); \
*fd_c_detection_result = *res; \
} \
return successful
FD_C_DetectionResultWrapperToCResult(fd_c_detection_result_wrapper, fd_c_detection_result); \
} \
FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); \
return successful

#define IMPLEMENT_INITIALIZED_FUNCTION(model_type, wrapper_var_name) auto& model = \
CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
return model->Initialized();

#define IMPLEMENT_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) std::vector<cv::Mat> imgs_vec; \
std::vector<fastdeploy::vision::DetectionResult> results_out; \
std::vector<FD_C_DetectionResultWrapper*> results_wrapper_out; \
for (int i = 0; i < imgs.size; i++) { \
imgs_vec.push_back(*(reinterpret_cast<cv::Mat*>(imgs.data[i]))); \
FD_C_DetectionResultWrapper* fd_detection_result_wrapper = FD_C_CreateDetectionResultWrapper(); \
results_wrapper_out.push_back(fd_detection_result_wrapper); \
} \
auto& model = \
CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
Expand All @@ -83,9 +85,14 @@ return model->Initialized();
results->size = results_out.size(); \
results->data = new FD_C_DetectionResult[results->size]; \
for (int i = 0; i < results_out.size(); i++) { \
results->data[i] = *FD_C_DetectionResultToC(&results_out[i]); \
(*CHECK_AND_CONVERT_FD_TYPE(DetectionResultWrapper, \
results_wrapper_out[i])) = std::move(results_out[i]); \
FD_C_DetectionResultWrapperToCResult(results_wrapper_out[i], &results->data[i]); \
} \
} \
for (int i = 0; i < results_out.size(); i++) { \
FD_C_DestroyDetectionResultWrapper(results_wrapper_out[i]); \
}\
return successful;

#define DECLARE_AND_IMPLEMENT_CREATE_WRAPPER_FUNCTION(model_type, var_name) FD_C_##model_type##Wrapper* FD_C_Create##model_type##Wrapper(\
Expand Down
61 changes: 0 additions & 61 deletions c_api/fastdeploy_capi/vision/detection/ppdet/model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,67 +46,6 @@ FD_C_Bool FD_C_PPYOLOEWrapperInitialized(
IMPLEMENT_INITIALIZED_FUNCTION(PPYOLOE, fd_ppyoloe_wrapper);
}

FD_C_DetectionResult* FD_C_DetectionResultToC(
fastdeploy::vision::DetectionResult* detection_result) {
// Internal use, transfer fastdeploy::vision::DetectionResult to
// FD_C_DetectionResult
FD_C_DetectionResult* fd_c_detection_result = new FD_C_DetectionResult();
// copy boxes
const int boxes_coordinate_dim = 4;
fd_c_detection_result->boxes.size = detection_result->boxes.size();
fd_c_detection_result->boxes.data =
new FD_C_OneDimArrayFloat[fd_c_detection_result->boxes.size];
for (size_t i = 0; i < detection_result->boxes.size(); i++) {
fd_c_detection_result->boxes.data[i].size = boxes_coordinate_dim;
fd_c_detection_result->boxes.data[i].data = new float[boxes_coordinate_dim];
for (size_t j = 0; j < boxes_coordinate_dim; j++) {
fd_c_detection_result->boxes.data[i].data[j] =
detection_result->boxes[i][j];
}
}
// copy scores
fd_c_detection_result->scores.size = detection_result->scores.size();
fd_c_detection_result->scores.data =
new float[fd_c_detection_result->scores.size];
memcpy(fd_c_detection_result->scores.data, detection_result->scores.data(),
sizeof(float) * fd_c_detection_result->scores.size);
// copy label_ids
fd_c_detection_result->label_ids.size = detection_result->label_ids.size();
fd_c_detection_result->label_ids.data =
new int32_t[fd_c_detection_result->label_ids.size];
memcpy(fd_c_detection_result->label_ids.data,
detection_result->label_ids.data(),
sizeof(int32_t) * fd_c_detection_result->label_ids.size);
// copy masks
fd_c_detection_result->masks.size = detection_result->masks.size();
fd_c_detection_result->masks.data =
new FD_C_Mask[fd_c_detection_result->masks.size];
for (size_t i = 0; i < detection_result->masks.size(); i++) {
// copy data in mask
fd_c_detection_result->masks.data[i].data.size =
detection_result->masks[i].data.size();
fd_c_detection_result->masks.data[i].data.data =
new uint8_t[detection_result->masks[i].data.size()];
memcpy(fd_c_detection_result->masks.data[i].data.data,
detection_result->masks[i].data.data(),
sizeof(uint8_t) * detection_result->masks[i].data.size());
// copy shape in mask
fd_c_detection_result->masks.data[i].shape.size =
detection_result->masks[i].shape.size();
fd_c_detection_result->masks.data[i].shape.data =
new int64_t[detection_result->masks[i].shape.size()];
memcpy(fd_c_detection_result->masks.data[i].shape.data,
detection_result->masks[i].shape.data(),
sizeof(int64_t) * detection_result->masks[i].shape.size());
fd_c_detection_result->masks.data[i].type =
static_cast<FD_C_ResultType>(detection_result->masks[i].type);
}
fd_c_detection_result->contain_masks = detection_result->contain_masks;
fd_c_detection_result->type =
static_cast<FD_C_ResultType>(detection_result->type);
return fd_c_detection_result;
}

FD_C_Bool FD_C_PPYOLOEWrapperBatchPredict(
FD_C_PPYOLOEWrapper* fd_ppyoloe_wrapper, FD_C_OneDimMat imgs,
FD_C_OneDimDetectionResult* results) {
Expand Down
Loading

0 comments on commit b6e8773

Please sign in to comment.