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