Add buffered crop UI and API controls

This commit is contained in:
2026-04-11 16:33:45 +02:00
parent 660ce4e7cc
commit 3b5a9e8635
2 changed files with 28 additions and 11 deletions

View File

@@ -89,6 +89,7 @@ def detect_salient_object(image: np.ndarray) -> BBox | None:
def square_bbox(bbox: BBox, image_shape: tuple[int, int, int], buffer_ratio: float = 0.15) -> BBox:
image_h, image_w = image_shape[:2]
buffer_ratio = max(0.0, min(buffer_ratio, 0.5))
side = int(round(max(bbox.w, bbox.h) * (1 + buffer_ratio * 2)))
side = max(1, min(side, image_w, image_h))
@@ -126,10 +127,10 @@ def _data_url(image_bytes: bytes, mime_type: str) -> str:
return f"data:{mime_type};base64,{base64.b64encode(image_bytes).decode('ascii')}"
def process_image(image_bytes: bytes, filename: str) -> dict[str, Any]:
def process_image(image_bytes: bytes, filename: str, buffer_ratio: float = 0.15) -> dict[str, Any]:
image = decode_image(image_bytes)
bbox, method = select_primary_bbox(image)
square = square_bbox(bbox, image.shape)
square = square_bbox(bbox, image.shape, buffer_ratio=buffer_ratio)
crop = crop_image(image, square)
annotated = draw_square(image, square)
@@ -139,6 +140,7 @@ def process_image(image_bytes: bytes, filename: str) -> dict[str, Any]:
return {
"filename": filename,
"method": method,
"buffer_ratio": buffer_ratio,
"detected_bbox": bbox.__dict__,
"square_bbox": square.__dict__,
"source_size": {"width": int(image.shape[1]), "height": int(image.shape[0])},