Spaces:
Paused
Paused
File size: 4,198 Bytes
3d46bcf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import cv2
import numpy as np
import pytest
from fastapi.testclient import TestClient
from backend.app import app
from backend.registration import register, RegistrationError
@pytest.fixture
def iris():
return cv2.resize(cv2.imread('fixtures/synthetic-iris.png'), (800, 800))
@pytest.mark.parametrize('angle,scale,gain', [(0, 1, 1), (12, .92, 1), (-8, 1.04, .55)])
def test_known_transform(iris, angle, scale, gain):
transform = np.vstack([cv2.getRotationMatrix2D((400, 400), angle, scale), [0, 0, 1]])
transform[:2, 2] += [16, -12]
moving = np.clip(cv2.warpPerspective(iris, transform, (800, 800))*gain, 0, 255).astype(np.uint8)
result = register(iris, moving)
pts = np.float32([[[200, 200], [600, 200], [600, 600], [200, 600]]])
recovered = cv2.perspectiveTransform(cv2.perspectiveTransform(pts, transform), np.array(result['metrics']['transform']))
assert np.max(np.linalg.norm(recovered-pts, axis=2)) < 1
assert np.array_equal(result['composite'][result['contribution'] == 0], iris[result['contribution'] == 0])
def test_blank_rejected():
with pytest.raises(RegistrationError):
register(np.zeros((400,400,3), np.uint8), np.zeros((400,400,3), np.uint8))
def test_unrelated_rejected(iris):
noise = np.random.default_rng(91).integers(0, 256, iris.shape, dtype=np.uint8)
with pytest.raises(RegistrationError):
register(iris, noise)
def test_api(iris):
client = TestClient(app)
assert client.get('/api/health').status_code == 200
assert client.post('/api/register', files={'reference': ('x.jpg', b'bad'), 'moving': ('y.jpg', b'bad')}).status_code == 422
data = cv2.imencode('.jpg', iris)[1].tobytes()
response = client.post('/api/register', files={'reference': ('x.jpg', data), 'moving': ('y.jpg', data)})
assert response.status_code == 200
assert response.json()['images']['aligned'].startswith('data:image/png;base64,')
def test_exposure_disabled_and_different_sizes(iris):
moving = cv2.resize(iris, (640, 640))
result = register(iris, moving, exposure=False)
assert result['metrics']['exposure_gain'] == 1
assert result['aligned'].shape == iris.shape
@pytest.mark.parametrize('case,size', [('local_warp',1000), ('local_dark',1000), ('local_perspective',1000), ('local_warp',1600)])
def test_local_known_correspondences(case, size):
from synthetic_cases import make_case, mapping_errors
reference = cv2.resize(cv2.imread('fixtures/synthetic-iris.png'),(size,size))
moving, truth, source = make_case(reference,case)
result = register(reference,moving,return_maps=True)
assert result['metrics']['local']['applied']
errors = mapping_errors(result['sampling_map'],truth,source)
# Threshold scales with image size; independent coordinates never fit flow.
assert np.percentile(errors,95) < size/1000
assert np.array_equal(result['composite'][result['contribution']==0],reference[result['contribution']==0])
assert np.all(result['contribution'][result['local_confidence']==0] == 0)
# Actual output must be one resample of original pixels, not a warped intermediate.
expected = cv2.remap(moving,result['sampling_map'][...,0],result['sampling_map'][...,1],cv2.INTER_LINEAR)
assert np.array_equal(result['aligned'],expected)
@pytest.mark.parametrize('case', ['identity','rotation_scale','perspective_dark','reflection'])
def test_local_does_not_degrade_global(case):
from synthetic_cases import make_case
reference = cv2.resize(cv2.imread('fixtures/synthetic-iris.png'),(1000,1000))
moving, _, _ = make_case(reference,case)
baseline = register(reference,moving,local=False,return_maps=True)
refined = register(reference,moving,local=True,return_maps=True)
assert not refined['metrics']['local']['applied']
assert np.array_equal(baseline['sampling_map'],refined['sampling_map'])
def test_local_disabled_still_rejects_nonlinear_pair():
from synthetic_cases import make_case
reference = cv2.resize(cv2.imread('fixtures/synthetic-iris.png'),(1000,1000))
moving, _, _ = make_case(reference,'local_warp')
with pytest.raises(RegistrationError):
register(reference,moving,local=False)
|