hashmap and serialization
not sure about the messagepack format, but its fine for now.
This commit is contained in:
+29
-10
@@ -1,17 +1,36 @@
|
||||
use descriptors::{Descriptor, Median};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
pub mod descriptors;
|
||||
|
||||
fn main() {
|
||||
//Init all descriptors:
|
||||
let desc = Median;
|
||||
let img0 = image::open("img/meowl.jpg").unwrap();
|
||||
let img1 = image::open("img/nyameowl.jpg").unwrap();
|
||||
let img2 = image::open("img/armmeowl.jpg").unwrap();
|
||||
let img0_phash = desc.describe(img0);
|
||||
let img1_phash = desc.describe(img1);
|
||||
let img2_phash = desc.describe(img2);
|
||||
println!("{:#x}", img0_phash);
|
||||
println!("{:#x}", img1_phash);
|
||||
println!("{:#x}", img2_phash);
|
||||
println!("meowl and armmeowl are {} different", desc.distance(img0_phash, img2_phash));
|
||||
|
||||
//Pre-allocate hashmap to right size:
|
||||
let count = fs::read_dir("img").unwrap().count();
|
||||
let mut map: HashMap<String, u64> = 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 img = image::open(file.path()).expect("Unable to open file");
|
||||
let phash = desc.describe(img);
|
||||
let name = file.file_name().into_string().expect("Issue with filename");
|
||||
map.insert(name, phash);
|
||||
}
|
||||
|
||||
for (key, val) in map.iter() {
|
||||
println!("{key}: {val}");
|
||||
}
|
||||
|
||||
//Test (de)serialize to vector with MessagePack:
|
||||
let serialized: Vec<u8> = rmp_serde::to_vec(&map).unwrap();
|
||||
|
||||
let x: HashMap<String, u64> = rmp_serde::from_slice(&serialized).unwrap();
|
||||
|
||||
for (key, val) in x.iter() {
|
||||
println!("{key}: {val}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user