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
47using namespace armarx;
48
55
56void
60
61void
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
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
89void
94
95void
99
100void
101OpenPoseSimulation::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
117void
118OpenPoseSimulation::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
133void
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
149void
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
165void
166OpenPoseSimulation::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
178void
179OpenPoseSimulation::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 {
193
194 // 2. Calculate 3D values
196 localRobot, robotStateInterface, timeProvidedImage);
197
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
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");
230 "CameraNodeName", "DepthCamera", "Name of the robot node for the input camera");
231}
std::string timestamp()
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
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
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
void start3DPoseEstimation(const Ice::Current &=Ice::emptyCurrent) override
void stop(const Ice::Current &=Ice::emptyCurrent) override
void start(const Ice::Current &=Ice::emptyCurrent) override
void stop3DPoseEstimation(const Ice::Current &=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onMessage(const Texting::TextMessage &text, const Ice::Current &=Ice::emptyCurrent) override
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
static bool synchronizeLocalCloneToTimestamp(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx, Ice::Long timestamp)
Synchronizes a local robot to a robot state at timestamp.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
#define ARMARX_TRACE
Definition trace.h:77