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.

peleenetssd_seg.cpp 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2017 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "net.h"
  4. #if defined(USE_NCNN_SIMPLEOCV)
  5. #include "simpleocv.h"
  6. #else
  7. #include <opencv2/core/core.hpp>
  8. #include <opencv2/highgui/highgui.hpp>
  9. #include <opencv2/imgproc/imgproc.hpp>
  10. #endif
  11. #include <stdio.h>
  12. #include <vector>
  13. struct Object
  14. {
  15. cv::Rect_<float> rect;
  16. int label;
  17. float prob;
  18. };
  19. static int detect_peleenet(const cv::Mat& bgr, std::vector<Object>& objects, ncnn::Mat& resized)
  20. {
  21. ncnn::Net peleenet;
  22. peleenet.opt.use_vulkan_compute = true;
  23. // model is converted from https://github.com/eric612/MobileNet-YOLO
  24. // and can be downloaded from https://drive.google.com/open?id=1Wt6jKv13sBRMHgrGAJYlOlRF-o80pC0g
  25. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  26. if (peleenet.load_param("pelee.param"))
  27. exit(-1);
  28. if (peleenet.load_model("pelee.bin"))
  29. exit(-1);
  30. const int target_size = 304;
  31. int img_w = bgr.cols;
  32. int img_h = bgr.rows;
  33. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  34. const float mean_vals[3] = {103.9f, 116.7f, 123.6f};
  35. const float norm_vals[3] = {0.017f, 0.017f, 0.017f};
  36. in.substract_mean_normalize(mean_vals, norm_vals);
  37. ncnn::Extractor ex = peleenet.create_extractor();
  38. ex.input("data", in);
  39. ncnn::Mat out;
  40. ex.extract("detection_out", out);
  41. // printf("%d %d %d\n", out.w, out.h, out.c);
  42. objects.clear();
  43. for (int i = 0; i < out.h; i++)
  44. {
  45. const float* values = out.row(i);
  46. Object object;
  47. object.label = values[0];
  48. object.prob = values[1];
  49. object.rect.x = values[2] * img_w;
  50. object.rect.y = values[3] * img_h;
  51. object.rect.width = values[4] * img_w - object.rect.x;
  52. object.rect.height = values[5] * img_h - object.rect.y;
  53. objects.push_back(object);
  54. }
  55. ncnn::Mat seg_out;
  56. ex.extract("sigmoid", seg_out);
  57. resize_bilinear(seg_out, resized, img_w, img_h);
  58. //resize_bicubic(seg_out,resized,img_w,img_h); // sharpness
  59. return 0;
  60. }
  61. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects, ncnn::Mat map)
  62. {
  63. static const char* class_names[] = {"background",
  64. "person", "rider", "car", "bus",
  65. "truck", "bike", "motor",
  66. "traffic light", "traffic sign", "train"
  67. };
  68. cv::Mat image = bgr.clone();
  69. const int color[] = {128, 255, 128, 244, 35, 232};
  70. const int color_count = sizeof(color) / sizeof(int);
  71. for (size_t i = 0; i < objects.size(); i++)
  72. {
  73. const Object& obj = objects[i];
  74. fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
  75. obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
  76. cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
  77. char text[256];
  78. sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
  79. int baseLine = 0;
  80. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  81. int x = obj.rect.x;
  82. int y = obj.rect.y - label_size.height - baseLine;
  83. if (y < 0)
  84. y = 0;
  85. if (x + label_size.width > image.cols)
  86. x = image.cols - label_size.width;
  87. cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
  88. cv::Scalar(255, 255, 255), -1);
  89. cv::putText(image, text, cv::Point(x, y + label_size.height),
  90. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  91. }
  92. int width = map.w;
  93. int height = map.h;
  94. int size = map.c;
  95. int img_index2 = 0;
  96. float threshold = 0.45;
  97. const float* ptr2 = map;
  98. for (int i = 0; i < height; i++)
  99. {
  100. unsigned char* ptr1 = image.ptr<unsigned char>(i);
  101. int img_index1 = 0;
  102. for (int j = 0; j < width; j++)
  103. {
  104. float maxima = threshold;
  105. int index = -1;
  106. for (int c = 0; c < size; c++)
  107. {
  108. //const float* ptr3 = map.channel(c);
  109. const float* ptr3 = ptr2 + c * width * height;
  110. if (ptr3[img_index2] > maxima)
  111. {
  112. maxima = ptr3[img_index2];
  113. index = c;
  114. }
  115. }
  116. if (index > -1)
  117. {
  118. int color_index = (index)*3;
  119. if (color_index < color_count)
  120. {
  121. int b = color[color_index];
  122. int g = color[color_index + 1];
  123. int r = color[color_index + 2];
  124. ptr1[img_index1] = b / 2 + ptr1[img_index1] / 2;
  125. ptr1[img_index1 + 1] = g / 2 + ptr1[img_index1 + 1] / 2;
  126. ptr1[img_index1 + 2] = r / 2 + ptr1[img_index1 + 2] / 2;
  127. }
  128. }
  129. img_index1 += 3;
  130. img_index2++;
  131. }
  132. }
  133. cv::imshow("image", image);
  134. cv::waitKey(0);
  135. }
  136. int main(int argc, char** argv)
  137. {
  138. if (argc != 2)
  139. {
  140. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  141. return -1;
  142. }
  143. const char* imagepath = argv[1];
  144. cv::Mat m = cv::imread(imagepath, 1);
  145. if (m.empty())
  146. {
  147. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  148. return -1;
  149. }
  150. std::vector<Object> objects;
  151. ncnn::Mat seg_out;
  152. detect_peleenet(m, objects, seg_out);
  153. draw_objects(m, objects, seg_out);
  154. return 0;
  155. }