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