UCLObjectRecognition.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::UCLObjectRecognition
17  * @author Mirko Waechter (waechter at kit dot edu)
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "UCLObjectRecognition.h"
24 
25 #include <Eigen/Geometry>
26 
30 
32 
34 
35 #include <Calibration/Calibration.h>
36 #include <Image/ImageProcessor.h>
37 #include <Image/IplImageAdaptor.h>
38 #include <Image/PrimitivesDrawer.h>
39 #include <Image/PrimitivesDrawerCV.h>
43 
44 using namespace armarx;
45 
46 using namespace std::filesystem;
47 
48 namespace visionx
49 {
50  //using namespace boost::python;
51 
52  //void visionx::UCLObjectRecognition::onInitObjectLocalizerProcessor()
53  void
55  {
56  usingProxy("ObjectPoseProvider");
57  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
58  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
59  }
60 
61  //void visionx::UCLObjectRecognition::onConnectObjectLocalizerProcessor()
62  void
64  {
65  getProxy(prx, "ObjectPoseProvider");
66  debugDrawerTopicPrx = getTopic<armarx::DebugDrawerInterfacePrx>(
67  getProperty<std::string>("DebugDrawerTopicName").getValue());
68  if (!debugDrawerTopicPrx)
69  {
70  ARMARX_ERROR << "Failed to obtain debug drawer proxy";
71  return;
72  }
73  robotStateComponent = getProxy<armarx::RobotStateComponentInterfacePrx>(
74  getProperty<std::string>("RobotStateComponentName").getValue());
77  }
78 
80  UCLObjectRecognition::createPropertyDefinitions()
81  {
83  new UCLObjectRecognitionPropertyDefinitions(getConfigIdentifier()));
84  }
85 
86  memoryx::ObjectLocalizationResultList
88  const memoryx::ObjectClassNameList& classes,
89  const Ice::Current&)
90  {
91  // static std::size_t flip = 0;
92  // static std::size_t flipn = 100;
93 
94  ARMARX_VERBOSE << /*deactivateSpam(10) <<*/ "Localizing: " << classes;
95  auto agentName = getProperty<std::string>("AgentName").getValue();
96  auto cameraFrame = getProperty<std::string>("CameraFrameName").getValue();
97  auto mapping = armarx::Split(getProperty<std::string>("ObjectNameIdMap"), ";", true, true);
98  std::map<std::string, int> nameIdMap;
99  for (auto entry : mapping)
100  {
101  auto pair = armarx::Split(entry, ":");
102  ARMARX_CHECK_EQUAL(pair.size(), 2) << pair;
103  nameIdMap[pair.at(0)] = armarx::toFloat(pair.at(1));
104 
105 
106  // debugDrawerTopicPrx->removePoseVisu(getName(), pair.at(0) + "_raw");
107  // debugDrawerTopicPrx->removePoseVisu(getName(), pair.at(0) + "_global");
108  }
109  memoryx::ObjectLocalizationResultList result;
110  for (auto className : classes)
111  {
112  ARMARX_CHECK_EXPRESSION(nameIdMap.count(className))
113  << VAROUT(className) << " " << VAROUT(nameIdMap);
114  auto id = nameIdMap[className];
115  auto regResult = prx->getObjectPose(id);
116  if (regResult.confidence <= 0)
117  {
118  ARMARX_INFO << deactivateSpam(5, className) << "Nothing found for " << className;
119  continue;
120  }
121  memoryx::ObjectLocalizationResult r;
122  r.instanceName = className;
123  r.objectClassName = className;
124  r.recognitionCertainty = regResult.confidence * 0.2;
125  ARMARX_CHECK_EQUAL(regResult.matrix4x4.size(), 16) << regResult.confidence;
126  Eigen::Matrix4f pose =
127  Eigen::Map<Eigen::Matrix4f>(regResult.matrix4x4.data()).transpose();
128  pose.block<3, 1>(0, 3) *= 1000.0;
129  ARMARX_INFO << "pose\n" << pose << '\n' << VAROUT(r.recognitionCertainty);
130 
131  {
132  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_raw", new Pose {pose});
133  RemoteRobot::synchronizeLocalClone(localRobot, robotStateComponent);
134  // const Eigen::Matrix4f gcam = localRobot->getRobotNode(cameraFrame)->getGlobalPose();
135  const Eigen::Matrix4f gpose =
136  localRobot->getRobotNode(cameraFrame)->getGlobalPose() * pose;
137  debugDrawerTopicPrx->setPoseVisu(getName(), className + "_global", new Pose{gpose});
138  // ARMARX_IMPORTANT << "drawing\n" << pose
139  // << "\ncam\n" << localRobot->getRobotNode(cameraFrame)->getGlobalPose()
140  // << "\ngp\n" << gpose;
141 
142 
143  // Eigen::Matrix4f manualcam = gcam;
144  // manualcam(2, 3) += 1000;
145  // Eigen::Matrix4f manualPose = pose;
146  // manualPose.topRightCorner<3, 1>() += manualcam.topRightCorner<3, 1>();
147 
148  // //manualPose(2, 3) += 1000;
149 
150  // const Eigen::Vector3f gcammanpos = manualcam.topRightCorner<3, 1>();
151  // const Eigen::Vector3f gobjmanpos = manualPose.topRightCorner<3, 1>();
152  // const Eigen::Vector3f gcammanposX = (manualcam * Eigen::Vector4f {1000, 0, 0, 1}).topRightCorner<3, 1>();
153  // const Eigen::Vector3f gcammanposY = (manualcam * Eigen::Vector4f {0, 1000, 0, 1}).topRightCorner<3, 1>();
154  // const Eigen::Vector3f gcammanposZ = (manualcam * Eigen::Vector4f {0, 0, 1000, 1}).topRightCorner<3, 1>();
155  // ARMARX_INFO << VAROUT(manualPose);
156  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_manualPose", new Pose {manualPose});
157  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_manualcam", new Pose {manualcam});
158 
159  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_x", new Vector3 {gcammanpos}, new Vector3 {gcammanposX}, 5, {1, 0, 0, 1});
160  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_y", new Vector3 {gcammanpos}, new Vector3 {gcammanposY}, 5, {0, 1, 0, 1});
161  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_z", new Vector3 {gcammanpos}, new Vector3 {gcammanposZ}, 5, {0, 0, 1, 1});
162  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_obj", new Vector3 {gcammanpos}, new Vector3 {gobjmanpos}, 5, {0, 0, 0, 1});
163  }
164 
165 
166  r.orientation = new FramedOrientation(pose, cameraFrame, agentName);
167  r.position = new FramedPosition(pose, cameraFrame, agentName);
168  Eigen::Vector3f cov(10, 10, 10000);
169  r.positionNoise = new memoryx::MultivariateNormalDistribution(Eigen::Vector3f::Zero(),
170  cov.asDiagonal());
171  ARMARX_INFO << "Pose is "
172  << (TimeUtil::GetTime() - IceUtil::Time::milliSeconds(regResult.timestamp))
173  .toMilliSeconds()
174  << " ms old";
175  r.timeStamp = new armarx::TimestampVariant(regResult.timestamp *
176  1000); // ToDo: get timestamp from image
177  result.push_back(r);
178  }
179 
180 
181  return result;
182  }
183 } // namespace visionx
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::eFull
@ eFull
Definition: MorphingItem.h:35
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:522
armarx::RemoteRobot::createLocalCloneFromFile
static VirtualRobot::RobotPtr createLocalCloneFromFile(RobotStateComponentInterfacePrx robotStatePrx, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
This is a convenience function for createLocalClone, which automatically gets the filename from the R...
Definition: RemoteRobot.cpp:512
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
visionx::UCLObjectRecognition::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: UCLObjectRecognition.cpp:54
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:100
armarx::TimestampVariant
Definition: TimestampVariant.h:54
PersistentEntitySegment.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
visionx::UCLObjectRecognitionPropertyDefinitions
Definition: UCLObjectRecognition.h:49
FramedPose.h
visionx::UCLObjectRecognition::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: UCLObjectRecognition.cpp:63
TimestampVariant.h
UCLObjectRecognition.h
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
CMakePackageFinder.h
armarx::Pose
The Pose class.
Definition: Pose.h:242
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
IceUtil::Handle< class PropertyDefinitionContainer >
MemoryXTypesObjectFactories.h
TypeMapping.h
visionx::UCLObjectRecognition::localizeObjectClasses
memoryx::ObjectLocalizationResultList localizeObjectClasses(const memoryx::ObjectClassNameList &classes, const Ice::Current &) override
Definition: UCLObjectRecognition.cpp:87
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:38
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:40