diff --git a/.github/scripts/compare-images.py b/.github/scripts/compare-images.py new file mode 100755 index 0000000..7aed68d --- /dev/null +++ b/.github/scripts/compare-images.py @@ -0,0 +1,47 @@ +#!/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 ") + 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: + print("Some formats failed the similarity score >90%") + sys.exit(1) diff --git a/.github/scripts/requirements.txt b/.github/scripts/requirements.txt new file mode 100644 index 0000000..7c78858 --- /dev/null +++ b/.github/scripts/requirements.txt @@ -0,0 +1,3 @@ +pyvidctrl @ git+https://github.com/antmicro/pyvidctrl@865d76b3b686c22a74e02b75b931d18138e81638 +ImageHash==4.3.* +pillow==10.2.* diff --git a/.github/workflows/grabthecam-build.yml b/.github/workflows/grabthecam-build.yml index 4956d4a..9358f0a 100644 --- a/.github/workflows/grabthecam-build.yml +++ b/.github/workflows/grabthecam-build.yml @@ -84,6 +84,8 @@ jobs: chmod a+rwx /dev/video0 v4l2-ctl --list-formats -d /dev/video0 + pip install -r .github/scripts/requirements.txt + - name: Build the project run: | # Prepare directories for the artifacts @@ -105,9 +107,19 @@ jobs: ./build/grabthecam-test-frame-fetch /dev/video0 cp frame_*.png artifacts/frames/ + - name: Test pyvidctrl + run: | + # Try to restore settings saved from grabthecam + pyvidctrl -r ./artifacts/.pyvidctrl-vivid | grep -q "Couldn't restore value of" && exit 1 || true + + - name: Compare images + run: | + # Using RGB3 as Ground Truth + .github/scripts/compare-images.py ./artifacts/frames ./artifacts/frames/frame_RGB3.png + - name: Upload test artifacts uses: actions/upload-artifact@v3 with: - name: test + name: test_results path: | artifacts