diff --git a/c_api/fastdeploy_capi/config.h b/c_api/fastdeploy_capi/config.h deleted file mode 100755 index 73de04c446..0000000000 --- a/c_api/fastdeploy_capi/config.h +++ /dev/null @@ -1,22 +0,0 @@ -// 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/types_internal.cc b/c_api/fastdeploy_capi/types_internal.cc index 55d126e0b1..adb7a088e5 100644 --- a/c_api/fastdeploy_capi/types_internal.cc +++ b/c_api/fastdeploy_capi/types_internal.cc @@ -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: @@ -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& diff --git a/c_api/fastdeploy_capi/types_internal.h b/c_api/fastdeploy_capi/types_internal.h index f9b0d206e6..5549deca44 100644 --- a/c_api/fastdeploy_capi/types_internal.h +++ b/c_api/fastdeploy_capi/types_internal.h @@ -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 varname; \ @@ -49,6 +50,10 @@ std::unique_ptr varname; \ } FD_C_##typename##Wrapper +#define DEFINE_SEGMENTATION_MODEL_WRAPPER_STRUCT(typename, varname) typedef struct FD_C_##typename##Wrapper { \ + std::unique_ptr varname; \ +} FD_C_##typename##Wrapper + // ------------- belows are wrapper struct define --------------------- // // Results: @@ -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: @@ -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 --------------------- // @@ -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& \ +FD_C_CheckAndConvert##typename##Wrapper( \ + FD_C_##typename##Wrapper* varname) + namespace fastdeploy { @@ -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: @@ -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 @@ -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& \ +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; \ +} diff --git a/c_api/fastdeploy_capi/vision.h b/c_api/fastdeploy_capi/vision.h index 1b03eba0a3..2edb072041 100644 --- a/c_api/fastdeploy_capi/vision.h +++ b/c_api/fastdeploy_capi/vision.h @@ -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 diff --git a/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc b/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc index f88f594715..ecd0460ed2 100644 --- a/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc +++ b/c_api/fastdeploy_capi/vision/classification/ppcls/model.cc @@ -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; } @@ -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(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 imgs_vec; + std::vector results_wrapper_out; std::vector results_out; for (int i = 0; i < imgs.size; i++) { imgs_vec.push_back(*(reinterpret_cast(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); @@ -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; } diff --git a/c_api/fastdeploy_capi/vision/detection/ppdet/base_define.h b/c_api/fastdeploy_capi/vision/detection/ppdet/base_define.h index 807419788d..07336cc87c 100644 --- a/c_api/fastdeploy_capi/vision/detection/ppdet/base_define.h +++ b/c_api/fastdeploy_capi/vision/detection/ppdet/base_define.h @@ -61,11 +61,10 @@ 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); \ @@ -73,8 +72,11 @@ return model->Initialized(); #define IMPLEMENT_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) std::vector imgs_vec; \ std::vector results_out; \ + std::vector results_wrapper_out; \ for (int i = 0; i < imgs.size; i++) { \ imgs_vec.push_back(*(reinterpret_cast(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); \ @@ -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(\ diff --git a/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc b/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc index 68771edea2..5db1b91a1f 100644 --- a/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc +++ b/c_api/fastdeploy_capi/vision/detection/ppdet/model.cc @@ -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(detection_result->masks[i].type); - } - fd_c_detection_result->contain_masks = detection_result->contain_masks; - fd_c_detection_result->type = - static_cast(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) { diff --git a/c_api/fastdeploy_capi/vision/ocr/ppocr/model.cc b/c_api/fastdeploy_capi/vision/ocr/ppocr/model.cc index f9d8bba30c..0085287954 100644 --- a/c_api/fastdeploy_capi/vision/ocr/ppocr/model.cc +++ b/c_api/fastdeploy_capi/vision/ocr/ppocr/model.cc @@ -353,72 +353,26 @@ FD_C_Bool FD_C_PPOCRv2WrapperPredict(FD_C_PPOCRv2Wrapper* fd_c_ppocrv2_wrapper, bool successful = model->Predict(im, ocr_result.get()); if (successful) { - FD_C_OCRResult* res = FD_C_OCRResultWrapperGetData(fd_c_ocr_result_wrapper); - *fd_c_ocr_result = *res; + FD_C_OCRResultWrapperToCResult(fd_c_ocr_result_wrapper, fd_c_ocr_result); } + FD_C_DestroyOCRResultWrapper(fd_c_ocr_result_wrapper); return successful; } PIPELINE_DECLARE_AND_IMPLEMENT_INITIALIZED_FUNCTION(PPOCRv2, fd_c_ppocrv2_wrapper) -FD_C_OCRResult* FD_C_OCRResultToC(fastdeploy::vision::OCRResult* ocr_result) { - // Internal use, transfer fastdeploy::vision::OCRResult to - // FD_C_OCRResult - FD_C_OCRResult* fd_c_ocr_result = new FD_C_OCRResult(); - // copy boxes - const int boxes_coordinate_dim = 8; - fd_c_ocr_result->boxes.size = ocr_result->boxes.size(); - fd_c_ocr_result->boxes.data = - new FD_C_OneDimArrayInt32[fd_c_ocr_result->boxes.size]; - for (size_t i = 0; i < ocr_result->boxes.size(); i++) { - fd_c_ocr_result->boxes.data[i].size = boxes_coordinate_dim; - fd_c_ocr_result->boxes.data[i].data = new int[boxes_coordinate_dim]; - for (size_t j = 0; j < boxes_coordinate_dim; j++) { - fd_c_ocr_result->boxes.data[i].data[j] = ocr_result->boxes[i][j]; - } - } - // copy text - fd_c_ocr_result->text.size = ocr_result->text.size(); - fd_c_ocr_result->text.data = new FD_C_Cstr[fd_c_ocr_result->text.size]; - for (size_t i = 0; i < ocr_result->text.size(); i++) { - fd_c_ocr_result->text.data[i].size = ocr_result->text[i].length(); - fd_c_ocr_result->text.data[i].data = - new char[ocr_result->text[i].length() + 1]; - strncpy(fd_c_ocr_result->text.data[i].data, ocr_result->text[i].c_str(), - ocr_result->text[i].length()); - } - - // copy rec_scores - fd_c_ocr_result->rec_scores.size = ocr_result->rec_scores.size(); - fd_c_ocr_result->rec_scores.data = - new float[fd_c_ocr_result->rec_scores.size]; - memcpy(fd_c_ocr_result->rec_scores.data, ocr_result->rec_scores.data(), - sizeof(float) * fd_c_ocr_result->rec_scores.size); - // copy cls_scores - fd_c_ocr_result->cls_scores.size = ocr_result->cls_scores.size(); - fd_c_ocr_result->cls_scores.data = - new float[fd_c_ocr_result->cls_scores.size]; - memcpy(fd_c_ocr_result->cls_scores.data, ocr_result->cls_scores.data(), - sizeof(float) * fd_c_ocr_result->cls_scores.size); - // copy cls_labels - fd_c_ocr_result->cls_labels.size = ocr_result->cls_labels.size(); - fd_c_ocr_result->cls_labels.data = - new int32_t[fd_c_ocr_result->cls_labels.size]; - memcpy(fd_c_ocr_result->cls_labels.data, ocr_result->cls_labels.data(), - sizeof(int32_t) * fd_c_ocr_result->cls_labels.size); - // copy type - fd_c_ocr_result->type = static_cast(ocr_result->type); - return fd_c_ocr_result; -} - FD_C_Bool FD_C_PPOCRv2WrapperBatchPredict( FD_C_PPOCRv2Wrapper* fd_c_ppocrv2_wrapper, FD_C_OneDimMat imgs, FD_C_OneDimOCRResult* results) { std::vector imgs_vec; + std::vector results_wrapper_out; std::vector results_out; for (int i = 0; i < imgs.size; i++) { imgs_vec.push_back(*(reinterpret_cast(imgs.data[i]))); + FD_C_OCRResultWrapper* fd_ocr_result_wrapper = + FD_C_CreateOCRResultWrapper(); + results_wrapper_out.push_back(fd_ocr_result_wrapper); } auto& model = CHECK_AND_CONVERT_FD_TYPE(PPOCRv2Wrapper, fd_c_ppocrv2_wrapper); bool successful = model->BatchPredict(imgs_vec, &results_out); @@ -427,9 +381,14 @@ FD_C_Bool FD_C_PPOCRv2WrapperBatchPredict( results->size = results_out.size(); results->data = new FD_C_OCRResult[results->size]; for (int i = 0; i < results_out.size(); i++) { - results->data[i] = *FD_C_OCRResultToC(&results_out[i]); + (*CHECK_AND_CONVERT_FD_TYPE(OCRResultWrapper, results_wrapper_out[i])) = + std::move(results_out[i]); + FD_C_OCRResultWrapperToCResult(results_wrapper_out[i], &results->data[i]); } } + for (int i = 0; i < results_out.size(); i++) { + FD_C_DestroyOCRResultWrapper(results_wrapper_out[i]); + } return successful; } @@ -468,9 +427,9 @@ FD_C_Bool FD_C_PPOCRv3WrapperPredict(FD_C_PPOCRv3Wrapper* fd_c_ppocrv3_wrapper, bool successful = model->Predict(im, ocr_result.get()); if (successful) { - FD_C_OCRResult* res = FD_C_OCRResultWrapperGetData(fd_c_ocr_result_wrapper); - *fd_c_ocr_result = *res; + FD_C_OCRResultWrapperToCResult(fd_c_ocr_result_wrapper, fd_c_ocr_result); } + FD_C_DestroyOCRResultWrapper(fd_c_ocr_result_wrapper); return successful; } @@ -481,9 +440,13 @@ FD_C_Bool FD_C_PPOCRv3WrapperBatchPredict( FD_C_PPOCRv3Wrapper* fd_c_ppocrv3_wrapper, FD_C_OneDimMat imgs, FD_C_OneDimOCRResult* results) { std::vector imgs_vec; + std::vector results_wrapper_out; std::vector results_out; for (int i = 0; i < imgs.size; i++) { imgs_vec.push_back(*(reinterpret_cast(imgs.data[i]))); + FD_C_OCRResultWrapper* fd_ocr_result_wrapper = + FD_C_CreateOCRResultWrapper(); + results_wrapper_out.push_back(fd_ocr_result_wrapper); } auto& model = CHECK_AND_CONVERT_FD_TYPE(PPOCRv3Wrapper, fd_c_ppocrv3_wrapper); bool successful = model->BatchPredict(imgs_vec, &results_out); @@ -492,9 +455,14 @@ FD_C_Bool FD_C_PPOCRv3WrapperBatchPredict( results->size = results_out.size(); results->data = new FD_C_OCRResult[results->size]; for (int i = 0; i < results_out.size(); i++) { - results->data[i] = *FD_C_OCRResultToC(&results_out[i]); + (*CHECK_AND_CONVERT_FD_TYPE(OCRResultWrapper, results_wrapper_out[i])) = + std::move(results_out[i]); + FD_C_OCRResultWrapperToCResult(results_wrapper_out[i], &results->data[i]); } } + for (int i = 0; i < results_out.size(); i++) { + FD_C_DestroyOCRResultWrapper(results_wrapper_out[i]); + } return successful; } diff --git a/c_api/fastdeploy_capi/vision/result.cc b/c_api/fastdeploy_capi/vision/result.cc index f8dfd5cdf0..197d3b0f05 100644 --- a/c_api/fastdeploy_capi/vision/result.cc +++ b/c_api/fastdeploy_capi/vision/result.cc @@ -47,11 +47,11 @@ void FD_C_DestroyClassifyResult( delete fd_c_classify_result; } -FD_C_ClassifyResult* FD_C_ClassifyResultWrapperGetData( - __fd_keep FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper) { +void FD_C_ClassifyResultWrapperToCResult( + __fd_keep FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper, + __fd_keep FD_C_ClassifyResult* fd_c_classify_result_data) { auto& classify_result = CHECK_AND_CONVERT_FD_TYPE( ClassifyResultWrapper, fd_c_classify_result_wrapper); - 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 = @@ -67,10 +67,10 @@ FD_C_ClassifyResult* FD_C_ClassifyResultWrapperGetData( sizeof(float) * fd_c_classify_result_data->scores.size); fd_c_classify_result_data->type = static_cast(classify_result->type); - return fd_c_classify_result_data; + return; } -FD_C_ClassifyResultWrapper* FD_C_CreateClassifyResultWrapperFromData( +FD_C_ClassifyResultWrapper* FD_C_CreateClassifyResultWrapperFromCResult( __fd_keep FD_C_ClassifyResult* fd_c_classify_result) { FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper = FD_C_CreateClassifyResultWrapper(); @@ -90,14 +90,16 @@ FD_C_ClassifyResultWrapper* FD_C_CreateClassifyResultWrapperFromData( return fd_c_classify_result_wrapper; } -char* FD_C_ClassifyResultWrapperStr( - FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper) { +void FD_C_ClassifyResultStr(FD_C_ClassifyResult* fd_c_classify_result, + char* str_buffer) { + FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper = + FD_C_CreateClassifyResultWrapperFromCResult(fd_c_classify_result); auto& classify_result = CHECK_AND_CONVERT_FD_TYPE( ClassifyResultWrapper, fd_c_classify_result_wrapper); std::string information = classify_result->Str(); - char* cstr = new char[information.length() + 1]; - std::strcpy(cstr, information.c_str()); - return cstr; + std::strcpy(str_buffer, information.c_str()); + FD_C_DestroyClassifyResultWrapper(fd_c_classify_result_wrapper); + return; } // Detection Results @@ -136,11 +138,11 @@ void FD_C_DestroyDetectionResult( delete fd_c_detection_result; } -FD_C_DetectionResult* FD_C_DetectionResultWrapperGetData( - __fd_keep FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) { +void FD_C_DetectionResultWrapperToCResult( + __fd_keep FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper, + __fd_keep FD_C_DetectionResult* fd_c_detection_result) { auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( DetectionResultWrapper, fd_c_detection_result_wrapper); - 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(); @@ -194,10 +196,10 @@ FD_C_DetectionResult* FD_C_DetectionResultWrapperGetData( fd_c_detection_result->contain_masks = detection_result->contain_masks; fd_c_detection_result->type = static_cast(detection_result->type); - return fd_c_detection_result; + return; } -FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapperFromData( +FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapperFromCResult( __fd_keep FD_C_DetectionResult* fd_c_detection_result) { FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = FD_C_CreateDetectionResultWrapper(); @@ -248,14 +250,16 @@ FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapperFromData( return fd_c_detection_result_wrapper; } -char* FD_C_DetectionResultWrapperStr( - FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper) { +void FD_C_DetectionResultStr(FD_C_DetectionResult* fd_c_detection_result, + char* str_buffer) { + FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = + FD_C_CreateDetectionResultWrapperFromCResult(fd_c_detection_result); auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( DetectionResultWrapper, fd_c_detection_result_wrapper); std::string information = detection_result->Str(); - char* cstr = new char[information.length() + 1]; - std::strcpy(cstr, information.c_str()); - return cstr; + std::strcpy(str_buffer, information.c_str()); + FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); + return; } // OCR Results @@ -294,11 +298,11 @@ void FD_C_DestroyOCRResult(__fd_take FD_C_OCRResult* fd_c_ocr_result) { delete fd_c_ocr_result; } -FD_C_OCRResult* FD_C_OCRResultWrapperGetData( - __fd_keep FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper) { +void FD_C_OCRResultWrapperToCResult( + __fd_keep FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper, + __fd_keep FD_C_OCRResult* fd_c_ocr_result) { auto& ocr_result = CHECK_AND_CONVERT_FD_TYPE(OCRResultWrapper, fd_c_ocr_result_wrapper); - FD_C_OCRResult* fd_c_ocr_result = new FD_C_OCRResult(); // copy boxes const int boxes_coordinate_dim = 8; fd_c_ocr_result->boxes.size = ocr_result->boxes.size(); @@ -342,10 +346,10 @@ FD_C_OCRResult* FD_C_OCRResultWrapperGetData( sizeof(int32_t) * fd_c_ocr_result->cls_labels.size); // copy type fd_c_ocr_result->type = static_cast(ocr_result->type); - return fd_c_ocr_result; + return; } -FD_C_OCRResultWrapper* FD_C_CreateOCRResultWrapperFromData( +FD_C_OCRResultWrapper* FD_C_CreateOCRResultWrapperFromCResult( __fd_keep FD_C_OCRResult* fd_c_ocr_result) { FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper = FD_C_CreateOCRResultWrapper(); @@ -389,13 +393,131 @@ FD_C_OCRResultWrapper* FD_C_CreateOCRResultWrapperFromData( return fd_c_ocr_result_wrapper; } -char* FD_C_OCRResultWrapperStr(FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper) { +void FD_C_OCRResultStr(FD_C_OCRResult* fd_c_ocr_result, char* str_buffer) { + FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper = + FD_C_CreateOCRResultWrapperFromCResult(fd_c_ocr_result); auto& ocr_result = CHECK_AND_CONVERT_FD_TYPE(OCRResultWrapper, fd_c_ocr_result_wrapper); std::string information = ocr_result->Str(); - char* cstr = new char[information.length() + 1]; - std::strcpy(cstr, information.c_str()); - return cstr; + std::strcpy(str_buffer, information.c_str()); + FD_C_DestroyOCRResultWrapper(fd_c_ocr_result_wrapper); + return; +} + +// Segmentation Results + +FD_C_SegmentationResultWrapper* FD_C_CreateSegmentationResultWrapper() { + FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper = + new FD_C_SegmentationResultWrapper(); + fd_c_segmentation_result_wrapper->segmentation_result = + std::unique_ptr( + new fastdeploy::vision::SegmentationResult()); + return fd_c_segmentation_result_wrapper; +} + +void FD_C_DestroySegmentationResultWrapper( + __fd_take FD_C_SegmentationResultWrapper* + fd_c_segmentation_result_wrapper) { + delete fd_c_segmentation_result_wrapper; +} + +void FD_C_DestroySegmentationResult( + __fd_take FD_C_SegmentationResult* fd_c_segmentation_result) { + if (fd_c_segmentation_result == nullptr) return; + // delete label_map + delete[] fd_c_segmentation_result->label_map.data; + // delete score_map + delete[] fd_c_segmentation_result->score_map.data; + // delete shape + delete[] fd_c_segmentation_result->shape.data; + delete fd_c_segmentation_result; +} + +void FD_C_SegmentationResultWrapperToCResult( + __fd_keep FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper, + __fd_keep FD_C_SegmentationResult* fd_c_segmentation_result) { + auto& segmentation_result = CHECK_AND_CONVERT_FD_TYPE( + SegmentationResultWrapper, fd_c_segmentation_result_wrapper); + + // copy label_map + fd_c_segmentation_result->label_map.size = + segmentation_result->label_map.size(); + fd_c_segmentation_result->label_map.data = + new uint8_t[fd_c_segmentation_result->label_map.size]; + memcpy(fd_c_segmentation_result->label_map.data, + segmentation_result->label_map.data(), + sizeof(uint8_t) * fd_c_segmentation_result->label_map.size); + // copy score_map + fd_c_segmentation_result->score_map.size = + segmentation_result->score_map.size(); + fd_c_segmentation_result->score_map.data = + new float[fd_c_segmentation_result->score_map.size]; + memcpy(fd_c_segmentation_result->score_map.data, + segmentation_result->score_map.data(), + sizeof(float) * fd_c_segmentation_result->score_map.size); + // copy shape + fd_c_segmentation_result->shape.size = segmentation_result->shape.size(); + fd_c_segmentation_result->shape.data = + new int64_t[fd_c_segmentation_result->shape.size]; + memcpy(fd_c_segmentation_result->shape.data, + segmentation_result->shape.data(), + sizeof(int64_t) * fd_c_segmentation_result->shape.size); + // copy contain_score_map + fd_c_segmentation_result->contain_score_map = + segmentation_result->contain_score_map; + // copy type + fd_c_segmentation_result->type = + static_cast(segmentation_result->type); + return; +} + +FD_C_SegmentationResultWrapper* FD_C_CreateSegmentationResultWrapperFromCResult( + __fd_keep FD_C_SegmentationResult* fd_c_segmentation_result) { + FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper = + FD_C_CreateSegmentationResultWrapper(); + auto& segmentation_result = CHECK_AND_CONVERT_FD_TYPE( + SegmentationResultWrapper, fd_c_segmentation_result_wrapper); + + // copy label_map + segmentation_result->label_map.resize( + fd_c_segmentation_result->label_map.size); + memcpy(segmentation_result->label_map.data(), + fd_c_segmentation_result->label_map.data, + sizeof(uint8_t) * fd_c_segmentation_result->label_map.size); + + // copy score_map + segmentation_result->score_map.resize( + fd_c_segmentation_result->score_map.size); + memcpy(segmentation_result->score_map.data(), + fd_c_segmentation_result->score_map.data, + sizeof(float) * fd_c_segmentation_result->score_map.size); + + // copy shape + segmentation_result->shape.resize(fd_c_segmentation_result->shape.size); + memcpy(segmentation_result->shape.data(), + fd_c_segmentation_result->shape.data, + sizeof(int64_t) * fd_c_segmentation_result->shape.size); + + // copy contain_score_map + segmentation_result->contain_score_map = + fd_c_segmentation_result->contain_score_map; + // copy type + segmentation_result->type = static_cast( + fd_c_segmentation_result->type); + + return fd_c_segmentation_result_wrapper; +} + +void FD_C_SegmentationResultStr( + FD_C_SegmentationResult* fd_c_segmentation_result, char* str_buffer) { + FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper = + FD_C_CreateSegmentationResultWrapperFromCResult(fd_c_segmentation_result); + auto& segmentation_result = CHECK_AND_CONVERT_FD_TYPE( + SegmentationResultWrapper, fd_c_segmentation_result_wrapper); + std::string information = segmentation_result->Str(); + std::strcpy(str_buffer, information.c_str()); + FD_C_DestroySegmentationResultWrapper(fd_c_segmentation_result_wrapper); + return; } #ifdef __cplusplus diff --git a/c_api/fastdeploy_capi/vision/result.h b/c_api/fastdeploy_capi/vision/result.h index 7da7d15388..320e4ba335 100644 --- a/c_api/fastdeploy_capi/vision/result.h +++ b/c_api/fastdeploy_capi/vision/result.h @@ -20,6 +20,7 @@ typedef struct FD_C_ClassifyResultWrapper FD_C_ClassifyResultWrapper; typedef struct FD_C_DetectionResultWrapper FD_C_DetectionResultWrapper; typedef struct FD_C_OCRResultWrapper FD_C_OCRResultWrapper; +typedef struct FD_C_SegmentationResultWrapper FD_C_SegmentationResultWrapper; #ifdef __cplusplus extern "C" { @@ -76,6 +77,20 @@ typedef struct FD_C_OneDimOCRResult { FD_C_OCRResult* data; } FD_C_OneDimOCRResult; +typedef struct FD_C_SegmentationResult { + FD_C_OneDimArrayUint8 label_map; + FD_C_OneDimArrayFloat score_map; + FD_C_OneDimArrayInt64 shape; + FD_C_Bool contain_score_map; + FD_C_ResultType type; +} FD_C_SegmentationResult; + +typedef struct FD_C_OneDimSegmentationResult { + size_t size; + FD_C_SegmentationResult* data; +} FD_C_OneDimSegmentationResult; + + // Classification Results /** \brief Create a new FD_C_ClassifyResultWrapper object @@ -105,11 +120,13 @@ FD_C_DestroyClassifyResult(__fd_take FD_C_ClassifyResult* fd_c_classify_result); /** \brief Get a FD_C_ClassifyResult object from FD_C_ClassifyResultWrapper object * * \param[in] fd_c_classify_result_wrapper pointer to FD_C_ClassifyResultWrapper object - * \return Return a pointer to FD_C_ClassifyResult object + * \param[out] fd_c_classify_result pointer to FD_C_ClassifyResult object used to store data */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_ClassifyResult* -FD_C_ClassifyResultWrapperGetData( - __fd_keep FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_ClassifyResultWrapperToCResult( + __fd_keep FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper, + __fd_keep FD_C_ClassifyResult* fd_c_classify_result); + /** \brief Create a new FD_C_ClassifyResultWrapper object from FD_C_ClassifyResult object * @@ -118,18 +135,19 @@ FD_C_ClassifyResultWrapperGetData( */ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_ClassifyResultWrapper* -FD_C_CreateClassifyResultWrapperFromData( +FD_C_CreateClassifyResultWrapperFromCResult( __fd_keep FD_C_ClassifyResult* fd_c_classify_result); /** \brief Print ClassifyResult formated information * - * \param[in] fd_c_classify_result_wrapper pointer to FD_C_ClassifyResultWrapper object - * \return Return a string pointer + * \param[in] fd_c_classify_result pointer to FD_C_ClassifyResult object + * \param[out] str_buffer used to store string */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give char* -FD_C_ClassifyResultWrapperStr( - __fd_keep FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_ClassifyResultStr( + __fd_keep FD_C_ClassifyResult* fd_c_classify_result, char* str_buffer); + // Detection Results @@ -160,11 +178,12 @@ FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyDetectionResult( /** \brief Get a FD_C_DetectionResult object from FD_C_DetectionResultWrapper object * * \param[in] fd_c_detection_result_wrapper pointer to FD_C_DetectionResultWrapper object - * \return Return a pointer to FD_C_DetectionResult object + * \param[out] fd_c_detection_result pointer to FD_C_DetectionResult object used to store data */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_DetectionResult* -FD_C_DetectionResultWrapperGetData( - __fd_keep FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_DetectionResultWrapperToCResult( + __fd_keep FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper, + __fd_keep FD_C_DetectionResult* fd_c_detection_result); /** \brief Create a new FD_C_DetectionResultWrapper object from FD_C_DetectionResult object * @@ -173,18 +192,19 @@ FD_C_DetectionResultWrapperGetData( */ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_DetectionResultWrapper* -FD_C_CreateDetectionResultWrapperFromData( +FD_C_CreateDetectionResultWrapperFromCResult( __fd_keep FD_C_DetectionResult* fd_c_detection_result); + /** \brief Print DetectionResult formated information * - * \param[in] fd_c_detection_result_wrapper pointer to FD_C_DetectionResultWrapper object - * \return Return a string pointer + * \param[in] fd_c_detection_result pointer to FD_C_DetectionResult object + * \param[out] str_buffer used to store string */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give char* -FD_C_DetectionResultWrapperStr( - __fd_keep FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern void +FD_C_DetectionResultStr( + __fd_keep FD_C_DetectionResult* fd_c_detection_result, char* str_buffer); // OCR Results @@ -216,11 +236,12 @@ FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyOCRResult( /** \brief Get a FD_C_OCRResult object from FD_C_OCRResultWrapper object * * \param[in] fd_c_ocr_result_wrapper pointer to FD_C_OCRResultWrapper object - * \return Return a pointer to FD_C_OCRResult object + * \param[out] fd_c_ocr_result pointer to FD_C_OCRResult object used to store data */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_OCRResult* -FD_C_OCRResultWrapperGetData( - __fd_keep FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_OCRResultWrapperToCResult( + __fd_keep FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper, + __fd_keep FD_C_OCRResult* fd_c_ocr_result); /** \brief Create a new FD_C_OCRResultWrapper object from FD_C_OCRResult object * @@ -229,18 +250,76 @@ FD_C_OCRResultWrapperGetData( */ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_OCRResultWrapper* -FD_C_CreateOCRResultWrapperFromData( +FD_C_CreateOCRResultWrapperFromCResult( __fd_keep FD_C_OCRResult* fd_c_ocr_result); /** \brief Print OCRResult formated information * - * \param[in] fd_c_ocr_result_wrapper pointer to FD_C_OCRResultWrapper object - * \return Return a string pointer + * \param[in] fd_c_ocr_result pointer to FD_C_OCRResult object + * \param[out] str_buffer used to store string */ -FASTDEPLOY_CAPI_EXPORT extern __fd_give char* -FD_C_OCRResultWrapperStr( - __fd_keep FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper); +FASTDEPLOY_CAPI_EXPORT extern void +FD_C_OCRResultStr( + __fd_keep FD_C_OCRResult* fd_c_ocr_result, char* str_buffer); + + +// Segmentation Results + +/** \brief Create a new FD_C_SegmentationResultWrapper object + * + * \return Return a pointer to FD_C_SegmentationResultWrapper object + */ + +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_SegmentationResultWrapper* +FD_C_CreateSegmentationResultWrapper(); + +/** \brief Destroy a FD_C_SegmentationResultWrapper object + * + * \param[in] fd_c_segmentation_result_wrapper pointer to FD_C_SegmentationResultWrapper object + */ + +FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroySegmentationResultWrapper( + __fd_take FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper); + +/** \brief Destroy a FD_C_SegmentationResult object + * + * \param[in] fd_c_segmentation_result pointer to FD_C_SegmentationResult object + */ + +FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroySegmentationResult( + __fd_take FD_C_SegmentationResult* fd_c_segmentation_result); + +/** \brief Get a FD_C_SegmentationResult object from FD_C_SegmentationResultWrapper object + * + * \param[in] fd_c_segmentation_result_wrapper pointer to FD_C_SegmentationResultWrapper object + * \param[out] fd_c_segmentation_result pointer to FD_C_SegmentationResult object used to store data + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_SegmentationResultWrapperToCResult( + __fd_keep FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper, + __fd_keep FD_C_SegmentationResult* fd_c_segmentation_result); + +/** \brief Create a new FD_C_SegmentationResultWrapper object from FD_C_SegmentationResult object + * + * \param[in] fd_c_segmentation_result pointer to FD_C_SegmentationResult object + * \return Return a pointer to FD_C_SegmentationResultWrapper object + */ + +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_SegmentationResultWrapper* +FD_C_CreateSegmentationResultWrapperFromCResult( + __fd_keep FD_C_SegmentationResult* fd_c_segmentation_result); + +/** \brief Print SegmentationResult formated information + * + * \param[in] fd_c_segmentation_result pointer to FD_C_SegmentationResult object + * \param[out] str_buffer used to store string + */ + +FASTDEPLOY_CAPI_EXPORT extern __fd_give void +FD_C_SegmentationResultStr( + __fd_keep FD_C_SegmentationResult* fd_c_segmentation_result, char* str_buffer); + #ifdef __cplusplus diff --git a/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.cc b/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.cc new file mode 100644 index 0000000000..f3f69dae36 --- /dev/null +++ b/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.cc @@ -0,0 +1,107 @@ +// 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/vision/segmentation/ppseg/model.h" + +#include "fastdeploy_capi/types_internal.h" + +#ifdef __cplusplus +extern "C" { +#endif + +FD_C_PaddleSegModelWrapper* FD_C_CreatePaddleSegModelWrapper( + const char* model_file, const char* params_file, const char* config_file, + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, + const FD_C_ModelFormat model_format) { + auto& runtime_option = CHECK_AND_CONVERT_FD_TYPE(RuntimeOptionWrapper, + fd_c_runtime_option_wrapper); + FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper = + new FD_C_PaddleSegModelWrapper(); + fd_c_paddleseg_model_wrapper->segmentation_model = + std::unique_ptr( + new fastdeploy::vision::segmentation::PaddleSegModel( + std::string(model_file), std::string(params_file), + std::string(config_file), *runtime_option, + static_cast(model_format))); + return fd_c_paddleseg_model_wrapper; +} + +void FD_C_DestroyPaddleSegModelWrapper( + FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper) { + delete fd_c_paddleseg_model_wrapper; +} + +FD_C_Bool FD_C_PaddleSegModelWrapperPredict( + FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper, FD_C_Mat img, + FD_C_SegmentationResult* fd_c_segmentation_result) { + cv::Mat* im = reinterpret_cast(img); + auto& paddleseg_model = CHECK_AND_CONVERT_FD_TYPE( + PaddleSegModelWrapper, fd_c_paddleseg_model_wrapper); + FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper = + FD_C_CreateSegmentationResultWrapper(); + auto& segmentation_result = CHECK_AND_CONVERT_FD_TYPE( + SegmentationResultWrapper, fd_c_segmentation_result_wrapper); + + bool successful = paddleseg_model->Predict(im, segmentation_result.get()); + if (successful) { + FD_C_SegmentationResultWrapperToCResult(fd_c_segmentation_result_wrapper, + fd_c_segmentation_result); + } + FD_C_DestroySegmentationResultWrapper(fd_c_segmentation_result_wrapper); + return successful; +} + +FD_C_Bool FD_C_PaddleSegModelWrapperInitialized( + FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper) { + auto& paddleseg_model = CHECK_AND_CONVERT_FD_TYPE( + PaddleSegModelWrapper, fd_c_paddleseg_model_wrapper); + return paddleseg_model->Initialized(); +} + +FD_C_Bool FD_C_PaddleSegModelWrapperBatchPredict( + FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper, + FD_C_OneDimMat imgs, FD_C_OneDimSegmentationResult* results) { + std::vector imgs_vec; + std::vector results_wrapper_out; + std::vector results_out; + for (int i = 0; i < imgs.size; i++) { + imgs_vec.push_back(*(reinterpret_cast(imgs.data[i]))); + FD_C_SegmentationResultWrapper* fd_segmentation_result_wrapper = + FD_C_CreateSegmentationResultWrapper(); + results_wrapper_out.push_back(fd_segmentation_result_wrapper); + } + auto& paddleseg_model = CHECK_AND_CONVERT_FD_TYPE( + PaddleSegModelWrapper, fd_c_paddleseg_model_wrapper); + bool successful = paddleseg_model->BatchPredict(imgs_vec, &results_out); + if (successful) { + // copy results back to FD_C_OneDimSegmentationResult + results->size = results_out.size(); + results->data = new FD_C_SegmentationResult[results->size]; + for (int i = 0; i < results_out.size(); i++) { + (*CHECK_AND_CONVERT_FD_TYPE(SegmentationResultWrapper, + results_wrapper_out[i])) = + std::move(results_out[i]); + FD_C_SegmentationResultWrapperToCResult(results_wrapper_out[i], + &results->data[i]); + } + } + for (int i = 0; i < results_out.size(); i++) { + FD_C_DestroySegmentationResultWrapper(results_wrapper_out[i]); + } + return successful; +} + +#ifdef __cplusplus +} +#endif diff --git a/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.h b/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.h new file mode 100644 index 0000000000..cf2a83ba5b --- /dev/null +++ b/c_api/fastdeploy_capi/vision/segmentation/ppseg/model.h @@ -0,0 +1,90 @@ +// 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/fd_common.h" +#include "fastdeploy_capi/fd_type.h" +#include "fastdeploy_capi/runtime_option.h" +#include "fastdeploy_capi/vision/result.h" + +typedef struct FD_C_PaddleSegModelWrapper FD_C_PaddleSegModelWrapper; + +#ifdef __cplusplus +extern "C" { +#endif + +/** \brief Create a new FD_C_PaddleSegModelWrapper object + * + * \param[in] model_file Path of model file, e.g net/model.pdmodel + * \param[in] params_file Path of parameter file, e.g unet/model.pdiparams, if the model format is ONNX, this parameter will be ignored + * \param[in] config_file Path of configuration file for deployment, e.g unet/deploy.yml + * \param[in] fd_c_runtime_option_wrapper RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends` + * \param[in] model_format Model format of the loaded model, default is Paddle format + * + * \return Return a pointer to FD_C_PaddleSegModelWrapper object + */ + +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_PaddleSegModelWrapper* +FD_C_CreatePaddleSegModelWrapper( + const char* model_file, const char* params_file, const char* config_file, + FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, + const FD_C_ModelFormat model_format); + +/** \brief Destroy a FD_C_PaddleSegModelWrapper object + * + * \param[in] fd_c_paddleseg_model_wrapper pointer to FD_C_PaddleSegModelWrapper object + */ + +FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyPaddleSegModelWrapper( + __fd_take FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper); + +/** \brief Predict the segmentation result for an input image + * + * \param[in] fd_c_paddleseg_model_wrapper pointer to FD_C_PaddleSegModelWrapper object + * \param[in] img pointer to cv::Mat image + * \param[in] fd_c_segmentation_result pointer to FD_C_SegmentationResult object, which stores the result. + */ + +FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PaddleSegModelWrapperPredict( + __fd_keep FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper, + FD_C_Mat img, FD_C_SegmentationResult* fd_c_segmentation_result); + +/** \brief Check if the model is initialized successfully + * + * \param[in] fd_c_paddleseg_model_wrapper pointer to FD_C_PaddleSegModelWrapper object + * + * \return Return a bool of value true if initialized successfully + */ + +FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PaddleSegModelWrapperInitialized( + __fd_keep FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper); + + +/** \brief Predict the segmentation results for a batch of input images + * + * \param[in] fd_c_paddleseg_model_wrapper pointer to FD_C_PaddleSegModelWrapper object + * \param[in] imgs The input image list, each element comes from cv::imread() + * \param[in] results The output segmentation result list + * \return true if the prediction successed, otherwise false + */ +FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PaddleSegModelWrapperBatchPredict( + __fd_keep FD_C_PaddleSegModelWrapper* fd_c_paddleseg_model_wrapper, + FD_C_OneDimMat imgs, + FD_C_OneDimSegmentationResult* results); + + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/c_api/fastdeploy_capi/vision/visualize.cc b/c_api/fastdeploy_capi/vision/visualize.cc index ab138c3a92..dbe3f6dba7 100644 --- a/c_api/fastdeploy_capi/vision/visualize.cc +++ b/c_api/fastdeploy_capi/vision/visualize.cc @@ -26,12 +26,13 @@ FD_C_Mat FD_C_VisDetection(FD_C_Mat im, float score_threshold, int line_size, float font_size) { FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = - FD_C_CreateDetectionResultWrapperFromData(fd_c_detection_result); + FD_C_CreateDetectionResultWrapperFromCResult(fd_c_detection_result); auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( DetectionResultWrapper, fd_c_detection_result_wrapper); cv::Mat result = fastdeploy::vision::VisDetection( *(reinterpret_cast(im)), *detection_result, score_threshold, line_size, font_size); + FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); return new cv::Mat(result); } @@ -45,12 +46,13 @@ FD_C_Mat FD_C_VisDetectionWithLabel(FD_C_Mat im, labels_in.emplace_back(labels->data[i].data); } FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = - FD_C_CreateDetectionResultWrapperFromData(fd_c_detection_result); + FD_C_CreateDetectionResultWrapperFromCResult(fd_c_detection_result); auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( DetectionResultWrapper, fd_c_detection_result_wrapper); cv::Mat result = fastdeploy::vision::VisDetection( *(reinterpret_cast(im)), *detection_result, labels_in, score_threshold, line_size, font_size); + FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); return new cv::Mat(result); } @@ -59,12 +61,13 @@ FD_C_Mat FD_C_VisClassification(FD_C_Mat im, int top_k, float score_threshold, float font_size) { FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper = - FD_C_CreateClassifyResultWrapperFromData(fd_c_classify_result); + FD_C_CreateClassifyResultWrapperFromCResult(fd_c_classify_result); auto& classify_result = CHECK_AND_CONVERT_FD_TYPE( ClassifyResultWrapper, fd_c_classify_result_wrapper); cv::Mat result = fastdeploy::vision::VisClassification( *(reinterpret_cast(im)), *classify_result, top_k, score_threshold, font_size); + FD_C_DestroyClassifyResultWrapper(fd_c_classify_result_wrapper); return new cv::Mat(result); } @@ -77,22 +80,37 @@ FD_C_Mat FD_C_VisClassificationWithLabel( labels_in.emplace_back(labels->data[i].data); } FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper = - FD_C_CreateClassifyResultWrapperFromData(fd_c_classify_result); + FD_C_CreateClassifyResultWrapperFromCResult(fd_c_classify_result); auto& classify_result = CHECK_AND_CONVERT_FD_TYPE( ClassifyResultWrapper, fd_c_classify_result_wrapper); cv::Mat result = fastdeploy::vision::VisClassification( *(reinterpret_cast(im)), *classify_result, labels_in, top_k, score_threshold, font_size); + FD_C_DestroyClassifyResultWrapper(fd_c_classify_result_wrapper); return new cv::Mat(result); } FD_C_Mat FD_C_VisOcr(FD_C_Mat im, FD_C_OCRResult* fd_c_ocr_result) { FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper = - FD_C_CreateOCRResultWrapperFromData(fd_c_ocr_result); + FD_C_CreateOCRResultWrapperFromCResult(fd_c_ocr_result); auto& ocr_result = CHECK_AND_CONVERT_FD_TYPE(OCRResultWrapper, fd_c_ocr_result_wrapper); cv::Mat result = fastdeploy::vision::VisOcr(*(reinterpret_cast(im)), *ocr_result); + FD_C_DestroyOCRResultWrapper(fd_c_ocr_result_wrapper); + return new cv::Mat(result); +} + +FD_C_Mat FD_C_VisSegmentation(FD_C_Mat im, + FD_C_SegmentationResult* fd_c_segmenation_result, + float weight) { + FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper = + FD_C_CreateSegmentationResultWrapperFromCResult(fd_c_segmenation_result); + auto& segmentation_result = CHECK_AND_CONVERT_FD_TYPE( + SegmentationResultWrapper, fd_c_segmentation_result_wrapper); + cv::Mat result = fastdeploy::vision::VisSegmentation( + *(reinterpret_cast(im)), *segmentation_result, weight); + FD_C_DestroySegmentationResultWrapper(fd_c_segmentation_result_wrapper); return new cv::Mat(result); } diff --git a/c_api/fastdeploy_capi/vision/visualize.h b/c_api/fastdeploy_capi/vision/visualize.h index e30d2939f4..05a0a66926 100644 --- a/c_api/fastdeploy_capi/vision/visualize.h +++ b/c_api/fastdeploy_capi/vision/visualize.h @@ -89,6 +89,17 @@ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_VisClassificationWithLabel */ FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_VisOcr(FD_C_Mat im, FD_C_OCRResult* ocr_result); +/** \brief Show the visualized results for segmentation models + * + * \param[in] im the input image data, comes from cv::imread(), is a 3-D array with layout HWC, BGR format + * \param[in] result the result produced by model + * \param[in] weight transparent weight of visualized result image + * \return cv::Mat type stores the visualized results + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_Mat FD_C_VisSegmentation(FD_C_Mat im, + FD_C_SegmentationResult* result, + float weight); + #ifdef __cplusplus } // extern "C" diff --git a/examples/vision/classification/paddleclas/c/README.md b/examples/vision/classification/paddleclas/c/README.md index 4b125e4ab3..ed7493980d 100755 --- a/examples/vision/classification/paddleclas/c/README.md +++ b/examples/vision/classification/paddleclas/c/README.md @@ -150,31 +150,16 @@ FD_C_Bool FD_C_PaddleClasModelWrapperPredict( #### Result ```c -FD_C_ClassifyResultWrapper* FD_C_CreateClassifyResultWrapperFromData( - FD_C_ClassifyResult* fd_c_classify_result) +void FD_C_ClassifyResultStr( + FD_C_ClassifyResult* fd_c_classify_result, + char* str_buffer); ``` > -> Create a pointer to FD_C_ClassifyResultWrapper structure, which contains `fastdeploy::vision::ClassifyResult` object in C++. You can call methods in C++ ClassifyResult object by C API with this pointer. +> print result > > **Params** > * **fd_c_classify_result**(FD_C_ClassifyResult*): pointer to FD_C_ClassifyResult structure -> -> **Return** -> * **fd_c_classify_result_wrapper**(FD_C_ClassifyResultWrapper*): pointer to FD_C_ClassifyResultWrapper structure - - -```c -char* FD_C_ClassifyResultWrapperStr( - FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper); -``` -> -> Call Str() methods in `fastdeploy::vision::ClassifyResult` object contained in FD_C_ClassifyResultWrapper structure,and return a string to describe information in result. -> -> **Params** -> * **fd_c_classify_result_wrapper**(FD_C_ClassifyResultWrapper*): pointer to FD_C_ClassifyResultWrapper structure -> -> **Return** -> * **str**(char*): a string to describe information in result +> * **str_buffer**(char*): used to store result string - [Model Description](../../) diff --git a/examples/vision/classification/paddleclas/c/README_CN.md b/examples/vision/classification/paddleclas/c/README_CN.md index 1cf59d21d0..bae85e8ad3 100644 --- a/examples/vision/classification/paddleclas/c/README_CN.md +++ b/examples/vision/classification/paddleclas/c/README_CN.md @@ -153,32 +153,15 @@ FD_C_Bool FD_C_PaddleClasModelWrapperPredict( #### Predict结果 ```c -FD_C_ClassifyResultWrapper* FD_C_CreateClassifyResultWrapperFromData( - FD_C_ClassifyResult* fd_c_classify_result) +void FD_C_ClassifyResultStr( + FD_C_ClassifyResult* fd_c_classify_result, char* str_buffer); ``` > -> 创建一个FD_C_ClassifyResultWrapper对象的指针,FD_C_ClassifyResultWrapper中包含了C++的`fastdeploy::vision::ClassifyResult`对象,通过该指针,使用C API可以访问调用对应C++中的函数。 -> +> 打印结果 > > **参数** > * **fd_c_classify_result**(FD_C_ClassifyResult*): 指向FD_C_ClassifyResult对象的指针 -> -> **返回** -> * **fd_c_classify_result_wrapper**(FD_C_ClassifyResultWrapper*): 指向FD_C_ClassifyResultWrapper的指针 - - -```c -char* FD_C_ClassifyResultWrapperStr( - FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper); -``` -> -> 调用FD_C_ClassifyResultWrapper所包含的`fastdeploy::vision::ClassifyResult`对象的Str()方法,返回相关结果内数据信息的字符串。 -> -> **参数** -> * **fd_c_classify_result_wrapper**(FD_C_ClassifyResultWrapper*): 指向FD_C_ClassifyResultWrapper对象的指针 -> -> **返回** -> * **str**(char*): 表示结果数据信息的字符串 +> * **str_buffer**(char*): 保存结果数据信息的字符串 diff --git a/examples/vision/classification/paddleclas/c/infer.c b/examples/vision/classification/paddleclas/c/infer.c index 97a8659d35..a1570f3916 100644 --- a/examples/vision/classification/paddleclas/c/infer.c +++ b/examples/vision/classification/paddleclas/c/infer.c @@ -62,19 +62,11 @@ void CpuInfer(const char* model_dir, const char* image_file) { } // print res - // You can directly access fields in FD_C_ClassifyResult and print it refer to - // ClassifyResult API Doc Or you can wrap it using - // FD_C_ClassifyResult_Wrapper, which containes C++ structure - // fastdeploy::vision::ClassifyResult, and using C API - // FD_C_ClassifyResultWrapperStr to call - // fastdeploy::vision::ClassifyResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_ClassifyResultWrapper* result_wrapper = - FD_C_CreateClassifyResultWrapperFromData(result); - printf("%s", FD_C_ClassifyResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_ClassifyResultStr(result, res); + printf("%s", res); FD_C_DestroyRuntimeOptionWrapper(option); FD_C_DestroyPaddleClasModelWrapper(model); - FD_C_DestroyClassifyResultWrapper(result_wrapper); FD_C_DestroyClassifyResult(result); FD_C_DestroyMat(im); } @@ -118,19 +110,11 @@ void GpuInfer(const char* model_dir, const char* image_file) { } // print res - // You can directly access fields in FD_C_ClassifyResult and print it refer to - // ClassifyResult API Doc Or you can wrap it using - // FD_C_ClassifyResult_Wrapper, which containes C++ structure - // fastdeploy::vision::ClassifyResult, and using C API - // FD_C_ClassifyResultWrapperStr to call - // fastdeploy::vision::ClassifyResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_ClassifyResultWrapper* result_wrapper = - FD_C_CreateClassifyResultWrapperFromData(result); - printf("%s", FD_C_ClassifyResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_ClassifyResultStr(result, res); + printf("%s", res); FD_C_DestroyRuntimeOptionWrapper(option); FD_C_DestroyPaddleClasModelWrapper(model); - FD_C_DestroyClassifyResultWrapper(result_wrapper); FD_C_DestroyClassifyResult(result); FD_C_DestroyMat(im); } diff --git a/examples/vision/ocr/PP-OCRv2/c/infer.c b/examples/vision/ocr/PP-OCRv2/c/infer.c index d5918bf13a..625b044f73 100644 --- a/examples/vision/ocr/PP-OCRv2/c/infer.c +++ b/examples/vision/ocr/PP-OCRv2/c/infer.c @@ -112,16 +112,9 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, } // print res - // You can directly access fields in FD_C_OCRResult and print it refer to - // OCRResult API Doc Or you can wrap it using - // FD_C_OCRResult_Wrapper, which containes C++ structure - // fastdeploy::vision::OCRResult, and using C API - // FD_C_OCRResultWrapperStr to call - // fastdeploy::vision::OCRResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_OCRResultWrapper* result_wrapper = - FD_C_CreateOCRResultWrapperFromData(result); - printf("%s", FD_C_OCRResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_OCRResultStr(result, res); + printf("%s", res); FD_C_Mat vis_im = FD_C_VisOcr(im, result); FD_C_Imwrite("vis_result.jpg", vis_im); printf("Visualized result saved in ./vis_result.jpg\n"); @@ -133,9 +126,9 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_DestroyDBDetectorWrapper(det_model); FD_C_DestroyRecognizerWrapper(rec_model); FD_C_DestroyPPOCRv2Wrapper(ppocr_v2); - FD_C_DestroyOCRResultWrapper(result_wrapper); FD_C_DestroyOCRResult(result); FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); } void GpuInfer(const char* det_model_dir, const char* cls_model_dir, @@ -213,16 +206,9 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, } // print res - // You can directly access fields in FD_C_OCRResult and print it refer to - // OCRResult API Doc Or you can wrap it using - // FD_C_OCRResult_Wrapper, which containes C++ structure - // fastdeploy::vision::OCRResult, and using C API - // FD_C_OCRResultWrapperStr to call - // fastdeploy::vision::OCRResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_OCRResultWrapper* result_wrapper = - FD_C_CreateOCRResultWrapperFromData(result); - printf("%s", FD_C_OCRResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_OCRResultStr(result, res); + printf("%s", res); FD_C_Mat vis_im = FD_C_VisOcr(im, result); FD_C_Imwrite("vis_result.jpg", vis_im); printf("Visualized result saved in ./vis_result.jpg\n"); @@ -234,9 +220,9 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_DestroyDBDetectorWrapper(det_model); FD_C_DestroyRecognizerWrapper(rec_model); FD_C_DestroyPPOCRv2Wrapper(ppocr_v2); - FD_C_DestroyOCRResultWrapper(result_wrapper); FD_C_DestroyOCRResult(result); FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); } int main(int argc, char* argv[]) { if (argc < 7) { diff --git a/examples/vision/ocr/PP-OCRv3/c/infer.c b/examples/vision/ocr/PP-OCRv3/c/infer.c index 7c214f2209..e3b0c0f50d 100644 --- a/examples/vision/ocr/PP-OCRv3/c/infer.c +++ b/examples/vision/ocr/PP-OCRv3/c/infer.c @@ -112,16 +112,9 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, } // print res - // You can directly access fields in FD_C_OCRResult and print it refer to - // OCRResult API Doc Or you can wrap it using - // FD_C_OCRResult_Wrapper, which containes C++ structure - // fastdeploy::vision::OCRResult, and using C API - // FD_C_OCRResultWrapperStr to call - // fastdeploy::vision::OCRResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_OCRResultWrapper* result_wrapper = - FD_C_CreateOCRResultWrapperFromData(result); - printf("%s", FD_C_OCRResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_OCRResultStr(result, res); + printf("%s", res); FD_C_Mat vis_im = FD_C_VisOcr(im, result); FD_C_Imwrite("vis_result.jpg", vis_im); printf("Visualized result saved in ./vis_result.jpg\n"); @@ -133,9 +126,9 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_DestroyDBDetectorWrapper(det_model); FD_C_DestroyRecognizerWrapper(rec_model); FD_C_DestroyPPOCRv3Wrapper(ppocr_v3); - FD_C_DestroyOCRResultWrapper(result_wrapper); FD_C_DestroyOCRResult(result); FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); } void GpuInfer(const char* det_model_dir, const char* cls_model_dir, @@ -213,16 +206,9 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, } // print res - // You can directly access fields in FD_C_OCRResult and print it refer to - // OCRResult API Doc Or you can wrap it using - // FD_C_OCRResult_Wrapper, which containes C++ structure - // fastdeploy::vision::OCRResult, and using C API - // FD_C_OCRResultWrapperStr to call - // fastdeploy::vision::OCRResult::Str() in it. For convenience, we choose - // this method to print it. - FD_C_OCRResultWrapper* result_wrapper = - FD_C_CreateOCRResultWrapperFromData(result); - printf("%s", FD_C_OCRResultWrapperStr(result_wrapper)); + char res[2000]; + FD_C_OCRResultStr(result, res); + printf("%s", res); FD_C_Mat vis_im = FD_C_VisOcr(im, result); FD_C_Imwrite("vis_result.jpg", vis_im); printf("Visualized result saved in ./vis_result.jpg\n"); @@ -234,9 +220,9 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_DestroyDBDetectorWrapper(det_model); FD_C_DestroyRecognizerWrapper(rec_model); FD_C_DestroyPPOCRv3Wrapper(ppocr_v3); - FD_C_DestroyOCRResultWrapper(result_wrapper); FD_C_DestroyOCRResult(result); FD_C_DestroyMat(im); + FD_C_DestroyMat(vis_im); } int main(int argc, char* argv[]) { if (argc < 7) { @@ -245,7 +231,7 @@ int main(int argc, char* argv[]) { "path/to/rec_model path/to/rec_label_file path/to/image " "run_option, " "e.g ./infer_demo ./ch_PP-OCRv3_det_infer " - "./ch_ppocr_mobile_v2.0_cls_infer ./ch_PP-OCRv3_rec_infer " + "./ch_ppocr_mobile_v3.0_cls_infer ./ch_PP-OCRv3_rec_infer " "./ppocr_keys_v1.txt ./12.jpg 0\n"); printf( "The data type of run_option is int, 0: run with cpu; 1: run with gpu" diff --git a/examples/vision/segmentation/paddleseg/cpu-gpu/c/CMakeLists.txt b/examples/vision/segmentation/paddleseg/cpu-gpu/c/CMakeLists.txt new file mode 100644 index 0000000000..b228346da8 --- /dev/null +++ b/examples/vision/segmentation/paddleseg/cpu-gpu/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_demo ${PROJECT_SOURCE_DIR}/infer.c) +target_link_libraries(infer_demo ${FASTDEPLOY_LIBS}) diff --git a/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c b/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c new file mode 100644 index 0000000000..be950b7d64 --- /dev/null +++ b/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c @@ -0,0 +1,148 @@ +// 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 +#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, "deploy.yaml"); + + FD_C_RuntimeOptionWrapper* option = FD_C_CreateRuntimeOptionWrapper(); + FD_C_RuntimeOptionWrapperUseCpu(option); + + FD_C_PaddleSegModelWrapper* model = FD_C_CreatePaddleSegModelWrapper( + model_file, params_file, config_file, option, PADDLE); + + if (!FD_C_PaddleSegModelWrapperInitialized(model)) { + printf("Failed to initialize.\n"); + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPaddleSegModelWrapper(model); + return; + } + + FD_C_Mat im = FD_C_Imread(image_file); + + FD_C_SegmentationResult* result = + (FD_C_SegmentationResult*)malloc(sizeof(FD_C_SegmentationResult)); + + if (!FD_C_PaddleSegModelWrapperPredict(model, im, result)) { + printf("Failed to predict.\n"); + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPaddleSegModelWrapper(model); + FD_C_DestroyMat(im); + free(result); + return; + } + + // print res + char res[2000]; + FD_C_SegmentationResultStr(result, res); + printf("%s", res); + + FD_C_Mat vis_im = FD_C_VisSegmentation(im, result, 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_DestroyPaddleSegModelWrapper(model); + FD_C_DestroySegmentationResult(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, "deploy.yaml"); + + FD_C_RuntimeOptionWrapper* option = FD_C_CreateRuntimeOptionWrapper(); + FD_C_RuntimeOptionWrapperUseGpu(option, 0); + + FD_C_PaddleSegModelWrapper* model = FD_C_CreatePaddleSegModelWrapper( + model_file, params_file, config_file, option, PADDLE); + + if (!FD_C_PaddleSegModelWrapperInitialized(model)) { + printf("Failed to initialize.\n"); + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPaddleSegModelWrapper(model); + return; + } + + FD_C_Mat im = FD_C_Imread(image_file); + + FD_C_SegmentationResult* result = + (FD_C_SegmentationResult*)malloc(sizeof(FD_C_SegmentationResult)); + + if (!FD_C_PaddleSegModelWrapperPredict(model, im, result)) { + printf("Failed to predict.\n"); + FD_C_DestroyRuntimeOptionWrapper(option); + FD_C_DestroyPaddleSegModelWrapper(model); + FD_C_DestroyMat(im); + free(result); + return; + } + + // print res + char res[2000]; + FD_C_SegmentationResultStr(result, res); + printf("%s", res); + + FD_C_Mat vis_im = FD_C_VisSegmentation(im, result, 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_DestroyPaddleSegModelWrapper(model); + FD_C_DestroySegmentationResult(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 ./ppseg_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; +}