Files
espresso_frame/server/app/immich_client.py
T
tfaour 6e353c2271
Build and push server image / build-and-push (push) Failing after 10s
Add server-configurable refresh interval + face-aware cropping
Two features, both toggleable/settable from the web config UI:

Refresh interval: new GET /frame/config returns
{"refresh_interval_s": ...} as plain JSON. Reuses the endpoint the frame
already needs to hit for a reachability check each wake cycle (previously
/health) rather than adding a third round trip, and always returns 200
with current settings regardless of Immich-configured state so it stays
valid as a pure reachability signal. Clamped to [60, 86400] seconds in
POST /api/config.

Face-aware cropping: GET /api/faces?id={assetId} on Immich already
returns real per-photo face bounding boxes from its own People-feature
ML -- confirmed against a live instance, boxes scaled to the asset's
native resolution. No face detection built or bundled here at all, just
an API call plus rectangle math. image_pipeline.render_frame() gains an
optional `faces` param: when present, computes the largest crop window
matching the panel's aspect ratio that fits in the source image, centered
on the union of all face boxes' centroid (scaled into the downloaded
preview's actual resolution) instead of the image's geometric center,
clamped to stay within bounds. No faces (or the smart_crop_faces config
toggle off) falls straight back to the existing ImageOps.fit() center-crop
-- zero behavior change in that case. A faces-lookup failure logs and
degrades to center-crop rather than failing the whole request.

Verified: unit tests for the crop-box math (horizontal shift toward an
off-center face, edge clamping), a full mock-Immich end-to-end pass
(extended to serve /faces) confirming the toggle changes output and the
response is still exactly 192,000 bytes, and a live comparison against a
real 4-face photo on the user's Immich instance (crop top shifted from
528px to 246px toward the detected faces).
2026-07-18 15:32:29 -04:00

58 lines
2.0 KiB
Python

"""Thin wrapper around the bits of the Immich API this project needs."""
from __future__ import annotations
import httpx
class ImmichClient:
def __init__(self, base_url: str, api_key: str):
self.base_url = base_url.rstrip("/")
self._headers = {"x-api-key": api_key}
def list_albums(self) -> list[dict]:
resp = httpx.get(f"{self.base_url}/api/albums", headers=self._headers, timeout=10)
resp.raise_for_status()
return resp.json()
def list_album_assets(self, album_id: str) -> list[dict]:
"""GET /api/albums/{id} doesn't embed assets in this Immich version
(AlbumResponseDto only has assetCount) -- assets live behind the
general search API instead, filtered by albumIds.
Only returns the first page. Fine for a photo frame cycling
through an album; worth adding nextPage pagination if someone
points this at an album large enough to need it.
"""
resp = httpx.post(
f"{self.base_url}/api/search/metadata",
headers=self._headers,
json={"albumIds": [album_id]},
timeout=15,
)
resp.raise_for_status()
return resp.json().get("assets", {}).get("items", [])
def get_asset_faces(self, asset_id: str) -> list[dict]:
"""Face bounding boxes Immich already computed for its own People
feature -- reused here to bias cropping toward keeping faces on
screen instead of running our own detection."""
resp = httpx.get(
f"{self.base_url}/api/faces",
params={"id": asset_id},
headers=self._headers,
timeout=10,
)
resp.raise_for_status()
return resp.json()
def download_asset_preview(self, asset_id: str) -> bytes:
resp = httpx.get(
f"{self.base_url}/api/assets/{asset_id}/thumbnail",
params={"size": "preview"},
headers=self._headers,
timeout=30,
)
resp.raise_for_status()
return resp.content