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
+21 -21
View File
@@ -7,33 +7,33 @@ use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cfg {
/// Path to the image
path: String,
/// Try to output the images to terminal with viuer
#[arg(short, long, default_value_t=false)]
show_images: bool,
distance: isize,
/// Path to the image
path: String,
/// Try to output the images to terminal with viuer
#[arg(short, long, default_value_t=false)]
show_images: bool,
distance: isize,
}
fn main() {
env_logger::init();
env_logger::init();
let desc = DCT::new().with_quality(50);
let desc = DCT::new().with_quality(50);
let cfg = Cfg::parse();
let cfg = Cfg::parse();
let img = image::open(cfg.path)
.expect("Unable to open file");
// let conf = viuer::Config {
// width: Some(16),
// height: Some(8),
// ..Default::default()
// };
// viuer::print(&img, &conf).expect("Image printing failed.");
let store =
DescriptorStore::new()
.with_file("dct50.messagepack");
let phash: u64 = desc.describe(&img);
let img = image::open(cfg.path)
.expect("Unable to open file");
// let conf = viuer::Config {
// width: Some(16),
// height: Some(8),
// ..Default::default()
// };
// viuer::print(&img, &conf).expect("Image printing failed.");
let phash: u64 = desc.describe(&img);
let store =
DescriptorStore::new(desc)
.with_file("dct50.messagepack");
info!("Phash integer:\n{phash}");
info!("Phash binary:\n{phash:064b}");
store.print_nn(phash, cfg.distance, cfg.show_images);
+3 -3
View File
@@ -12,12 +12,12 @@ struct Cfg {
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 =
DescriptorStore::new()
DescriptorStore::new(descriptor)
.with_file(save_path);
info!("{}", store);
store.insert_directory(input_path, descriptor);
store.insert_directory(input_path);
info!("{}", store);
store.save().expect("Error saving");
store
+8 -6
View File
@@ -17,7 +17,8 @@ pub enum SaveError {
/// Uses a hashmap to map descriptors to buckets of files.
/// 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
map: HashMap<u64, Vec<String>>,
@@ -31,14 +32,15 @@ pub struct DescriptorStore {
seen: Option<HashSet<String>>,
}
impl DescriptorStore {
impl<T: Descriptor> DescriptorStore<T> {
/// 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 seen: HashSet<String> = HashSet::new();
//let seen = None;
DescriptorStore {
map,
descriptor,
save_location: std::path::PathBuf::from("store.messagepack"),
bktree: BkTree::new(hamming_distance),
seen: None,
@@ -119,7 +121,7 @@ impl DescriptorStore {
}
/// 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() {
let file = node.expect("Error walking directory");
let name = match file.file_name().into_string() {
@@ -138,7 +140,7 @@ impl DescriptorStore {
continue
}
};
let phash = desc.describe(&img);
let phash = self.descriptor.describe(&img);
if self.contains(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 {
let mut output = String::new();
for (key, bucket) in self.map.iter() {