You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

yolov4.cpp 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "net.h"
  4. #include <opencv2/core/core.hpp>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #include <opencv2/imgproc/imgproc.hpp>
  7. #if CV_MAJOR_VERSION >= 3
  8. #include <opencv2/videoio/videoio.hpp>
  9. #endif
  10. #include <vector>
  11. #include <stdio.h>
  12. #define NCNN_PROFILING
  13. #define YOLOV4_TINY //Using yolov4_tiny, if undef, using original yolov4
  14. #ifdef NCNN_PROFILING
  15. #include "benchmark.h"
  16. #endif
  17. struct Object
  18. {
  19. cv::Rect_<float> rect;
  20. int label;
  21. float prob;
  22. };
  23. static int init_yolov4(ncnn::Net* yolov4, int* target_size)
  24. {
  25. /* --> Set the params you need for the ncnn inference <-- */
  26. yolov4->opt.num_threads = 4; //You need to compile with libgomp for multi thread support
  27. yolov4->opt.use_vulkan_compute = true; //You need to compile with libvulkan for gpu support
  28. yolov4->opt.use_winograd_convolution = true;
  29. yolov4->opt.use_sgemm_convolution = true;
  30. yolov4->opt.use_fp16_packed = true;
  31. yolov4->opt.use_fp16_storage = true;
  32. yolov4->opt.use_fp16_arithmetic = true;
  33. yolov4->opt.use_packing_layout = true;
  34. yolov4->opt.use_shader_pack8 = false;
  35. /* --> End of setting params <-- */
  36. int ret = 0;
  37. // original pretrained model from https://github.com/AlexeyAB/darknet
  38. // the ncnn model https://drive.google.com/drive/folders/1YzILvh0SKQPS_lrb33dmGNq7aVTKPWS0?usp=sharing
  39. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  40. #ifdef YOLOV4_TINY
  41. const char* yolov4_param = "yolov4-tiny-opt.param";
  42. const char* yolov4_model = "yolov4-tiny-opt.bin";
  43. *target_size = 416;
  44. #else
  45. const char* yolov4_param = "yolov4-opt.param";
  46. const char* yolov4_model = "yolov4-opt.bin";
  47. *target_size = 608;
  48. #endif
  49. if (yolov4->load_param(yolov4_param))
  50. exit(-1);
  51. if (yolov4->load_model(yolov4_model))
  52. exit(-1);
  53. return 0;
  54. }
  55. static int detect_yolov4(const cv::Mat& bgr, std::vector<Object>& objects, int target_size, ncnn::Net* yolov4)
  56. {
  57. int img_w = bgr.cols;
  58. int img_h = bgr.rows;
  59. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_size, target_size);
  60. const float mean_vals[3] = {0, 0, 0};
  61. const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
  62. in.substract_mean_normalize(mean_vals, norm_vals);
  63. ncnn::Extractor ex = yolov4->create_extractor();
  64. ex.input("data", in);
  65. ncnn::Mat out;
  66. ex.extract("output", out);
  67. objects.clear();
  68. for (int i = 0; i < out.h; i++)
  69. {
  70. const float* values = out.row(i);
  71. Object object;
  72. object.label = values[0];
  73. object.prob = values[1];
  74. object.rect.x = values[2] * img_w;
  75. object.rect.y = values[3] * img_h;
  76. object.rect.width = values[4] * img_w - object.rect.x;
  77. object.rect.height = values[5] * img_h - object.rect.y;
  78. objects.push_back(object);
  79. }
  80. return 0;
  81. }
  82. static int draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects, int is_streaming)
  83. {
  84. static const char* class_names[] = {"background", "person", "bicycle",
  85. "car", "motorbike", "aeroplane", "bus", "train", "truck",
  86. "boat", "traffic light", "fire hydrant", "stop sign",
  87. "parking meter", "bench", "bird", "cat", "dog", "horse",
  88. "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
  89. "backpack", "umbrella", "handbag", "tie", "suitcase",
  90. "frisbee", "skis", "snowboard", "sports ball", "kite",
  91. "baseball bat", "baseball glove", "skateboard", "surfboard",
  92. "tennis racket", "bottle", "wine glass", "cup", "fork",
  93. "knife", "spoon", "bowl", "banana", "apple", "sandwich",
  94. "orange", "broccoli", "carrot", "hot dog", "pizza", "donut",
  95. "cake", "chair", "sofa", "pottedplant", "bed", "diningtable",
  96. "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard",
  97. "cell phone", "microwave", "oven", "toaster", "sink",
  98. "refrigerator", "book", "clock", "vase", "scissors",
  99. "teddy bear", "hair drier", "toothbrush"
  100. };
  101. cv::Mat image = bgr.clone();
  102. for (size_t i = 0; i < objects.size(); i++)
  103. {
  104. const Object& obj = objects[i];
  105. fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
  106. obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
  107. cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
  108. char text[256];
  109. sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
  110. int baseLine = 0;
  111. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  112. int x = obj.rect.x;
  113. int y = obj.rect.y - label_size.height - baseLine;
  114. if (y < 0)
  115. y = 0;
  116. if (x + label_size.width > image.cols)
  117. x = image.cols - label_size.width;
  118. cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
  119. cv::Scalar(255, 255, 255), -1);
  120. cv::putText(image, text, cv::Point(x, y + label_size.height),
  121. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  122. }
  123. cv::imshow("image", image);
  124. if (is_streaming)
  125. {
  126. cv::waitKey(1);
  127. }
  128. else
  129. {
  130. cv::waitKey(0);
  131. }
  132. return 0;
  133. }
  134. int main(int argc, char** argv)
  135. {
  136. cv::Mat frame;
  137. std::vector<Object> objects;
  138. cv::VideoCapture cap;
  139. ncnn::Net yolov4;
  140. const char* devicepath;
  141. int target_size = 0;
  142. int is_streaming = 0;
  143. if (argc < 2)
  144. {
  145. fprintf(stderr, "Usage: %s [v4l input device or image]\n", argv[0]);
  146. return -1;
  147. }
  148. devicepath = argv[1];
  149. #ifdef NCNN_PROFILING
  150. double t_load_start = ncnn::get_current_time();
  151. #endif
  152. int ret = init_yolov4(&yolov4, &target_size); //We load model and param first!
  153. if (ret != 0)
  154. {
  155. fprintf(stderr, "Failed to load model or param, error %d", ret);
  156. return -1;
  157. }
  158. #ifdef NCNN_PROFILING
  159. double t_load_end = ncnn::get_current_time();
  160. fprintf(stdout, "NCNN Init time %.02lfms\n", t_load_end - t_load_start);
  161. #endif
  162. if (strstr(devicepath, "/dev/video") == NULL)
  163. {
  164. frame = cv::imread(argv[1], 1);
  165. if (frame.empty())
  166. {
  167. fprintf(stderr, "Failed to read image %s.\n", argv[1]);
  168. return -1;
  169. }
  170. }
  171. else
  172. {
  173. cap.open(devicepath);
  174. if (!cap.isOpened())
  175. {
  176. fprintf(stderr, "Failed to open %s", devicepath);
  177. return -1;
  178. }
  179. cap >> frame;
  180. if (frame.empty())
  181. {
  182. fprintf(stderr, "Failed to read from device %s.\n", devicepath);
  183. return -1;
  184. }
  185. is_streaming = 1;
  186. }
  187. while (1)
  188. {
  189. if (is_streaming)
  190. {
  191. #ifdef NCNN_PROFILING
  192. double t_capture_start = ncnn::get_current_time();
  193. #endif
  194. cap >> frame;
  195. #ifdef NCNN_PROFILING
  196. double t_capture_end = ncnn::get_current_time();
  197. fprintf(stdout, "NCNN OpenCV capture time %.02lfms\n", t_capture_end - t_capture_start);
  198. #endif
  199. if (frame.empty())
  200. {
  201. fprintf(stderr, "OpenCV Failed to Capture from device %s\n", devicepath);
  202. return -1;
  203. }
  204. }
  205. #ifdef NCNN_PROFILING
  206. double t_detect_start = ncnn::get_current_time();
  207. #endif
  208. detect_yolov4(frame, objects, target_size, &yolov4); //Create an extractor and run detection
  209. #ifdef NCNN_PROFILING
  210. double t_detect_end = ncnn::get_current_time();
  211. fprintf(stdout, "NCNN detection time %.02lfms\n", t_detect_end - t_detect_start);
  212. #endif
  213. #ifdef NCNN_PROFILING
  214. double t_draw_start = ncnn::get_current_time();
  215. #endif
  216. draw_objects(frame, objects, is_streaming); //Draw detection results on opencv image
  217. #ifdef NCNN_PROFILING
  218. double t_draw_end = ncnn::get_current_time();
  219. fprintf(stdout, "NCNN OpenCV draw result time %.02lfms\n", t_draw_end - t_draw_start);
  220. #endif
  221. if (!is_streaming)
  222. { //If it is a still image, exit!
  223. return 0;
  224. }
  225. }
  226. return 0;
  227. }