-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add test for avatar image generation
move scenes snapshots to it's own folder
- Loading branch information
Showing
92 changed files
with
189 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,7 @@ mono_crash.*.json | |
# System/tool-specific ignores | ||
.directory | ||
.DS_Store | ||
*~ | ||
*~ | ||
|
||
# Decentraland ignores | ||
/output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use image::{GenericImageView, Pixel}; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
|
||
// Function to compare two images and calculate similarity | ||
pub fn compare_images_similarity(image_path_1: &Path, image_path_2: &Path) -> Result<f64, String> { | ||
let img1 = image::open(image_path_1).map_err(|_| format!("Failed to open image: {:?}", image_path_1))?; | ||
let img2 = image::open(image_path_2).map_err(|_| format!("Failed to open image: {:?}", image_path_2))?; | ||
|
||
if img1.dimensions() != img2.dimensions() { | ||
return Err("Images have different dimensions".to_string()); | ||
} | ||
|
||
let (width, height) = img1.dimensions(); | ||
let mut total_diff = 0.0; | ||
let mut total_pixels = 0.0; | ||
|
||
for y in 0..height { | ||
for x in 0..width { | ||
let pixel1 = img1.get_pixel(x, y); | ||
let pixel2 = img2.get_pixel(x, y); | ||
|
||
let diff = pixel1.channels().iter() | ||
.zip(pixel2.channels().iter()) | ||
.map(|(p1, p2)| (*p1 as f64 - *p2 as f64).powi(2)) | ||
.sum::<f64>(); | ||
|
||
total_diff += diff.sqrt(); | ||
total_pixels += 1.0; | ||
} | ||
} | ||
|
||
let max_diff_per_pixel = (255.0_f64 * 3.0_f64).sqrt(); | ||
let average_diff = total_diff / total_pixels; | ||
let similarity = 1.0 - (average_diff / max_diff_per_pixel); | ||
|
||
Ok(similarity) | ||
} | ||
|
||
// Function to list all PNG files in a directory | ||
fn list_png_files(directory: &Path) -> Result<Vec<PathBuf>, String> { | ||
let mut files = vec![]; | ||
|
||
for entry in fs::read_dir(directory).map_err(|_| format!("Failed to read directory: {:?}", directory))? { | ||
let entry = entry.map_err(|_| "Failed to access entry in directory".to_string())?; | ||
let path = entry.path(); | ||
if path.extension().and_then(|ext| ext.to_str()) == Some("png") { | ||
files.push(path); | ||
} | ||
} | ||
|
||
files.sort(); // Ensure files are in the same order | ||
Ok(files) | ||
} | ||
|
||
// Function to compare all PNG files in two folders | ||
pub fn compare_images_folders(snapshot_folder: &Path, result_folder: &Path, similarity_threshold: f64) -> Result<(), String> { | ||
let snapshot_files = list_png_files(snapshot_folder)?; | ||
let result_files = list_png_files(result_folder)?; | ||
|
||
// Ensure both folders have the same number of files | ||
if snapshot_files.len() != result_files.len() { | ||
return Err("Snapshot and result folders contain different numbers of files".to_string()); | ||
} | ||
|
||
// Compare each corresponding file | ||
for (snapshot_file, result_file) in snapshot_files.iter().zip(result_files.iter()) { | ||
let similarity = compare_images_similarity(snapshot_file, result_file)?; | ||
|
||
// If similarity is less than the `similarity_threshold`, the test fails | ||
if similarity < similarity_threshold { | ||
return Err(format!( | ||
"Files {:?} and {:?} are too different! Similarity: {:.5}%", | ||
snapshot_file, | ||
result_file, | ||
similarity * 100.0 | ||
)); | ||
} | ||
|
||
println!( | ||
"Files {:?} and {:?} are {:.5}% similar.", | ||
snapshot_file, | ||
result_file, | ||
similarity * 100.0 | ||
); | ||
} | ||
|
||
println!("All files match with 99.95% similarity or higher!"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::{collections::HashMap, path::Path}; | ||
|
||
use anyhow::Ok; | ||
|
||
use crate::{copy_files::move_dir_recursive, image_comparison::compare_images_folders, run}; | ||
|
||
pub fn test_godot_tools(with_build_envs: Option<HashMap<String, String>>) -> Result<(), anyhow::Error> { | ||
let avatar_snapshot_folder = Path::new("./tests/snapshots/avatar-image-generation").canonicalize()?; | ||
let comparison_folder = avatar_snapshot_folder.join("comparison"); | ||
|
||
println!("=== running godot avatar generation ==="); | ||
|
||
let extra_args = [ | ||
"--rendering-driver", | ||
"opengl3", | ||
"--avatar-renderer", | ||
"--use-test-input", | ||
] | ||
.iter() | ||
.map(|it| it.to_string()) | ||
.collect(); | ||
|
||
run::run( | ||
false, | ||
false, | ||
false, | ||
false, | ||
false, | ||
false, | ||
vec![], | ||
extra_args, | ||
with_build_envs, | ||
)?; | ||
|
||
// Move files | ||
let avatar_output = Path::new("./godot/output/").canonicalize()?; | ||
move_dir_recursive(&avatar_output, &comparison_folder)?; | ||
|
||
// Images comparison | ||
compare_images_folders(&avatar_snapshot_folder, &comparison_folder, 0.995) | ||
.map_err(|e| anyhow::anyhow!(e))?; | ||
|
||
|
||
Ok(()) | ||
} |
Binary file added
BIN
+29.9 KB
...mage-generation/bafkreia6x2mlr2mmdry66czgpze6khwcdmcpqbkjeu7bmrydzaxf3v4x6q.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+60.9 KB
...generation/bafkreia6x2mlr2mmdry66czgpze6khwcdmcpqbkjeu7bmrydzaxf3v4x6q_face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+22.2 KB
...mage-generation/bafkreid7hnqaw5qpb2ohwj2in344f5gx5ehxyc7wvlgbii5fbcbqh2am6q.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.3 KB
...generation/bafkreid7hnqaw5qpb2ohwj2in344f5gx5ehxyc7wvlgbii5fbcbqh2am6q_face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.7 KB
...mage-generation/bafkreidw3wn32cugqfi4jlqgzldakfbhjcyn377mm3qrjkquo5v4ud5k2u.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+64.7 KB
...generation/bafkreidw3wn32cugqfi4jlqgzldakfbhjcyn377mm3qrjkquo5v4ud5k2u_face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes