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.

squeezenetssd.cpp 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #if defined(USE_NCNN_SIMPLEOCV)
  16. #include "simpleocv.h"
  17. #else
  18. #include <opencv2/core/core.hpp>
  19. #include <opencv2/highgui/highgui.hpp>
  20. #include <opencv2/imgproc/imgproc.hpp>
  21. #endif
  22. #include <stdio.h>
  23. #include <vector>
  24. struct Object
  25. {
  26. cv::Rect_<float> rect;
  27. int label;
  28. float prob;
  29. };
  30. static int detect_squeezenet(const cv::Mat& bgr, std::vector<Object>& objects)
  31. {
  32. ncnn::Net squeezenet;
  33. squeezenet.opt.use_vulkan_compute = true;
  34. // original pretrained model from https://github.com/chuanqi305/SqueezeNet-SSD
  35. // squeezenet_ssd_voc_deploy.prototxt
  36. // https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc
  37. // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
  38. squeezenet.load_param("squeezenet_ssd_voc.param");
  39. squeezenet.load_model("squeezenet_ssd_voc.bin");
  40. const int target_size = 300;
  41. int img_w = bgr.cols;
  42. int img_h = bgr.rows;
  43. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
  44. const float mean_vals[3] = {104.f, 117.f, 123.f};
  45. in.substract_mean_normalize(mean_vals, 0);
  46. ncnn::Extractor ex = squeezenet.create_extractor();
  47. ex.input("data", in);
  48. ncnn::Mat out;
  49. ex.extract("detection_out", out);
  50. // printf("%d %d %d\n", out.w, out.h, out.c);
  51. objects.clear();
  52. for (int i = 0; i < out.h; i++)
  53. {
  54. const float* values = out.row(i);
  55. Object object;
  56. object.label = values[0];
  57. object.prob = values[1];
  58. object.rect.x = values[2] * img_w;
  59. object.rect.y = values[3] * img_h;
  60. object.rect.width = values[4] * img_w - object.rect.x;
  61. object.rect.height = values[5] * img_h - object.rect.y;
  62. objects.push_back(object);
  63. }
  64. return 0;
  65. }
  66. static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
  67. {
  68. static const char* class_names[] = {"background",
  69. "aeroplane", "bicycle", "bird", "boat",
  70. "bottle", "bus", "car", "cat", "chair",
  71. "cow", "diningtable", "dog", "horse",
  72. "motorbike", "person", "pottedplant",
  73. "sheep", "sofa", "train", "tvmonitor"
  74. };
  75. cv::Mat image = bgr.clone();
  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. cv::imshow("image", image);
  98. cv::waitKey(0);
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. if (argc != 2)
  103. {
  104. fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
  105. return -1;
  106. }
  107. const char* imagepath = argv[1];
  108. cv::Mat m = cv::imread(imagepath, 1);
  109. if (m.empty())
  110. {
  111. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  112. return -1;
  113. }
  114. std::vector<Object> objects;
  115. detect_squeezenet(m, objects);
  116. draw_objects(m, objects);
  117. return 0;
  118. }