model_infer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <string>
  2. #include <vector>
  3. #include "model_deploy/common/include/paddle_deploy.h"
  4. // Global model pointer
  5. PaddleDeploy::Model* model;
  6. /*
  7. * Model initialization / registration API
  8. *
  9. * model_type: det,seg,clas,paddlex
  10. *
  11. * model_filename: Model file path
  12. *
  13. * params_filename: Parameter file path
  14. *
  15. * cfg_file: Configuration file path
  16. *
  17. * use_gpu: Whether to use GPU
  18. *
  19. * gpu_id: Specify GPU x
  20. *
  21. * paddlex_model_type: When Model_Type is paddlx, the type of actual Paddlex model returned - det, seg, clas
  22. *
  23. */
  24. extern "C" void InitModel(const char* model_type, const char* model_filename, const char* params_filename, const char* cfg_file, bool use_gpu, int gpu_id, char* paddlex_model_type)
  25. {
  26. // create model
  27. model = PaddleDeploy::CreateModel(model_type); //FLAGS_model_type
  28. // model init
  29. model->Init(cfg_file);
  30. // inference engine init
  31. PaddleDeploy::PaddleEngineConfig engine_config;
  32. engine_config.model_filename = model_filename;
  33. engine_config.params_filename = params_filename;
  34. engine_config.use_gpu = use_gpu;
  35. engine_config.gpu_id = gpu_id;
  36. bool init = model->PaddleEngineInit(engine_config);
  37. if (init)
  38. {
  39. std::cout << "init model success" << std::endl;
  40. }
  41. // det, seg, clas, paddlex
  42. if (strcmp(model_type, "paddlex") == 0) // If it is a PADDLEX model, return the specifically supported model type: det, seg, clas
  43. {
  44. // detector
  45. if (model->yaml_config_["model_type"].as<std::string>() == std::string("detector"))
  46. {
  47. strcpy(paddlex_model_type, "det");
  48. }
  49. else if (model->yaml_config_["model_type"].as<std::string>() == std::string("segmenter"))
  50. {
  51. strcpy(paddlex_model_type, "seg");
  52. }
  53. else if (model->yaml_config_["model_type"].as<std::string>() == std::string("classifier"))
  54. {
  55. strcpy(paddlex_model_type, "clas");
  56. }
  57. }
  58. }
  59. /*
  60. * Detection inference API
  61. *
  62. * img: input for predicting.
  63. *
  64. * nWidth: width of img.
  65. *
  66. * nHeight: height of img.
  67. *
  68. * nChannel: channel of img.
  69. *
  70. * output: result of pridict ,include category_id£¬score£¬coordinate¡£
  71. *
  72. * nBoxesNum£º number of box
  73. *
  74. * LabelList: label list of result
  75. *
  76. * extern "C"
  77. */
  78. extern "C" void Det_ModelPredict(const unsigned char* img, int nWidth, int nHeight, int nChannel, float* output, int* nBoxesNum, char* LabelList)
  79. {
  80. // prepare data
  81. std::vector<cv::Mat> imgs;
  82. int nType = 0;
  83. if (nChannel == 3)
  84. {
  85. nType = CV_8UC3;
  86. }
  87. else
  88. {
  89. std::cout << "Only support 3 channel image." << std::endl;
  90. return;
  91. }
  92. cv::Mat input = cv::Mat::zeros(cv::Size(nWidth, nHeight), nType);
  93. memcpy(input.data, img, nHeight * nWidth * nChannel * sizeof(uchar));
  94. imgs.push_back(std::move(input));
  95. // predict
  96. std::vector<PaddleDeploy::Result> results;
  97. model->Predict(imgs, &results, 1);
  98. // nBoxesNum[0] = results.size(); // results.size() is returning batch_size
  99. nBoxesNum[0] = results[0].det_result->boxes.size(); // Get the predicted Bounding Box number of a single image
  100. std::string label = "";
  101. //std::cout << "res: " << results[num] << std::endl;
  102. for (int i = 0; i < results[0].det_result->boxes.size(); i++) // Get the data for all the boxes
  103. {
  104. label = label + results[0].det_result->boxes[i].category + " ";
  105. // labelindex
  106. output[i * 6 + 0] = results[0].det_result->boxes[i].category_id; // Category ID
  107. // score
  108. output[i * 6 + 1] = results[0].det_result->boxes[i].score; // Score
  109. //// box
  110. output[i * 6 + 2] = results[0].det_result->boxes[i].coordinate[0]; // x1, y1, x2, y2
  111. output[i * 6 + 3] = results[0].det_result->boxes[i].coordinate[1]; // Upper left and lower right vertices
  112. output[i * 6 + 4] = results[0].det_result->boxes[i].coordinate[2];
  113. output[i * 6 + 5] = results[0].det_result->boxes[i].coordinate[3];
  114. }
  115. memcpy(LabelList, label.c_str(), strlen(label.c_str()));
  116. }
  117. /*
  118. * Segmented inference
  119. *
  120. * img: input for predicting.
  121. *
  122. * nWidth: width of img.
  123. *
  124. * nHeight: height of img.
  125. *
  126. * nChannel: channel of img.
  127. *
  128. * output: result of pridict ,include label_map
  129. *
  130. * extern "C"
  131. */
  132. extern "C" void Seg_ModelPredict(const unsigned char* img, int nWidth, int nHeight, int nChannel, unsigned char* output)
  133. {
  134. // prepare data
  135. std::vector<cv::Mat> imgs;
  136. int nType = 0;
  137. if (nChannel == 3)
  138. {
  139. nType = CV_8UC3;
  140. }
  141. else
  142. {
  143. std::cout << "Only support 3 channel image." << std::endl;
  144. return;
  145. }
  146. cv::Mat input = cv::Mat::zeros(cv::Size(nWidth, nHeight), nType);
  147. memcpy(input.data, img, nHeight * nWidth * nChannel * sizeof(uchar));
  148. imgs.push_back(std::move(input));
  149. // predict
  150. std::vector<PaddleDeploy::Result> results;
  151. model->Predict(imgs, &results, 1);
  152. std::vector<uint8_t> result_map = results[0].seg_result->label_map.data; // vector<uint8_t> -- Result Map
  153. // Copy output result to the output back -- from vector<uint8_t> to unsigned char *
  154. memcpy(output, &result_map[0], result_map.size() * sizeof(uchar));
  155. }
  156. /*
  157. * Recognition inference API
  158. *
  159. * img: input for predicting.
  160. *
  161. * nWidth: width of img.
  162. *
  163. * nHeight: height of img.
  164. *
  165. * nChannel: channel of img.
  166. *
  167. * score: result of pridict ,include score
  168. *
  169. * category: result of pridict ,include category_string
  170. *
  171. * category_id: result of pridict ,include category_id
  172. *
  173. * extern "C"
  174. */
  175. extern "C" void Cls_ModelPredict(const unsigned char* img, int nWidth, int nHeight, int nChannel, float* score, char* category, int* category_id)
  176. {
  177. // prepare data
  178. std::vector<cv::Mat> imgs;
  179. int nType = 0;
  180. if (nChannel == 3)
  181. {
  182. nType = CV_8UC3;
  183. }
  184. else
  185. {
  186. std::cout << "Only support 3 channel image." << std::endl;
  187. return;
  188. }
  189. cv::Mat input = cv::Mat::zeros(cv::Size(nWidth, nHeight), nType);
  190. memcpy(input.data, img, nHeight * nWidth * nChannel * sizeof(uchar));
  191. imgs.push_back(std::move(input));
  192. // predict
  193. std::vector<PaddleDeploy::Result> results;
  194. model->Predict(imgs, &results, 1);
  195. *category_id = results[0].clas_result->category_id;
  196. // Copy output category result to output -- string --> char*
  197. memcpy(category, results[0].clas_result->category.c_str(), strlen(results[0].clas_result->category.c_str()));
  198. // Copy output probability value
  199. *score = results[0].clas_result->score;
  200. }
  201. /*
  202. * MaskRCNN Reasoning
  203. *
  204. * img: input for predicting.
  205. *
  206. * nWidth: width of img.
  207. *
  208. * nHeight: height of img.
  209. *
  210. * nChannel: channel of img.
  211. *
  212. * box_output: result of pridict ,include label+score+bbox
  213. *
  214. * mask_output: result of pridict ,include label_map
  215. *
  216. * nBoxesNum: result of pridict ,include BoxesNum
  217. *
  218. * LabelList: result of pridict ,include LabelList
  219. *
  220. * extern "C"
  221. */
  222. extern "C" void Mask_ModelPredict(const unsigned char* img, int nWidth, int nHeight, int nChannel, float* box_output, unsigned char* mask_output, int* nBoxesNum, char* LabelList)
  223. {
  224. // prepare data
  225. std::vector<cv::Mat> imgs;
  226. int nType = 0;
  227. if (nChannel == 3)
  228. {
  229. nType = CV_8UC3;
  230. }
  231. else
  232. {
  233. std::cout << "Only support 3 channel image." << std::endl;
  234. return;
  235. }
  236. cv::Mat input = cv::Mat::zeros(cv::Size(nWidth, nHeight), nType);
  237. memcpy(input.data, img, nHeight * nWidth * nChannel * sizeof(uchar));
  238. imgs.push_back(std::move(input));
  239. // predict
  240. std::vector<PaddleDeploy::Result> results;
  241. model->Predict(imgs, &results, 1);
  242. nBoxesNum[0] = results[0].det_result->boxes.size(); // Get the predicted Bounding Box number of a single image
  243. std::string label = "";
  244. for (int i = 0; i < results[0].det_result->boxes.size(); i++) // Get the data for all the boxes
  245. {
  246. // prediction results
  247. label = label + results[0].det_result->boxes[i].category + " ";
  248. // labelindex
  249. box_output[i * 6 + 0] = results[0].det_result->boxes[i].category_id; // Category ID
  250. // score
  251. box_output[i * 6 + 1] = results[0].det_result->boxes[i].score; // Score
  252. //// box
  253. box_output[i * 6 + 2] = results[0].det_result->boxes[i].coordinate[0]; // x1, y1, x2, y2
  254. box_output[i * 6 + 3] = results[0].det_result->boxes[i].coordinate[1]; // Upper left and lower right vertices
  255. box_output[i * 6 + 4] = results[0].det_result->boxes[i].coordinate[2];
  256. box_output[i * 6 + 5] = results[0].det_result->boxes[i].coordinate[3];
  257. // Mask prediction results
  258. for (int j = 0; j < results[0].det_result->boxes[i].mask.data.size(); j++)
  259. {
  260. if (mask_output[j] == 0)
  261. {
  262. mask_output[j] = results[0].det_result->boxes[i].mask.data[j];
  263. }
  264. }
  265. }
  266. memcpy(LabelList, label.c_str(), strlen(label.c_str()));
  267. }
  268. /*
  269. * Model destruction API
  270. *
  271. * extern "C"
  272. */
  273. extern "C" void DestructModel()
  274. {
  275. delete model;
  276. std::cout << "destruct model success" << std::endl;
  277. }