diff --git a/.gitignore b/.gitignore index 7cd589f..a29fb96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /Cargo.lock .DS_Store .idea -/.vscode \ No newline at end of file +/.vscode +*.json \ No newline at end of file diff --git a/test_img/gray_image.png b/res/integration/gray_image.png similarity index 100% rename from test_img/gray_image.png rename to res/integration/gray_image.png diff --git a/test_img/hut.png b/res/integration/hut.png similarity index 100% rename from test_img/hut.png rename to res/integration/hut.png diff --git a/test_img/red_image.png b/res/integration/red_image.png similarity index 100% rename from test_img/red_image.png rename to res/integration/red_image.png diff --git a/test_img/rot.png b/res/integration/rot.png similarity index 100% rename from test_img/rot.png rename to res/integration/rot.png diff --git a/res/test_img/gray_image.png b/res/test_img/gray_image.png new file mode 100644 index 0000000..fb26f09 Binary files /dev/null and b/res/test_img/gray_image.png differ diff --git a/res/test_img/hut.png b/res/test_img/hut.png new file mode 100644 index 0000000..61ce192 Binary files /dev/null and b/res/test_img/hut.png differ diff --git a/test_img/indexed_image.png b/res/test_img/indexed_image.png similarity index 100% rename from test_img/indexed_image.png rename to res/test_img/indexed_image.png diff --git a/res/test_img/red_image.png b/res/test_img/red_image.png new file mode 100644 index 0000000..896198b Binary files /dev/null and b/res/test_img/red_image.png differ diff --git a/res/test_img/rot.png b/res/test_img/rot.png new file mode 100644 index 0000000..a40594e Binary files /dev/null and b/res/test_img/rot.png differ diff --git a/test_img/town_blue.png b/res/test_img/town_blue.png similarity index 100% rename from test_img/town_blue.png rename to res/test_img/town_blue.png diff --git a/test_img/wrong colorspace_bitdepth.png b/res/test_img/wrong colorspace_bitdepth.png similarity index 100% rename from test_img/wrong colorspace_bitdepth.png rename to res/test_img/wrong colorspace_bitdepth.png diff --git a/test_img/wrong pixel count.png b/res/test_img/wrong pixel count.png similarity index 100% rename from test_img/wrong pixel count.png rename to res/test_img/wrong pixel count.png diff --git a/test_img/wrong size.png b/res/test_img/wrong size.png similarity index 100% rename from test_img/wrong size.png rename to res/test_img/wrong size.png diff --git a/src/image_loader/mod.rs b/src/image_loader/mod.rs index 829d589..1aaced6 100644 --- a/src/image_loader/mod.rs +++ b/src/image_loader/mod.rs @@ -267,7 +267,7 @@ mod test { #[test] fn test_image_loader() { - let path = Path::new("test_img/red_image.png"); + let path = Path::new("res/test_img/red_image.png"); let test = image_loader(path); let image = Image::new( @@ -300,7 +300,7 @@ mod test { #[test] #[should_panic] fn test_wrong_img() { - let path = Path::new("test_img/wrong pixel count.png"); + let path = Path::new("res/test_img/wrong pixel count.png"); let test = image_loader(path); let image = Image::new( diff --git a/src/search_index/mod.rs b/src/search_index/mod.rs index e9f0586..bc549d5 100644 --- a/src/search_index/mod.rs +++ b/src/search_index/mod.rs @@ -313,6 +313,11 @@ impl Database { threadpool: ThreadPool::new(), } } + + pub fn write_to_file(&self, path: &Path) { + let filestring = serde_json::to_string(&self.images).expect("unable to serialize the file"); + fs::write(path, filestring).expect("unable to write the file"); + } } ///IndexedImages stores the images of the Database and is serializable diff --git a/tests/integration_test.rs b/tests/integration_test.rs new file mode 100644 index 0000000..4e348e7 --- /dev/null +++ b/tests/integration_test.rs @@ -0,0 +1,82 @@ +use std::{path::Path, sync::Arc}; + +use imsearch::{ + image::Image, + search_index::{Database, FeatureGenerator, FeatureResult}, +}; + +fn average_brightness(image: Arc>) -> (String, FeatureResult) { + let bright = image + .pixels() + .iter() + .map(|(r, g, b, _)| (r + g + b) / 3.0 / 255.0) + .sum::() + / image.pixels().len() as f32; + + ( + String::from("average_brightness"), + FeatureResult::Percent(bright), + ) +} + +#[test] +fn test_database_basic() { + let files: Vec = std::fs::read_dir("res/integration/") + .unwrap() + .map(|f| f.unwrap().path()) + .collect(); + + let feats: Vec = vec![average_brightness]; + + let db = Database::new(&files, feats).unwrap(); + + for results in db + .search( + std::path::Path::new("res/integration/gray_image.png"), + average_brightness, + ) + .unwrap() + { + println!( + "path: {} similarity: {}", + results.0.as_os_str().to_str().unwrap(), + results.1 + ); + } +} + +#[test] +fn test_database_files() { + let json = Path::new("db.json"); + + { + let files: Vec = std::fs::read_dir("res/integration/") + .unwrap() + .map(|f| f.unwrap().path()) + .collect(); + + let feats: Vec = vec![average_brightness]; + + let db = Database::new(&files, feats).unwrap(); + + db.write_to_file(json); + } + + { + let db = Database::from_file(json); + + for results in db + .search( + std::path::Path::new("res/integration/gray_image.png"), + average_brightness, + ) + .unwrap() + { + println!( + "path: {} similarity: {}", + results.0.as_os_str().to_str().unwrap(), + results.1 + ); + } + } +}