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

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