headpose.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "opencv2/imgproc/imgproc.hpp"
  15. #include "ultra_infer/vision/visualize/visualize.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. cv::Mat VisHeadPose(const cv::Mat &im, const HeadPoseResult &result, int size,
  19. int line_size) {
  20. const float PI = 3.1415926535;
  21. auto vis_im = im.clone();
  22. int h = im.rows;
  23. int w = im.cols;
  24. // vis headpose
  25. float pitch = result.euler_angles[0] * PI / 180.f;
  26. float yaw = -result.euler_angles[1] * PI / 180.f;
  27. float roll = result.euler_angles[2] * PI / 180.f;
  28. int tdx = w / 2;
  29. int tdy = h / 2;
  30. // X-Axis | drawn in red
  31. int x1 = static_cast<int>(size * std::cos(yaw) * std::cos(roll)) + tdx;
  32. int y1 = static_cast<int>(
  33. size * (std::cos(pitch) * std::sin(roll) +
  34. std::cos(roll) * std::sin(pitch) * std::sin(yaw))) +
  35. tdy;
  36. // Y-Axis | drawn in green
  37. int x2 = static_cast<int>(-size * std::cos(yaw) * std::sin(roll)) + tdx;
  38. int y2 = static_cast<int>(
  39. size * (std::cos(pitch) * std::cos(roll) -
  40. std::sin(pitch) * std::sin(yaw) * std::sin(roll))) +
  41. tdy;
  42. // Z-Axis | drawn in blue
  43. int x3 = static_cast<int>(size * std::sin(yaw)) + tdx;
  44. int y3 = static_cast<int>(-size * std::cos(yaw) * std::sin(pitch)) + tdy;
  45. cv::line(vis_im, cv::Point2i(tdx, tdy), cv::Point2i(x1, y1),
  46. cv::Scalar(0, 0, 255), line_size);
  47. cv::line(vis_im, cv::Point2i(tdx, tdy), cv::Point2i(x2, y2),
  48. cv::Scalar(0, 255, 0), line_size);
  49. cv::line(vis_im, cv::Point2i(tdx, tdy), cv::Point2i(x3, y3),
  50. cv::Scalar(255, 0, 0), line_size);
  51. return vis_im;
  52. }
  53. } // namespace vision
  54. } // namespace ultra_infer