File size: 3,611 Bytes
e04bbc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import base64
from pathlib import Path

import cv2
from fastapi.testclient import TestClient

from app import app

REPO_ROOT = Path(__file__).resolve().parent.parent.parent
SAMPLE = REPO_ROOT / "dataset" / "photo_2026-01-22_19-20-24.jpg"


def _b64_image() -> str:
    raw = SAMPLE.read_bytes()
    return "data:image/jpeg;base64," + base64.b64encode(raw).decode()


def test_analyze_returns_bundles_and_stages():
    client = TestClient(app)
    r = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {},
        "return_stages": ["binary", "final"],
    })
    assert r.status_code == 200
    body = r.json()
    assert isinstance(body["bundles"], list)
    assert len(body["bundles"]) > 0
    b0 = body["bundles"][0]
    assert "contour" in b0 and "center" in b0 and "area" in b0
    assert "binary" in body["stages"]
    assert "final" in body["stages"]
    assert "mask" not in body["stages"]
    assert "score" not in body


def test_analyze_with_ground_truth_returns_score():
    client = TestClient(app)
    first = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {}, "return_stages": [],
    }).json()
    gt = [{"center": b["center"]} for b in first["bundles"]]
    r = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {}, "return_stages": [],
        "ground_truth": {"bundles": gt},
        "match_radius": 20,
    })
    assert r.status_code == 200
    s = r.json()["score"]
    assert s["precision"] >= 0.95 and s["recall"] >= 0.95


def test_analyze_defaults_to_the_relative_matcher_when_contours_are_sent():
    client = TestClient(app)
    first = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {}, "return_stages": [],
    }).json()
    gt = [{"center": b["center"], "contour": b["contour"]} for b in first["bundles"]]
    r = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {}, "return_stages": [],
        "ground_truth": {"bundles": gt},
    })
    assert r.status_code == 200
    body = r.json()
    assert body["matcher"] == {"kind": "relative_radius", "param": 0.5}
    assert body["score"]["precision"] >= 0.95 and body["score"]["recall"] >= 0.95


def test_relative_matcher_refuses_ground_truth_without_contours():
    """Silently scoring against zero usable targets is how the 20 px bug read as data."""
    client = TestClient(app)
    r = client.post("/analyze", json={
        "image_b64": _b64_image(),
        "params": {}, "return_stages": [],
        "ground_truth": {"bundles": [{"center": [10, 10]}]},
    })
    assert r.status_code == 400
    assert "contours" in r.json()["detail"]


def test_score_endpoint_standalone():
    client = TestClient(app)
    r = client.post("/score", json={
        "run_bundles": [{"center": [10, 10]}, {"center": [50, 50]}],
        "gt_bundles": [{"center": [11, 11]}],
        "match_radius": 20,
    })
    assert r.status_code == 200
    body = r.json()
    assert (body["tp"], body["fp"], body["fn"]) == (1, 1, 0)


def test_score_endpoint_accepts_the_relative_matcher():
    client = TestClient(app)
    square = [[0, 0], [200, 0], [200, 200], [0, 200]]
    r = client.post("/score", json={
        "run_bundles": [{"center": [140, 100]}],
        "gt_bundles": [{"center": [100, 100], "contour": square}],
        "matcher_kind": "relative_radius",
        "matcher_param": 0.5,
    })
    assert r.status_code == 200
    assert r.json()["tp"] == 1