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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2018 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_yolov2(const cv::Mat& bgr, std::vector<Object>& objects)
  20. {
  21. ncnn::Net yolov2;
  22. yolov2.opt.use_vulkan_compute = true;
  23. // original pretrained model from https://github.com/eric612/MobileNet-YOLO
  24. // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy.prototxt
  25. // https://github.com/eric612/MobileNet-YOLO/blob/master/models/yolov2/mobilenet_yolo_deploy_iter_80000.caffemodel
  26. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  27. if (yolov2.load_param("mobilenet_yolo.param"))
  28. exit(-1);
  29. if (yolov2.load_model("mobilenet_yolo.bin"))
  30. exit(-1);
  31. const int target_size = 416;
  32. int img_w = bgr.cols;
  33. int img_h = bgr.rows;
  34. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  35. // the Caffe-YOLOv2-Windows style
  36. // X' = X * scale - mean
  37. const float mean_vals[3] = {1.0f, 1.0f, 1.0f};
  38. const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
  39. in.substract_mean_normalize(0, norm_vals);
  40. in.substract_mean_normalize(mean_vals, 0);
  41. ncnn::Extractor ex = yolov2.create_extractor();
  42. ex.input("data", in);
  43. ncnn::Mat out;
  44. ex.extract("detection_out", out);
  45. // printf("%d %d %d\n", out.w, out.h, out.c);
  46. objects.clear();
  47. for (int i = 0; i < out.h; i++)
  48. {
  49. const float* values = out.row(i);
  50. Object object;
  51. object.label = values[0];
  52. object.prob = values[1];
  53. object.rect.x = values[2] * img_w;
  54. object.rect.y = values[3] * img_h;
  55. object.rect.width = values[4] * img_w - object.rect.x;
  56. object.rect.height = values[5] * img_h - object.rect.y;
  57. objects.push_back(object);
  58. }
  59. return 0;
  60. }
  61. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
  62. {
  63. static const char* class_names[] = {"background",
  64. "aeroplane", "bicycle", "bird", "boat",
  65. "bottle", "bus", "car", "cat", "chair",
  66. "cow", "diningtable", "dog", "horse",
  67. "motorbike", "person", "pottedplant",
  68. "sheep", "sofa", "train", "tvmonitor"
  69. };
  70. cv::Mat image = bgr.clone();
  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. cv::imshow("image", image);
  93. cv::waitKey(0);
  94. }
  95. int main(int argc, char** argv)
  96. {
  97. if (argc != 2)
  98. {
  99. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  100. return -1;
  101. }
  102. const char* imagepath = argv[1];
  103. cv::Mat m = cv::imread(imagepath, 1);
  104. if (m.empty())
  105. {
  106. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  107. return -1;
  108. }
  109. std::vector<Object> objects;
  110. detect_yolov2(m, objects);
  111. draw_objects(m, objects);
  112. return 0;
  113. }