OpenPoseAdapter.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package visionx::MonocularOpenPoseEstimation
17  * @author Fabian Peller <fabian.peller@kit.edu>
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #pragma once
25 
26 
27 // STD/STL
28 #include <filesystem>
29 #include <memory>
30 #include <mutex>
31 
32 // OpenCV 2
33 #include <opencv2/core/core.hpp>
34 
35 // IVT
36 #include <Image/ByteImage.h>
37 #include <Math/Math2d.h>
38 
39 // OpenPose
40 #include <openpose/core/headers.hpp>
41 #include <openpose/pose/headers.hpp>
42 #include <openpose/utilities/headers.hpp>
43 
44 // ugly hack to check op version. Since 1.6.0 they used custom headers to reduce the amounts of includes (E.g. string or cv2)
45 #if __has_include(<openpose/core/string.hpp>)
46 #define OP_CUSTOM_STRING_HEADERS = 1
47 #endif
48 #if __has_include(<openpose/core/matrix.hpp>)
49 #define OP_CUSTOM_MATRIX_HEADERS = 1
50 #endif
51 
52 // ArmarX
54 
55 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
56 
57 #include <VisionX/interface/components/OpenPoseEstimationInterface.h>
59 
60 namespace armarx
61 {
63  typedef std::shared_ptr<OpenPoseAdapter> MonocularOpenPoseEstimationPtr;
64 
66  {
67  public:
69  {
70  std::string net_resolution = "-1x368";
71  std::string output_resolution = "-1x-1";
73  std::string model_folder = "models/";
74  double scale_gap = 0.3;
75  int scale_number = 1;
76  int num_gpu_start = 0;
78  };
79 
80  public:
82 
83 #if defined(OP_CUSTOM_STRING_HEADERS)
84  static op::String
85  ConvertToOPCustomType(const std::string& x)
86  {
87  return op::String(x);
88  }
89 #else
90  static std::string
91  ConvertToOPCustomType(const std::string& x)
92  {
93  return x;
94  }
95 #endif
96 
97 #if defined(OP_CUSTOM_MATRIX_HEADERS)
98  static op::Matrix
99  ConvertToOPCustomType(const cv::Mat& x)
100  {
101  return op::Matrix(&x);
102  }
103 #else
104  static cv::Mat
105  ConvertToOPCustomType(const cv::Mat& x)
106  {
107  return x;
108  }
109 #endif
110 
111  op::Array<float> getOpenposeKeypoints(const CByteImage* imageBuffer);
112  HumanPose2DMap convert2DKeypointsToIce(const op::Array<float>& op_keypoints,
113  const CByteImage* rgbImage) const;
114 
115  std::vector<float> getPoseColors() const;
116  std::vector<unsigned int> getPoseBodyPartPairsRender() const;
117 
118  // generate images out of pose
119  void render2DResultImage(const CByteImage& inputImage,
120  const op::Array<float>& keypoints,
121  CByteImage& resultImage,
122  float renderThreshold);
123 
124  // get color
125  armarx::DrawColor24Bit
126  getDominantColorOfPatch(const CByteImage& image, const Vec2d& point, int windowSize) const;
127 
128  private:
129  void setupOpenPoseEnvironment();
130 
131 
132  public:
134 
135  // OpenPose
136  std::mutex op_running_mutex;
137  std::shared_ptr<op::ScaleAndSizeExtractor> scaleAndSizeExtractor;
138  std::shared_ptr<op::CvMatToOpInput> cvMatToOpInput;
139  std::shared_ptr<op::CvMatToOpOutput> cvMatToOpOutput;
140  std::shared_ptr<op::PoseExtractorCaffe> poseExtractorCaffe;
141  std::shared_ptr<op::OpOutputToCvMat> opOutputToCvMat;
142  op::PoseModel poseModel;
143  };
144 } // namespace armarx
armarx::OpenPoseAdapter::OpenPoseSettings::model_pose
std::string model_pose
Definition: OpenPoseAdapter.h:72
Matrix
Eigen::Matrix< T, 3, 3 > Matrix
Definition: UnscentedKalmanFilterTest.cpp:40
armarx::OpenPoseAdapter::convert2DKeypointsToIce
HumanPose2DMap convert2DKeypointsToIce(const op::Array< float > &op_keypoints, const CByteImage *rgbImage) const
Definition: OpenPoseAdapter.cpp:152
armarx::OpenPoseAdapter::OpenPoseAdapter
OpenPoseAdapter(const OpenPoseSettings &)
Definition: OpenPoseAdapter.cpp:58
armarx::OpenPoseAdapter::poseExtractorCaffe
std::shared_ptr< op::PoseExtractorCaffe > poseExtractorCaffe
Definition: OpenPoseAdapter.h:140
armarx::OpenPoseAdapter::OpenPoseSettings::scale_number
int scale_number
Definition: OpenPoseAdapter.h:75
armarx::OpenPoseAdapter::render2DResultImage
void render2DResultImage(const CByteImage &inputImage, const op::Array< float > &keypoints, CByteImage &resultImage, float renderThreshold)
Definition: OpenPoseAdapter.cpp:260
armarx::OpenPoseAdapter::OpenPoseSettings::model_folder
std::string model_folder
Definition: OpenPoseAdapter.h:73
armarx::MonocularOpenPoseEstimationPtr
std::shared_ptr< OpenPoseAdapter > MonocularOpenPoseEstimationPtr
Definition: OpenPoseAdapter.h:62
armarx::OpenPoseAdapter::opOutputToCvMat
std::shared_ptr< op::OpOutputToCvMat > opOutputToCvMat
Definition: OpenPoseAdapter.h:141
armarx::OpenPoseAdapter::getPoseColors
std::vector< float > getPoseColors() const
armarx::OpenPoseAdapter::scaleAndSizeExtractor
std::shared_ptr< op::ScaleAndSizeExtractor > scaleAndSizeExtractor
Definition: OpenPoseAdapter.h:137
armarx::OpenPoseAdapter::op_running_mutex
std::mutex op_running_mutex
Definition: OpenPoseAdapter.h:136
armarx::OpenPoseAdapter::ConvertToOPCustomType
static std::string ConvertToOPCustomType(const std::string &x)
Definition: OpenPoseAdapter.h:91
armarx::OpenPoseAdapter::getDominantColorOfPatch
armarx::DrawColor24Bit getDominantColorOfPatch(const CByteImage &image, const Vec2d &point, int windowSize) const
Definition: OpenPoseAdapter.cpp:208
armarx::OpenPoseAdapter::ConvertToOPCustomType
static cv::Mat ConvertToOPCustomType(const cv::Mat &x)
Definition: OpenPoseAdapter.h:105
armarx::OpenPoseAdapter::OpenPoseSettings::scale_gap
double scale_gap
Definition: OpenPoseAdapter.h:74
armarx::OpenPoseAdapter::getOpenposeKeypoints
op::Array< float > getOpenposeKeypoints(const CByteImage *imageBuffer)
Definition: OpenPoseAdapter.cpp:121
armarx::OpenPoseAdapter::cvMatToOpInput
std::shared_ptr< op::CvMatToOpInput > cvMatToOpInput
Definition: OpenPoseAdapter.h:138
armarx::OpenPoseAdapter::OpenPoseSettings
Definition: OpenPoseAdapter.h:68
armarx::OpenPoseAdapter::cvMatToOpOutput
std::shared_ptr< op::CvMatToOpOutput > cvMatToOpOutput
Definition: OpenPoseAdapter.h:139
armarx::OpenPoseAdapter
Definition: OpenPoseAdapter.h:65
armarx::OpenPoseAdapter::OpenPoseSettings::output_resolution
std::string output_resolution
Definition: OpenPoseAdapter.h:71
armarx::OpenPoseAdapter::OpenPoseSettings::minimum_number_of_valid_keypoints_per_entitiy
unsigned int minimum_number_of_valid_keypoints_per_entitiy
Definition: OpenPoseAdapter.h:77
armarx::OpenPoseAdapter::OpenPoseSettings::net_resolution
std::string net_resolution
Definition: OpenPoseAdapter.h:70
GfxTL::Vec2d
VectorXD< 2, double > Vec2d
Definition: VectorXD.h:736
openpose_body_25.h
armarx::OpenPoseAdapter::poseModel
op::PoseModel poseModel
Definition: OpenPoseAdapter.h:142
armarx::OpenPoseAdapter::OpenPoseSettings::num_gpu_start
int num_gpu_start
Definition: OpenPoseAdapter.h:76
armarx::OpenPoseAdapter::getPoseBodyPartPairsRender
std::vector< unsigned int > getPoseBodyPartPairsRender() const
armarx::human::pose::model::openpose_body_25::ModelId
const std::string ModelId
Definition: openpose_body_25.h:29
cxxopts::String
std::string String
Definition: cxxopts.hpp:192
Logging.h
armarx::OpenPoseAdapter::settings
OpenPoseSettings settings
Definition: OpenPoseAdapter.h:133
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27