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

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