iii_formosa_dataset/
lib.rs

1//! Se/Deserialization toolkit for Formosa dataset from Institute for the Information Industry
2
3mod common;
4mod types;
5
6pub use crate::types::*;
7
8use crate::common::*;
9
10/// The sample corresponds to an image along with annotations
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct Sample {
13    pub annotation: Annotation,
14    pub annotation_file: PathBuf,
15    pub image_file: PathBuf,
16    pub roi_dir: PathBuf,
17    pub roi_files: Vec<PathBuf>,
18}
19
20/// Load dataset directory
21pub fn load(dataset_dir: impl AsRef<Path>) -> Result<Vec<Sample>> {
22    let dataset_dir = dataset_dir.as_ref();
23
24    let samples: Result<Vec<_>> = glob::glob(
25        dataset_dir
26            .join("**")
27            .join("*.xml")
28            .to_str()
29            .ok_or_else(|| format_err!("the path '{}' is not UTF-8", dataset_dir.display()))?,
30    )?
31    .map(|result| {
32        let annotation_file = result?;
33        let parent = annotation_file.parent().unwrap();
34        let name = annotation_file
35            .file_stem()
36            .unwrap()
37            .to_str()
38            .ok_or_else(|| format_err!("non-UTF-8 filename '{}'", annotation_file.display()))?;
39        let image_file = parent.join(format!("{}.jpg", name));
40        let roi_dir = parent.join(format!("{}_roi", name));
41
42        let annotation: Annotation = {
43            let content = fs::read_to_string(&annotation_file)
44                .with_context(|| format!("cannot open file '{}'", annotation_file.display()))?;
45            serde_xml_rs::from_str(&content)
46                .with_context(|| format!("failed to parse file '{}'", annotation_file.display()))?
47        };
48
49        let roi_files: Result<Vec<_>> = (1..=annotation.object.len())
50            .map(|index| {
51                let file_name = format!("{}.jpg", index);
52                let roi_file = roi_dir.join(&file_name);
53                ensure!(
54                    roi_file.is_file(),
55                    "the ROI file '{}' is missing",
56                    roi_file.display()
57                );
58                Ok(roi_file)
59            })
60            .collect();
61        let roi_files = roi_files?;
62
63        Ok(Sample {
64            annotation,
65            annotation_file,
66            image_file,
67            roi_dir,
68            roi_files,
69        })
70    })
71    .collect();
72    let samples = samples?;
73
74    Ok(samples)
75}