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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. yolov3.load_param("mobilenetv2_yolov3.param");
  40. yolov3.load_model("mobilenetv2_yolov3.bin");
  41. const int target_size = 352;
  42. int img_w = bgr.cols;
  43. int img_h = bgr.rows;
  44. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  45. const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
  46. const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
  47. in.substract_mean_normalize(mean_vals, norm_vals);
  48. ncnn::Extractor ex = yolov3.create_extractor();
  49. ex.set_num_threads(4);
  50. ex.input("data", in);
  51. ncnn::Mat out;
  52. ex.extract("detection_out", out);
  53. // printf("%d %d %d\n", out.w, out.h, out.c);
  54. objects.clear();
  55. for (int i=0; i<out.h; i++)
  56. {
  57. const float* values = out.row(i);
  58. Object object;
  59. object.label = values[0];
  60. object.prob = values[1];
  61. object.rect.x = values[2] * img_w;
  62. object.rect.y = values[3] * img_h;
  63. object.rect.width = values[4] * img_w - object.rect.x;
  64. object.rect.height = values[5] * img_h - object.rect.y;
  65. objects.push_back(object);
  66. }
  67. return 0;
  68. }
  69. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
  70. {
  71. static const char* class_names[] = {"background",
  72. "aeroplane", "bicycle", "bird", "boat",
  73. "bottle", "bus", "car", "cat", "chair",
  74. "cow", "diningtable", "dog", "horse",
  75. "motorbike", "person", "pottedplant",
  76. "sheep", "sofa", "train", "tvmonitor"};
  77. cv::Mat image = bgr.clone();
  78. for (size_t i = 0; i < objects.size(); i++)
  79. {
  80. const Object& obj = objects[i];
  81. fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
  82. obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
  83. cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
  84. char text[256];
  85. sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
  86. int baseLine = 0;
  87. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  88. int x = obj.rect.x;
  89. int y = obj.rect.y - label_size.height - baseLine;
  90. if (y < 0)
  91. y = 0;
  92. if (x + label_size.width > image.cols)
  93. x = image.cols - label_size.width;
  94. cv::rectangle(image, cv::Rect(cv::Point(x, y),
  95. cv::Size(label_size.width, label_size.height + baseLine)),
  96. cv::Scalar(255, 255, 255), -1);
  97. cv::putText(image, text, cv::Point(x, y + label_size.height),
  98. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  99. }
  100. cv::imshow("image", image);
  101. cv::waitKey(0);
  102. }
  103. int main(int argc, char** argv)
  104. {
  105. if (argc != 2)
  106. {
  107. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  108. return -1;
  109. }
  110. const char* imagepath = argv[1];
  111. cv::Mat m = cv::imread(imagepath, 1);
  112. if (m.empty())
  113. {
  114. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  115. return -1;
  116. }
  117. #if NCNN_VULKAN
  118. ncnn::create_gpu_instance();
  119. #endif // NCNN_VULKAN
  120. std::vector<Object> objects;
  121. detect_yolov3(m, objects);
  122. #if NCNN_VULKAN
  123. ncnn::destroy_gpu_instance();
  124. #endif // NCNN_VULKAN
  125. draw_objects(m, objects);
  126. return 0;
  127. }