From 091e0399917bae0f87bc2dde6d27c80fb070bf63 Mon Sep 17 00:00:00 2001 From: chenjian Date: Thu, 9 Mar 2023 11:11:47 +0800 Subject: [PATCH] [C API] Add create api for c results (#1547) add create api for c results --- c_api/fastdeploy_capi/vision/result.cc | 25 +++++++++++++++ c_api/fastdeploy_capi/vision/result.h | 31 +++++++++++++++++++ .../classification/paddleclas/c/infer.c | 6 ++-- .../paddledetection/c/infer_ppyoloe.c | 6 ++-- examples/vision/detection/yolov5/c/infer.c | 6 ++-- examples/vision/ocr/PP-OCRv2/c/infer.c | 4 +-- examples/vision/ocr/PP-OCRv3/c/infer.c | 4 +-- .../segmentation/paddleseg/cpu-gpu/c/infer.c | 6 ++-- 8 files changed, 68 insertions(+), 20 deletions(-) diff --git a/c_api/fastdeploy_capi/vision/result.cc b/c_api/fastdeploy_capi/vision/result.cc index 44cc6cbfe..86e656ead 100644 --- a/c_api/fastdeploy_capi/vision/result.cc +++ b/c_api/fastdeploy_capi/vision/result.cc @@ -37,6 +37,11 @@ void FD_C_DestroyClassifyResultWrapper( delete fd_c_classify_result_wrapper; } +FD_C_ClassifyResult* FD_C_CreateClassifyResult() { + FD_C_ClassifyResult* fd_c_classify_result = new FD_C_ClassifyResult(); + return fd_c_classify_result; +} + void FD_C_DestroyClassifyResult( __fd_take FD_C_ClassifyResult* fd_c_classify_result) { if (fd_c_classify_result == nullptr) return; @@ -44,6 +49,7 @@ void FD_C_DestroyClassifyResult( delete[] fd_c_classify_result->label_ids.data; // delete scores delete[] fd_c_classify_result->scores.data; + delete fd_c_classify_result; } void FD_C_ClassifyResultWrapperToCResult( @@ -117,6 +123,11 @@ void FD_C_DestroyDetectionResultWrapper( delete fd_c_detection_result_wrapper; } +FD_C_DetectionResult* FD_C_CreateDetectionResult() { + FD_C_DetectionResult* fd_c_detection_result = new FD_C_DetectionResult(); + return fd_c_detection_result; +} + void FD_C_DestroyDetectionResult( __fd_take FD_C_DetectionResult* fd_c_detection_result) { if (fd_c_detection_result == nullptr) return; @@ -134,6 +145,7 @@ void FD_C_DestroyDetectionResult( delete[] fd_c_detection_result->masks.data[i].data.data; delete[] fd_c_detection_result->masks.data[i].shape.data; } + delete fd_c_detection_result; } void FD_C_DetectionResultWrapperToCResult( @@ -275,6 +287,11 @@ void FD_C_DestroyOCRResultWrapper( delete fd_c_ocr_result_wrapper; } +FD_C_OCRResult* FD_C_CreateOCRResult() { + FD_C_OCRResult* fd_c_ocr_result = new FD_C_OCRResult(); + return fd_c_ocr_result; +} + void FD_C_DestroyOCRResult(__fd_take FD_C_OCRResult* fd_c_ocr_result) { if (fd_c_ocr_result == nullptr) return; // delete boxes @@ -293,6 +310,7 @@ void FD_C_DestroyOCRResult(__fd_take FD_C_OCRResult* fd_c_ocr_result) { delete[] fd_c_ocr_result->cls_scores.data; // delete cls_labels delete[] fd_c_ocr_result->cls_labels.data; + delete fd_c_ocr_result; } void FD_C_OCRResultWrapperToCResult( @@ -418,6 +436,12 @@ void FD_C_DestroySegmentationResultWrapper( delete fd_c_segmentation_result_wrapper; } +FD_C_SegmentationResult* FD_C_CreateSegmentationResult() { + FD_C_SegmentationResult* fd_c_segmentation_result = + new FD_C_SegmentationResult(); + return fd_c_segmentation_result; +} + void FD_C_DestroySegmentationResult( __fd_take FD_C_SegmentationResult* fd_c_segmentation_result) { if (fd_c_segmentation_result == nullptr) return; @@ -427,6 +451,7 @@ void FD_C_DestroySegmentationResult( 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( diff --git a/c_api/fastdeploy_capi/vision/result.h b/c_api/fastdeploy_capi/vision/result.h index 853983ee0..36a0de4fc 100644 --- a/c_api/fastdeploy_capi/vision/result.h +++ b/c_api/fastdeploy_capi/vision/result.h @@ -109,6 +109,13 @@ FD_C_CreateClassifyResultWrapper(); FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyClassifyResultWrapper( __fd_take FD_C_ClassifyResultWrapper* fd_c_classify_result_wrapper); +/** \brief Create a new FD_C_ClassifyResult object + * + * \return Return a pointer to FD_C_ClassifyResult object + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_ClassifyResult* +FD_C_CreateClassifyResult(); + /** \brief Destroy a FD_C_ClassifyResult object * * \param[in] fd_c_classify_result pointer to FD_C_ClassifyResult object @@ -159,6 +166,7 @@ FD_C_ClassifyResultStr( FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_DetectionResultWrapper* FD_C_CreateDetectionResultWrapper(); + /** \brief Destroy a FD_C_DetectionResultWrapper object * * \param[in] fd_c_detection_result_wrapper pointer to FD_C_DetectionResultWrapper object @@ -167,6 +175,14 @@ FD_C_CreateDetectionResultWrapper(); FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyDetectionResultWrapper( __fd_take FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper); + +/** \brief Create a new FD_C_DetectionResult object + * + * \return Return a pointer to FD_C_DetectionResult object + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_DetectionResult* +FD_C_CreateDetectionResult(); + /** \brief Destroy a FD_C_DetectionResult object * * \param[in] fd_c_detection_result pointer to FD_C_DetectionResult object @@ -225,6 +241,14 @@ FD_C_CreateOCRResultWrapper(); FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyOCRResultWrapper( __fd_take FD_C_OCRResultWrapper* fd_c_ocr_result_wrapper); + +/** \brief Create a new FD_C_OCRResult object + * + * \return Return a pointer to FD_C_OCRResult object + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_OCRResult* +FD_C_CreateOCRResult(); + /** \brief Destroy a FD_C_OCRResult object * * \param[in] fd_c_ocr_result pointer to FD_C_OCRResult object @@ -282,6 +306,13 @@ FD_C_CreateSegmentationResultWrapper(); FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroySegmentationResultWrapper( __fd_take FD_C_SegmentationResultWrapper* fd_c_segmentation_result_wrapper); +/** \brief Create a new FD_C_SegmentationResult object + * + * \return Return a pointer to FD_C_SegmentationResult object + */ +FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_SegmentationResult* +FD_C_CreateSegmentationResult(); + /** \brief Destroy a FD_C_SegmentationResult object * * \param[in] fd_c_segmentation_result pointer to FD_C_SegmentationResult object diff --git a/examples/vision/classification/paddleclas/c/infer.c b/examples/vision/classification/paddleclas/c/infer.c index 204494be7..ab183d511 100644 --- a/examples/vision/classification/paddleclas/c/infer.c +++ b/examples/vision/classification/paddleclas/c/infer.c @@ -49,8 +49,7 @@ void CpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_ClassifyResult* result = - (FD_C_ClassifyResult*)malloc(sizeof(FD_C_ClassifyResult)); + FD_C_ClassifyResult* result = FD_C_CreateClassifyResult(); if (!FD_C_PaddleClasModelWrapperPredict(model, im, result)) { printf("Failed to predict.\n"); @@ -97,8 +96,7 @@ void GpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_ClassifyResult* result = - (FD_C_ClassifyResult*)malloc(sizeof(FD_C_ClassifyResult)); + FD_C_ClassifyResult* result = FD_C_CreateClassifyResult(); if (!FD_C_PaddleClasModelWrapperPredict(model, im, result)) { printf("Failed to predict.\n"); diff --git a/examples/vision/detection/paddledetection/c/infer_ppyoloe.c b/examples/vision/detection/paddledetection/c/infer_ppyoloe.c index cdc019989..319e796a2 100644 --- a/examples/vision/detection/paddledetection/c/infer_ppyoloe.c +++ b/examples/vision/detection/paddledetection/c/infer_ppyoloe.c @@ -47,8 +47,7 @@ void CpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_DetectionResult* result = - (FD_C_DetectionResult*)malloc(sizeof(FD_C_DetectionResult)); + FD_C_DetectionResult* result = FD_C_CreateDetectionResult(); if (!FD_C_PPYOLOEWrapperPredict(model, im, result)) { printf("Failed to predict.\n"); @@ -95,8 +94,7 @@ void GpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_DetectionResult* result = - (FD_C_DetectionResult*)malloc(sizeof(FD_C_DetectionResult)); + FD_C_DetectionResult* result = FD_C_CreateDetectionResult(); if (!FD_C_PPYOLOEWrapperPredict(model, im, result)) { printf("Failed to predict.\n"); diff --git a/examples/vision/detection/yolov5/c/infer.c b/examples/vision/detection/yolov5/c/infer.c index 74d4568a7..d3c8cae98 100644 --- a/examples/vision/detection/yolov5/c/infer.c +++ b/examples/vision/detection/yolov5/c/infer.c @@ -39,8 +39,7 @@ void CpuInfer(const char* model_file, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_DetectionResult* result = - (FD_C_DetectionResult*)malloc(sizeof(FD_C_DetectionResult)); + FD_C_DetectionResult* result = FD_C_CreateDetectionResult(); if (!FD_C_YOLOv5WrapperPredict(model, im, result)) { printf("Failed to predict.\n"); @@ -79,8 +78,7 @@ void GpuInfer(const char* model_file, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_DetectionResult* result = - (FD_C_DetectionResult*)malloc(sizeof(FD_C_DetectionResult)); + FD_C_DetectionResult* result = FD_C_CreateDetectionResult(); if (!FD_C_YOLOv5WrapperPredict(model, im, result)) { printf("Failed to predict.\n"); diff --git a/examples/vision/ocr/PP-OCRv2/c/infer.c b/examples/vision/ocr/PP-OCRv2/c/infer.c index f7bb1b87d..7eddb7b4b 100644 --- a/examples/vision/ocr/PP-OCRv2/c/infer.c +++ b/examples/vision/ocr/PP-OCRv2/c/infer.c @@ -96,7 +96,7 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_Mat im = FD_C_Imread(image_file); - FD_C_OCRResult* result = (FD_C_OCRResult*)malloc(sizeof(FD_C_OCRResult)); + FD_C_OCRResult* result = FD_C_CreateOCRResult(); if (!FD_C_PPOCRv2WrapperPredict(ppocr_v2, im, result)) { printf("Failed to predict.\n"); @@ -191,7 +191,7 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_Mat im = FD_C_Imread(image_file); - FD_C_OCRResult* result = (FD_C_OCRResult*)malloc(sizeof(FD_C_OCRResult)); + FD_C_OCRResult* result = FD_C_CreateOCRResult(); if (!FD_C_PPOCRv2WrapperPredict(ppocr_v2, im, result)) { printf("Failed to predict.\n"); diff --git a/examples/vision/ocr/PP-OCRv3/c/infer.c b/examples/vision/ocr/PP-OCRv3/c/infer.c index 72486edfc..007d2ed4d 100644 --- a/examples/vision/ocr/PP-OCRv3/c/infer.c +++ b/examples/vision/ocr/PP-OCRv3/c/infer.c @@ -96,7 +96,7 @@ void CpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_Mat im = FD_C_Imread(image_file); - FD_C_OCRResult* result = (FD_C_OCRResult*)malloc(sizeof(FD_C_OCRResult)); + FD_C_OCRResult* result = FD_C_CreateOCRResult(); if (!FD_C_PPOCRv3WrapperPredict(ppocr_v3, im, result)) { printf("Failed to predict.\n"); @@ -191,7 +191,7 @@ void GpuInfer(const char* det_model_dir, const char* cls_model_dir, FD_C_Mat im = FD_C_Imread(image_file); - FD_C_OCRResult* result = (FD_C_OCRResult*)malloc(sizeof(FD_C_OCRResult)); + FD_C_OCRResult* result = FD_C_CreateOCRResult(); if (!FD_C_PPOCRv3WrapperPredict(ppocr_v3, im, result)) { printf("Failed to predict.\n"); diff --git a/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c b/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c index 3225d5264..131734086 100644 --- a/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c +++ b/examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c @@ -47,8 +47,7 @@ void CpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_SegmentationResult* result = - (FD_C_SegmentationResult*)malloc(sizeof(FD_C_SegmentationResult)); + FD_C_SegmentationResult* result = FD_C_CreateSegmentationResult(); if (!FD_C_PaddleSegModelWrapperPredict(model, im, result)) { printf("Failed to predict.\n"); @@ -99,8 +98,7 @@ void GpuInfer(const char* model_dir, const char* image_file) { FD_C_Mat im = FD_C_Imread(image_file); - FD_C_SegmentationResult* result = - (FD_C_SegmentationResult*)malloc(sizeof(FD_C_SegmentationResult)); + FD_C_SegmentationResult* result = FD_C_CreateSegmentationResult(); if (!FD_C_PaddleSegModelWrapperPredict(model, im, result)) { printf("Failed to predict.\n");