main.cpp 7.0 KB

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