make descriptor member of store
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-05-26 12:29:25 +02:00
parent d3b551f6ed
commit 4e6d9e09ba
3 changed files with 32 additions and 30 deletions
+3 -3
View File
@@ -30,10 +30,10 @@ fn main() {
// ..Default::default() // ..Default::default()
// }; // };
// viuer::print(&img, &conf).expect("Image printing failed."); // viuer::print(&img, &conf).expect("Image printing failed.");
let store =
DescriptorStore::new()
.with_file("dct50.messagepack");
let phash: u64 = desc.describe(&img); let phash: u64 = desc.describe(&img);
let store =
DescriptorStore::new(desc)
.with_file("dct50.messagepack");
info!("Phash integer:\n{phash}"); info!("Phash integer:\n{phash}");
info!("Phash binary:\n{phash:064b}"); info!("Phash binary:\n{phash:064b}");
store.print_nn(phash, cfg.distance, cfg.show_images); store.print_nn(phash, cfg.distance, cfg.show_images);
+3 -3
View File
@@ -12,12 +12,12 @@ struct Cfg {
path: PathBuf path: PathBuf
} }
fn make_store<T: Descriptor>(descriptor: T, input_path: PathBuf, save_path: PathBuf) -> DescriptorStore { fn make_store<T: Descriptor>(descriptor: T, input_path: PathBuf, save_path: PathBuf) -> DescriptorStore<T> {
let mut store = let mut store =
DescriptorStore::new() DescriptorStore::new(descriptor)
.with_file(save_path); .with_file(save_path);
info!("{}", store); info!("{}", store);
store.insert_directory(input_path, descriptor); store.insert_directory(input_path);
info!("{}", store); info!("{}", store);
store.save().expect("Error saving"); store.save().expect("Error saving");
store store
+8 -6
View File
@@ -17,7 +17,8 @@ pub enum SaveError {
/// Uses a hashmap to map descriptors to buckets of files. /// Uses a hashmap to map descriptors to buckets of files.
/// Also keeps a BK-tree for quick distance ranking /// Also keeps a BK-tree for quick distance ranking
pub struct DescriptorStore { pub struct DescriptorStore<T: Descriptor> {
descriptor: T,
/// Main hashmap that maps descriptors to buckets of filenames /// Main hashmap that maps descriptors to buckets of filenames
map: HashMap<u64, Vec<String>>, map: HashMap<u64, Vec<String>>,
@@ -31,14 +32,15 @@ pub struct DescriptorStore {
seen: Option<HashSet<String>>, seen: Option<HashSet<String>>,
} }
impl DescriptorStore { impl<T: Descriptor> DescriptorStore<T> {
/// Makes a new empty DescriptorStore with default settings /// Makes a new empty DescriptorStore with default settings
pub fn new() -> Self { pub fn new(descriptor: T) -> Self {
let map: HashMap<u64, Vec<String>> = HashMap::new(); let map: HashMap<u64, Vec<String>> = HashMap::new();
//let seen: HashSet<String> = HashSet::new(); //let seen: HashSet<String> = HashSet::new();
//let seen = None; //let seen = None;
DescriptorStore { DescriptorStore {
map, map,
descriptor,
save_location: std::path::PathBuf::from("store.messagepack"), save_location: std::path::PathBuf::from("store.messagepack"),
bktree: BkTree::new(hamming_distance), bktree: BkTree::new(hamming_distance),
seen: None, seen: None,
@@ -119,7 +121,7 @@ impl DescriptorStore {
} }
/// Calculates all descriptions with a given descriptor /// Calculates all descriptions with a given descriptor
pub fn insert_directory<T: Descriptor, U: AsRef<Path>>(&mut self, dir: U, desc: T) { pub fn insert_directory<U: AsRef<Path>>(&mut self, dir: U) {
for node in fs::read_dir(dir).unwrap() { for node in fs::read_dir(dir).unwrap() {
let file = node.expect("Error walking directory"); let file = node.expect("Error walking directory");
let name = match file.file_name().into_string() { let name = match file.file_name().into_string() {
@@ -138,7 +140,7 @@ impl DescriptorStore {
continue continue
} }
}; };
let phash = desc.describe(&img); let phash = self.descriptor.describe(&img);
if self.contains(phash) { if self.contains(phash) {
println!("{} duplicate of {:?}", name, self.map.get(&phash)); println!("{} duplicate of {:?}", name, self.map.get(&phash));
} }
@@ -222,7 +224,7 @@ impl DescriptorStore {
} }
} }
impl fmt::Display for DescriptorStore { impl<T: Descriptor> fmt::Display for DescriptorStore<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut output = String::new(); let mut output = String::new();
for (key, bucket) in self.map.iter() { for (key, bucket) in self.map.iter() {