akaze_util/
lib.rs

1use akaze::types::feature_match::Match;
2use akaze::types::keypoint::{Descriptor, Keypoint};
3use failure::Error;
4use log::*;
5use serde::{Deserialize, Serialize};
6use std::ffi::OsStr;
7use std::fs::File;
8use std::path::Path;
9
10#[derive(Serialize, Deserialize)]
11pub struct Features {
12    pub keypoints: Vec<Keypoint>,
13    pub descriptors: Vec<Descriptor>,
14}
15
16/// Serialize features to a file.
17pub fn serialize_features_to_file(
18    features: &Features,
19    path: impl AsRef<Path>,
20) -> Result<(), Error> {
21    let path = path.as_ref();
22    debug!("Writing features to {:?}", path);
23    let file = File::create(path)?;
24    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
25    match extension {
26        "json" => serde_json::to_writer(file, features)?,
27        _ => bincode::serialize_into(file, features)?,
28    }
29    Ok(())
30}
31
32/// Serialize features to a file.
33pub fn deserialize_features_from_file(path: impl AsRef<Path>) -> Result<Features, Error> {
34    let path = path.as_ref();
35    debug!("Reading features from {:?}", path);
36    let file = File::open(path)?;
37    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
38    Ok(match extension {
39        "json" => serde_json::from_reader(file)?,
40        _ => bincode::deserialize_from(file)?,
41    })
42}
43
44/// Serialize matches to a file.
45pub fn serialize_matches_to_file(matches: &[Match], path: impl AsRef<Path>) -> Result<(), Error> {
46    let path = path.as_ref();
47    debug!("Writing matches to {:?}", path);
48    let file = File::create(path)?;
49    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
50    match extension {
51        "json" => serde_json::to_writer(file, matches)?,
52        _ => bincode::serialize_into(file, matches)?,
53    }
54    Ok(())
55}
56
57/// Serialize matches to a file.
58pub fn deserialize_matches_from_file(path: impl AsRef<Path>) -> Result<Vec<Match>, Error> {
59    let path = path.as_ref();
60    debug!("Reading matches to {:?}", path);
61    let file = File::open(path)?;
62    let extension = path.extension().and_then(OsStr::to_str).unwrap_or("bin");
63    Ok(match extension {
64        "json" => serde_json::from_reader(file)?,
65        _ => bincode::deserialize_from(file)?,
66    })
67}