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 authored and jbylicki committed Jan 22, 2024
1 parent 09b4a7e commit 676fd89
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/scripts/compare-images.py
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)
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.*
38 changes: 38 additions & 0 deletions .github/workflows/grabthecam-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,41 @@ jobs:
name: test
path: |
artifacts
test:
needs: test-build
runs-on: ubuntu-22.04

steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Download test artifacts
uses: actions/download-artifact@v3
with:
name: test
path: artifacts

- name: Install dependencies
run: |
# By default Github Ubuntu runner uses a regular user (non-root).
sudo .github/scripts/prepare-env.sh
sudo apt install v4l-utils linux-modules-extra-$(uname -r) 1>/dev/null
sudo modprobe vivid
lsmod | grep -i vivid
sudo chown $USER /dev/video0
chmod a+rwx /dev/video0
v4l2-ctl --list-formats -d /dev/video0
pip install -r .github/scripts/requirements.txt
- 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

0 comments on commit 676fd89

Please sign in to comment.