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

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