OpenPoseSimulation.cpp
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::ArmarXObjects::OpenPoseEstimation
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "OpenPoseSimulation.h"
24 
25 #include <stdexcept>
26 
29 
33 
34 #include <VisionX/interface/components/OpenPoseEstimationInterface.h>
35 #include <VisionX/interface/components/RGBDImageProvider.h>
39 
40 #include <Image/IplImageAdaptor.h>
41 #include <Image/PrimitivesDrawer.h>
42 
43 
44 // json
45 #include <SimoxUtility/json/json.hpp>
46 
47 using namespace armarx;
48 
51 {
54 }
55 
56 void
58 {
59 }
60 
61 void
63 {
64 
65  offeringTopic(getProperty<std::string>("OpenPoseEstimation2DTopicName").getValue());
66  listener2DPrx = getTopic<OpenPose2DListenerPrx>(
67  getProperty<std::string>("OpenPoseEstimation2DTopicName").getValue());
68  offeringTopic(getProperty<std::string>("OpenPoseEstimation3DTopicName").getValue());
69  listener3DPrx = getTopic<OpenPose3DListenerPrx>(
70  getProperty<std::string>("OpenPoseEstimation3DTopicName").getValue());
71 
72  ARMARX_VERBOSE << "Trying to get RobotStateComponent proxy";
73  // Trying to access robotStateComponent if it is avaiable
74  robotStateInterface = getProxy<RobotStateComponentInterfacePrx>(
75  getProperty<std::string>("RobotStateComponentName").getValue(), false, "", false);
76  if (robotStateInterface)
77  {
78  localRobot = RemoteRobot::createLocalClone(robotStateInterface);
79  ARMARX_VERBOSE << "RobotStateComponent available";
80  }
81  else
82  {
83  ARMARX_ERROR << "RobotStateCompontent is not avaiable, 3D-Estimation will be deactivated!!";
84  }
85 
87 }
88 
89 void
91 {
93 }
94 
95 void
97 {
98 }
99 
100 void
101 OpenPoseSimulation::start(const Ice::Current&)
102 {
103  if (running2D)
104  {
105  ARMARX_INFO << "already running";
106  return;
107  }
108  else
109  {
110  ARMARX_INFO << "Starting OpenPoseEstimation";
111  running2D = true;
112  runTask = new RunningTask<OpenPoseSimulation>(this, &OpenPoseSimulation::run);
113  runTask->start();
114  }
115 }
116 
117 void
118 OpenPoseSimulation::stop(const Ice::Current&)
119 {
120  if (running2D)
121  {
122  ARMARX_INFO << "Stopping OpenPoseEstimation";
123  running2D = false;
124  runTask->stop(true);
126  }
127  else
128  {
129  ARMARX_INFO << "not running";
130  }
131 }
132 
133 void
135 {
136  if (running3D)
137  {
138  return;
139  }
140  else
141  {
142  ARMARX_INFO << "Starting OpenPoseEstimation -- 3D";
143 
144  running3D = true;
145  start();
146  }
147 }
148 
149 void
151 {
152  if (running3D)
153  {
154  ARMARX_INFO << "Stopping OpenPoseEstimation -- 3D";
155  running3D = false;
156  }
157  {
158  // In any case, let's clear the layer.
159  viz::Layer openPoseArVizLayer = arviz.layer(layerName);
160  openPoseArVizLayer.clear();
161  arviz.commit(openPoseArVizLayer);
162  }
163 }
164 
165 void
166 OpenPoseSimulation::onMessage(const Texting::TextMessage& text, const Ice::Current&)
167 {
168  using json = nlohmann::json;
169  // parsing message
170  json jsonView = json::parse(text.message);
171 
172  // Reading values
173  long timestamp = jsonView["timestamp"].get<long>();
174  json jsonValues = jsonView["values"];
175  // TODO
176 }
177 
178 void
179 OpenPoseSimulation::run()
180 {
181  ARMARX_VERBOSE << "Worker thread started.";
182  ARMARX_DEBUG << "Transitioning into worker loop.";
183 
184  while (running2D && !runTask->isStopped())
185  {
186 
187  ARMARX_DEBUG << "Done calculating 2D keypoints.";
188  long timeProvidedImage = IceUtil::Time::now().toMilliSeconds();
189 
190  if (running3D && localRobot)
191  {
192  ARMARX_TRACE;
193 
194  // 2. Calculate 3D values
196  localRobot, robotStateInterface, timeProvidedImage);
197 
198  ARMARX_TRACE;
199 
200 
201  ARMARX_VERBOSE << deactivateSpam(0.5) << "Reporting 2Dkeypoints for " << 1
202  << " objects";
203  listener2DPrx->report2DKeypoints({}, timeProvidedImage);
204  listener2DPrx->report2DKeypointsNormalized({}, timeProvidedImage);
205  ARMARX_VERBOSE << deactivateSpam(0.5) << "Reporting 3Dkeypoints for " << 1
206  << " objects";
207  listener3DPrx->report3DKeypoints({}, timeProvidedImage);
208  }
209  else
210  {
211  ARMARX_VERBOSE << deactivateSpam(0.5) << "Reporting 2Dkeypoints for " << 1
212  << " objects";
213  listener2DPrx->report2DKeypoints({}, timeProvidedImage);
214  listener2DPrx->report2DKeypointsNormalized({}, timeProvidedImage);
215  }
216 
217  ARMARX_TRACE;
218  sleep(1);
219  }
220 }
221 
224 {
225  defineOptionalProperty<std::string>("OpenPoseEstimation2DTopicName", "OpenPoseEstimation2D");
226  defineOptionalProperty<std::string>("OpenPoseEstimation3DTopicName", "OpenPoseEstimation3D");
227 
228  defineOptionalProperty<std::string>("RobotStateComponentName", "RobotStateComponent");
229  defineOptionalProperty<std::string>(
230  "CameraNodeName", "DepthCamera", "Name of the robot node for the input camera");
231 }
RemoteRobot.h
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::viz::Layer::clear
void clear()
Definition: Layer.h:24
armarx::OpenPoseSimulation::start3DPoseEstimation
void start3DPoseEstimation(const Ice::Current &=Ice::emptyCurrent) override
Definition: OpenPoseSimulation.cpp:134
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
ColorUtils.h
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::OpenPoseSimulation::onConnectComponent
void onConnectComponent() override
Definition: OpenPoseSimulation.cpp:62
armarx::OpenPoseSimulation::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: OpenPoseSimulation.cpp:50
HumanPoseBody25.h
FramedPose.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::OpenPoseSimulationPropertyDefinitions::OpenPoseSimulationPropertyDefinitions
OpenPoseSimulationPropertyDefinitions(std::string prefix)
Definition: OpenPoseSimulation.cpp:222
armarx::OpenPoseSimulation::onInitComponent
void onInitComponent() override
Definition: OpenPoseSimulation.cpp:57
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:42
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
OpenPoseSimulation.h
armarx::OpenPoseSimulation::onDisconnectComponent
void onDisconnectComponent() override
Definition: OpenPoseSimulation.cpp:90
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::OpenPoseSimulation::stop
void stop(const Ice::Current &=Ice::emptyCurrent) override
Definition: OpenPoseSimulation.cpp:118
armarx::RemoteRobot::synchronizeLocalCloneToTimestamp
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
Definition: RemoteRobot.cpp:566
armarx::OpenPoseSimulationPropertyDefinitions
Definition: OpenPoseSimulation.h:42
ImageUtil.h
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:99
armarx::OpenPoseSimulation::stop3DPoseEstimation
void stop3DPoseEstimation(const Ice::Current &=Ice::emptyCurrent) override
Definition: OpenPoseSimulation.cpp:150
TypeMapping.h
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::viz::Layer
Definition: Layer.h:12
armarx::OpenPoseSimulation::onExitComponent
void onExitComponent() override
Definition: OpenPoseSimulation.cpp:96
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::OpenPoseSimulation::start
void start(const Ice::Current &=Ice::emptyCurrent) override
Definition: OpenPoseSimulation.cpp:101
armarx::OpenPoseSimulation::onMessage
void onMessage(const Texting::TextMessage &text, const Ice::Current &=Ice::emptyCurrent) override
Definition: OpenPoseSimulation.cpp:166