30 #include <pcl/PointIndices.h>
31 #include <pcl/filters/statistical_outlier_removal.h>
32 #include <pcl/sample_consensus/mlesac.h>
33 #include <pcl/segmentation/extract_clusters.h>
34 #include <pcl/common/centroid.h>
40 void MaskRCNNPointCloudObjectLocalizer::onInitPointCloudProcessor()
42 pcl::console::setVerbosityLevel(pcl::console::L_ALWAYS);
45 agentName = getProperty<std::string>(
"agentName").getValue();
47 sourceNodeName = getProperty<std::string>(
"sourceNodeName").getValue();
49 offeringTopic(getProperty<std::string>(
"DebugDrawerTopic").getValue());
51 offeringTopic(
"ServiceRequests");
54 void MaskRCNNPointCloudObjectLocalizer::onConnectPointCloudProcessor()
56 serviceTopic = getTopic<armarx::RequestableServiceListenerInterfacePrx>(
"ServiceRequests");
58 enableResultPointClouds<PointL>();
61 void MaskRCNNPointCloudObjectLocalizer::onDisconnectPointCloudProcessor()
66 void MaskRCNNPointCloudObjectLocalizer::onExitPointCloudProcessor()
70 void MaskRCNNPointCloudObjectLocalizer::process()
79 memoryx::ObjectLocalizationResultList MaskRCNNPointCloudObjectLocalizer::localizeObjectClasses(
const memoryx::ObjectClassNameList& objectClassNames,
const Ice::Current&
c)
83 serviceTopic->requestService(
"MaskRCNNImageProcessor", 1000);
84 std::scoped_lock lock2(pointCloudMutex);
85 memoryx::ObjectLocalizationResultList resultList;
86 pcl::PointCloud<PointL>::Ptr cloudPtr(
new pcl::PointCloud<PointL>());
87 pcl::PointCloud<PointL>::Ptr resultCloud(
new pcl::PointCloud<PointL>());
90 if (!waitForPointClouds(10000))
97 getPointClouds(cloudPtr);
100 uint32_t backgroundLabel = getProperty<uint32_t>(
"BackgroundLabelId").getValue();
101 auto mapping =
armarx::Split(getProperty<std::string>(
"ObjectNameIdMap"),
";",
true,
true);
102 std::map<std::string, int> nameIdMap;
103 std::map<int, std::string> idNameMap;
104 for (
auto entry : mapping)
114 cloudPtr->is_dense =
false;
115 std::vector<int>
index;
116 ARMARX_INFO <<
"Cloud size with NaN: " << cloudPtr->size();
117 pcl::removeNaNFromPointCloud(*cloudPtr, *cloudPtr,
index);
118 ARMARX_INFO <<
"Cloud size without NaN: " << cloudPtr->size();
121 std::map<uint32_t, pcl::PointIndices> labeledPointMap;
122 visionx::tools::fillLabelMap<pcl::PointCloud<PointL>::Ptr>(cloudPtr, labeledPointMap);
123 std::map<std::string, Eigen::Vector3f> positionMap;
126 pcl::search::KdTree<PointL>::Ptr tree(
new pcl::search::KdTree<PointL>);
127 tree->setInputCloud(cloudPtr);
129 pcl::EuclideanClusterExtraction<PointL> ec;
130 ec.setClusterTolerance(20);
131 ec.setMinClusterSize(100);
132 ec.setMaxClusterSize(25000);
133 ec.setInputCloud(cloudPtr);
134 ec.setSearchMethod(tree);
137 for (
auto& it : labeledPointMap)
139 auto label = it.first;
140 if (label == backgroundLabel)
146 if (!idNameMap.count(label))
151 auto labelName = idNameMap.at(label);
153 if (std::find(objectClassNames.begin(), objectClassNames.end(), labelName) == objectClassNames.end())
159 auto labelIndices = it.second.indices;
161 pcl::IndicesPtr i(
new std::vector<int>(it.second.indices));
163 std::vector<pcl::PointIndices> cluster_indices;
164 ec.extract(cluster_indices);
165 ARMARX_VERBOSE <<
"Found " << cluster_indices.size() <<
" clusters";
167 if (!cluster_indices.size())
174 size_t maxClusterSize = 0;
175 size_t indexBiggestCluster = 0;
176 for (
auto& cluster : cluster_indices)
179 if (cluster.indices.size() > maxClusterSize)
181 indexBiggestCluster = j;
182 maxClusterSize = cluster.indices.size();
186 labelIndices = cluster_indices.at(indexBiggestCluster).indices;
189 pcl::PointCloud<PointL>::Ptr segment(
new pcl::PointCloud<PointL>());
190 copyPointCloud(*cloudPtr, labelIndices, *segment);
191 *resultCloud += *segment;
194 Eigen::Vector4f centroid;
195 pcl::compute3DCentroid(*segment, centroid);
196 positionMap[labelName] = centroid.head<3>();
201 for (
auto pair : positionMap)
203 auto name = pair.first;
204 Eigen::Vector3f position = pair.second;
206 memoryx::ObjectLocalizationResult result;
207 result.position =
new FramedPosition(position, sourceNodeName, agentName);
211 result.objectClassName = name;
212 result.recognitionCertainty = 0.6;
215 Eigen::Vector3f cov(500, 500, 500);
217 resultList.push_back(result);
221 provideResultPointClouds(resultCloud);
227 void MaskRCNNPointCloudObjectLocalizer::getLocation(FramedOrientationBasePtr& orientation, FramedPositionBasePtr& position,
const Ice::Current&
c)
229 throw LocalException(
"NYI");
236 getConfigIdentifier()));