| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime |
| 4 |
import errno |
| 5 |
import functools |
| 6 |
import os |
| 7 |
import typing |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
from unittest import TestCase as _Base |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
|
| 13 |
from av.datasets import fate as fate_suite |
| 14 |
|
| 15 |
try: |
| 16 |
import PIL # noqa |
| 17 |
|
| 18 |
has_pillow = True |
| 19 |
except ImportError: |
| 20 |
has_pillow = False |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from typing import Any, Callable, TypeVar |
| 24 |
|
| 25 |
from PIL.Image import Image |
| 26 |
|
| 27 |
T = TypeVar("T") |
| 28 |
|
| 29 |
|
| 30 |
__all__ = ("fate_suite",) |
| 31 |
|
| 32 |
|
| 33 |
is_windows = os.name == "nt" |
| 34 |
skip_tests = frozenset(os.environ.get("PYAV_SKIP_TESTS", "").split(",")) |
| 35 |
|
| 36 |
|
| 37 |
def safe_makedirs(path: str) -> None: |
| 38 |
try: |
| 39 |
os.makedirs(path) |
| 40 |
except OSError as e: |
| 41 |
if e.errno != errno.EEXIST: |
| 42 |
raise |
| 43 |
|
| 44 |
|
| 45 |
_start_time = datetime.datetime.now() |
| 46 |
|
| 47 |
|
| 48 |
def _sandbox(timed: bool = False) -> str: |
| 49 |
root = os.path.abspath(os.path.join(__file__, "..", "..", "sandbox")) |
| 50 |
|
| 51 |
if timed: |
| 52 |
sandbox = os.path.join(root, _start_time.strftime("%Y%m%d-%H%M%S")) |
| 53 |
else: |
| 54 |
sandbox = root |
| 55 |
|
| 56 |
if not os.path.exists(sandbox): |
| 57 |
os.makedirs(sandbox) |
| 58 |
|
| 59 |
return sandbox |
| 60 |
|
| 61 |
|
| 62 |
def asset(*args: str) -> str: |
| 63 |
adir = os.path.dirname(__file__) |
| 64 |
return os.path.abspath(os.path.join(adir, "assets", *args)) |
| 65 |
|
| 66 |
|
| 67 |
# Store all of the sample data here. |
| 68 |
os.environ["PYAV_TESTDATA_DIR"] = asset() |
| 69 |
|
| 70 |
|
| 71 |
def fate_png() -> str: |
| 72 |
return fate_suite("png1/55c99e750a5fd6_50314226.png") |
| 73 |
|
| 74 |
|
| 75 |
def sandboxed( |
| 76 |
*args: str, makedirs: bool = True, sandbox: str | None = None, timed: bool = False |
| 77 |
) -> str: |
| 78 |
path = os.path.join(_sandbox(timed) if sandbox is None else sandbox, *args) |
| 79 |
if makedirs: |
| 80 |
safe_makedirs(os.path.dirname(path)) |
| 81 |
|
| 82 |
return path |
| 83 |
|
| 84 |
|
| 85 |
# Decorator for running a test in the sandbox directory |
| 86 |
def run_in_sandbox(func: Callable[..., T]) -> Callable[..., T]: |
| 87 |
@functools.wraps(func) |
| 88 |
def _inner(self: Any, *args: Any, **kwargs: Any) -> T: |
| 89 |
current_dir = os.getcwd() |
| 90 |
try: |
| 91 |
os.chdir(self.sandbox) |
| 92 |
return func(self, *args, **kwargs) |
| 93 |
finally: |
| 94 |
os.chdir(current_dir) |
| 95 |
|
| 96 |
return _inner |
| 97 |
|
| 98 |
|
| 99 |
def assertNdarraysEqual(a: np.ndarray, b: np.ndarray) -> None: |
| 100 |
assert a.shape == b.shape |
| 101 |
|
| 102 |
comparison = a == b |
| 103 |
if not comparison.all(): |
| 104 |
it = np.nditer(comparison, flags=["multi_index"]) |
| 105 |
msg = "" |
| 106 |
for equal in it: |
| 107 |
if not equal: |
| 108 |
msg += "- arrays differ at index {}; {} {}\n".format( |
| 109 |
it.multi_index, |
| 110 |
a[it.multi_index], |
| 111 |
b[it.multi_index], |
| 112 |
) |
| 113 |
assert False, f"ndarrays contents differ\n{msg}" |
| 114 |
|
| 115 |
|
| 116 |
@typing.no_type_check |
| 117 |
def assertImagesAlmostEqual(a: Image, b: Image, epsilon: float = 0.1) -> None: |
| 118 |
import PIL.ImageFilter as ImageFilter |
| 119 |
|
| 120 |
assert a.size == b.size |
| 121 |
a = a.filter(ImageFilter.BLUR).getdata() |
| 122 |
b = b.filter(ImageFilter.BLUR).getdata() |
| 123 |
for i, ax, bx in zip(range(len(a)), a, b): |
| 124 |
diff = sum(abs(ac / 256 - bc / 256) for ac, bc in zip(ax, bx)) / 3 |
| 125 |
assert diff < epsilon, f"images differed by {diff} at index {i}; {ax} {bx}" |
| 126 |
|
| 127 |
|
| 128 |
class TestCase(_Base): |
| 129 |
@classmethod |
| 130 |
def _sandbox(cls, timed: bool = True) -> str: |
| 131 |
path = os.path.join(_sandbox(timed=timed), cls.__name__) |
| 132 |
safe_makedirs(path) |
| 133 |
return path |
| 134 |
|
| 135 |
@property |
| 136 |
def sandbox(self) -> str: |
| 137 |
return self._sandbox(timed=True) |
| 138 |
|
| 139 |
def sandboxed( |
| 140 |
self, |
| 141 |
*args: str, |
| 142 |
makedirs: bool = True, |
| 143 |
timed: bool = True, |
| 144 |
sandbox: str | None = None, |
| 145 |
) -> str: |
| 146 |
if sandbox is None: |
| 147 |
return sandboxed( |
| 148 |
*args, makedirs=makedirs, timed=timed, sandbox=self.sandbox |
| 149 |
) |
| 150 |
else: |
| 151 |
return sandboxed(*args, makedirs=makedirs, timed=timed, sandbox=sandbox) |