added integration test
moved test image files to res folder fixed bugs in image loader
|
@ -2,4 +2,5 @@
|
|||
/Cargo.lock
|
||||
.DS_Store
|
||||
.idea
|
||||
/.vscode
|
||||
/.vscode
|
||||
*.json
|
Before Width: | Height: | Size: 258 B After Width: | Height: | Size: 258 B |
Before Width: | Height: | Size: 1.6 MiB After Width: | Height: | Size: 1.6 MiB |
Before Width: | Height: | Size: 539 B After Width: | Height: | Size: 539 B |
Before Width: | Height: | Size: 540 B After Width: | Height: | Size: 540 B |
After Width: | Height: | Size: 258 B |
After Width: | Height: | Size: 1.6 MiB |
Before Width: | Height: | Size: 711 B After Width: | Height: | Size: 711 B |
After Width: | Height: | Size: 539 B |
After Width: | Height: | Size: 540 B |
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 1.2 MiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
|
@ -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(
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
use std::{path::Path, sync::Arc};
|
||||
|
||||
use imsearch::{
|
||||
image::Image,
|
||||
search_index::{Database, FeatureGenerator, FeatureResult},
|
||||
};
|
||||
|
||||
fn average_brightness(image: Arc<Image<f32>>) -> (String, FeatureResult) {
|
||||
let bright = image
|
||||
.pixels()
|
||||
.iter()
|
||||
.map(|(r, g, b, _)| (r + g + b) / 3.0 / 255.0)
|
||||
.sum::<f32>()
|
||||
/ image.pixels().len() as f32;
|
||||
|
||||
(
|
||||
String::from("average_brightness"),
|
||||
FeatureResult::Percent(bright),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_database_basic() {
|
||||
let files: Vec<std::path::PathBuf> = std::fs::read_dir("res/integration/")
|
||||
.unwrap()
|
||||
.map(|f| f.unwrap().path())
|
||||
.collect();
|
||||
|
||||
let feats: Vec<FeatureGenerator> = 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::path::PathBuf> = std::fs::read_dir("res/integration/")
|
||||
.unwrap()
|
||||
.map(|f| f.unwrap().path())
|
||||
.collect();
|
||||
|
||||
let feats: Vec<FeatureGenerator> = 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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|