FFFF Add photo station management UI by noobydp · Pull Request #136 · tracefinity/tracefinity · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
429 changes: 409 additions & 20 deletions backend/app/api/routes.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Settings(BaseSettings):
tool_label_ollama_url: str = "http://localhost:11434"
tool_label_timeout_seconds: float = 30.0
tool_label_max_crop_px: int = 512
photo_stations: bool = False

model_config = {
"env_file": ".env",
Expand Down
124 changes: 124 additions & 0 deletions backend/app/models/schemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
from typing import Literal

from pydantic import BaseModel, field_validator, model_validator
Expand All @@ -11,6 +12,41 @@ class Point(BaseModel):
x: float
y: float

@field_validator("x", "y")
@classmethod
def validate_finite(cls, v: float) -> float:
if not math.isfinite(v):
raise ValueError("point coordinate must be finite")
return v


class CaptureCrop(BaseModel):
x: float
y: float
width: float
height: float

@field_validator("x", "y", "width", "height")
@classmethod
def validate_finite(cls, v: float) -> float:
if not math.isfinite(v):
raise ValueError("capture crop value must be finite")
return v

@field_validator("x", "y")
@classmethod
def validate_origin(cls, v: float) -> float:
if v < 0 or v > 1:
raise ValueError("capture crop origin must be between 0 and 1")
return v

@field_validator("width", "height")
@classmethod
def validate_size(cls, v: float) -> float:
if v <= 0 or v > 1:
raise ValueError("capture crop size must be between 0 and 1")
return v


class PhotoWarning(BaseModel):
code: str
Expand Down Expand Up @@ -52,17 +88,27 @@ class UploadResponse(BaseModel):
session_id: str
image_url: str
detected_corners: list[Point] | None
image_width: int | None = None
image_height: int | None = None
corner_source: Literal["detected", "station", "none"] = "none"
station_id: str | None = None


class CornersRequest(BaseModel):
corners: list[Point]
paper_size: PaperSize
save_station_name: str | None = None


class CornersResponse(BaseModel):
corrected_image_url: str
scale_factor: float
warnings: list[PhotoWarning] = []
station: "PhotoStation | None" = None


class RedetectCornersResponse(BaseModel):
corners: list[Point]


class TraceRequest(BaseModel):
Expand Down Expand Up @@ -221,6 +267,9 @@ class Session(BaseModel):
tags: list[str] = []
created_at: str | None = None
original_image_path: str | None = None
original_image_width: int | None = None
original_image_height: int | None = None
capture_crop: CaptureCrop | None = None
corrected_image_path: str | None = None
mask_image_path: str | None = None
corners: list[Point] | None = None
Expand Down Expand Up @@ -259,6 +308,81 @@ class StatusResponse(BaseModel):
status: str


# --- photo stations ---

PhotoStationMatchStatus = Literal["exact", "near", "far"]


class PhotoStation(BaseModel):
id: str
name: str
image_width: int
image_height: int
image_path: str | None = None
capture_crop: CaptureCrop | None = None
paper_size: PaperSize
corners: list[Point]
created_at: str | None = None
updated_at: str | None = None
last_used_at: str | None = None

@field_validator("image_width", "image_height")
@classmethod
def validate_image_dimension(cls, v: int) -> int:
if v <= 0:
raise ValueError("station image dimensions must be positive")
return v

@field_validator("corners")
@classmethod
def validate_corners(cls, v: list[Point]) -> list[Point]:
if len(v) != 4:
raise ValueError("station corners must contain four points")
return v


class PhotoStationSuggestion(BaseModel):
station: PhotoStation
match_status: PhotoStationMatchStatus
width_delta_percent: float = 0.0
height_delta_percent: float = 0.0
max_corner_drift_px: float | None = None
max_corner_drift_percent: float | None = None
warnings: list[str] = []


class PhotoStationListResponse(BaseModel):
stations: list[PhotoStation]


class PhotoStationSuggestionsResponse(BaseModel):
suggestions: list[PhotoStationSuggestion]
station_count: int


class PhotoStationCreateRequest(BaseModel):
name: str
session_id: str
paper_size: PaperSize | None = None
corners: list[Point] | None = None


class PhotoStationUpdateRequest(BaseModel):
name: str | None = None
paper_size: PaperSize | None = None
corners: list[Point] | None = None


class ReuseCornersRequest(BaseModel):
station_id: str


class ReuseCornersResponse(BaseModel):
corners: list[Point]
paper_size: PaperSize
suggestion: PhotoStationSuggestion


# --- tool library ---

class Tool(BaseModel):
Expand Down
30 changes: 26 additions & 4 deletions backend/app/services/image_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from PIL import Image, ImageOps

from app.models.schemas import CaptureCrop

logger = logging.getLogger(__name__)

HEIC_EXTENSIONS = {".heic", ".heif"}
Expand All @@ -23,13 +25,27 @@
pass


def ingest_image(content: bytes, ext: str, max_dim: int | None = None) -> tuple[bytes, str, float]:
def _crop_bounds(width: int, height: int, crop: CaptureCrop) -> tuple[int, int, int, int]:
left = max(0, min(width - 1, int(round(crop.x * width))))
top = max(0, min(height - 1, int(round(crop.y * height))))
right = max(left + 4, min(width, int(round((crop.x + crop.width) * width))))
bottom = max(top + 4, min(height, int(round((crop.y + crop.height) * height))))
return left, top, right, bottom


def ingest_image(
content: bytes,
ext: str,
max_dim: int | None = None,
capture_crop: CaptureCrop | None = None,
) -> tuple[bytes, str, float]:
"""normalise an uploaded image. returns (bytes, ext, downscale_ratio).

HEIC becomes JPEG, EXIF orientation is applied to the pixels and the tag
dropped, and the long edge is capped at max_dim. ratio is <1 when shrunk;
the long edge lands exactly on max_dim so the ratio is exact for it and
within half a pixel for the short edge (dimensions are rounded)."""
dropped, an optional fractional crop is applied to the upright image, and
the long edge is capped at max_dim. ratio is <1 when shrunk; the long edge
lands exactly on max_dim so the ratio is exact for it and within half a
pixel for the short edge (dimensions are rounded)."""
img = Image.open(io.BytesIO(content))
changed = False
new_ext = ext.lower()
Expand All @@ -42,6 +58,12 @@ def ingest_image(content: bytes, ext: str, max_dim: int | None = None) -> tuple[
img = ImageOps.exif_transpose(img)
changed = True

if capture_crop is not None:
w, h = img.size
img = img.crop(_crop_bounds(w, h, capture_crop))
logger.info("cropped image %dx%d -> %dx%d", w, h, img.width, img.height)
changed = True

ratio = 1.0
w, h = img.size
if max_dim and max(w, h) > max_dim:
Expand Down
75 changes: 75 additions & 0 deletions backend/app/services/photo_station_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from __future__ import annotations

import json
import logging
import tempfile
import threading
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

from app.models.schemas import PhotoStation

logger = logging.getLogger(__name__)


class PhotoStationStore:
def __init__(self, storage_path: Path):
self.file_path = storage_path / "photo-stations.json"
self._stations: dict[str, PhotoStation] = {}
self._lock = threading.Lock()
self._load()

def _load(self):
if self.file_path.exists():
try:
data = json.loads(self.file_path.read_text())
for sid, sdata in data.items():
self._stations[sid] = PhotoStation.model_validate(sdata)
except OSError:
logger.error(f"Failed to load {self.file_path}: permission denied")
raise
except Exception as e:
corrupt_path = self._corrupt_path()
logger.error(f"Failed to load {self.file_path}: {e}; moving to {corrupt_path}")
self.file_path.replace(corrupt_path)
self._stations = {}

def _corrupt_path(self) -> Path:
stamp = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S%f")
return self.file_path.with_name(f"{self.file_path.stem}.corrupt-{stamp}{self.file_path.suffix}")

def _save(self):
data = {sid: s.model_dump() for sid, s in self._stations.items()}
temp_fd, temp_path = tempfile.mkstemp(
dir=self.file_path.parent,
prefix=".photo-stations_",
suffix=".tmp",
)
try:
with open(temp_fd, "w") as f:
json.dump(data, f, indent=2)
Path(temp_path).replace(self.file_path)
except Exception:
Path(temp_path).unlink(missing_ok=True)
raise

def get(self, station_id: str) -> Optional[PhotoStation]:
with self._lock:
return self._stations.get(station_id)

def set(self, station_id: str, station: PhotoStation):
with self._lock:
self._stations[station_id] = station
self._save()

def delete(self, station_id: str) -> Optional[PhotoStation]:
with self._lock:
station = self._stations.pop(station_id, None)
if station:
self._save()
return station

def all(self) -> dict[str, PhotoStation]:
with self._lock:
return self._stations.copy()
19 changes: 19 additions & 0 deletions backend/tests/test_image_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from PIL import Image

from app.models.schemas import CaptureCrop
from app.services.image_ingest import ingest_image


Expand Down Expand Up @@ -89,3 +90,21 @@ def test_no_max_dim_skips_resize_but_normalises_orientation(self):
img = _open(out)
assert (img.width, img.height) == (2402, 3300)
assert ratio == 1.0

def test_crop_happens_before_downscale(self):
img = Image.new("RGB", (4032, 3024), "green")
buf = io.BytesIO()
img.save(buf, format="PNG")

out, ext, ratio = ingest_image(
buf.getvalue(),
".png",
max_dim=2048,
capture_crop=CaptureCrop(x=0, y=0, width=0.5, height=1),
)

cropped = _open(out)
assert ext == ".png"
assert cropped.height == 2048
assert cropped.width == round(2016 * (2048 / 3024))
assert ratio == pytest.approx(2048 / 3024)
Loading
Loading
0