feat: split image timestamp CSV files when frame sync is disabled

This commit is contained in:
slz
2026-08-25 14:57:51 +08:00
parent 077afd1fff
commit a5a093fc79
4 changed files with 133 additions and 47 deletions
@@ -35,25 +35,26 @@ int64_t getExpectedIntervalUs(const std::shared_ptr<ob::Frame> &frame) {
FrameTimestampCsvLogger::FrameTimestampCsvLogger(bool drop_log_enabled,
const std::string &csv_file_path,
rclcpp::Logger logger)
OutputMode output_mode, rclcpp::Logger logger)
: logger_(std::move(logger)),
enabled_(drop_log_enabled || !csv_file_path.empty()),
csv_enabled_(!csv_file_path.empty()),
drop_log_enabled_(drop_log_enabled),
csv_file_path_(csv_file_path) {
csv_file_path_(csv_file_path),
output_mode_(output_mode) {
if (!enabled_) {
return;
}
if (csv_enabled_) {
try {
auto path = std::filesystem::path(csv_file_path_);
auto path = std::filesystem::path(csvFilePathForIndex(0));
if (path.has_parent_path() && !std::filesystem::exists(path.parent_path())) {
std::filesystem::create_directories(path.parent_path());
}
} catch (const std::exception &e) {
RCLCPP_ERROR_STREAM(logger_, "Failed to prepare frame timestamp CSV path "
<< csv_file_path_ << ": " << e.what());
<< csvFilePathForIndex(0) << ": " << e.what());
csv_enabled_ = false;
csv_writer_failed_ = true;
}
@@ -74,7 +75,7 @@ FrameTimestampCsvLogger::FrameTimestampCsvLogger(bool drop_log_enabled,
if (enabled_) {
RCLCPP_INFO_STREAM(logger_,
"Frame timestamp logger enabled: csv_file="
<< (csv_enabled_ ? csv_file_path_ : "disabled")
<< (csv_enabled_ ? csvFilePathForIndex(0) : "disabled")
<< " frame_drop_log=" << (drop_log_enabled_ ? "enabled" : "disabled"));
}
}
@@ -90,6 +91,9 @@ void FrameTimestampCsvLogger::recordFrameSet(const std::shared_ptr<ob::Frame> &c
if (!enabled_) {
return;
}
if (output_mode_ != OutputMode::SYNCED) {
return;
}
recordFrameSetInternal(color_frame, depth_frame, arrival_system_us, arrival_steady_us,
track_color, track_depth, color_image_publish_expected,
depth_image_publish_expected);
@@ -100,7 +104,9 @@ void FrameTimestampCsvLogger::recordStandaloneFrameArrival(OBStreamType stream_t
int64_t arrival_system_us,
int64_t arrival_steady_us,
bool image_publish_expected) {
if (!enabled_ || !frame || !isTrackedStream(stream_type)) {
if (!enabled_ || !frame || !isTrackedStream(stream_type) ||
(stream_type == OB_STREAM_COLOR && output_mode_ != OutputMode::COLOR) ||
(stream_type == OB_STREAM_DEPTH && output_mode_ != OutputMode::DEPTH)) {
return;
}
recordStandaloneFrameArrivalInternal(stream_type, frame, arrival_system_us, arrival_steady_us,
@@ -479,6 +485,12 @@ void FrameTimestampCsvLogger::eraseFrameIndexMappingLocked(const PendingRow &row
}
std::string FrameTimestampCsvLogger::serializeRow(const PendingRow &row) const {
if (output_mode_ == OutputMode::COLOR) {
return serializeStreamColumns(row.color);
}
if (output_mode_ == OutputMode::DEPTH) {
return serializeStreamColumns(row.depth);
}
std::ostringstream ss;
ss << serializeStreamColumns(row.color) << "," << serializeStreamColumns(row.depth);
return ss.str();
@@ -529,9 +541,9 @@ std::string FrameTimestampCsvLogger::formatOptionalIntColumn(const std::optional
return std::to_string(*value);
}
std::string FrameTimestampCsvLogger::csvHeader() {
std::string FrameTimestampCsvLogger::csvHeader() const {
std::ostringstream ss;
for (const auto *prefix : {"color", "depth"}) {
const auto append_stream_header = [&ss](const char *prefix) {
ss << prefix << "_sdk_frame_index,";
ss << prefix << "_hardware_frame_number,";
ss << prefix << "_sensor_ts_sec,";
@@ -547,9 +559,15 @@ std::string FrameTimestampCsvLogger::csvHeader() {
ss << prefix << "_arrival_to_publish_steady_us,";
ss << prefix << "_sdk_delay_from_global_us,";
ss << prefix << "_sdk_delay_from_system_us";
if (std::string(prefix) == "color") {
ss << ",";
}
};
if (output_mode_ == OutputMode::SYNCED || output_mode_ == OutputMode::COLOR) {
append_stream_header("color");
}
if (output_mode_ == OutputMode::SYNCED) {
ss << ",";
}
if (output_mode_ == OutputMode::SYNCED || output_mode_ == OutputMode::DEPTH) {
append_stream_header("depth");
}
return ss.str();
}
@@ -621,13 +639,19 @@ void FrameTimestampCsvLogger::writerThreadMain() {
}
std::string FrameTimestampCsvLogger::csvFilePathForIndex(uint64_t file_index) const {
if (file_index == 0) {
return csv_file_path_;
const std::filesystem::path original_path(csv_file_path_);
std::string suffix;
if (output_mode_ == OutputMode::COLOR) {
suffix = "_color";
} else if (output_mode_ == OutputMode::DEPTH) {
suffix = "_depth";
}
const std::filesystem::path original_path(csv_file_path_);
const auto indexed_filename = original_path.stem().string() + "_" + std::to_string(file_index) +
original_path.extension().string();
auto indexed_filename = original_path.stem().string() + suffix;
if (file_index != 0) {
indexed_filename += "_" + std::to_string(file_index);
}
indexed_filename += original_path.extension().string();
return (original_path.parent_path() / indexed_filename).string();
}