mirror of
https://github.com/brycedrennan/imaginAIry
synced 2024-10-31 03:20:40 +00:00
9b95e8b0b6
- do not provide automatically imported api functions and objects in `imaginairy` root module - horrible hack to overcome horrible design choices by easy_install/setuptools The hack modifies the installed script to remove the __import__ pkg_resources line If we don't do this then the scripts will be slow to start up because of pkg_resources.require() which is called by setuptools to ensure the "correct" version of the package is installed. before modification example: ``` __requires__ = 'imaginAIry==14.0.0b5' __import__('pkg_resources').require('imaginAIry==14.0.0b5') __file__ = '/home/user/projects/imaginairy/imaginairy/bin/aimg' with open(__file__) as f: exec(compile(f.read(), __file__, 'exec')) ```
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import time
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
|
|
def assert_image_similar_to_expectation(img, img_path, threshold=100):
|
|
img.save(img_path)
|
|
expected_img_path = img_path.replace("/test_output/", "/expected_output/")
|
|
expected_img = Image.open(expected_img_path)
|
|
norm_sum_sq_diff = calc_norm_sum_sq_diff(img, expected_img)
|
|
|
|
if norm_sum_sq_diff > threshold:
|
|
diff_img = Image.fromarray(np.asarray(img) - np.asarray(expected_img))
|
|
diff_img.save(img_path + f"_diff_{norm_sum_sq_diff:.1f}.png")
|
|
expected_img.save(img_path + "_expected.png")
|
|
assert (
|
|
norm_sum_sq_diff < threshold
|
|
), f"{norm_sum_sq_diff:.3f} is bigger than threshold {threshold}"
|
|
|
|
|
|
def calc_norm_sum_sq_diff(img, img2):
|
|
sum_sq_diff = np.sum(
|
|
(np.asarray(img).astype("float") - np.asarray(img2).astype("float")) ** 2
|
|
)
|
|
norm_sum_sq_diff = sum_sq_diff / np.sqrt(sum_sq_diff)
|
|
return norm_sum_sq_diff
|
|
|
|
|
|
class Timer:
|
|
def __init__(self, name):
|
|
self.name = name
|
|
self.start = None
|
|
self.elapsed = None
|
|
self.end = None
|
|
|
|
def __enter__(self):
|
|
self.start = time.perf_counter()
|
|
return self
|
|
|
|
def __exit__(self, *args):
|
|
self.end = time.perf_counter()
|
|
self.elapsed = self.end - self.start
|
|
|
|
print(f"{self.name} took {self.elapsed*1000:.2f} ms")
|