This commit is contained in:
+30
-71
@@ -34,8 +34,7 @@ impl PRStats {
|
||||
};
|
||||
let tpos = self.true_positives[t] as f64;
|
||||
let fpos = self.false_positives[t] as f64;
|
||||
let p = tpos / (tpos + fpos);
|
||||
p
|
||||
tpos / (tpos + fpos)
|
||||
}
|
||||
pub fn recall(&self, threshold: usize) -> f64 {
|
||||
let t = match threshold {
|
||||
@@ -44,8 +43,7 @@ impl PRStats {
|
||||
};
|
||||
let tpos = self.true_positives[t] as f64;
|
||||
let fneg = self.false_negatives[t] as f64;
|
||||
let r = tpos / (tpos + fneg);
|
||||
r
|
||||
tpos / (tpos + fneg)
|
||||
}
|
||||
pub fn pr(&self, threshold: usize) -> (f64, f64) {
|
||||
let t = match threshold {
|
||||
@@ -87,7 +85,7 @@ pub struct DescriptorStore {
|
||||
}
|
||||
|
||||
impl DescriptorStore {
|
||||
/// Makes a new empty DescriptorStore with default settings
|
||||
/// Makes a new empty `DescriptorStore` with default settings
|
||||
pub fn new(descriptor: Box<dyn Descriptor>) -> Self {
|
||||
let map: HashMap<u64, Vec<String>> = HashMap::new();
|
||||
//let seen: HashSet<String> = HashSet::new();
|
||||
@@ -105,23 +103,22 @@ impl DescriptorStore {
|
||||
pub fn with_file<P: AsRef<Path>>(mut self, path: P) -> Self {
|
||||
self.save_location = std::path::PathBuf::from(path.as_ref());
|
||||
let map_file = fs::read(&self.save_location);
|
||||
match map_file {
|
||||
Ok(f) => {
|
||||
self.map = rmp_serde::from_slice(&f).unwrap();
|
||||
for i in self.map.keys() {
|
||||
self.bktree.insert(*i);
|
||||
}
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
for bucket in self.map.values() {
|
||||
for element in bucket {
|
||||
if !element.starts_with("mut.") {
|
||||
seen.insert(element.clone());
|
||||
}
|
||||
if let Ok(f) = map_file {
|
||||
self.map = rmp_serde::from_slice(&f).unwrap();
|
||||
for i in self.map.keys() {
|
||||
self.bktree.insert(*i);
|
||||
}
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
for bucket in self.map.values() {
|
||||
for element in bucket {
|
||||
if !element.starts_with("mut.") {
|
||||
seen.insert(element.clone());
|
||||
}
|
||||
}
|
||||
self.seen = Some(seen);
|
||||
},
|
||||
Err(_) => info!("{} not found, starting from empty store.", self.save_location.display()),
|
||||
}
|
||||
self.seen = Some(seen);
|
||||
} else {
|
||||
info!("{} not found, starting from empty store.", self.save_location.display());
|
||||
};
|
||||
self
|
||||
}
|
||||
@@ -147,8 +144,8 @@ impl DescriptorStore {
|
||||
Ok(value) => value,
|
||||
Err(_e) => return Err(SaveError::Serialization),
|
||||
};
|
||||
match fs::write(&self.save_location, &serialized) {
|
||||
Ok(_) => Ok(()),
|
||||
match fs::write(&self.save_location, serialized) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(_e) => Err(SaveError::File),
|
||||
}
|
||||
}
|
||||
@@ -167,7 +164,7 @@ impl DescriptorStore {
|
||||
},
|
||||
None => {
|
||||
for bucket in self.map.values() {
|
||||
if bucket.contains(&value) {
|
||||
if bucket.contains(value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -178,7 +175,7 @@ impl DescriptorStore {
|
||||
|
||||
pub fn get(&self, value: &String) -> Option<u64> {
|
||||
for (key, bucket) in self.map.iter() {
|
||||
if bucket.contains(&value) {
|
||||
if bucket.contains(value) {
|
||||
return Some(*key);
|
||||
}
|
||||
}
|
||||
@@ -187,7 +184,7 @@ impl DescriptorStore {
|
||||
|
||||
/// Inserts a single value into the store
|
||||
pub fn insert(&mut self, key: &u64, value: String) {
|
||||
let bucket = match self.map.get(&key) {
|
||||
let bucket = match self.map.get(key) {
|
||||
Some(b) => {
|
||||
let mut n = b.clone();
|
||||
if !n.contains(&value) {
|
||||
@@ -213,7 +210,9 @@ impl DescriptorStore {
|
||||
continue
|
||||
}
|
||||
};
|
||||
if !self.has_value(&name) { // !self.contains(name.to_string()) {
|
||||
if !self.has_value(&name) {
|
||||
debug!("{} already known, skipping.", name);
|
||||
} else {
|
||||
info!("Processing {}", name);
|
||||
let img = match image::open(file.path()) {
|
||||
Ok(v) => v,
|
||||
@@ -227,8 +226,6 @@ impl DescriptorStore {
|
||||
println!("{} duplicate of {:?}", name, self.map.get(&phash));
|
||||
}
|
||||
self.insert(&phash, name);
|
||||
} else {
|
||||
debug!("{} already known, skipping.", name);
|
||||
}
|
||||
self.save().expect("error");
|
||||
}
|
||||
@@ -238,15 +235,15 @@ impl DescriptorStore {
|
||||
|
||||
pub fn store(&mut self, img: &DynamicImage, name: String) {
|
||||
if !self.has_value(&name) {
|
||||
let phash = self.descriptor.describe(&img);
|
||||
let phash = self.descriptor.describe(img);
|
||||
self.insert(&phash, name);
|
||||
}
|
||||
}
|
||||
|
||||
/// Nearest neighbours
|
||||
pub fn nn(&self, from: u64, max_distance: usize) -> Vec<(&u64, isize)> {
|
||||
let mut neighbours = self.bktree.find(from, max_distance.try_into().unwrap());
|
||||
neighbours.sort_by(|a, b| a.1.cmp(&b.1));
|
||||
let neighbours = self.bktree.find(from, max_distance.try_into().unwrap());
|
||||
//neighbours.sort_by(|a, b| a.1.cmp(&b.1));
|
||||
neighbours
|
||||
}
|
||||
|
||||
@@ -263,44 +260,6 @@ impl DescriptorStore {
|
||||
results
|
||||
}
|
||||
|
||||
pub fn print_nn(&self, from: u64, max_distance: usize, show_images: bool) {
|
||||
//println!("{from:064b}");
|
||||
let neighbours = self.nn(from, max_distance);
|
||||
let (term_width, _) = viuer::terminal_size();
|
||||
let mut x = 0;
|
||||
let mut y = 8;
|
||||
for (element, distance) in neighbours {
|
||||
let elements = self.map.get(element);
|
||||
match elements {
|
||||
Some(paths) => {
|
||||
for path in paths {
|
||||
println!("{element:064b}: {:?} (distance: {distance})", path);
|
||||
if show_images {
|
||||
if x+16 >= term_width {
|
||||
x = 0;
|
||||
y += 8;
|
||||
}
|
||||
let conf = viuer::Config {
|
||||
width: Some(16),
|
||||
height: Some(8),
|
||||
x,
|
||||
y,
|
||||
use_kitty: false,
|
||||
..Default::default()
|
||||
};
|
||||
x += 16;
|
||||
let path = "data/".to_string() + path;
|
||||
let img = image::open(&path).unwrap();
|
||||
let img = img.grayscale().thumbnail_exact(8, 8);
|
||||
viuer::print(&img, &conf).expect("Image printing failed.");
|
||||
}
|
||||
}
|
||||
},
|
||||
None => ()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_most_dups(&self) {
|
||||
let mut most = 0;
|
||||
for bucket in self.map.values() {
|
||||
@@ -319,7 +278,7 @@ impl DescriptorStore {
|
||||
let value_entry = format!("{value}\n");
|
||||
output.push_str(&value_entry);
|
||||
}
|
||||
output.push_str("\n");
|
||||
output.push('\n');
|
||||
}
|
||||
}
|
||||
print!("DescriptorStore:\n{}Total: {}", output, self.map.len())
|
||||
@@ -415,7 +374,7 @@ impl fmt::Display for DescriptorStore {
|
||||
let value_entry = format!("\t{value}\n");
|
||||
output.push_str(&value_entry);
|
||||
}
|
||||
output.push_str("\n");
|
||||
output.push('\n');
|
||||
}
|
||||
write!(f, "DescriptorStore:\n{}Total: {}", output, self.map.len())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user