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
use akaze::types::feature_match::Match;
use akaze::types::keypoint::{Descriptor, Keypoint};
use failure::Error;
use log::*;
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::fs::File;
use std::path::Path;

#[derive(Serialize, Deserialize)]
pub struct Features {
    pub keypoints: Vec<Keypoint>,
    pub descriptors: Vec<Descriptor>,
}

/// Serialize features to a file.
pub fn serialize_features_to_file(
    features: &Features,
    path: impl AsRef<Path>,
) -> Result<(), Error> {
    let path = path.as_ref();
    debug!("Writing features to {:?}", path);
    let file = File::create(path)?;
    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
    match extension {
        "json" => serde_json::to_writer(file, features)?,
        _ => bincode::serialize_into(file, features)?,
    }
    Ok(())
}

/// Serialize features to a file.
pub fn deserialize_features_from_file(path: impl AsRef<Path>) -> Result<Features, Error> {
    let path = path.as_ref();
    debug!("Reading features from {:?}", path);
    let file = File::open(path)?;
    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
    Ok(match extension {
        "json" => serde_json::from_reader(file)?,
        _ => bincode::deserialize_from(file)?,
    })
}

/// Serialize matches to a file.
pub fn serialize_matches_to_file(matches: &[Match], path: impl AsRef<Path>) -> Result<(), Error> {
    let path = path.as_ref();
    debug!("Writing matches to {:?}", path);
    let file = File::create(path)?;
    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
    match extension {
        "json" => serde_json::to_writer(file, matches)?,
        _ => bincode::serialize_into(file, matches)?,
    }
    Ok(())
}

/// Serialize matches to a file.
pub fn deserialize_matches_from_file(path: impl AsRef<Path>) -> Result<Vec<Match>, Error> {
    let path = path.as_ref();
    debug!("Reading matches to {:?}", path);
    let file = File::open(path)?;
    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
    Ok(match extension {
        "json" => serde_json::from_reader(file)?,
        _ => bincode::deserialize_from(file)?,
    })
}