LegacyRGBDOpenPoseEstimation.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 
24 
27 
28 using namespace visionx;
29 
30 namespace armarx
31 {
33  LegacyRGBDOpenPoseEstimation::createPropertyDefinitions()
34  {
36  new armarx::ComponentPropertyDefinitions{getConfigIdentifier()}};
37 
38  def->required(providerName, "ImageProviderName");
39 
40  RGBDOpenPoseEstimationComponentPluginUser::postCreatePropertyDefinitions(def);
41  return def;
42  }
43 
44  void
45  LegacyRGBDOpenPoseEstimation::onInitImageProcessor()
46  {
47  RGBDOpenPoseEstimationComponentPluginUser::preOnInitImageProcessor();
48  usingImageProvider(providerName);
49 
50  timeoutCounter2d = 0;
51  readErrorCounter2d = 0;
52  sucessCounter2d = 0;
53  }
54 
55  void
56  LegacyRGBDOpenPoseEstimation::onConnectImageProcessor()
57  {
58  RGBDOpenPoseEstimationComponentPluginUser::preOnConnectImageProcessor();
59 
60  visionx::ImageType imageDisplayType = visionx::tools::typeNameToImageType("rgb");
61  imageProviderInfo = getImageProvider(providerName, imageDisplayType);
62  rgbImageFormat = imageProviderInfo.imageFormat;
63 
64  numImages = static_cast<unsigned int>(imageProviderInfo.numberImages);
65  if (numImages != 2)
66  {
67  ARMARX_FATAL << "invalid number of images. aborting";
68  return;
69  }
70 
71  imageBuffer = new CByteImage*[2];
72  openposeResultImage = new CByteImage*[1];
73  imageBuffer[0] = visionx::tools::createByteImage(imageProviderInfo);
74  imageBuffer[1] = visionx::tools::createByteImage(imageProviderInfo);
75  rgbImageBuffer = visionx::tools::createByteImage(imageProviderInfo);
76  depthImageBuffer = visionx::tools::createByteImage(imageProviderInfo);
77 
78  enableResultImages(
79  1, imageProviderInfo.imageFormat.dimension, imageProviderInfo.imageFormat.type);
80 
81  ARMARX_INFO << getName() << " connect done";
82 
83  RGBDOpenPoseEstimationComponentPluginUser::postOnConnectImageProcessor();
84  }
85 
86  void
87  LegacyRGBDOpenPoseEstimation::onDisconnectImageProcessor()
88  {
89  RGBDOpenPoseEstimationComponentPluginUser::preOnDisconnectImageProcessor();
90 
91  delete[] imageBuffer;
92 
93  RGBDOpenPoseEstimationComponentPluginUser::postOnDisconnectImageProcessor();
94  }
95 
96  void
97  LegacyRGBDOpenPoseEstimation::onExitImageProcessor()
98  {
99  }
100 
101  void
102  LegacyRGBDOpenPoseEstimation::process()
103  {
104  if (running2D)
105  {
106  // check for result images
107  if (result_image_ready)
108  {
109  std::lock_guard outputImage_lock(openposeResultImageMutex);
110  ARMARX_DEBUG << deactivateSpam() << "publish result openpose image";
111  provideResultImages(openposeResultImage, imageMetaInfo);
112  }
113 
114  // check for new images
115  if (!waitForImages(providerName))
116  {
117  ++timeoutCounter2d;
118  ARMARX_WARNING << "Timeout or error in wait for images"
119  << " (#timeout " << timeoutCounter2d << ", #read error "
120  << readErrorCounter2d << ", #success " << sucessCounter2d << ")";
121  }
122  else
123  {
124  std::lock_guard lock_images(imageBufferMutex);
125  if (static_cast<unsigned int>(
126  getImages(providerName, imageBuffer, imageMetaInfo)) != numImages)
127  {
128  ++readErrorCounter2d;
129  ARMARX_WARNING << "Unable to transfer or read images"
130  << " (#timeout " << timeoutCounter2d << ", #read error "
131  << readErrorCounter2d << ", #success " << sucessCounter2d << ")";
132  return;
133  }
134  else
135  {
136  ARMARX_DEBUG << "Received an Image.";
137  ++sucessCounter2d;
138 
139  std::lock_guard lock_rgb(rgbImageBufferMutex);
140  std::lock_guard lock_depth(depthImageBufferMutex);
141  ::ImageProcessor::CopyImage(imageBuffer[0], rgbImageBuffer);
142  ::ImageProcessor::CopyImage(imageBuffer[1], depthImageBuffer);
143 
144  timestamp_of_update = imageMetaInfo->timeProvided;
145  update_ready = true;
146  }
147  }
148  }
149  else
150  {
151  ARMARX_DEBUG << deactivateSpam() << "Not running. Wait until start signal comes";
152  usleep(10000);
153  }
154  }
155 } // namespace armarx
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::PropertyDefinitionContainer::required
decltype(auto) required(PropertyType &setter, const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
Definition: PropertyDefinitionContainer.h:73
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
ARMARX_FATAL
#define ARMARX_FATAL
Definition: Logging.h:199
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
LegacyRGBDOpenPoseEstimation.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
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
TypeMapping.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27