27 #include <pcl/PointIndices.h>
28 #include <pcl/common/centroid.h>
29 #include <pcl/filters/statistical_outlier_removal.h>
30 #include <pcl/sample_consensus/mlesac.h>
31 #include <pcl/segmentation/extract_clusters.h>
41 MaskRCNNPointCloudObjectLocalizer::onInitPointCloudProcessor()
43 pcl::console::setVerbosityLevel(pcl::console::L_ALWAYS);
46 agentName = getProperty<std::string>(
"agentName").getValue();
48 sourceNodeName = getProperty<std::string>(
"sourceNodeName").getValue();
50 offeringTopic(getProperty<std::string>(
"DebugDrawerTopic").getValue());
52 offeringTopic(
"ServiceRequests");
56 MaskRCNNPointCloudObjectLocalizer::onConnectPointCloudProcessor()
58 serviceTopic = getTopic<armarx::RequestableServiceListenerInterfacePrx>(
"ServiceRequests");
60 enableResultPointClouds<PointL>();
64 MaskRCNNPointCloudObjectLocalizer::onDisconnectPointCloudProcessor()
69 MaskRCNNPointCloudObjectLocalizer::onExitPointCloudProcessor()
74 MaskRCNNPointCloudObjectLocalizer::process()
79 memoryx::ObjectLocalizationResultList
80 MaskRCNNPointCloudObjectLocalizer::localizeObjectClasses(
81 const memoryx::ObjectClassNameList& objectClassNames,
82 const Ice::Current&
c)
86 serviceTopic->requestService(
"MaskRCNNImageProcessor", 1000);
87 std::scoped_lock lock2(pointCloudMutex);
88 memoryx::ObjectLocalizationResultList resultList;
89 pcl::PointCloud<PointL>::Ptr cloudPtr(
new pcl::PointCloud<PointL>());
90 pcl::PointCloud<PointL>::Ptr resultCloud(
new pcl::PointCloud<PointL>());
93 if (!waitForPointClouds(10000))
95 ARMARX_WARNING <<
"Timeout or error while waiting for point cloud data"
101 getPointClouds(cloudPtr);
104 uint32_t backgroundLabel = getProperty<uint32_t>(
"BackgroundLabelId").getValue();
105 auto mapping =
armarx::Split(getProperty<std::string>(
"ObjectNameIdMap"),
";",
true,
true);
106 std::map<std::string, int> nameIdMap;
107 std::map<int, std::string> idNameMap;
108 for (
auto entry : mapping)
117 cloudPtr->is_dense =
false;
118 std::vector<int>
index;
119 ARMARX_INFO <<
"Cloud size with NaN: " << cloudPtr->size();
120 pcl::removeNaNFromPointCloud(*cloudPtr, *cloudPtr,
index);
121 ARMARX_INFO <<
"Cloud size without NaN: " << cloudPtr->size();
124 std::map<uint32_t, pcl::PointIndices> labeledPointMap;
125 visionx::tools::fillLabelMap<pcl::PointCloud<PointL>::Ptr>(cloudPtr, labeledPointMap);
126 std::map<std::string, Eigen::Vector3f> positionMap;
129 pcl::search::KdTree<PointL>::Ptr tree(
new pcl::search::KdTree<PointL>);
130 tree->setInputCloud(cloudPtr);
132 pcl::EuclideanClusterExtraction<PointL> ec;
133 ec.setClusterTolerance(20);
134 ec.setMinClusterSize(100);
135 ec.setMaxClusterSize(25000);
136 ec.setInputCloud(cloudPtr);
137 ec.setSearchMethod(tree);
140 for (
auto& it : labeledPointMap)
142 auto label = it.first;
143 if (label == backgroundLabel)
149 if (!idNameMap.count(label))
154 auto labelName = idNameMap.at(label);
156 if (std::find(objectClassNames.begin(), objectClassNames.end(), labelName) ==
157 objectClassNames.end())
163 auto labelIndices = it.second.indices;
165 pcl::IndicesPtr i(
new std::vector<int>(it.second.indices));
167 std::vector<pcl::PointIndices> cluster_indices;
168 ec.extract(cluster_indices);
169 ARMARX_VERBOSE <<
"Found " << cluster_indices.size() <<
" clusters";
171 if (!cluster_indices.size())
178 size_t maxClusterSize = 0;
179 size_t indexBiggestCluster = 0;
180 for (
auto& cluster : cluster_indices)
183 if (cluster.indices.size() > maxClusterSize)
185 indexBiggestCluster = j;
186 maxClusterSize = cluster.indices.size();
190 labelIndices = cluster_indices.at(indexBiggestCluster).indices;
193 pcl::PointCloud<PointL>::Ptr segment(
new pcl::PointCloud<PointL>());
194 copyPointCloud(*cloudPtr, labelIndices, *segment);
195 *resultCloud += *segment;
198 Eigen::Vector4f centroid;
199 pcl::compute3DCentroid(*segment, centroid);
200 positionMap[labelName] = centroid.head<3>();
204 for (
auto pair : positionMap)
206 auto name = pair.first;
207 Eigen::Vector3f position = pair.second;
209 memoryx::ObjectLocalizationResult result;
210 result.position =
new FramedPosition(position, sourceNodeName, agentName);
214 result.objectClassName = name;
215 result.recognitionCertainty = 0.6;
218 Eigen::Vector3f cov(500, 500, 500);
220 Eigen::Vector3f::Zero(), cov.asDiagonal());
221 resultList.push_back(result);
224 provideResultPointClouds(resultCloud);
230 MaskRCNNPointCloudObjectLocalizer::getLocation(FramedOrientationBasePtr& orientation,
231 FramedPositionBasePtr& position,
232 const Ice::Current&
c)
234 throw LocalException(
"NYI");
238 MaskRCNNPointCloudObjectLocalizer::createPropertyDefinitions()