Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C API] Add c apis for matting #2497

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions c_api/fastdeploy_capi/vision.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "fastdeploy_capi/vision/detection/contrib/yolo/model.h"
#include "fastdeploy_capi/vision/ocr/ppocr/model.h"
#include "fastdeploy_capi/vision/segmentation/ppseg/model.h"
#include "fastdeploy_capi/vision/matting/ppmatting/model.h"
#include "fastdeploy_capi/vision/result.h"
#include "fastdeploy_capi/vision/visualize.h"
#endif
Expand Down
77 changes: 77 additions & 0 deletions c_api/fastdeploy_capi/vision/matting/ppmatting/model.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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/matting/ppmatting/model.h"

#include "fastdeploy_capi/internal/types_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

FD_C_PPMattingWrapper* FD_C_CreatePPMattingWrapper(
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_PPMattingWrapper* fd_c_ppmatting_wrapper =
new FD_C_PPMattingWrapper();
fd_c_ppmatting_wrapper->matting_model =
std::unique_ptr<fastdeploy::vision::matting::PPMatting>(
new fastdeploy::vision::matting::PPMatting(
std::string(model_file), std::string(params_file),
std::string(config_file), *runtime_option,
static_cast<fastdeploy::ModelFormat>(model_format)));
return fd_c_ppmatting_wrapper;
}

void FD_C_DestroyPPMattingWrapper(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper) {
delete fd_c_ppmatting_wrapper;
}

FD_C_Bool FD_C_PPMattingWrapperPredict(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper, FD_C_Mat img,
FD_C_MattingResult* fd_c_matting_result) {
cv::Mat* im = reinterpret_cast<cv::Mat*>(img);
auto& ppmatting = CHECK_AND_CONVERT_FD_TYPE(
PPMattingWrapper, fd_c_ppmatting_wrapper);
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapper();
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

bool successful = ppmatting->Predict(im, matting_result.get());
if (successful) {
FD_C_MattingResultWrapperToCResult(fd_c_matting_result_wrapper,
fd_c_matting_result);
}
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return successful;
}

FD_C_Bool FD_C_PPMattingWrapperInitialized(
FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper) {
auto& ppmatting = CHECK_AND_CONVERT_FD_TYPE(
PPMattingWrapper, fd_c_ppmatting_wrapper);
return ppmatting->Initialized();
}




#ifdef __cplusplus
}
#endif
76 changes: 76 additions & 0 deletions c_api/fastdeploy_capi/vision/matting/ppmatting/model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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/core/fd_common.h"
#include "fastdeploy_capi/core/fd_type.h"
#include "fastdeploy_capi/runtime/runtime_option.h"
#include "fastdeploy_capi/vision/result.h"

typedef struct FD_C_PPMattingWrapper FD_C_PPMattingWrapper;

#ifdef __cplusplus
extern "C" {
#endif

/** \brief Create a new FD_C_PPMattingWrapper object
*
* \param[in] model_file Path of model file, e.g PPMatting-512/model.pdmodel
* \param[in] params_file Path of parameter file, e.g PPMatting-512/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 PPMatting-512/infer_cfg.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_PPMattingWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_PPMattingWrapper*
FD_C_CreatePPMattingWrapper(
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_PPMattingWrapper object
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyPPMattingWrapper(
__fd_take FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper);

/** \brief Predict the segmentation result for an input image
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
* \param[in] img pointer to cv::Mat image
* \param[in] fd_c_matting_result pointer to FD_C_SegmentationResult object, which stores the result.
*/
FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PPMattingWrapperPredict(
__fd_keep FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper,
FD_C_Mat img, FD_C_MattingResult* fd_c_matting_result);

/** \brief Check if the model is initialized successfully
*
* \param[in] fd_c_ppmatting_wrapper pointer to FD_C_PPMattingWrapper object
*
* \return Return a bool of value true if initialized successfully
*/

FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_PPMattingWrapperInitialized(
__fd_keep FD_C_PPMattingWrapper* fd_c_ppmatting_wrapper);


#ifdef __cplusplus
} // extern "C"
#endif
122 changes: 122 additions & 0 deletions c_api/fastdeploy_capi/vision/result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,128 @@ void FD_C_SegmentationResultStr(
return;
}

// Matting Results

FD_C_MattingResultWrapper* FD_C_CreateMattingResultWrapper() {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
new FD_C_MattingResultWrapper();
fd_c_matting_result_wrapper->matting_result =
std::unique_ptr<fastdeploy::vision::MattingResult>(
new fastdeploy::vision::MattingResult());
return fd_c_matting_result_wrapper;
}

void FD_C_DestroyMattingResultWrapper(
__fd_take FD_C_MattingResultWrapper*
fd_c_matting_result_wrapper) {
delete fd_c_matting_result_wrapper;
}

FD_C_MattingResult* FD_C_CreatePPMattingResult() {
FD_C_MattingResult* fd_c_matting_result =
new FD_C_MattingResult();
return fd_c_matting_result;
}

void FD_C_DestroyPPMattingResult(
__fd_take FD_C_MattingResult* fd_c_matting_result) {
if (fd_c_matting_result == nullptr) return;
// delete alpha
delete[] fd_c_matting_result->alpha.data;
// delete foreground
delete[] fd_c_matting_result->foreground.data;
// delete shape
delete[] fd_c_matting_result->shape.data;
delete fd_c_matting_result;
}

void FD_C_MattingResultWrapperToCResult(
__fd_keep FD_C_MattingResultWrapper* fd_c_matting_result_wrapper,
__fd_keep FD_C_MattingResult* fd_c_matting_result) {
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

// copy alpha
fd_c_matting_result->alpha.size =
matting_result->alpha.size();
fd_c_matting_result->alpha.data =
new float[fd_c_matting_result->alpha.size];
memcpy(fd_c_matting_result->alpha.data,
matting_result->alpha.data(),
sizeof(float) * fd_c_matting_result->alpha.size);
// copy foreground
fd_c_matting_result->foreground.size =
matting_result->foreground.size();
fd_c_matting_result->foreground.data =
new float[fd_c_matting_result->foreground.size];
memcpy(fd_c_matting_result->foreground.data,
matting_result->foreground.data(),
sizeof(float) * fd_c_matting_result->foreground.size);
// copy shape
fd_c_matting_result->shape.size = matting_result->shape.size();
fd_c_matting_result->shape.data =
new int64_t[fd_c_matting_result->shape.size];
memcpy(fd_c_matting_result->shape.data,
matting_result->shape.data(),
sizeof(int64_t) * fd_c_matting_result->shape.size);
// copy contain_foreground
fd_c_matting_result->contain_foreground =
matting_result->contain_foreground;
// copy type
fd_c_matting_result->type =
static_cast<FD_C_ResultType>(matting_result->type);
return;
}

FD_C_MattingResultWrapper* FD_C_CreateMattingResultWrapperFromCResult(
__fd_keep FD_C_MattingResult* fd_c_matting_result) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapper();
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);

// copy alpha
matting_result->alpha.resize(
fd_c_matting_result->alpha.size);
memcpy(matting_result->alpha.data(),
fd_c_matting_result->alpha.data,
sizeof(float) * fd_c_matting_result->alpha.size);

// copy foreground
matting_result->foreground.resize(
fd_c_matting_result->foreground.size);
memcpy(matting_result->foreground.data(),
fd_c_matting_result->foreground.data,
sizeof(float) * fd_c_matting_result->foreground.size);

// copy shape
matting_result->shape.resize(fd_c_matting_result->shape.size);
memcpy(matting_result->shape.data(),
fd_c_matting_result->shape.data,
sizeof(int64_t) * fd_c_matting_result->shape.size);

// copy contain_score_map
matting_result->contain_foreground =
fd_c_matting_result->contain_foreground;
// copy type
matting_result->type = static_cast<fastdeploy::vision::ResultType>(
fd_c_matting_result->type);

return fd_c_matting_result_wrapper;
}

void FD_C_PPMattingResultStr(
FD_C_MattingResult* fd_c_matting_result, char* str_buffer) {
FD_C_MattingResultWrapper* fd_c_matting_result_wrapper =
FD_C_CreateMattingResultWrapperFromCResult(fd_c_matting_result);
auto& matting_result = CHECK_AND_CONVERT_FD_TYPE(
MattingResultWrapper, fd_c_matting_result_wrapper);
std::string information = matting_result->Str();
std::strcpy(str_buffer, information.c_str());
FD_C_DestroyMattingResultWrapper(fd_c_matting_result_wrapper);
return;
}

#ifdef __cplusplus
}
#endif
77 changes: 77 additions & 0 deletions c_api/fastdeploy_capi/vision/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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;
typedef struct FD_C_MattingResultWrapper FD_C_MattingResultWrapper;

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -94,6 +95,18 @@ typedef struct FD_C_OneDimSegmentationResult {
FD_C_SegmentationResult* data;
} FD_C_OneDimSegmentationResult;

typedef struct FD_C_MattingResult {
FD_C_OneDimArrayFloat alpha;
FD_C_OneDimArrayFloat foreground;
FD_C_OneDimArrayInt64 shape;
FD_C_Bool contain_foreground;
FD_C_ResultType type;
} FD_C_MattingResult;

typedef struct FD_C_OneDimPPMattingResult {
size_t size;
FD_C_MattingResult* data;
} FD_C_OneDimPPMattingResult;

// Classification Results

Expand Down Expand Up @@ -356,6 +369,70 @@ FD_C_SegmentationResultStr(
__fd_keep FD_C_SegmentationResult* fd_c_segmentation_result, char* str_buffer);


// Matting Results

/** \brief Create a new FD_C_MattingResultWrapper object
*
* \return Return a pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResultWrapper*
FD_C_CreateMattingResultWrapper();

/** \brief Destroy a FD_C_MattingResultWrapper object
*
* \param[in] fd_c_matting_result_wrapper pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyMattingResultWrapper(
__fd_take FD_C_MattingResultWrapper* fd_c_matting_result_wrapper);

/** \brief Create a new FD_C_MattingResult object
*
* \return Return a pointer to FD_C_MattingResult object
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResult*
FD_C_CreatePPMattingResult();

/** \brief Destroy a FD_C_MattingResult object
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
*/

FASTDEPLOY_CAPI_EXPORT extern void FD_C_DestroyPPMattingResult(
__fd_take FD_C_MattingResult* fd_c_matting_result);

/** \brief Get a FD_C_MattingResult object from FD_C_MattingResultWrapper object
*
* \param[in] fd_c_matting_result_wrapper pointer to FD_C_MattingResultWrapper object
* \param[out] fd_c_matting_result pointer to FD_C_MattingResult object used to store data
*/
FASTDEPLOY_CAPI_EXPORT extern __fd_give void
FD_C_MattingResultWrapperToCResult(
__fd_keep FD_C_MattingResultWrapper* fd_c_matting_result_wrapper,
__fd_keep FD_C_MattingResult* fd_c_matting_result);

/** \brief Create a new FD_C_MattingResultWrapper object from FD_C_MattingResult object
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
* \return Return a pointer to FD_C_MattingResultWrapper object
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_MattingResultWrapper*
FD_C_CreateMattingResultWrapperFromCResult(
__fd_keep FD_C_MattingResult* fd_c_matting_result);

/** \brief Print PPmattingResult formated information
*
* \param[in] fd_c_matting_result pointer to FD_C_MattingResult object
* \param[out] str_buffer used to store string
*/

FASTDEPLOY_CAPI_EXPORT extern __fd_give void
FD_C_PPMattingResultStr(
__fd_keep FD_C_MattingResult* fd_c_matting_result, char* str_buffer);



#ifdef __cplusplus
} // extern "C"
Expand Down
Loading