Skip to content

Commit

Permalink
continue removing ROS camera names
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Jan 29, 2024
1 parent 88bcf1b commit a92028f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
11 changes: 4 additions & 7 deletions braid-offline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ where
count += 1;
info!("Calibration contains camera: {cam_id_in_calibration}");
if !cam_ids.iter().any(|x| x == cam_id_in_calibration) {
let ros_name_calib = RawCamName::new(cam_id_in_calibration.clone()).to_ros();
let raw_name_calib = RawCamName::new(cam_id_in_calibration.clone());
if cam_ids
.iter()
.any(|x| x.as_str() == ros_name_calib.as_str())
.any(|x| x.as_str() == raw_name_calib.as_str())
{
found += 1;
}
Expand All @@ -291,11 +291,8 @@ where
info!("Converting camera calibration names from original to ROS-compatible names.");
let mut new_cams = std::collections::BTreeMap::new();
for (orig_name, orig_value) in cams.cams_by_name().iter() {
let ros_name = RawCamName::new(orig_name.clone())
.to_ros()
.as_str()
.to_string();
new_cams.insert(ros_name, orig_value.clone());
let raw_name = RawCamName::new(orig_name.clone()).as_str().to_string();
new_cams.insert(raw_name, orig_value.clone());
}
cams = if let Some(comment) = cams.comment() {
mvg::MultiCameraSystem::new_with_comment(new_cams, comment.clone())
Expand Down
40 changes: 30 additions & 10 deletions braid-process-video/src/braidz_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ struct BraidArchivePerCam<'a> {
cam_rows_peek_iter: std::iter::Peekable<std::slice::Iter<'a, Data2dDistortedRow>>,
}

fn as_ros_camid(raw_name: &str) -> String {
let ros_name: String = raw_name.replace('-', "_");
let ros_name: String = ros_name.replace(' ', "_");
let ros_name: String = ros_name.replace('/', "_");
ros_name
}

/// Iterate across multiple movies with a simultaneously recorded .braidz file
/// used to synchronize the frames.
pub(crate) struct BraidArchiveSyncVideoData<'a> {
Expand All @@ -137,11 +144,11 @@ impl<'a> BraidArchiveSyncVideoData<'a> {
pub(crate) fn new(
archive: braidz_parser::BraidzArchive<std::io::BufReader<std::fs::File>>,
data2d: &'a BTreeMap<CamNum, Vec<Data2dDistortedRow>>,
ros_camera_names: &[&str],
camera_names: &[&str],
frame_readers: Vec<Peek2<Box<dyn Iterator<Item = Result<FrameData>>>>>,
sync_threshold: chrono::Duration,
) -> Result<Self> {
assert_eq!(ros_camera_names.len(), frame_readers.len());
assert_eq!(camera_names.len(), frame_readers.len());

// The readers will all have the current read position at
// `approx_start_time` when this is called.
Expand All @@ -164,7 +171,24 @@ impl<'a> BraidArchiveSyncVideoData<'a> {
// Get earliest starting video
let i = t0.iter().argmin().unwrap();
let earliest_start_rdr = &frame_readers[i];
let earliest_start_cam_name = &ros_camera_names[i];
let earliest_start_cam_name = &camera_names[i];

let camid2camn = &archive.cam_info.camid2camn;

// Backwards compatibility with old ROS names.
let as_camid = if camid2camn.contains_key(*earliest_start_cam_name) {
// If the camid2camn table has the new, "raw" name, use it.
str::to_string
} else {
// Otherwise, use the old ROS name.
if !camid2camn.contains_key(&as_ros_camid(earliest_start_cam_name)) {
anyhow::bail!(
"Braidz archive does not contain raw camera name, but it also does not contain a ROS name."
);
}
as_ros_camid
};

let earliest_start = earliest_start_rdr
.peek1()
.unwrap()
Expand All @@ -174,11 +198,7 @@ impl<'a> BraidArchiveSyncVideoData<'a> {
.unwrap()
.extra()
.host_timestamp();
let earliest_start_cam_num = archive
.cam_info
.camid2camn
.get(*earliest_start_cam_name)
.unwrap();
let earliest_start_cam_num = &camid2camn.get(&as_camid(*earliest_start_cam_name)).unwrap();

// Now get data2d row with this timestamp to find the synchronized frame number.
let cam_rows = data2d.get(earliest_start_cam_num).ok_or_else(|| {
Expand Down Expand Up @@ -206,11 +226,11 @@ impl<'a> BraidArchiveSyncVideoData<'a> {
}
let found_frame = found_frame.unwrap();

let per_cam = ros_camera_names
let per_cam = camera_names
.iter()
.zip(frame_readers.into_iter())
.map(|(cam_name, frame_reader)| {
let cam_num = *archive.cam_info.camid2camn.get(*cam_name).unwrap();
let cam_num = *camid2camn.get(&as_camid(*cam_name)).unwrap();

let cam_rows = data2d.get(&cam_num).unwrap();
for row in cam_rows.iter() {
Expand Down
64 changes: 31 additions & 33 deletions braid-process-video/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ fn synchronize_readers_from(
}
}

#[derive(Debug)]
struct PerCamRender {
best_name: String,
raw_name: RawCamName,
Expand Down Expand Up @@ -300,6 +301,7 @@ impl<'a> PerCamRenderFrame<'a> {
}
}

#[derive(Debug)]
struct CameraSource {
cam_id: CameraIdentifier,
per_cam_render: PerCamRender,
Expand All @@ -316,6 +318,7 @@ impl CameraSource {
}
}

#[derive(Debug)]
enum CameraIdentifier {
MovieOnly(MovieCamId),
BraidzOnly(BraidzCamId),
Expand Down Expand Up @@ -361,25 +364,35 @@ struct MovieCamId {
cfg_name: Option<String>,
/// Title given in movie metadata
title: Option<String>,
/// Title converted to ROS name (`-` replaced with `_`)
title_as_ros: Option<String>,
/// Filename converted to ROS name (`-` replaced with `_`)
filename_as_ros: Option<String>,
/// Camera name extracted from filename
cam_from_filename: Option<String>,
frame0_time: chrono::DateTime<chrono::FixedOffset>,
}

impl MovieCamId {
fn ros_name(&self) -> Option<String> {
if let Some(title_as_ros) = &self.title_as_ros {
return Some(title_as_ros.clone());
fn raw_name(&self) -> Option<String> {
if let Some(title) = &self.title {
return Some(title.clone());
}
if let Some(filename_as_ros) = &self.filename_as_ros {
return Some(filename_as_ros.clone());
if let Some(cam_from_filename) = &self.cam_from_filename {
return Some(cam_from_filename.clone());
}
None
}
}

impl std::fmt::Debug for MovieCamId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MovieCamId")
.field("filename", &self.filename)
.field("cfg_name", &self.cfg_name)
.field("title", &self.title)
.field("cam_from_filename", &self.cam_from_filename)
.field("frame0_time", &self.frame0_time)
.finish()
}
}

#[derive(Clone, Debug, PartialEq)]
struct BraidzCamId {
cam_id_str: String,
Expand Down Expand Up @@ -447,10 +460,6 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:

let reader = Some(Peek2::new(frame_source.iter()));

let title_as_ros: Option<String> = title
.as_ref()
.map(|title| RawCamName::new(title.clone()).to_ros().as_str().to_string());

let full_path = std::path::PathBuf::from(&s.filename);
let filename = full_path
.file_name()
Expand All @@ -463,27 +472,17 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
let stem = filename.as_str().split('.').next().unwrap();
// example: stem = "movie20211108_084523_Basler-22445994"

let filename_as_ros = movie_re.captures(stem).map(|caps| {
let cam_from_filename = movie_re.captures(stem).map(|caps| {
// get the raw camera name
let raw = caps.get(1).unwrap().as_str();
// example: raw = "Basler-22445994"

let ros = RawCamName::new(raw.to_string())
.to_ros()
.as_str()
.to_string();
// example: ros = "Basler_22445994"

ros
caps.get(1).unwrap().as_str().to_string()
});

let cam_id = CameraIdentifier::MovieOnly(MovieCamId {
_full_path: full_path,
filename,
cfg_name: s.camera_name.clone(),
title,
title_as_ros,
filename_as_ros,
cam_from_filename,
frame0_time,
reader,
});
Expand Down Expand Up @@ -522,7 +521,7 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:

let cam_id = match cam_id {
CameraIdentifier::MovieOnly(m) => {
if Some(braidz_cam_id.cam_id_str.clone()) == m.ros_name() {
if Some(braidz_cam_id.cam_id_str.clone()) == m.raw_name() {
CameraIdentifier::Both((m, braidz_cam_id.clone()))
} else {
CameraIdentifier::MovieOnly(m)
Expand Down Expand Up @@ -575,11 +574,11 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
}
}

let ros_camera_names: Vec<String> = sources
let camera_names: Vec<String> = sources
.iter()
.map(|s| match &s.cam_id {
CameraIdentifier::MovieOnly(m) | CameraIdentifier::Both((m, _)) => {
m.ros_name().unwrap()
m.raw_name().unwrap()
}
CameraIdentifier::BraidzOnly(b) => b.cam_id_str.clone(),
})
Expand Down Expand Up @@ -664,13 +663,12 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
// In this path, we use the .braidz file as the source of
// synchronization.

let ros_camera_names_ref: Vec<&str> =
ros_camera_names.iter().map(|x| x.as_str()).collect();
let camera_names_ref: Vec<&str> = camera_names.iter().map(|x| x.as_str()).collect();

Box::new(braidz_iter::BraidArchiveSyncVideoData::new(
archive,
&data2d,
&ros_camera_names_ref,
&camera_names_ref,
frame_readers,
sync_threshold,
)?)
Expand All @@ -691,7 +689,7 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
}
};

let all_expected_cameras = ros_camera_names
let all_expected_cameras = camera_names
.iter()
.map(|x| RawCamName::new(x.clone()))
.collect::<std::collections::BTreeSet<_>>();
Expand Down
2 changes: 1 addition & 1 deletion strand-cam-pseudo-cal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl PseudoCameraCalibrationData {
let cam_name = self.cam_name.clone();
let cam = self.to_cam()?;
let mut cams_by_name = std::collections::BTreeMap::new();
cams_by_name.insert(cam_name.to_ros().as_str().to_string(), cam);
cams_by_name.insert(cam_name.as_str().to_string(), cam);

let data = serde_json::json!({
"pseudo_camera_calibration": 1,
Expand Down
3 changes: 1 addition & 2 deletions strand-cam/src/strand-cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4536,13 +4536,12 @@ where
.collect()
};

let ros_cam_name = cam_name2.to_ros();
let local: chrono::DateTime<chrono::Local> = chrono::Local::now();

if let Some(debug_dir) = &checkerboard_save_debug {
let format_str = format!(
"checkerboard_input_{}.%Y%m%d_%H%M%S.yaml",
ros_cam_name.as_str()
cam_name2.as_str()
);
let stamped = local.format(&format_str).to_string();

Expand Down

0 comments on commit a92028f

Please sign in to comment.