RGBDPoseEstimationWithMemoryWriter.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::RGBDOpenPoseEstimation
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 
29 #include <VisionX/libraries/armem_human/aron/HumanPose.aron.generated.h>
32 
33 
34 using namespace visionx;
35 
36 namespace armarx
37 {
38 
39  const std::string
40  RGBDPoseEstimationWithMemoryWriter::defaultName = "RGBDPoseEstimationWithMemoryWriter";
41 
42 
44  RGBDPoseEstimationWithMemoryWriter::createPropertyDefinitions()
45  {
47 
48  def->required(properties.providerName, "ImageProviderName");
49 
50  def->optional(properties.enableMemory, "humanMemory.enable", "Enables or disables writing to the human memory.");
51  def->optional(properties.humanMemory.memoryName, "humanMemory.memory_name", "Name of the human memory.");
52  def->optional(properties.humanMemory.coreSegmentName, "humanMemory.core_segment_name", "Name of the core segment in the human memory.");
53 
54  RGBDOpenPoseEstimationComponentPluginUser::postCreatePropertyDefinitions(def);
55  return def;
56  }
57 
58 
59  std::string
60  RGBDPoseEstimationWithMemoryWriter::getDefaultName() const
61  {
62  return RGBDPoseEstimationWithMemoryWriter::defaultName;
63  }
64 
65 
66  std::string
67  RGBDPoseEstimationWithMemoryWriter::GetDefaultName()
68  {
69  return RGBDPoseEstimationWithMemoryWriter::defaultName;
70  }
71 
72 
73  void
74  RGBDPoseEstimationWithMemoryWriter::reportEntities()
75  {
76  if (properties.enableMemory)
77  {
78  armem::Time ts = armem::Time(armem::Duration::MicroSeconds(timestamp_of_update));
79 
81  update.entityID = armem::MemoryID()
82  .withMemoryName(properties.humanMemory.memoryName)
83  .withCoreSegmentName(properties.humanMemory.coreSegmentName)
84  .withProviderSegmentName(getName())
85  .withEntityName("3DDetections")
86  .withTimestamp(ts);
87  update.referencedTime = ts;
88 
89  for (auto& [_, humanPose] : openposeResult3D)
90  {
91  auto pose = humanPose.keypointMap;
92 
93  armarx::human::arondto::HumanPose dto;
94  dto.poseModelId = op_settings.model_pose;
95  // OpenPose does not allow for tracking.
96  dto.humanTrackingId = std::nullopt;
97 
98  for (auto& [name, keypoint] : pose)
99  {
100  armarx::human::arondto::PoseKeypoint kp;
101  kp.confidence = keypoint.confidence;
102 
104  posGlobal.header.agent = localRobot->getName();
105  posGlobal.header.frame = armarx::GlobalFrame;
106  posGlobal.position.x() = keypoint.globalX;
107  posGlobal.position.y() = keypoint.globalY;
108  posGlobal.position.z() = keypoint.globalZ;
109  FramedPosition positionGlobal(posGlobal.position, armarx::GlobalFrame, posGlobal.header.agent);
110  toAron(kp.positionGlobal.emplace(), positionGlobal);
111 
112  FramedPosition pos(Eigen::Vector3f(keypoint.x, keypoint.y, keypoint.z), cameraNodeName, localRobot->getName());
113  armarx::FramedPosition posRobot(pos.toRootFrame(localRobot)->toEigen(), localRobot->getRootNode()->getName(), localRobot->getName());
114  toAron(kp.positionRobot.emplace(), posRobot);
115 
116  dto.keypoints[keypoint.label] = kp;
117  }
118 
119  update.instancesData.push_back(dto.toAron());
120  }
121 
122  const armem::EntityUpdateResult updateResult = humanMemoryWriter.commit(update);
123 
124  if (not updateResult.success)
125  {
126  ARMARX_ERROR << updateResult.errorMessage;
127  }
128  }
129 
130  RGBDOpenPoseEstimationComponentPluginUser::reportEntities();
131  }
132 
133  void RGBDPoseEstimationWithMemoryWriter::onInitImageProcessor()
134  {
135  RGBDOpenPoseEstimationComponentPluginUser::preOnInitImageProcessor();
136  usingImageProvider(properties.providerName);
137 
138  timeoutCounter2d = 0;
139  readErrorCounter2d = 0;
140  sucessCounter2d = 0;
141  }
142 
143  void RGBDPoseEstimationWithMemoryWriter::onConnectImageProcessor()
144  {
145  RGBDOpenPoseEstimationComponentPluginUser::preOnConnectImageProcessor();
146 
147  if (properties.enableMemory)
148  {
149  ARMARX_IMPORTANT << "Waiting for memory ...";
150  try
151  {
152  humanMemoryWriter = memoryNameSystem().useWriter(properties.humanMemory);
153  }
155  {
156  ARMARX_ERROR << e.what();
157  return;
158  }
159  }
160  else
161  {
162  ARMARX_IMPORTANT << "Writing to memroy disabled via properties.";
163  }
164 
165  visionx::ImageType imageDisplayType = visionx::tools::typeNameToImageType("rgb");
166  imageProviderInfo = getImageProvider(properties.providerName, imageDisplayType);
167  rgbImageFormat = imageProviderInfo.imageFormat;
168 
169  numImages = static_cast<unsigned int>(imageProviderInfo.numberImages);
170  if (numImages != 2)
171  {
172  ARMARX_FATAL << "invalid number of images. aborting";
173  return;
174  }
175 
176  imageBuffer = new CByteImage*[2];
177  openposeResultImage = new CByteImage*[1];
178  imageBuffer[0] = visionx::tools::createByteImage(imageProviderInfo);
179  imageBuffer[1] = visionx::tools::createByteImage(imageProviderInfo);
180  rgbImageBuffer = visionx::tools::createByteImage(imageProviderInfo);
181  depthImageBuffer = visionx::tools::createByteImage(imageProviderInfo);
182 
183  enableResultImages(1, imageProviderInfo.imageFormat.dimension, imageProviderInfo.imageFormat.type);
184 
185  ARMARX_INFO << getName() << " connect done";
186 
187  RGBDOpenPoseEstimationComponentPluginUser::postOnConnectImageProcessor();
188  }
189 
190  void RGBDPoseEstimationWithMemoryWriter::onDisconnectImageProcessor()
191  {
192  RGBDOpenPoseEstimationComponentPluginUser::preOnDisconnectImageProcessor();
193 
194  delete[] imageBuffer;
195 
196  RGBDOpenPoseEstimationComponentPluginUser::postOnDisconnectImageProcessor();
197  }
198 
199  void RGBDPoseEstimationWithMemoryWriter::onExitImageProcessor()
200  {
201  }
202 
203  void RGBDPoseEstimationWithMemoryWriter::process()
204  {
205  if (running2D)
206  {
207  // check for result images
208  if (result_image_ready)
209  {
210  std::lock_guard outputImage_lock(openposeResultImageMutex);
211  ARMARX_DEBUG << deactivateSpam() << "publish result openpose image";
212  provideResultImages(openposeResultImage, imageMetaInfo);
213  }
214 
215  // check for new images
216  if (!waitForImages(properties.providerName))
217  {
218  ++timeoutCounter2d;
219  ARMARX_WARNING << "Timeout or error in wait for images"
220  << " (#timeout " << timeoutCounter2d
221  << ", #read error " << readErrorCounter2d
222  << ", #success " << sucessCounter2d << ")";
223  }
224  else
225  {
226  std::lock_guard lock_images(imageBufferMutex);
227  if (static_cast<unsigned int>(getImages(properties.providerName, imageBuffer, imageMetaInfo)) != numImages)
228  {
229  ++readErrorCounter2d;
230  ARMARX_WARNING << "Unable to transfer or read images"
231  << " (#timeout " << timeoutCounter2d
232  << ", #read error " << readErrorCounter2d
233  << ", #success " << sucessCounter2d << ")";
234  return;
235  }
236  else
237  {
238  ARMARX_DEBUG << "Received an Image.";
239  ++sucessCounter2d;
240 
241  std::lock_guard lock_rgb(rgbImageBufferMutex);
242  std::lock_guard lock_depth(depthImageBufferMutex);
243  ::ImageProcessor::CopyImage(imageBuffer[0], rgbImageBuffer);
244  ::ImageProcessor::CopyImage(imageBuffer[1], depthImageBuffer);
245 
246  timestamp_of_update = imageMetaInfo->timeProvided;
247  update_ready = true;
248  }
249  }
250  }
251  else
252  {
253  ARMARX_DEBUG << deactivateSpam() << "Not running. Wait until start signal comes";
254  usleep(10000);
255  }
256  }
257 }
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::armem::MemoryID::withMemoryName
MemoryID withMemoryName(const std::string &name) const
Definition: MemoryID.cpp:396
framed.h
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:72
armarx::PropertyDefinitionContainer::required
decltype(auto) required(PropertyType &setter, const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
Definition: PropertyDefinitionContainer.h:74
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
Clock.h
ARMARX_FATAL
#define ARMARX_FATAL
Definition: Logging.h:192
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
FramedPose.h
armarx::armem::MemoryID::withCoreSegmentName
MemoryID withCoreSegmentName(const std::string &name) const
Definition: MemoryID.cpp:404
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
error.h
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::FramedPosition::toRootFrame
FramedPositionPtr toRootFrame(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:769
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::tools::typeNameToImageType
ImageType typeNameToImageType(const std::string &imageTypeName)
Converts an image type name as string into an ImageType integer.
Definition: TypeMapping.cpp:42
ImageUtil.h
armarx::armem::MemoryID::withTimestamp
MemoryID withTimestamp(Time time) const
Definition: MemoryID.cpp:428
armarx::armem::EntityUpdateResult::errorMessage
std::string errorMessage
Definition: Commit.h:79
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
TypeMapping.h
MemoryNameSystem.h
armarx::toAron
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::armem::EntityUpdateResult::success
bool success
Definition: Commit.h:74
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
RGBDPoseEstimationWithMemoryWriter.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29