main.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "picodet_mnn.hpp"
  15. #include <iostream>
  16. #include <opencv2/core/core.hpp>
  17. #include <opencv2/highgui/highgui.hpp>
  18. #include <opencv2/imgproc/imgproc.hpp>
  19. #define __SAVE_RESULT__ // if defined save drawed results to ../results, else
  20. // show it in windows
  21. struct object_rect {
  22. int x;
  23. int y;
  24. int width;
  25. int height;
  26. };
  27. std::vector<int> GenerateColorMap(int num_class) {
  28. auto colormap = std::vector<int>(3 * num_class, 0);
  29. for (int i = 0; i < num_class; ++i) {
  30. int j = 0;
  31. int lab = i;
  32. while (lab) {
  33. colormap[i * 3] |= (((lab >> 0) & 1) << (7 - j));
  34. colormap[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j));
  35. colormap[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j));
  36. ++j;
  37. lab >>= 3;
  38. }
  39. }
  40. return colormap;
  41. }
  42. void draw_bboxes(const cv::Mat &im, const std::vector<BoxInfo> &bboxes,
  43. std::string save_path = "None") {
  44. static const char *class_names[] = {
  45. "person", "bicycle", "car",
  46. "motorcycle", "airplane", "bus",
  47. "train", "truck", "boat",
  48. "traffic light", "fire hydrant", "stop sign",
  49. "parking meter", "bench", "bird",
  50. "cat", "dog", "horse",
  51. "sheep", "cow", "elephant",
  52. "bear", "zebra", "giraffe",
  53. "backpack", "umbrella", "handbag",
  54. "tie", "suitcase", "frisbee",
  55. "skis", "snowboard", "sports ball",
  56. "kite", "baseball bat", "baseball glove",
  57. "skateboard", "surfboard", "tennis racket",
  58. "bottle", "wine glass", "cup",
  59. "fork", "knife", "spoon",
  60. "bowl", "banana", "apple",
  61. "sandwich", "orange", "broccoli",
  62. "carrot", "hot dog", "pizza",
  63. "donut", "cake", "chair",
  64. "couch", "potted plant", "bed",
  65. "dining table", "toilet", "tv",
  66. "laptop", "mouse", "remote",
  67. "keyboard", "cell phone", "microwave",
  68. "oven", "toaster", "sink",
  69. "refrigerator", "book", "clock",
  70. "vase", "scissors", "teddy bear",
  71. "hair drier", "toothbrush"};
  72. cv::Mat image = im.clone();
  73. int src_w = image.cols;
  74. int src_h = image.rows;
  75. int thickness = 2;
  76. auto colormap = GenerateColorMap(sizeof(class_names));
  77. for (size_t i = 0; i < bboxes.size(); i++) {
  78. const BoxInfo &bbox = bboxes[i];
  79. std::cout << bbox.x1 << ". " << bbox.y1 << ". " << bbox.x2 << ". "
  80. << bbox.y2 << ". " << std::endl;
  81. int c1 = colormap[3 * bbox.label + 0];
  82. int c2 = colormap[3 * bbox.label + 1];
  83. int c3 = colormap[3 * bbox.label + 2];
  84. cv::Scalar color = cv::Scalar(c1, c2, c3);
  85. // cv::Scalar color = cv::Scalar(0, 0, 255);
  86. cv::rectangle(image, cv::Rect(cv::Point(bbox.x1, bbox.y1),
  87. cv::Point(bbox.x2, bbox.y2)),
  88. color, 1, cv::LINE_AA);
  89. char text[256];
  90. sprintf(text, "%s %.1f%%", class_names[bbox.label], bbox.score * 100);
  91. int baseLine = 0;
  92. cv::Size label_size =
  93. cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine);
  94. int x = bbox.x1;
  95. int y = bbox.y1 - label_size.height - baseLine;
  96. if (y < 0)
  97. y = 0;
  98. if (x + label_size.width > image.cols)
  99. x = image.cols - label_size.width;
  100. cv::rectangle(image, cv::Rect(cv::Point(x, y),
  101. cv::Size(label_size.width,
  102. label_size.height + baseLine)),
  103. color, -1);
  104. cv::putText(image, text, cv::Point(x, y + label_size.height),
  105. cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255), 1,
  106. cv::LINE_AA);
  107. }
  108. if (save_path == "None") {
  109. cv::imshow("image", image);
  110. } else {
  111. cv::imwrite(save_path, image);
  112. std::cout << save_path << std::endl;
  113. }
  114. }
  115. int image_demo(PicoDet &detector, const char *imagepath) {
  116. std::vector<cv::String> filenames;
  117. cv::glob(imagepath, filenames, false);
  118. for (auto img_name : filenames) {
  119. cv::Mat image = cv::imread(img_name, cv::IMREAD_COLOR);
  120. if (image.empty()) {
  121. fprintf(stderr, "cv::imread %s failed\n", img_name.c_str());
  122. return -1;
  123. }
  124. std::vector<BoxInfo> results;
  125. detector.detect(image, results, false);
  126. std::cout << "detect done." << std::endl;
  127. #ifdef __SAVE_RESULT__
  128. std::string save_path = img_name;
  129. draw_bboxes(image, results, save_path.replace(3, 4, "results"));
  130. #else
  131. draw_bboxes(image, results);
  132. cv::waitKey(0);
  133. #endif
  134. }
  135. return 0;
  136. }
  137. int benchmark(PicoDet &detector, int width, int height) {
  138. int loop_num = 100;
  139. int warm_up = 8;
  140. double time_min = DBL_MAX;
  141. double time_max = -DBL_MAX;
  142. double time_avg = 0;
  143. cv::Mat image(width, height, CV_8UC3, cv::Scalar(1, 1, 1));
  144. for (int i = 0; i < warm_up + loop_num; i++) {
  145. auto start = std::chrono::steady_clock::now();
  146. std::vector<BoxInfo> results;
  147. detector.detect(image, results, false);
  148. auto end = std::chrono::steady_clock::now();
  149. std::chrono::duration<double> elapsed = end - start;
  150. double time = elapsed.count();
  151. if (i >= warm_up) {
  152. time_min = (std::min)(time_min, time);
  153. time_max = (std::max)(time_max, time);
  154. time_avg += time;
  155. }
  156. }
  157. time_avg /= loop_num;
  158. fprintf(stderr, "%20s min = %7.2f max = %7.2f avg = %7.2f\n", "picodet",
  159. time_min, time_max, time_avg);
  160. return 0;
  161. }
  162. int main(int argc, char **argv) {
  163. int mode = atoi(argv[1]);
  164. std::string model_path = argv[2];
  165. int height = 320;
  166. int width = 320;
  167. if (argc == 4) {
  168. height = atoi(argv[3]);
  169. width = atoi(argv[4]);
  170. }
  171. PicoDet detector = PicoDet(model_path, width, height, 4, 0.45, 0.3);
  172. if (mode == 1) {
  173. benchmark(detector, width, height);
  174. } else {
  175. if (argc != 5) {
  176. std::cout << "Must set image file, such as ./picodet-mnn 0 "
  177. "../picodet_s_320_lcnet.mnn 320 320 img.jpg"
  178. << std::endl;
  179. }
  180. const char *images = argv[5];
  181. image_demo(detector, images);
  182. }
  183. }