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

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