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.

simplepose.cpp 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 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 <algorithm>
  16. #include <vector>
  17. #include <opencv2/core/core.hpp>
  18. #include <opencv2/highgui/highgui.hpp>
  19. #include <opencv2/imgproc/imgproc.hpp>
  20. #include "net.h"
  21. #include "gpu.h"
  22. struct KeyPoint
  23. {
  24. cv::Point2f p;
  25. float prob;
  26. };
  27. static int detect_posenet(const cv::Mat& bgr, std::vector<KeyPoint>& keypoints)
  28. {
  29. ncnn::Net posenet;
  30. #if NCNN_VULKAN
  31. posenet.opt.use_vulkan_compute = true;
  32. #endif // NCNN_VULKAN
  33. // the simple baseline human pose estimation from gluon-cv
  34. // https://gluon-cv.mxnet.io/build/examples_pose/demo_simple_pose.html
  35. // mxnet model exported via
  36. // pose_net.hybridize()
  37. // pose_net.export('pose')
  38. // then mxnet2ncnn
  39. posenet.load_param("pose.param");
  40. posenet.load_model("pose.bin");
  41. int w = bgr.cols;
  42. int h = bgr.rows;
  43. ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, w, h, 192, 256);
  44. // transforms.ToTensor(),
  45. // transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
  46. // R' = (R / 255 - 0.485) / 0.229 = (R - 0.485 * 255) / 0.229 / 255
  47. // G' = (G / 255 - 0.456) / 0.224 = (G - 0.456 * 255) / 0.224 / 255
  48. // B' = (B / 255 - 0.406) / 0.225 = (B - 0.406 * 255) / 0.225 / 255
  49. const float mean_vals[3] = {0.485f*255.f, 0.456f*255.f, 0.406f*255.f};
  50. const float norm_vals[3] = {1/0.229f/255.f, 1/0.224f/255.f, 1/0.225f/255.f};
  51. in.substract_mean_normalize(mean_vals, norm_vals);
  52. ncnn::Extractor ex = posenet.create_extractor();
  53. ex.input("data", in);
  54. ncnn::Mat out;
  55. ex.extract("conv3_fwd", out);
  56. // resolve point from heatmap
  57. keypoints.clear();
  58. for (int p = 0; p < out.c; p++)
  59. {
  60. const ncnn::Mat m = out.channel(p);
  61. float max_prob = 0.f;
  62. int max_x = 0;
  63. int max_y = 0;
  64. for (int y = 0; y < out.h; y++)
  65. {
  66. const float* ptr = m.row(y);
  67. for (int x = 0; x < out.w; x++)
  68. {
  69. float prob = ptr[x];
  70. if (prob > max_prob)
  71. {
  72. max_prob = prob;
  73. max_x = x;
  74. max_y = y;
  75. }
  76. }
  77. }
  78. KeyPoint keypoint;
  79. keypoint.p = cv::Point2f(max_x * w / (float)out.w, max_y * h / (float)out.h);
  80. keypoint.prob = max_prob;
  81. keypoints.push_back(keypoint);
  82. }
  83. return 0;
  84. }
  85. static void draw_pose(const cv::Mat& bgr, const std::vector<KeyPoint>& keypoints)
  86. {
  87. cv::Mat image = bgr.clone();
  88. // draw bone
  89. static const int joint_pairs[16][2] = {
  90. {0, 1}, {1, 3}, {0, 2}, {2, 4},
  91. {5, 6}, {5, 7}, {7, 9}, {6, 8}, {8, 10},
  92. {5, 11}, {6, 12}, {11, 12},
  93. {11, 13}, {12, 14}, {13, 15}, {14, 16}
  94. };
  95. for (int i = 0; i < 16; i++)
  96. {
  97. const KeyPoint& p1 = keypoints[ joint_pairs[i][0] ];
  98. const KeyPoint& p2 = keypoints[ joint_pairs[i][1] ];
  99. if (p1.prob < 0.2f || p2.prob < 0.2f)
  100. continue;
  101. cv::line(image, p1.p, p2.p, cv::Scalar(255, 0, 0), 2);
  102. }
  103. // draw joint
  104. for (size_t i = 0; i < keypoints.size(); i++)
  105. {
  106. const KeyPoint& keypoint = keypoints[i];
  107. fprintf(stderr, "%.2f %.2f = %.5f\n", keypoint.p.x, keypoint.p.y, keypoint.prob);
  108. if (keypoint.prob < 0.2f)
  109. continue;
  110. cv::circle(image, keypoint.p, 3, cv::Scalar(0, 255, 0), -1);
  111. }
  112. cv::imshow("image", image);
  113. cv::waitKey(0);
  114. }
  115. int main(int argc, char** argv)
  116. {
  117. const char* imagepath = argv[1];
  118. cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
  119. if (m.empty())
  120. {
  121. fprintf(stderr, "cv::imread %s failed\n", imagepath);
  122. return -1;
  123. }
  124. #if NCNN_VULKAN
  125. ncnn::create_gpu_instance();
  126. #endif // NCNN_VULKAN
  127. std::vector<KeyPoint> keypoints;
  128. detect_posenet(m, keypoints);
  129. draw_pose(m, keypoints);
  130. #if NCNN_VULKAN
  131. ncnn::destroy_gpu_instance();
  132. #endif // NCNN_VULKAN
  133. return 0;
  134. }