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.

yolov3.cpp 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2018 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 <stdio.h>
  15. #include <vector>
  16. #include <opencv2/core/core.hpp>
  17. #include <opencv2/highgui/highgui.hpp>
  18. #include <opencv2/imgproc/imgproc.hpp>
  19. #include "platform.h"
  20. #include "net.h"
  21. #if NCNN_VULKAN
  22. #include "gpu.h"
  23. #endif // NCNN_VULKAN
  24. struct Object
  25. {
  26. cv::Rect_<float> rect;
  27. int label;
  28. float prob;
  29. };
  30. static int detect_yolov3(const cv::Mat& bgr, std::vector<Object>& objects)
  31. {
  32. ncnn::Net yolov3;
  33. #if NCNN_VULKAN
  34. yolov3.opt.use_vulkan_compute = true;
  35. #endif // NCNN_VULKAN
  36. // original pretrained model from https://github.com/eric612/MobileNet-YOLO
  37. // param : https://drive.google.com/open?id=1V9oKHP6G6XvXZqhZbzNKL6FI_clRWdC-
  38. // bin : https://drive.google.com/open?id=1DBcuFCr-856z3FRQznWL_S5h-Aj3RawA
  39. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  40. yolov3.load_param("mobilenetv2_yolov3.param");
  41. yolov3.load_model("mobilenetv2_yolov3.bin");
  42. const int target_size = 352;
  43. int img_w = bgr.cols;
  44. int img_h = bgr.rows;
  45. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  46. const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
  47. const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
  48. in.substract_mean_normalize(mean_vals, norm_vals);
  49. ncnn::Extractor ex = yolov3.create_extractor();
  50. ex.set_num_threads(4);
  51. ex.input("data", in);
  52. ncnn::Mat out;
  53. ex.extract("detection_out", out);
  54. // printf("%d %d %d\n", out.w, out.h, out.c);
  55. objects.clear();
  56. for (int i=0; i<out.h; i++)
  57. {
  58. const float* values = out.row(i);
  59. Object object;
  60. object.label = values[0];
  61. object.prob = values[1];
  62. object.rect.x = values[2] * img_w;
  63. object.rect.y = values[3] * img_h;
  64. object.rect.width = values[4] * img_w - object.rect.x;
  65. object.rect.height = values[5] * img_h - object.rect.y;
  66. objects.push_back(object);
  67. }
  68. return 0;
  69. }
  70. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
  71. {
  72. static const char* class_names[] = {"background",
  73. "aeroplane", "bicycle", "bird", "boat",
  74. "bottle", "bus", "car", "cat", "chair",
  75. "cow", "diningtable", "dog", "horse",
  76. "motorbike", "person", "pottedplant",
  77. "sheep", "sofa", "train", "tvmonitor"};
  78. cv::Mat image = bgr.clone();
  79. for (size_t i = 0; i < objects.size(); i++)
  80. {
  81. const Object& obj = objects[i];
  82. fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
  83. obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
  84. cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
  85. char text[256];
  86. sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
  87. int baseLine = 0;
  88. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  89. int x = obj.rect.x;
  90. int y = obj.rect.y - label_size.height - baseLine;
  91. if (y < 0)
  92. y = 0;
  93. if (x + label_size.width > image.cols)
  94. x = image.cols - label_size.width;
  95. cv::rectangle(image, cv::Rect(cv::Point(x, y),
  96. cv::Size(label_size.width, label_size.height + baseLine)),
  97. cv::Scalar(255, 255, 255), -1);
  98. cv::putText(image, text, cv::Point(x, y + label_size.height),
  99. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  100. }
  101. cv::imshow("image", image);
  102. cv::waitKey(0);
  103. }
  104. int main(int argc, char** argv)
  105. {
  106. if (argc != 2)
  107. {
  108. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  109. return -1;
  110. }
  111. const char* imagepath = argv[1];
  112. cv::Mat m = cv::imread(imagepath, 1);
  113. if (m.empty())
  114. {
  115. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  116. return -1;
  117. }
  118. #if NCNN_VULKAN
  119. ncnn::create_gpu_instance();
  120. #endif // NCNN_VULKAN
  121. std::vector<Object> objects;
  122. detect_yolov3(m, objects);
  123. #if NCNN_VULKAN
  124. ncnn::destroy_gpu_instance();
  125. #endif // NCNN_VULKAN
  126. draw_objects(m, objects);
  127. return 0;
  128. }