mirror of
https://github.com/azaion/gps-denied-onboard.git
synced 2026-04-22 11:16:37 +00:00
feat: stage8 — Global Place Recognition and Metric Refinement
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""Tests for Global Place Recognition (F08)."""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from gps_denied.core.gpr import GlobalPlaceRecognition
|
||||
from gps_denied.core.models import ModelManager
|
||||
from gps_denied.schemas.gpr import TileCandidate
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gpr():
|
||||
manager = ModelManager()
|
||||
gpr = GlobalPlaceRecognition(manager)
|
||||
gpr.load_index("flight_123", "dummy_path.faiss")
|
||||
return gpr
|
||||
|
||||
def test_compute_location_descriptor(gpr):
|
||||
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
||||
desc = gpr.compute_location_descriptor(img)
|
||||
|
||||
assert desc.shape == (4096,)
|
||||
# Should be L2 normalized
|
||||
assert np.isclose(np.linalg.norm(desc), 1.0)
|
||||
|
||||
def test_retrieve_candidate_tiles(gpr):
|
||||
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
||||
candidates = gpr.retrieve_candidate_tiles(img, top_k=5)
|
||||
|
||||
assert len(candidates) == 5
|
||||
for c in candidates:
|
||||
assert isinstance(c, TileCandidate)
|
||||
assert c.similarity_score >= 0.0
|
||||
|
||||
def test_retrieve_candidate_tiles_for_chunk(gpr):
|
||||
imgs = [np.zeros((200, 200, 3), dtype=np.uint8) for _ in range(5)]
|
||||
candidates = gpr.retrieve_candidate_tiles_for_chunk(imgs, top_k=3)
|
||||
|
||||
assert len(candidates) == 3
|
||||
# Ensure they are sorted
|
||||
assert candidates[0].similarity_score >= candidates[1].similarity_score
|
||||
@@ -0,0 +1,74 @@
|
||||
"""Tests for Metric Refinement (F09)."""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from gps_denied.core.metric import MetricRefinement
|
||||
from gps_denied.core.models import ModelManager
|
||||
from gps_denied.schemas.flight import GPSPoint
|
||||
from gps_denied.schemas.metric import AlignmentResult, ChunkAlignmentResult
|
||||
from gps_denied.schemas.satellite import TileBounds
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def metric():
|
||||
manager = ModelManager()
|
||||
return MetricRefinement(manager)
|
||||
|
||||
@pytest.fixture
|
||||
def bounds():
|
||||
# Covers precisely 1 degree lat and lon around 49, 32
|
||||
return TileBounds(
|
||||
nw=GPSPoint(lat=50.0, lon=32.0),
|
||||
ne=GPSPoint(lat=50.0, lon=33.0),
|
||||
sw=GPSPoint(lat=49.0, lon=32.0),
|
||||
se=GPSPoint(lat=49.0, lon=33.0),
|
||||
center=GPSPoint(lat=49.5, lon=32.5),
|
||||
gsd=1.0 # dummy
|
||||
)
|
||||
|
||||
def test_extract_gps_from_alignment(metric, bounds):
|
||||
# Homography is identity -> map center to center
|
||||
H = np.eye(3, dtype=np.float64)
|
||||
# The image is 256x256 in our mock
|
||||
# Center pixel is 128, 128
|
||||
gps = metric.extract_gps_from_alignment(H, bounds, (128, 128))
|
||||
|
||||
# 128 is middle -> should be EXACTLY at 49.5 lat and 32.5 lon
|
||||
assert np.isclose(gps.lat, 49.5)
|
||||
assert np.isclose(gps.lon, 32.5)
|
||||
|
||||
def test_align_to_satellite(metric, bounds, monkeypatch):
|
||||
# Monkeypatch random to ensure matched=True and high inliers
|
||||
def mock_infer(*args, **kwargs):
|
||||
H = np.eye(3, dtype=np.float64)
|
||||
return {"homography": H, "inlier_count": 80, "confidence": 0.8}
|
||||
|
||||
engine = metric.model_manager.get_inference_engine("LiteSAM")
|
||||
monkeypatch.setattr(engine, "infer", mock_infer)
|
||||
|
||||
uav = np.zeros((256, 256, 3))
|
||||
sat = np.zeros((256, 256, 3))
|
||||
|
||||
res = metric.align_to_satellite(uav, sat, bounds)
|
||||
assert res is not None
|
||||
assert isinstance(res, AlignmentResult)
|
||||
assert res.matched is True
|
||||
assert res.inlier_count == 80
|
||||
|
||||
def test_align_chunk_to_satellite(metric, bounds, monkeypatch):
|
||||
def mock_infer(*args, **kwargs):
|
||||
H = np.eye(3, dtype=np.float64)
|
||||
return {"homography": H, "inlier_count": 80, "confidence": 0.8}
|
||||
|
||||
engine = metric.model_manager.get_inference_engine("LiteSAM")
|
||||
monkeypatch.setattr(engine, "infer", mock_infer)
|
||||
|
||||
uavs = [np.zeros((256, 256, 3)) for _ in range(5)]
|
||||
sat = np.zeros((256, 256, 3))
|
||||
|
||||
res = metric.align_chunk_to_satellite(uavs, sat, bounds)
|
||||
assert res is not None
|
||||
assert isinstance(res, ChunkAlignmentResult)
|
||||
assert res.matched is True
|
||||
assert res.chunk_id == "chunk1"
|
||||
Reference in New Issue
Block a user