Apollo 10.0
自动驾驶开放平台
offline_camera_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#include <vector>
20
21#include "gflags/gflags.h"
22
23#include "cyber/common/file.h"
32
33DEFINE_int32(height, 1080, "image height");
34DEFINE_int32(width, 1920, "image width");
35DEFINE_int32(gpu_id, 0, "gpu id");
36DEFINE_string(dest_dir, "./data/output", "output dir");
37DEFINE_string(dist_type, "", "dist pred type: H-on-h, H-from-h");
38DEFINE_string(kitti_dir, "", "pre-detected obstacles (skip Detect)");
40 "modules/perception/tools/offline_camera_detection/",
41 "image root dir");
42DEFINE_string(image_ext, ".jpg", "extension of image name");
43DEFINE_string(config_path, "perception/camera_detection_multi_stage/data",
44 "config path");
45DEFINE_string(config_file, "yolox3d.pb.txt", "config file");
46DEFINE_string(camera_name, "front_6mm", "camera name");
47DEFINE_string(detector_name, "Yolox3DObstacleDetector", "detector name");
48
49namespace apollo {
50namespace perception {
51namespace camera {
52
54 DataProvider::InitOptions init_options;
55 init_options.sensor_name = FLAGS_camera_name;
56 init_options.image_height = FLAGS_height;
57 init_options.image_width = FLAGS_width;
58 init_options.device_id = FLAGS_gpu_id;
59
60 bool res = camera_frame->data_provider->Init(init_options);
61 return res;
62}
63
65 // Init frame
66 onboard::CameraFrame camera_frame;
67 camera_frame.data_provider.reset(new DataProvider());
68 camera_frame.feature_blob.reset(new base::Blob<float>());
69
70 // Init data
71 ACHECK(InitDataProvider(&camera_frame));
72
73 ObstacleDetectorInitOptions init_options;
74 // Init conf file
75 init_options.config_path = FLAGS_config_path;
76 init_options.config_file = FLAGS_config_file;
77 init_options.gpu_id = FLAGS_gpu_id;
78
79 // Init camera params
80 std::string camera_name = FLAGS_camera_name;
82 algorithm::SensorManager::Instance()->GetUndistortCameraModel(
83 camera_name);
84 ACHECK(model) << "Can't find " << camera_name
85 << " in data/conf/sensor_meta.pb.txt";
86 auto pinhole = static_cast<base::PinholeCameraModel*>(model.get());
87 init_options.intrinsic = pinhole->get_intrinsic_params();
88 camera_frame.camera_k_matrix = pinhole->get_intrinsic_params();
89 init_options.image_height = model->get_height();
90 init_options.image_width = model->get_width();
91 // Init detection pipeline
92 std::shared_ptr<BaseObstacleDetector> detector;
93 detector.reset(
94 BaseObstacleDetectorRegisterer::GetInstanceByName(FLAGS_detector_name));
95 detector->Init(init_options);
96
97 // Load image list
98 std::string images_path = FLAGS_root_dir + "/images/";
99 std::vector<std::string> img_file_names;
100 if (!algorithm::GetFileList(images_path, ".jpg", &img_file_names)) {
101 AERROR << "images_path: " << images_path << " get file list error.";
102 return false;
103 }
104
105 std::sort(img_file_names.begin(), img_file_names.end(),
106 [](const std::string& lhs, const std::string& rhs) {
107 if (lhs.length() != rhs.length()) {
108 return lhs.length() < rhs.length();
109 }
110 return lhs <= rhs;
111 });
112
113 // Main process
114 for (const auto& img_file : img_file_names) {
115 AINFO << "img_file: " << img_file;
116
117 std::string image_name = cyber::common::GetFileName(img_file, true);
118
119 if (FLAGS_kitti_dir != "") {
120 std::string kitti_path = FLAGS_kitti_dir + "/" + image_name + ".txt";
121
122 if (!LoadKittiLabel(&camera_frame, kitti_path, FLAGS_dist_type)) {
123 AERROR << "Loading kitti result failed: " << kitti_path;
124 continue;
125 }
126 } else {
127 ACHECK(FillImage(&camera_frame, img_file));
128 ACHECK(detector->Detect(&camera_frame));
129 }
130
131 // Results visualization
132 if (!cyber::common::EnsureDirectory(FLAGS_dest_dir)) {
133 AERROR << "Failed to create: " << FLAGS_dest_dir;
134 return false;
135 }
136
137 std::string result_path = FLAGS_dest_dir + "/" + image_name + ".txt";
138 ACHECK(SaveCameraDetectionResult(&camera_frame, result_path));
139
140 result_path = FLAGS_dest_dir + "/" + image_name + ".jpg";
141 ACHECK(CameraVisualization(&camera_frame, result_path));
142 }
143
144 return true;
145}
146
147} // namespace camera
148} // namespace perception
149} // namespace apollo
150
151int main(int argc, char* argv[]) {
152 FLAGS_alsologtostderr = 1;
153 google::ParseCommandLineFlags(&argc, &argv, true);
154 google::InitGoogleLogging(argv[0]);
155 google::SetUsageMessage(
156 "command line brew\n"
157 "Usage: camera_benchmark <args>\n");
159 return 0;
160}
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::string GetFileName(const std::string &path, const bool remove_extension)
Definition file.cc:415
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.
Definition file.cc:299
bool GetFileList(const std::string &path, const std::string &suffix, std::vector< std::string > *files)
Definition io_util.cc:163
std::shared_ptr< BaseCameraModel > BaseCameraModelPtr
Definition camera.h:75
bool LoadKittiLabel(onboard::CameraFrame *frame, const std::string &kitti_path, const std::string &dist_type)
Read KITTI labels and fill to frame->detected_objects
bool FillImage(onboard::CameraFrame *frame, const std::string &file_name)
Read image from file_name and fill in frame->data_provider
Definition util.cc:28
bool CameraVisualization(onboard::CameraFrame *frame, const std::string &file_name)
Save visualization results to file_name(.jpg)
Definition visualizer.cc:40
bool InitDataProvider(onboard::CameraFrame *camera_frame)
bool SaveCameraDetectionResult(onboard::CameraFrame *frame, const std::string &file_name)
Save detection result to file_name
Definition util.cc:84
class register implement
Definition arena_queue.h:37
DEFINE_int32(height, 1080, "image height")
int main(int argc, char *argv[])
DEFINE_string(dest_dir, "./data/output", "output dir")
std::shared_ptr< camera::DataProvider > data_provider