acra-backend / code /tests /test_consistency.py
Zeref8425's picture
Deploy backend snapshot
78661be
Raw History Blame Contribute Delete
3.02 kB
"""Same image in, same result out -- whatever device sent it.
Regression for "I ran the same image on PC and on my phone and got different
results". The cause was the browser re-encoding uploads; these tests pin the
server side: identical bytes must give identical pixels and metrics, and a
phone photo stored sideways with an EXIF rotation tag must decode to the same
pixels as the already-upright copy.
"""
import asyncio
import io
import sys
from pathlib import Path
import numpy as np
from PIL import Image
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from main import _validate_upload # noqa: E402
from pipeline import run_full_pipeline # noqa: E402
class _Upload:
def __init__(self, data, name="photo.jpg", ctype="image/jpeg"):
self._data, self.filename, self.content_type = data, name, ctype
async def read(self):
return self._data
def _poster():
# Red and green panels a deutan viewer confuses, on white, plus grey text-ish bars.
img = np.full((120, 160, 3), 255, np.uint8)
img[10:60, 10:70] = (200, 40, 40)
img[10:60, 90:150] = (60, 150, 50)
img[80:90, 10:150] = (40, 40, 40)
return img
def _decode(data):
img, _ = asyncio.run(_validate_upload(_Upload(data), "deutan", 0.8, 0.45, 8.0, 0, None, None, max_dim=1920))
return img
def test_same_pixels_give_identical_output_and_metrics():
img = _poster()
out1, m1 = run_full_pipeline(img, 0.8, "deutan", use_segmentation=False)
out2, m2 = run_full_pipeline(img, 0.8, "deutan", use_segmentation=False)
assert np.array_equal(out1, out2)
for k in ("n_conflicts_total", "n_conflicts_resolved", "conflict_resolution_rate",
"de_improvement", "naturalness_preservation", "auto_clusters"):
assert m1[k] == m2[k], k
def test_same_bytes_decode_to_same_pixels():
buf = io.BytesIO()
Image.fromarray(_poster()).save(buf, "PNG")
assert np.array_equal(_decode(buf.getvalue()), _decode(buf.getvalue()))
def test_exif_rotated_photo_matches_upright_copy():
upright = Image.fromarray(_poster())
# A phone stores the sensor image sideways and adds Orientation=6
# ("rotate 90 degrees clockwise to display").
sideways = upright.transpose(Image.Transpose.ROTATE_90)
exif = Image.Exif()
exif[0x0112] = 6
a, b = io.BytesIO(), io.BytesIO()
upright.save(a, "PNG")
sideways.save(b, "PNG", exif=exif)
assert np.array_equal(_decode(a.getvalue()), _decode(b.getvalue()))
def test_nothing_to_fix_returns_the_exact_original():
# Regression: the output conversion used astype (truncates), so a poster
# with no conflicts still came back with most pixels one step darker.
rng = np.random.default_rng(1)
img = rng.integers(0, 256, (64, 64, 3), dtype=np.uint8)
img[..., :] = img[..., :1] # greys only: no red-green conflicts possible
out, m = run_full_pipeline(img, 1.0, "deutan", use_segmentation=False)
assert m["n_conflicts_total"] == 0
assert np.array_equal(out, img)