1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
mod common;
pub mod types;
pub use crate::types::*;
use crate::common::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Sample {
pub image_path: PathBuf,
pub annotation: Annotation,
}
pub fn load<P: AsRef<Path>>(dataset_dir: P) -> Result<Vec<Sample>> {
let dataset_dir_r = dataset_dir.as_ref();
let image_dir = dataset_dir_r.join("JPEGImages");
let annotations_dir = dataset_dir_r.join("Annotations");
let samples = image_dir
.read_dir()?
.map(|entry_result| -> Result<Option<_>> {
let entry = entry_result?;
if entry.file_type()?.is_file() {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "jpg" {
let name = path.file_stem().unwrap();
return Ok(Some((name.to_owned(), path.to_owned())));
}
}
}
Ok(None)
})
.filter_map(|arg| match arg {
Ok(None) => None,
Ok(Some(pair)) => Some(Ok(pair)),
Err(err) => Some(Err(err)),
})
.map(|arg| {
let (image_name, image_path) = arg?;
let mut xml_path = annotations_dir.join(&image_name);
ensure!(xml_path.set_extension("xml"), "set_extension() failed");
info!("Loading {}", xml_path.display());
let content = fs::read_to_string(&xml_path)
.with_context(|| format!("cannot open file {}", xml_path.display()))?;
let annotation: Annotation = serde_xml_rs::from_str(&content)
.with_context(|| format!("failed to parse file {}", xml_path.display()))?;
let mut file_name = image_name.clone();
file_name.push(".jpg");
ensure!(
file_name == annotation.filename.as_str(),
"Expect \"{}\" in <filename>, but get \"{}\"",
file_name.to_str().unwrap(),
annotation.filename,
);
let sample = Sample {
image_path,
annotation,
};
Ok(sample)
})
.collect::<Result<Vec<Sample>, _>>()?;
Ok(samples)
}