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 #include <Image/PrimitivesDrawerCV.h>
25 #include <Image/PrimitivesDrawer.h>
27 
32 
34 
37 
38 #include <Image/ImageProcessor.h>
39 #include <Image/IplImageAdaptor.h>
40 
41 #include <Eigen/Geometry>
42 #include <Calibration/Calibration.h>
43 
44 using namespace armarx;
45 
46 using namespace std::filesystem;
47 namespace visionx
48 {
49  //using namespace boost::python;
50 
51  //void visionx::UCLObjectRecognition::onInitObjectLocalizerProcessor()
53  {
54  usingProxy("ObjectPoseProvider");
55  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
56  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
57 
58  }
59 
60  //void visionx::UCLObjectRecognition::onConnectObjectLocalizerProcessor()
62  {
63  getProxy(prx, "ObjectPoseProvider");
64  debugDrawerTopicPrx = getTopic<armarx::DebugDrawerInterfacePrx>(getProperty<std::string>("DebugDrawerTopicName").getValue());
65  if (!debugDrawerTopicPrx)
66  {
67  ARMARX_ERROR << "Failed to obtain debug drawer proxy";
68  return;
69  }
70  robotStateComponent = getProxy<armarx::RobotStateComponentInterfacePrx>(getProperty<std::string>("RobotStateComponentName").getValue());
72 
73 
74  }
75 
76 
77 
78 
79 
80  armarx::PropertyDefinitionsPtr UCLObjectRecognition::createPropertyDefinitions()
81  {
83  getConfigIdentifier()));
84  }
85 
86 
87 
88  memoryx::ObjectLocalizationResultList visionx::UCLObjectRecognition::localizeObjectClasses(const memoryx::ObjectClassNameList& classes, const Ice::Current&)
89  {
90  // static std::size_t flip = 0;
91  // static std::size_t flipn = 100;
92 
93  ARMARX_VERBOSE << /*deactivateSpam(10) <<*/ "Localizing: " << classes;
94  auto agentName = getProperty<std::string>("AgentName").getValue();
95  auto cameraFrame = getProperty<std::string>("CameraFrameName").getValue();
96  auto mapping = armarx::Split(getProperty<std::string>("ObjectNameIdMap"), ";", true, true);
97  std::map<std::string, int> nameIdMap;
98  for (auto entry : mapping)
99  {
100  auto pair = armarx::Split(entry, ":");
101  ARMARX_CHECK_EQUAL(pair.size(), 2) << pair;
102  nameIdMap[pair.at(0)] = armarx::toFloat(pair.at(1));
103 
104 
105  // debugDrawerTopicPrx->removePoseVisu(getName(), pair.at(0) + "_raw");
106  // debugDrawerTopicPrx->removePoseVisu(getName(), pair.at(0) + "_global");
107  }
108  memoryx::ObjectLocalizationResultList result;
109  for (auto className : classes)
110  {
111  ARMARX_CHECK_EXPRESSION(nameIdMap.count(className)) << VAROUT(className) << " " << VAROUT(nameIdMap);
112  auto id = nameIdMap[className];
113  auto regResult = prx->getObjectPose(id);
114  if (regResult.confidence <= 0)
115  {
116  ARMARX_INFO << deactivateSpam(5, className) << "Nothing found for " << className;
117  continue;
118  }
119  memoryx::ObjectLocalizationResult r;
120  r.instanceName = className;
121  r.objectClassName = className;
122  r.recognitionCertainty = regResult.confidence * 0.2;
123  ARMARX_CHECK_EQUAL(regResult.matrix4x4.size(), 16) << regResult.confidence;
124  Eigen::Matrix4f pose = Eigen::Map<Eigen::Matrix4f>(regResult.matrix4x4.data()).transpose();
125  pose.block<3, 1>(0, 3) *= 1000.0;
126  ARMARX_INFO << "pose\n" << pose << '\n' << VAROUT(r.recognitionCertainty);
127 
128  {
129  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_raw", new Pose {pose});
130  RemoteRobot::synchronizeLocalClone(localRobot, robotStateComponent);
131  // const Eigen::Matrix4f gcam = localRobot->getRobotNode(cameraFrame)->getGlobalPose();
132  const Eigen::Matrix4f gpose = localRobot->getRobotNode(cameraFrame)->getGlobalPose() * pose;
133  debugDrawerTopicPrx->setPoseVisu(getName(), className + "_global", new Pose {gpose});
134  // ARMARX_IMPORTANT << "drawing\n" << pose
135  // << "\ncam\n" << localRobot->getRobotNode(cameraFrame)->getGlobalPose()
136  // << "\ngp\n" << gpose;
137 
138 
139  // Eigen::Matrix4f manualcam = gcam;
140  // manualcam(2, 3) += 1000;
141  // Eigen::Matrix4f manualPose = pose;
142  // manualPose.topRightCorner<3, 1>() += manualcam.topRightCorner<3, 1>();
143 
144  // //manualPose(2, 3) += 1000;
145 
146  // const Eigen::Vector3f gcammanpos = manualcam.topRightCorner<3, 1>();
147  // const Eigen::Vector3f gobjmanpos = manualPose.topRightCorner<3, 1>();
148  // const Eigen::Vector3f gcammanposX = (manualcam * Eigen::Vector4f {1000, 0, 0, 1}).topRightCorner<3, 1>();
149  // const Eigen::Vector3f gcammanposY = (manualcam * Eigen::Vector4f {0, 1000, 0, 1}).topRightCorner<3, 1>();
150  // const Eigen::Vector3f gcammanposZ = (manualcam * Eigen::Vector4f {0, 0, 1000, 1}).topRightCorner<3, 1>();
151  // ARMARX_INFO << VAROUT(manualPose);
152  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_manualPose", new Pose {manualPose});
153  // debugDrawerTopicPrx->setPoseVisu(getName(), className + "_manualcam", new Pose {manualcam});
154 
155  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_x", new Vector3 {gcammanpos}, new Vector3 {gcammanposX}, 5, {1, 0, 0, 1});
156  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_y", new Vector3 {gcammanpos}, new Vector3 {gcammanposY}, 5, {0, 1, 0, 1});
157  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_z", new Vector3 {gcammanpos}, new Vector3 {gcammanposZ}, 5, {0, 0, 1, 1});
158  // debugDrawerTopicPrx->setLineVisu(getName(), className + "_cam_obj", new Vector3 {gcammanpos}, new Vector3 {gobjmanpos}, 5, {0, 0, 0, 1});
159  }
160 
161 
162  r.orientation = new FramedOrientation(pose, cameraFrame, agentName);
163  r.position = new FramedPosition(pose, cameraFrame, agentName);
164  Eigen::Vector3f cov(10, 10, 10000);
165  r.positionNoise = new memoryx::MultivariateNormalDistribution(Eigen::Vector3f::Zero(), cov.asDiagonal());
166  ARMARX_INFO << "Pose is " << (TimeUtil::GetTime() - IceUtil::Time::milliSeconds(regResult.timestamp)).toMilliSeconds() << " ms old";
167  r.timeStamp = new armarx::TimestampVariant(regResult.timestamp * 1000); // ToDo: get timestamp from image
168  result.push_back(r);
169  }
170 
171 
172  return result;
173  }
174 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
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:448
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:441
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
visionx::UCLObjectRecognition::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: UCLObjectRecognition.cpp:52
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
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:72
visionx::UCLObjectRecognitionPropertyDefinitions
Definition: UCLObjectRecognition.h:49
FramedPose.h
visionx::UCLObjectRecognition::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: UCLObjectRecognition.cpp:61
TimestampVariant.h
UCLObjectRecognition.h
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
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
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
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:40
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
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:88
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
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:34
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::VariantType::MultivariateNormalDistribution
const armarx::VariantTypeId MultivariateNormalDistribution
Definition: ProbabilityMeasures.h:36