Skip to content

Commit

Permalink
ci_runner: Add test stage
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoscik committed Jan 22, 2024
1 parent 62de2ec commit 8a9bfa8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .github/scripts/compare-images.py
Original file line number Diff line number Diff line change
@@ -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 <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:
print("Some formats failed the similarity score >90%")
sys.exit(1)
3 changes: 3 additions & 0 deletions .github/scripts/requirements.txt
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.*
14 changes: 13 additions & 1 deletion .github/workflows/grabthecam-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 8a9bfa8

Please sign in to comment.