plants-analysis-api / tests /test_api_analyze.py
DmitryPlant's picture
Deploy local
e04bbc2 verified
Raw History Blame Contribute Delete
3.61 kB
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