hashmap and serialization

not sure about the messagepack format, but its fine for now.
This commit is contained in:
2022-11-11 01:50:14 +01:00
parent b449855bab
commit cd97a70e0b
3 changed files with 67 additions and 10 deletions
Generated
+36
View File
@@ -235,6 +235,8 @@ name = "image-similarity"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"image", "image",
"rmp-serde",
"serde",
] ]
[[package]] [[package]]
@@ -368,6 +370,12 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "paste"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1"
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.0.12" version = "1.0.12"
@@ -442,6 +450,28 @@ dependencies = [
"num_cpus", "num_cpus",
] ]
[[package]]
name = "rmp"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44519172358fd6d58656c86ab8e7fbc9e1490c3e8f14d35ed78ca0dd07403c9f"
dependencies = [
"byteorder",
"num-traits",
"paste",
]
[[package]]
name = "rmp-serde"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5b13be192e0220b8afb7222aa5813cb62cc269ebb5cac346ca6487681d2913e"
dependencies = [
"byteorder",
"rmp",
"serde",
]
[[package]] [[package]]
name = "scoped_threadpool" name = "scoped_threadpool"
version = "0.1.9" version = "0.1.9"
@@ -454,6 +484,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965"
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.10.0" version = "1.10.0"
+2
View File
@@ -7,3 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
image = "0.24.4" image = "0.24.4"
serde = "1.0.147"
rmp-serde = "1.1.1"
+29 -10
View File
@@ -1,17 +1,36 @@
use descriptors::{Descriptor, Median}; use descriptors::{Descriptor, Median};
use std::collections::HashMap;
use std::fs;
pub mod descriptors; pub mod descriptors;
fn main() { fn main() {
//Init all descriptors:
let desc = Median; let desc = Median;
let img0 = image::open("img/meowl.jpg").unwrap();
let img1 = image::open("img/nyameowl.jpg").unwrap(); //Pre-allocate hashmap to right size:
let img2 = image::open("img/armmeowl.jpg").unwrap(); let count = fs::read_dir("img").unwrap().count();
let img0_phash = desc.describe(img0); let mut map: HashMap<String, u64> = HashMap::with_capacity(count);
let img1_phash = desc.describe(img1);
let img2_phash = desc.describe(img2); //Calculate phashes for all images
println!("{:#x}", img0_phash); for node in fs::read_dir("img").unwrap() {
println!("{:#x}", img1_phash); let file = node.expect("Error walking directory");
println!("{:#x}", img2_phash); let img = image::open(file.path()).expect("Unable to open file");
println!("meowl and armmeowl are {} different", desc.distance(img0_phash, img2_phash)); 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}");
}
} }