main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Copyright (c) 2021 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. // reference from https://github.com/RangiLyu/nanodet
  15. #include "picodet_openvino.h"
  16. #include <iostream>
  17. #include <opencv2/core/core.hpp>
  18. #include <opencv2/highgui/highgui.hpp>
  19. #include <opencv2/imgproc/imgproc.hpp>
  20. #define image_size 416
  21. struct object_rect {
  22. int x;
  23. int y;
  24. int width;
  25. int height;
  26. };
  27. int resize_uniform(cv::Mat &src, cv::Mat &dst, cv::Size dst_size,
  28. object_rect &effect_area) {
  29. int w = src.cols;
  30. int h = src.rows;
  31. int dst_w = dst_size.width;
  32. int dst_h = dst_size.height;
  33. dst = cv::Mat(cv::Size(dst_w, dst_h), CV_8UC3, cv::Scalar(0));
  34. float ratio_src = w * 1.0 / h;
  35. float ratio_dst = dst_w * 1.0 / dst_h;
  36. int tmp_w = 0;
  37. int tmp_h = 0;
  38. if (ratio_src > ratio_dst) {
  39. tmp_w = dst_w;
  40. tmp_h = floor((dst_w * 1.0 / w) * h);
  41. } else if (ratio_src < ratio_dst) {
  42. tmp_h = dst_h;
  43. tmp_w = floor((dst_h * 1.0 / h) * w);
  44. } else {
  45. cv::resize(src, dst, dst_size);
  46. effect_area.x = 0;
  47. effect_area.y = 0;
  48. effect_area.width = dst_w;
  49. effect_area.height = dst_h;
  50. return 0;
  51. }
  52. cv::Mat tmp;
  53. cv::resize(src, tmp, cv::Size(tmp_w, tmp_h));
  54. if (tmp_w != dst_w) {
  55. int index_w = floor((dst_w - tmp_w) / 2.0);
  56. for (int i = 0; i < dst_h; i++) {
  57. memcpy(dst.data + i * dst_w * 3 + index_w * 3, tmp.data + i * tmp_w * 3,
  58. tmp_w * 3);
  59. }
  60. effect_area.x = index_w;
  61. effect_area.y = 0;
  62. effect_area.width = tmp_w;
  63. effect_area.height = tmp_h;
  64. } else if (tmp_h != dst_h) {
  65. int index_h = floor((dst_h - tmp_h) / 2.0);
  66. memcpy(dst.data + index_h * dst_w * 3, tmp.data, tmp_w * tmp_h * 3);
  67. effect_area.x = 0;
  68. effect_area.y = index_h;
  69. effect_area.width = tmp_w;
  70. effect_area.height = tmp_h;
  71. } else {
  72. printf("error\n");
  73. }
  74. return 0;
  75. }
  76. const int color_list[80][3] = {
  77. {216, 82, 24}, {236, 176, 31}, {125, 46, 141}, {118, 171, 47},
  78. {76, 189, 237}, {238, 19, 46}, {76, 76, 76}, {153, 153, 153},
  79. {255, 0, 0}, {255, 127, 0}, {190, 190, 0}, {0, 255, 0},
  80. {0, 0, 255}, {170, 0, 255}, {84, 84, 0}, {84, 170, 0},
  81. {84, 255, 0}, {170, 84, 0}, {170, 170, 0}, {170, 255, 0},
  82. {255, 84, 0}, {255, 170, 0}, {255, 255, 0}, {0, 84, 127},
  83. {0, 170, 127}, {0, 255, 127}, {84, 0, 127}, {84, 84, 127},
  84. {84, 170, 127}, {84, 255, 127}, {170, 0, 127}, {170, 84, 127},
  85. {170, 170, 127}, {170, 255, 127}, {255, 0, 127}, {255, 84, 127},
  86. {255, 170, 127}, {255, 255, 127}, {0, 84, 255}, {0, 170, 255},
  87. {0, 255, 255}, {84, 0, 255}, {84, 84, 255}, {84, 170, 255},
  88. {84, 255, 255}, {170, 0, 255}, {170, 84, 255}, {170, 170, 255},
  89. {170, 255, 255}, {255, 0, 255}, {255, 84, 255}, {255, 170, 255},
  90. {42, 0, 0}, {84, 0, 0}, {127, 0, 0}, {170, 0, 0},
  91. {212, 0, 0}, {255, 0, 0}, {0, 42, 0}, {0, 84, 0},
  92. {0, 127, 0}, {0, 170, 0}, {0, 212, 0}, {0, 255, 0},
  93. {0, 0, 42}, {0, 0, 84}, {0, 0, 127}, {0, 0, 170},
  94. {0, 0, 212}, {0, 0, 255}, {0, 0, 0}, {36, 36, 36},
  95. {72, 72, 72}, {109, 109, 109}, {145, 145, 145}, {182, 182, 182},
  96. {218, 218, 218}, {0, 113, 188}, {80, 182, 188}, {127, 127, 0},
  97. };
  98. void draw_bboxes(const cv::Mat &bgr, const std::vector<BoxInfo> &bboxes,
  99. object_rect effect_roi) {
  100. static const char *class_names[] = {
  101. "person", "bicycle", "car",
  102. "motorcycle", "airplane", "bus",
  103. "train", "truck", "boat",
  104. "traffic light", "fire hydrant", "stop sign",
  105. "parking meter", "bench", "bird",
  106. "cat", "dog", "horse",
  107. "sheep", "cow", "elephant",
  108. "bear", "zebra", "giraffe",
  109. "backpack", "umbrella", "handbag",
  110. "tie", "suitcase", "frisbee",
  111. "skis", "snowboard", "sports ball",
  112. "kite", "baseball bat", "baseball glove",
  113. "skateboard", "surfboard", "tennis racket",
  114. "bottle", "wine glass", "cup",
  115. "fork", "knife", "spoon",
  116. "bowl", "banana", "apple",
  117. "sandwich", "orange", "broccoli",
  118. "carrot", "hot dog", "pizza",
  119. "donut", "cake", "chair",
  120. "couch", "potted plant", "bed",
  121. "dining table", "toilet", "tv",
  122. "laptop", "mouse", "remote",
  123. "keyboard", "cell phone", "microwave",
  124. "oven", "toaster", "sink",
  125. "refrigerator", "book", "clock",
  126. "vase", "scissors", "teddy bear",
  127. "hair drier", "toothbrush"};
  128. cv::Mat image = bgr.clone();
  129. int src_w = image.cols;
  130. int src_h = image.rows;
  131. int dst_w = effect_roi.width;
  132. int dst_h = effect_roi.height;
  133. float width_ratio = (float)src_w / (float)dst_w;
  134. float height_ratio = (float)src_h / (float)dst_h;
  135. for (size_t i = 0; i < bboxes.size(); i++) {
  136. const BoxInfo &bbox = bboxes[i];
  137. cv::Scalar color =
  138. cv::Scalar(color_list[bbox.label][0], color_list[bbox.label][1],
  139. color_list[bbox.label][2]);
  140. cv::rectangle(image,
  141. cv::Rect(cv::Point((bbox.x1 - effect_roi.x) * width_ratio,
  142. (bbox.y1 - effect_roi.y) * height_ratio),
  143. cv::Point((bbox.x2 - effect_roi.x) * width_ratio,
  144. (bbox.y2 - effect_roi.y) * height_ratio)),
  145. color);
  146. char text[256];
  147. sprintf(text, "%s %.1f%%", class_names[bbox.label], bbox.score * 100);
  148. int baseLine = 0;
  149. cv::Size label_size =
  150. cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine);
  151. int x = (bbox.x1 - effect_roi.x) * width_ratio;
  152. int y =
  153. (bbox.y1 - effect_roi.y) * height_ratio - label_size.height - baseLine;
  154. if (y < 0)
  155. y = 0;
  156. if (x + label_size.width > image.cols)
  157. x = image.cols - label_size.width;
  158. cv::rectangle(image, cv::Rect(cv::Point(x, y),
  159. cv::Size(label_size.width,
  160. label_size.height + baseLine)),
  161. color, -1);
  162. cv::putText(image, text, cv::Point(x, y + label_size.height),
  163. cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255));
  164. }
  165. cv::imwrite("../predict.jpg", image);
  166. }
  167. int image_demo(PicoDet &detector, const char *imagepath) {
  168. std::vector<std::string> filenames;
  169. cv::glob(imagepath, filenames, false);
  170. for (auto img_name : filenames) {
  171. cv::Mat image = cv::imread(img_name);
  172. if (image.empty()) {
  173. return -1;
  174. }
  175. object_rect effect_roi;
  176. cv::Mat resized_img;
  177. resize_uniform(image, resized_img, cv::Size(image_size, image_size),
  178. effect_roi);
  179. auto results = detector.detect(resized_img, 0.4, 0.5);
  180. draw_bboxes(image, results, effect_roi);
  181. }
  182. return 0;
  183. }
  184. int webcam_demo(PicoDet &detector, int cam_id) {
  185. cv::Mat image;
  186. cv::VideoCapture cap(cam_id);
  187. while (true) {
  188. cap >> image;
  189. object_rect effect_roi;
  190. cv::Mat resized_img;
  191. resize_uniform(image, resized_img, cv::Size(image_size, image_size),
  192. effect_roi);
  193. auto results = detector.detect(resized_img, 0.4, 0.5);
  194. draw_bboxes(image, results, effect_roi);
  195. cv::waitKey(1);
  196. }
  197. return 0;
  198. }
  199. int video_demo(PicoDet &detector, const char *path) {
  200. cv::Mat image;
  201. cv::VideoCapture cap(path);
  202. while (true) {
  203. cap >> image;
  204. object_rect effect_roi;
  205. cv::Mat resized_img;
  206. resize_uniform(image, resized_img, cv::Size(image_size, image_size),
  207. effect_roi);
  208. auto results = detector.detect(resized_img, 0.4, 0.5);
  209. draw_bboxes(image, results, effect_roi);
  210. cv::waitKey(1);
  211. }
  212. return 0;
  213. }
  214. int benchmark(PicoDet &detector) {
  215. int loop_num = 100;
  216. int warm_up = 8;
  217. double time_min = DBL_MAX;
  218. double time_max = -DBL_MAX;
  219. double time_avg = 0;
  220. cv::Mat image(image_size, image_size, CV_8UC3, cv::Scalar(1, 1, 1));
  221. for (int i = 0; i < warm_up + loop_num; i++) {
  222. auto start = std::chrono::steady_clock::now();
  223. std::vector<BoxInfo> results;
  224. results = detector.detect(image, 0.4, 0.5);
  225. auto end = std::chrono::steady_clock::now();
  226. double time =
  227. std::chrono::duration<double, std::milli>(end - start).count();
  228. if (i >= warm_up) {
  229. time_min = (std::min)(time_min, time);
  230. time_max = (std::max)(time_max, time);
  231. time_avg += time;
  232. }
  233. }
  234. time_avg /= loop_num;
  235. fprintf(stderr, "%20s min = %7.2f max = %7.2f avg = %7.2f\n", "picodet",
  236. time_min, time_max, time_avg);
  237. return 0;
  238. }
  239. int main(int argc, char **argv) {
  240. if (argc != 3) {
  241. fprintf(stderr, "usage: %s [mode] [path]. \n For webcam mode=0, path is "
  242. "cam id; \n For image demo, mode=1, path=xxx/xxx/*.jpg; \n "
  243. "For video, mode=2; \n For benchmark, mode=3 path=0.\n",
  244. argv[0]);
  245. return -1;
  246. }
  247. std::cout << "start init model" << std::endl;
  248. auto detector = PicoDet("../weight/picodet_m_416.xml");
  249. std::cout << "success" << std::endl;
  250. int mode = atoi(argv[1]);
  251. switch (mode) {
  252. case 0: {
  253. int cam_id = atoi(argv[2]);
  254. webcam_demo(detector, cam_id);
  255. break;
  256. }
  257. case 1: {
  258. const char *images = argv[2];
  259. image_demo(detector, images);
  260. break;
  261. }
  262. case 2: {
  263. const char *path = argv[2];
  264. video_demo(detector, path);
  265. break;
  266. }
  267. case 3: {
  268. benchmark(detector);
  269. break;
  270. }
  271. default: {
  272. fprintf(stderr, "usage: %s [mode] [path]. \n For webcam mode=0, path is "
  273. "cam id; \n For image demo, mode=1, path=xxx/xxx/*.jpg; \n "
  274. "For video, mode=2; \n For benchmark, mode=3 path=0.\n",
  275. argv[0]);
  276. break;
  277. }
  278. }
  279. }