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
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
44using namespace armarx;
45
46using namespace std::filesystem;
47
48namespace 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");
67 getProperty<std::string>("DebugDrawerTopicName").getValue());
68 if (!debugDrawerTopicPrx)
69 {
70 ARMARX_ERROR << "Failed to obtain debug drawer proxy";
71 return;
72 }
74 getProperty<std::string>("RobotStateComponentName").getValue());
76 robotStateComponent, VirtualRobot::RobotIO::RobotDescription::eFull);
77 }
78
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
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define VAROUT(x)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
The FramedOrientation class.
Definition FramedPose.h:216
The FramedPosition class.
Definition FramedPose.h:158
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The Pose class.
Definition Pose.h:243
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...
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
Implements a Variant type for timestamps.
The MultivariateNormalDistribution class.
void onInitComponent() override
Pure virtual hook for the subclass.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
memoryx::ObjectLocalizationResultList localizeObjectClasses(const memoryx::ObjectClassNameList &classes, const Ice::Current &) override
void onConnectComponent() override
Pure virtual hook for the subclass.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
ArmarX headers.