diff --git a/src/bin/experiment.rs b/src/bin/experiment.rs new file mode 100644 index 0000000..a1ffec5 --- /dev/null +++ b/src/bin/experiment.rs @@ -0,0 +1,35 @@ +use image_similarity::descriptors::{Descriptor, Median, DCT}; +use std::collections::HashMap; +use std::fs; + +fn main() { + //Init all descriptors: + let desc = Median; + + //Initialize hashmap, empty or from cached file: + let count = fs::read_dir("img").unwrap().count(); + let map_file = fs::read("map.messagepack"); + let mut map: HashMap = match map_file { + Ok(f) => rmp_serde::from_slice(&f).unwrap(), + Err(_e) => HashMap::with_capacity(count), + }; + + //Calculate phashes for all images + for node in fs::read_dir("img").unwrap() { + let file = node.expect("Error walking directory"); + let name = file.file_name().into_string().expect("Issue with filename"); + if !map.contains_key(&name) { + let img = image::open(file.path()).expect("Unable to open file"); + let phash = desc.describe(img); + map.insert(name, phash); + } + } + + for (key, val) in map.iter() { + println!("{key: >15}: {val}\t {val:b}"); + } + + //Serialize hashmap with MessagePack: + let serialized: Vec = rmp_serde::to_vec(&map).unwrap(); + //fs::write("map.messagepack",&serialized).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e27c649 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod descriptors; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b8883fb..f8ed123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,3 @@ -use descriptors::{Descriptor, Median, DCT}; -use std::collections::HashMap; -use std::fs; - -pub mod descriptors; - fn main() { - //Init all descriptors: - let desc = Median; - - //Initialize hashmap, empty or from cached file: - let count = fs::read_dir("img").unwrap().count(); - let map_file = fs::read("map.messagepack"); - let mut map: HashMap = match map_file { - Ok(f) => rmp_serde::from_slice(&f).unwrap(), - Err(_e) => HashMap::with_capacity(count), - }; - - //Calculate phashes for all images - for node in fs::read_dir("img").unwrap() { - let file = node.expect("Error walking directory"); - let name = file.file_name().into_string().expect("Issue with filename"); - if !map.contains_key(&name) { - let img = image::open(file.path()).expect("Unable to open file"); - let phash = desc.describe(img); - map.insert(name, phash); - } - } - - for (key, val) in map.iter() { - println!("{key: >15}: {val}\t {val:b}"); - } - - //Serialize hashmap with MessagePack: - let serialized: Vec = rmp_serde::to_vec(&map).unwrap(); - //fs::write("map.messagepack",&serialized).unwrap(); + print!("Hello world!") }