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.

yolov2.cpp 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "net.h"
  20. struct Object
  21. {
  22. cv::Rect_<float> rect;
  23. int label;
  24. float prob;
  25. };
  26. static int detect_yolov2(const cv::Mat& bgr, std::vector<Object>& objects)
  27. {
  28. ncnn::Net yolov2;
  29. // original pretrained model from https://github.com/eric612/MobileNet-YOLO
  30. // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy.prototxt
  31. // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy_iter_80000.caffemodel
  32. yolov2.load_param("mobilenet_yolo.param");
  33. yolov2.load_model("mobilenet_yolo.bin");
  34. const int target_size = 416;
  35. int img_w = bgr.cols;
  36. int img_h = bgr.rows;
  37. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  38. // the Caffe-YOLOv2-Windows style
  39. // X' = X * scale - mean
  40. const float mean_vals[3] = {0.5f, 0.5f, 0.5f};
  41. const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
  42. in.substract_mean_normalize(0, norm_vals);
  43. in.substract_mean_normalize(mean_vals, 0);
  44. ncnn::Extractor ex = yolov2.create_extractor();
  45. ex.set_num_threads(4);
  46. ex.input("data", in);
  47. ncnn::Mat out;
  48. ex.extract("detection_out", out);
  49. // printf("%d %d %d\n", out.w, out.h, out.c);
  50. objects.clear();
  51. for (int i=0; i<out.h; i++)
  52. {
  53. const float* values = out.row(i);
  54. Object object;
  55. object.label = values[0];
  56. object.prob = values[1];
  57. object.rect.x = values[2] * img_w;
  58. object.rect.y = values[3] * img_h;
  59. object.rect.width = values[4] * img_w - object.rect.x;
  60. object.rect.height = values[5] * img_h - object.rect.y;
  61. objects.push_back(object);
  62. }
  63. return 0;
  64. }
  65. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
  66. {
  67. static const char* class_names[] = {"background",
  68. "aeroplane", "bicycle", "bird", "boat",
  69. "bottle", "bus", "car", "cat", "chair",
  70. "cow", "diningtable", "dog", "horse",
  71. "motorbike", "person", "pottedplant",
  72. "sheep", "sofa", "train", "tvmonitor"};
  73. cv::Mat image = bgr.clone();
  74. for (size_t i = 0; i < objects.size(); i++)
  75. {
  76. const Object& obj = objects[i];
  77. fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
  78. obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
  79. cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
  80. char text[256];
  81. sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
  82. int baseLine = 0;
  83. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  84. int x = obj.rect.x;
  85. int y = obj.rect.y - label_size.height - baseLine;
  86. if (y < 0)
  87. y = 0;
  88. if (x + label_size.width > image.cols)
  89. x = image.cols - label_size.width;
  90. cv::rectangle(image, cv::Rect(cv::Point(x, y),
  91. cv::Size(label_size.width, label_size.height + baseLine)),
  92. cv::Scalar(255, 255, 255), CV_FILLED);
  93. cv::putText(image, text, cv::Point(x, y + label_size.height),
  94. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  95. }
  96. cv::imshow("image", image);
  97. cv::waitKey(0);
  98. }
  99. int main(int argc, char** argv)
  100. {
  101. if (argc != 2)
  102. {
  103. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  104. return -1;
  105. }
  106. const char* imagepath = argv[1];
  107. cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
  108. if (m.empty())
  109. {
  110. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  111. return -1;
  112. }
  113. std::vector<Object> objects;
  114. detect_yolov2(m, objects);
  115. draw_objects(m, objects);
  116. return 0;
  117. }