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 6.2 kB

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