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 YOLO_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, \
20  FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, \
21  const FD_C_ModelFormat model_format)
22 
23 #define YOLO_DECLARE_DESTROY_WRAPPER_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern void \
24 FD_C_Destroy##model_type##Wrapper(__fd_take FD_C_##model_type##Wrapper* wrapper_var_name)
25 
26 #define YOLO_DECLARE_PREDICT_FUNCTION(model_type, wrapper_var_name) \
27  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 YOLO_DECLARE_PREDICT_FUNCTION_WITH_THRESHOLD(model_type, wrapper_var_name) \
32  FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperPredict( \
33  __fd_take FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_Mat img, \
34  FD_C_DetectionResult* fd_c_detection_result, float conf_threshold, \
35  float nms_threshold)
36 
37 #define YOLO_DECLARE_INITIALIZED_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperInitialized( \
38  __fd_keep FD_C_##model_type##Wrapper* wrapper_var_name)
39 
40 
41 #define YOLO_DECLARE_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) FASTDEPLOY_CAPI_EXPORT extern FD_C_Bool FD_C_##model_type##WrapperBatchPredict( \
42  __fd_keep FD_C_##model_type##Wrapper* wrapper_var_name, \
43  FD_C_OneDimMat imgs, \
44  FD_C_OneDimDetectionResult* results)
45 
46 #define YOLO_DECLARE_AND_IMPLEMENT_CREATE_WRAPPER_FUNCTION(model_type, var_name) FD_C_##model_type##Wrapper* FD_C_Create##model_type##Wrapper(\
47  const char* model_file, const char* params_file, \
48  FD_C_RuntimeOptionWrapper* fd_c_runtime_option_wrapper, \
49  const FD_C_ModelFormat model_format) { \
50  auto& runtime_option = CHECK_AND_CONVERT_FD_TYPE(RuntimeOptionWrapper, \
51  fd_c_runtime_option_wrapper); \
52  FD_C_##model_type##Wrapper* fd_c_##model_type##_wrapper = new FD_C_##model_type##Wrapper(); \
53  fd_c_##model_type##_wrapper->var_name = \
54  std::unique_ptr<fastdeploy::vision::detection::model_type>( \
55  new fastdeploy::vision::detection::model_type( \
56  std::string(model_file), std::string(params_file), \
57  *runtime_option, \
58  static_cast<fastdeploy::ModelFormat>(model_format))); \
59  return fd_c_##model_type##_wrapper; \
60 }
61 
62 #define YOLO_DECLARE_AND_IMPLEMENT_DESTROY_WRAPPER_FUNCTION(model_type, wrapper_var_name) void FD_C_Destroy##model_type##Wrapper( \
63  __fd_take FD_C_##model_type##Wrapper* wrapper_var_name) { \
64  delete wrapper_var_name; \
65 }
66 
67 
68 #define YOLO_DECLARE_AND_IMPLEMENT_PREDICT_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperPredict( \
69  FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_Mat img, \
70  FD_C_DetectionResult* fd_c_detection_result) { \
71  cv::Mat* im = reinterpret_cast<cv::Mat*>(img); \
72  auto& model = \
73  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
74  FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = \
75  FD_C_CreateDetectionResultWrapper(); \
76  auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( \
77  DetectionResultWrapper, fd_c_detection_result_wrapper); \
78  bool successful = model->Predict(*im, detection_result.get()); \
79  if (successful) { \
80  FD_C_DetectionResultWrapperToCResult(fd_c_detection_result_wrapper, fd_c_detection_result); \
81  }\
82  FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); \
83  return successful; \
84 }
85 
86 #define YOLO_DECLARE_AND_IMPLEMENT_PREDICT_FUNCTION_WITH_THREASHOLD(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperPredict( \
87  FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_Mat img, \
88  FD_C_DetectionResult* fd_c_detection_result, float conf_threshold, \
89  float nms_threshold) { \
90  cv::Mat* im = reinterpret_cast<cv::Mat*>(img); \
91  auto& model = \
92  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
93  FD_C_DetectionResultWrapper* fd_c_detection_result_wrapper = \
94  FD_C_CreateDetectionResultWrapper(); \
95  auto& detection_result = CHECK_AND_CONVERT_FD_TYPE( \
96  DetectionResultWrapper, fd_c_detection_result_wrapper); \
97  bool successful = model->Predict(im, detection_result.get(), conf_threshold, nms_threshold); \
98  if (successful) { \
99  FD_C_DetectionResultWrapperToCResult(fd_c_detection_result_wrapper, fd_c_detection_result); \
100  } \
101  FD_C_DestroyDetectionResultWrapper(fd_c_detection_result_wrapper); \
102  return successful; \
103 }
104 
105 #define YOLO_DECLARE_AND_IMPLEMENT_INITIALIZED_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperInitialized( \
106  FD_C_##model_type##Wrapper* wrapper_var_name) { \
107  auto& model = \
108  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
109  return model->Initialized(); \
110 }
111 
112 #define YOLO_DECLARE_AND_IMPLEMENT_BATCH_PREDICT_FUNCTION(model_type, wrapper_var_name) FD_C_Bool FD_C_##model_type##WrapperBatchPredict( \
113  FD_C_##model_type##Wrapper* wrapper_var_name, FD_C_OneDimMat imgs, \
114  FD_C_OneDimDetectionResult* results) { \
115  std::vector<cv::Mat> imgs_vec; \
116  std::vector<fastdeploy::vision::DetectionResult> results_out; \
117  std::vector<FD_C_DetectionResultWrapper*> results_wrapper_out; \
118  for (int i = 0; i < imgs.size; i++) { \
119  imgs_vec.push_back(*(reinterpret_cast<cv::Mat*>(imgs.data[i]))); \
120  FD_C_DetectionResultWrapper* fd_detection_result_wrapper = FD_C_CreateDetectionResultWrapper(); \
121  results_wrapper_out.push_back(fd_detection_result_wrapper); \
122  } \
123  auto& model = \
124  CHECK_AND_CONVERT_FD_TYPE(model_type##Wrapper, wrapper_var_name); \
125  bool successful = model->BatchPredict(imgs_vec, &results_out); \
126  if (successful) { \
127  results->size = results_out.size(); \
128  results->data = new FD_C_DetectionResult[results->size]; \
129  for (int i = 0; i < results_out.size(); i++) { \
130  (*CHECK_AND_CONVERT_FD_TYPE(DetectionResultWrapper, \
131  results_wrapper_out[i])) = std::move(results_out[i]); \
132  FD_C_DetectionResultWrapperToCResult(results_wrapper_out[i], &results->data[i]); \
133  } \
134  } \
135  for (int i = 0; i < results_out.size(); i++) { \
136  FD_C_DestroyDetectionResultWrapper(results_wrapper_out[i]); \
137  }\
138  return successful; \
139 }