Skip to content

Commit

Permalink
[C API] Add create api for c results (#1547)
Browse files Browse the repository at this point in the history
add create api for c results
  • Loading branch information
rainyfly authored Mar 9, 2023
1 parent ad14d0e commit 091e039
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
25 changes: 25 additions & 0 deletions c_api/fastdeploy_capi/vision/result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ 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;
// delete label_ids
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(
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions c_api/fastdeploy_capi/vision/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions examples/vision/classification/paddleclas/c/infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions examples/vision/detection/paddledetection/c/infer_ppyoloe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions examples/vision/detection/yolov5/c/infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions examples/vision/ocr/PP-OCRv2/c/infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions examples/vision/ocr/PP-OCRv3/c/infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions examples/vision/segmentation/paddleseg/cpu-gpu/c/infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 091e039

Please sign in to comment.