FastDeploy  latest
Fast & Easy to Deploy!
base_define.h
Go to the documentation of this file.
1 // Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #define DECLARE_CREATE_WRAPPER_FUNCTION(model_type) FASTDEPLOY_CAPI_EXPORT extern __fd_give FD_C_##model_type##Wrapper* \
18 FD_C_Create##model_type##Wrapper( \
19  const char* model_file, const char* params_file, const char* config_file, \
20  FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, \
21  const FD_C_ModelFormat model_format)
22 
23 
24 #define DECLARE_DESTROY_WRAPPER_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern void \
25 FD_C_Destroy##model_type##Wrapper(__fd_take FD_C_##model_type##Wrapper* wrapper_var_name);
26 
27 #define DECLARE_PREDICT_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperPredict( \
28  __fd_take FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_Mat img, \
29  FD_C_DetectionResult* fd_c_detection_result)
30 
31 #define DECLARE_INITIALIZED_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperInitialized( \
32  __fd_keep FD_C_##model_type##Wrapper* wrapper_var_name)
33 
34 
35 #define DECLARE_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperBatchPredict( \
36  __fd_keep FD_C_##model_type##Wrapper* wrapper_var_name, \
37  FD_C_OneDimMat imgs, \
38  FD_C_OneDimDetectionResult* results)
39 
40 
41 #define DECLARE_AND_IMPLEMENT_CREATE_WRAPPER_FUNCTION(model_type, var_name) FD_C_##model_type##Wrapper* FD_C_Create##model_type##Wrapper(\
42  const char* model_file, const char* params_file, const char* config_file, \
43  FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, \
44  const FD_C_ModelFormat model_format) { \
45  auto& runtime_option = CHECK_AND_CONVERT_FD_TYPE(RuntimeOptionWrapper, \
46  fd_c_runtime_option_wrapper); \
47  FD_C_##model_type##Wrapper* fd_c_##model_type##_wrapper = new FD_C_##model_type##Wrapper(); \
48  fd_c_##model_type##_wrapper->var_name = \
49  std::unique_ptr<fastdeploy::vision::detection::model_type>( \
50  new fastdeploy::vision::detection::model_type( \
51  std::string(model_file), std::string(params_file), \
52  std::string(config_file), *runtime_option, \
53  static_cast<fastdeploy::ModelFormat>(model_format))); \
54  return fd_c_##model_type##_wrapper;\
55 }
56 
57 #define DECLARE_AND_IMPLEMENT_DESTROY_WRAPPER_FUNCTION(model_type, wrapper_var_name) void FD_C_Destroy##model_type##Wrapper( \
58  __fd_take FD_C_##model_type##Wrapper* wrapper_var_name) { \
59  delete wrapper_var_name; \
60 }
61 
62 
63 #define DECLARE_AND_IMPLEMENT_PREDICT_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperPredict( \
64  FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_Mat img, \
65  FD_C_DetectionResult* fd_c_detection_result) { \
66  cv::Mat* im = reinterpret_cast<cv::Mat*>(img); \
67  auto& model = \
68  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
69  FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = \
70  FD_C_CreateDetectionResultWrapper(); \
71  auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( \
72  DetectionResultWrapper, fd_c_detection_result_wrapper); \
73  bool successful = model->Predict(im, detection_result.get()); \
74  if (successful) { \
75  FD_C_DetectionResultWrapperToCResult(fd_c_detection_result_wrapper, fd_c_detection_result); \
76  } \
77 FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); \
78 return successful; \
79 }
80 
81 #define DECLARE_AND_IMPLEMENT_INITIALIZED_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperInitialized( \
82  FD_C_##model_type##Wrapper* wrapper_var_name) { \
83  auto& model = \
84  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
85  return model->Initialized(); \
86 }
87 
88 #define DECLARE_AND_IMPLEMENT_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperBatchPredict( \
89  FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_OneDimMat imgs, \
90  FD_C_OneDimDetectionResult* results) { \
91  std::vector<cv::Mat> imgs_vec; \
92  std::vector<fastdeploy::vision::DetectionResult> results_out; \
93  std::vector<FD_C_DetectionResultWrapper*> results_wrapper_out; \
94  for (int i = 0; i < imgs.size; i++) { \
95  imgs_vec.push_back(*(reinterpret_cast<cv::Mat*>(imgs.data[i]))); \
96  FD_C_DetectionResultWrapper* fd_detection_result_wrapper = FD_C_CreateDetectionResultWrapper(); \
97  results_wrapper_out.push_back(fd_detection_result_wrapper); \
98  } \
99  auto& model = \
100  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
101  bool successful = model->BatchPredict(imgs_vec, &results_out); \
102  if (successful) { \
103  results->size = results_out.size(); \
104  results->data = new FD_C_DetectionResult[results->size]; \
105  for (int i = 0; i < results_out.size(); i++) { \
106  (*CHECK_AND_CONVERT_FD_TYPE(DetectionResultWrapper, \
107  results_wrapper_out[i])) = std::move(results_out[i]); \
108  FD_C_DetectionResultWrapperToCResult(results_wrapper_out[i], &results->data[i]); \
109  } \
110  } \
111  for (int i = 0; i < results_out.size(); i++) { \
112  FD_C_DestroyDetectionResultWrapper(results_wrapper_out[i]); \
113  }\
114  return successful; \
115 }