Apollo 11.0
自动驾驶开放平台
offline_pointcloud_ground_detection.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <memory>
18#include <string>
19
20#include "Eigen/Dense"
21#include "gflags/gflags.h"
22
23#include "cyber/common/file.h"
30
31DEFINE_string(pcd_path, "./pcd/", "pcd path");
32DEFINE_string(output_path, "./output/", "output path");
33DEFINE_string(ground_detector_name, "SpatioTemporalGroundDetector",
34 "ground detector name");
35DEFINE_string(config_path, "perception/pointcloud_ground_detection/data",
36 "config path");
37DEFINE_string(config_file, "spatio_temporal_ground_detector.pb.txt",
38 "config file");
39
40namespace apollo {
41namespace perception {
42namespace lidar {
43
45 public:
47
49
50 bool Init() {
51 BaseGroundDetector* ground_detector =
52 BaseGroundDetectorRegisterer::GetInstanceByName(
53 FLAGS_ground_detector_name);
54 CHECK_NOTNULL(ground_detector);
55 ground_detector_.reset(ground_detector);
56
57 GroundDetectorInitOptions ground_detector_init_options;
58 ground_detector_init_options.config_path = FLAGS_config_path;
59 ground_detector_init_options.config_file = FLAGS_config_file;
60 ACHECK(ground_detector_->Init(ground_detector_init_options))
61 << "Failed to init ground detection.";
62
63 return true;
64 }
65
66 bool TransformCloud(const base::PointFCloudPtr& local_cloud,
67 const Eigen::Affine3d& pose,
68 base::PointDCloudPtr world_cloud) const {
69 if (local_cloud == nullptr) {
70 return false;
71 }
72 world_cloud->clear();
73 world_cloud->reserve(local_cloud->size());
74 for (size_t i = 0; i < local_cloud->size(); ++i) {
75 auto& pt = local_cloud->at(i);
76 Eigen::Vector3d trans_point(pt.x, pt.y, pt.z);
77 trans_point = pose * trans_point;
78 base::PointD world_point;
79 world_point.x = trans_point(0);
80 world_point.y = trans_point(1);
81 world_point.z = trans_point(2);
82 world_point.intensity = pt.intensity;
83 world_cloud->push_back(world_point, local_cloud->points_timestamp(i),
84 std::numeric_limits<float>::max(),
85 local_cloud->points_beam_id()[i], 0);
86 }
87 return true;
88 }
89
90 bool Run() {
91 std::string pcd_folder = FLAGS_pcd_path;
92 std::vector<std::string> pcd_file_names;
93 if (!algorithm::GetFileList(pcd_folder, ".pcd", &pcd_file_names)) {
94 AERROR << "pcd_folder: " << pcd_folder << " get file list error.";
95 return false;
96 }
97
98 std::sort(pcd_file_names.begin(), pcd_file_names.end(),
99 [](const std::string& lhs, const std::string& rhs) {
100 if (lhs.length() != rhs.length()) {
101 return lhs.length() < rhs.length();
102 }
103 return lhs <= rhs;
104 });
105
106 for (size_t i = 0; i < pcd_file_names.size(); ++i) {
107 AINFO << "***************** Frame " << i << " ******************";
108 AINFO << pcd_file_names[i];
109
110 std::shared_ptr<LidarFrame> frame = LidarFramePool::Instance().Get();
111 if (frame->cloud == nullptr) {
112 frame->cloud = base::PointFCloudPool::Instance().Get();
113 }
114 if (frame->world_cloud == nullptr) {
115 frame->world_cloud = base::PointDCloudPool::Instance().Get();
116 }
117 LoadPCLPCD(pcd_file_names[i], frame->cloud.get());
118 // frame->lidar2world_pose is default identity matrix
119 TransformCloud(frame->cloud, frame->lidar2world_pose, frame->world_cloud);
120 AINFO << "Read point cloud from " << pcd_file_names[i]
121 << " with cloud size: " << frame->cloud->size();
122
123 GroundDetectorOptions ground_detector_options;
124 if (!ground_detector_->Detect(ground_detector_options, frame.get())) {
125 AERROR << "Ground detect error.";
126 return false;
127 }
128
129 std::string output_path = FLAGS_output_path;
130 SavePCD(i, frame.get(), output_path);
131 }
132
133 return true;
134 }
135
136 bool SavePCD(size_t frame_id, LidarFrame* frame, const std::string& path) {
138 AERROR << "Failed to create: " << path;
139 return false;
140 }
141
142 std::string file_path = absl::StrCat(path, "/", frame_id, ".pcd");
143 WritePcdByLabel(file_path, *(frame->cloud), LidarPointLabel::GROUND);
144
145 return true;
146 }
147
148 protected:
149 std::unique_ptr<BaseGroundDetector> ground_detector_;
150};
151
152} // namespace lidar
153} // namespace perception
154} // namespace apollo
155
156int main(int argc, char** argv) {
157 FLAGS_alsologtostderr = 1;
158 google::ParseCommandLineFlags(&argc, &argv, true);
159 google::InitGoogleLogging(argv[0]);
160
162 if (!test.Init()) {
163 AINFO << "Failed to setup OfflinePointcloudGroundDetection";
164 return -1;
165 }
166 return test.Run() ? 0 : -1;
167}
std::shared_ptr< ObjectType > Get() override
bool SavePCD(size_t frame_id, LidarFrame *frame, const std::string &path)
bool TransformCloud(const base::PointFCloudPtr &local_cloud, const Eigen::Affine3d &pose, base::PointDCloudPtr world_cloud) const
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.
Definition file.cc:285
bool GetFileList(const std::string &path, const std::string &suffix, std::vector< std::string > *files)
Definition io_util.cc:163
std::shared_ptr< PointDCloud > PointDCloudPtr
std::shared_ptr< PointFCloud > PointFCloudPtr
bool WritePcdByLabel(const std::string &file_path, const base::PointFCloud &cloud, const LidarPointLabel &label)
Definition pcl_util.h:74
bool LoadPCLPCD(const std::string &file_path, base::PointFCloud *cloud_out)
Definition pcl_util.h:54
class register implement
Definition arena_queue.h:37
int main(int argc, char **argv)
DEFINE_string(pcd_path, "./pcd/", "pcd path")
std::shared_ptr< base::AttributePointCloud< base::PointF > > cloud
Definition lidar_frame.h:35