Apollo 10.0
自动驾驶开放平台
audio_offline_processing.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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 <string>
18
19#include <boost/filesystem.hpp>
20#include <boost/range/iterator_range.hpp>
21
22#include "cyber/common/file.h"
27#include "modules/audio/proto/audio_conf.pb.h"
28#include "modules/common_msgs/audio_msgs/audio_event.pb.h"
29#include "modules/common_msgs/localization_msgs/localization.pb.h"
30#include "modules/common_msgs/perception_msgs/perception_obstacle.pb.h"
31
32namespace apollo {
33namespace audio {
34
41
42void GetRecordFileNames(const boost::filesystem::path& p,
43 std::vector<std::string>* record_files) {
44 if (!boost::filesystem::exists(p)) {
45 return;
46 }
47 if (boost::filesystem::is_regular_file(p)) {
48 AINFO << "Found record file: " << p.c_str();
49 record_files->push_back(p.c_str());
50 return;
51 }
52 if (boost::filesystem::is_directory(p)) {
53 for (auto& entry : boost::make_iterator_range(
54 boost::filesystem::directory_iterator(p), {})) {
55 GetRecordFileNames(entry.path(), record_files);
56 }
57 }
58}
59
60void ProcessSingleRecordFile(const AudioConf& audio_conf,
61 const std::string& input_record_filepath,
62 const std::string& output_record_filepath,
63 AudioInfo* audio_info,
64 DirectionDetection* direction_detection,
65 MovingDetection* moving_detection,
66 SirenDetection* siren_detection) {
67 RecordReader reader(input_record_filepath);
68 RecordMessage message;
69 RecordWriter writer;
70 writer.Open(output_record_filepath);
71 while (reader.ReadMessage(&message)) {
72 if (message.channel_name ==
73 audio_conf.topic_conf().audio_data_topic_name()) {
74 AudioData audio_data;
75 if (audio_data.ParseFromString(message.content)) {
76 AudioDetection audio_detection;
77 std::string respeaker_extrinsics_file =
78 audio_conf.respeaker_extrinsics_path();
79 MessageProcess::OnMicrophone(audio_data, respeaker_extrinsics_file,
80 audio_info, direction_detection, moving_detection, siren_detection,
81 &audio_detection);
84 audio_detection, message.time);
85 AINFO << "Generate a new audio detection message.";
86 }
87 } else if (message.channel_name ==
88 audio_conf.topic_conf().audio_event_topic_name()) {
89 AudioEvent audio_event;
90 if (audio_event.ParseFromString(message.content)) {
91 writer.WriteMessage<AudioEvent>(message.channel_name, audio_event,
92 message.time);
93 AINFO << "Save an audio even message.";
94 }
95 } else if (message.channel_name ==
96 audio_conf.topic_conf().localization_topic_name()) {
97 LocalizationEstimate localization;
98 if (localization.ParseFromString(message.content)) {
100 message.channel_name, localization, message.time);
101 AINFO << "Save a localization message.";
102 }
103 } else if (message.channel_name ==
104 audio_conf.topic_conf().perception_topic_name()) {
105 PerceptionObstacles perception_obstacles;
106 if (perception_obstacles.ParseFromString(message.content)) {
108 message.channel_name, perception_obstacles, message.time);
109 AINFO << "Save a perception message.";
110 }
111 }
112 }
113 writer.Close();
114}
115
117 if (FLAGS_audio_records_dir.empty()) {
118 AERROR << "The input folder is empty";
119 return;
120 }
121 AudioConf audio_conf;
122 if (!cyber::common::GetProtoFromFile(FLAGS_audio_conf_file,
123 &audio_conf)) {
124 return;
125 }
126 AudioInfo audio_info;
127 DirectionDetection direction_detection;
128 MovingDetection moving_detection;
129 SirenDetection siren_detection;
130 std::vector<std::string> offline_bags;
131 GetRecordFileNames(boost::filesystem::path(FLAGS_audio_records_dir),
132 &offline_bags);
133 std::sort(offline_bags.begin(), offline_bags.end());
134 for (std::size_t i = 0; i < offline_bags.size(); ++i) {
135 const std::string& input_record_filepath = offline_bags[i];
136 std::string output_record_filepath =
137 input_record_filepath + ".new_audio_detection";
138 ProcessSingleRecordFile(audio_conf, input_record_filepath,
139 output_record_filepath, &audio_info, &direction_detection,
140 &moving_detection, &siren_detection);
141 }
142}
143
144} // namespace audio
145} // namespace apollo
146
147int main(int argc, char* argv[]) {
148 google::ParseCommandLineFlags(&argc, &argv, true);
150 return 0;
151}
int main(int argc, char *argv[])
static void OnMicrophone(const apollo::drivers::microphone::config::AudioData &audio_data, const std::string &respeaker_extrinsics_file, AudioInfo *audio_info, DirectionDetection *direction_detection, MovingDetection *moving_detection, SirenDetection *siren_detection, AudioDetection *audio_detection)
bool ReadMessage(RecordMessage *message, uint64_t begin_time=0, uint64_t end_time=std::numeric_limits< uint64_t >::max())
Read one message from reader.
bool Open(const std::string &file)
Open a record to write.
bool WriteMessage(const std::string &channel_name, const MessageT &message, const uint64_t time_nanosec, const std::string &proto_desc="")
Write a message to record.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
void ProcessSingleRecordFile(const AudioConf &audio_conf, const std::string &input_record_filepath, const std::string &output_record_filepath, AudioInfo *audio_info, DirectionDetection *direction_detection, MovingDetection *moving_detection, SirenDetection *siren_detection)
void GetRecordFileNames(const boost::filesystem::path &p, std::vector< std::string > *record_files)
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
class register implement
Definition arena_queue.h:37
optional TopicConf topic_conf
optional string respeaker_extrinsics_path
optional string audio_data_topic_name
optional string audio_event_topic_name
optional string localization_topic_name
optional string perception_topic_name
optional string audio_detection_topic_name
Basic data struct of record message.
std::string content
The content of the message.
uint64_t time
The time (nanosecond) of the message.
std::string channel_name
The channel name of the message.