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 <memory>
29 #include <filesystem>
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 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
55 #include <VisionX/interface/components/OpenPoseEstimationInterface.h>
57 
58 namespace armarx
59 {
61  typedef std::shared_ptr<OpenPoseAdapter> MonocularOpenPoseEstimationPtr;
62 
64  {
65  public:
67  {
68  std::string net_resolution = "-1x368";
69  std::string output_resolution = "-1x-1";
71  std::string model_folder = "models/";
72  double scale_gap = 0.3;
73  int scale_number = 1;
74  int num_gpu_start = 0;
76  };
77 
78  public:
80 
81 #if defined(OP_CUSTOM_STRING_HEADERS)
82  static op::String ConvertToOPCustomType(const std::string& x)
83  {
84  return op::String(x);
85  }
86 #else
87  static std::string ConvertToOPCustomType(const std::string& x)
88  {
89  return x;
90  }
91 #endif
92 
93 #if defined(OP_CUSTOM_MATRIX_HEADERS)
94  static op::Matrix ConvertToOPCustomType(const cv::Mat& x)
95  {
96  return op::Matrix(&x);
97  }
98 #else
99  static cv::Mat ConvertToOPCustomType(const cv::Mat& x)
100  {
101  return x;
102  }
103 #endif
104 
105  op::Array<float> getOpenposeKeypoints(const CByteImage* imageBuffer);
106  HumanPose2DMap convert2DKeypointsToIce(const op::Array<float>& op_keypoints, const CByteImage* rgbImage) const;
107 
108  std::vector<float> getPoseColors() const;
109  std::vector<unsigned int> getPoseBodyPartPairsRender() const;
110 
111  // generate images out of pose
112  void render2DResultImage(const CByteImage& inputImage, const op::Array<float>& keypoints, CByteImage& resultImage, float renderThreshold);
113 
114  // get color
115  armarx::DrawColor24Bit getDominantColorOfPatch(const CByteImage& image, const Vec2d& point, int windowSize) const;
116 
117  private:
118  void setupOpenPoseEnvironment();
119 
120 
121  public:
123 
124  // OpenPose
125  std::mutex op_running_mutex;
126  std::shared_ptr<op::ScaleAndSizeExtractor> scaleAndSizeExtractor;
127  std::shared_ptr<op::CvMatToOpInput> cvMatToOpInput;
128  std::shared_ptr<op::CvMatToOpOutput> cvMatToOpOutput;
129  std::shared_ptr<op::PoseExtractorCaffe> poseExtractorCaffe;
130  std::shared_ptr<op::OpOutputToCvMat> opOutputToCvMat;
131  op::PoseModel poseModel;
132  };
133 }
armarx::OpenPoseAdapter::OpenPoseSettings::model_pose
std::string model_pose
Definition: OpenPoseAdapter.h:70
Matrix
Eigen::Matrix< T, 3, 3 > Matrix
Definition: UnscentedKalmanFilterTest.cpp:37
armarx::OpenPoseAdapter::convert2DKeypointsToIce
HumanPose2DMap convert2DKeypointsToIce(const op::Array< float > &op_keypoints, const CByteImage *rgbImage) const
Definition: OpenPoseAdapter.cpp:140
armarx::OpenPoseAdapter::OpenPoseAdapter
OpenPoseAdapter(const OpenPoseSettings &)
Definition: OpenPoseAdapter.cpp:58
armarx::OpenPoseAdapter::poseExtractorCaffe
std::shared_ptr< op::PoseExtractorCaffe > poseExtractorCaffe
Definition: OpenPoseAdapter.h:129
armarx::OpenPoseAdapter::OpenPoseSettings::scale_number
int scale_number
Definition: OpenPoseAdapter.h:73
armarx::OpenPoseAdapter::render2DResultImage
void render2DResultImage(const CByteImage &inputImage, const op::Array< float > &keypoints, CByteImage &resultImage, float renderThreshold)
Definition: OpenPoseAdapter.cpp:240
armarx::OpenPoseAdapter::OpenPoseSettings::model_folder
std::string model_folder
Definition: OpenPoseAdapter.h:71
armarx::MonocularOpenPoseEstimationPtr
std::shared_ptr< OpenPoseAdapter > MonocularOpenPoseEstimationPtr
Definition: OpenPoseAdapter.h:60
GfxTL::Vec2d
VectorXD< 2, double > Vec2d
Definition: VectorXD.h:694
armarx::OpenPoseAdapter::opOutputToCvMat
std::shared_ptr< op::OpOutputToCvMat > opOutputToCvMat
Definition: OpenPoseAdapter.h:130
armarx::OpenPoseAdapter::getPoseColors
std::vector< float > getPoseColors() const
armarx::OpenPoseAdapter::scaleAndSizeExtractor
std::shared_ptr< op::ScaleAndSizeExtractor > scaleAndSizeExtractor
Definition: OpenPoseAdapter.h:126
armarx::OpenPoseAdapter::op_running_mutex
std::mutex op_running_mutex
Definition: OpenPoseAdapter.h:125
armarx::OpenPoseAdapter::ConvertToOPCustomType
static std::string ConvertToOPCustomType(const std::string &x)
Definition: OpenPoseAdapter.h:87
armarx::OpenPoseAdapter::getDominantColorOfPatch
armarx::DrawColor24Bit getDominantColorOfPatch(const CByteImage &image, const Vec2d &point, int windowSize) const
Definition: OpenPoseAdapter.cpp:192
armarx::OpenPoseAdapter::ConvertToOPCustomType
static cv::Mat ConvertToOPCustomType(const cv::Mat &x)
Definition: OpenPoseAdapter.h:99
armarx::OpenPoseAdapter::OpenPoseSettings::scale_gap
double scale_gap
Definition: OpenPoseAdapter.h:72
armarx::OpenPoseAdapter::getOpenposeKeypoints
op::Array< float > getOpenposeKeypoints(const CByteImage *imageBuffer)
Definition: OpenPoseAdapter.cpp:112
armarx::OpenPoseAdapter::cvMatToOpInput
std::shared_ptr< op::CvMatToOpInput > cvMatToOpInput
Definition: OpenPoseAdapter.h:127
armarx::OpenPoseAdapter::OpenPoseSettings
Definition: OpenPoseAdapter.h:66
armarx::OpenPoseAdapter::cvMatToOpOutput
std::shared_ptr< op::CvMatToOpOutput > cvMatToOpOutput
Definition: OpenPoseAdapter.h:128
armarx::OpenPoseAdapter
Definition: OpenPoseAdapter.h:63
armarx::OpenPoseAdapter::OpenPoseSettings::output_resolution
std::string output_resolution
Definition: OpenPoseAdapter.h:69
armarx::OpenPoseAdapter::OpenPoseSettings::minimum_number_of_valid_keypoints_per_entitiy
unsigned int minimum_number_of_valid_keypoints_per_entitiy
Definition: OpenPoseAdapter.h:75
armarx::OpenPoseAdapter::OpenPoseSettings::net_resolution
std::string net_resolution
Definition: OpenPoseAdapter.h:68
openpose_body_25.h
armarx::OpenPoseAdapter::poseModel
op::PoseModel poseModel
Definition: OpenPoseAdapter.h:131
armarx::OpenPoseAdapter::OpenPoseSettings::num_gpu_start
int num_gpu_start
Definition: OpenPoseAdapter.h:74
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:30
cxxopts::String
std::string String
Definition: cxxopts.hpp:209
Logging.h
armarx::OpenPoseAdapter::settings
OpenPoseSettings settings
Definition: OpenPoseAdapter.h:122
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28