added integration test

moved test image files to res folder
fixed bugs in image loader
This commit is contained in:
Sven Vogel 2023-06-17 23:27:07 +02:00
parent b498aee8a7
commit c3405c42cc
17 changed files with 91 additions and 3 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
/Cargo.lock
.DS_Store
.idea
/.vscode
/.vscode
*.json

View File

Before

Width:  |  Height:  |  Size: 258 B

After

Width:  |  Height:  |  Size: 258 B

View File

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

Before

Width:  |  Height:  |  Size: 539 B

After

Width:  |  Height:  |  Size: 539 B

View File

Before

Width:  |  Height:  |  Size: 540 B

After

Width:  |  Height:  |  Size: 540 B

BIN
res/test_img/gray_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 B

BIN
res/test_img/hut.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

View File

Before

Width:  |  Height:  |  Size: 711 B

After

Width:  |  Height:  |  Size: 711 B

BIN
res/test_img/red_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 539 B

BIN
res/test_img/rot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 B

View File

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View File

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

View File

@ -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(

View File

@ -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

82
tests/integration_test.rs Normal file
View File

@ -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
);
}
}
}