forked from antmicro/grabthecam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
from PIL import Image | ||
import imagehash | ||
|
||
failed = False | ||
|
||
def compute_hash(image_path): | ||
return imagehash.whash(Image.open(image_path)) | ||
|
||
def compare_images(image_folder, ground_truth_path): | ||
global failed | ||
# Compute hash for the ground truth image | ||
ground_truth_hash = compute_hash(ground_truth_path) | ||
|
||
# Generate list of image paths in the given folder | ||
image_paths = [os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))] | ||
|
||
# Compare each image with the ground truth | ||
for image_path in image_paths: | ||
image_hash = compute_hash(image_path) | ||
hamming_distance = image_hash - ground_truth_hash | ||
similarity_percentage = 100 * (1 - (hamming_distance / len(str(ground_truth_hash)))) | ||
|
||
# Fail if `similarity_percentage` is below 90% | ||
if not failed: | ||
failed = similarity_percentage < 90.0 | ||
|
||
print(f"Image: {image_path}, Similarity: {similarity_percentage:.2f}%") | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 3: | ||
print("Usage: python script.py <image_folder> <ground_truth_image>") | ||
sys.exit(1) | ||
|
||
# Get folder path and ground truth image path from command line arguments | ||
image_folder = sys.argv[1] | ||
ground_truth_path = sys.argv[2] | ||
|
||
# Compare images | ||
compare_images(image_folder, ground_truth_path) | ||
|
||
if failed: | ||
sys.exit(1) |
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,3 @@ | ||
pyvidctrl @ git+https://github.com/antmicro/pyvidctrl@865d76b3b686c22a74e02b75b931d18138e81638 | ||
ImageHash==4.3.* | ||
pillow==10.2.* |
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