Spaces:
Running on Zero
Running on Zero
Upload src/backends/geometric.py with huggingface_hub
Browse files- src/backends/geometric.py +128 -0
src/backends/geometric.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Geometric fallback backend — no GPU and no HF token required.
|
| 2 |
+
|
| 3 |
+
When neither a CUDA GPU (local Qwen) nor an HF token (serverless Inference
|
| 4 |
+
Providers) is available, AngleForge would otherwise have no usable engine.
|
| 5 |
+
This backend approximates each camera-angle preset with cheap Pillow
|
| 6 |
+
geometric transforms (perspective tilt, rotation, zoom, translation) so the
|
| 7 |
+
Space is always functional as a bootstrap. It is **not** AI image editing —
|
| 8 |
+
results are geometric approximations of the requested viewpoint.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
from typing import Dict, List
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
from PIL import Image
|
| 17 |
+
|
| 18 |
+
from ..config import ANGLE_PRESETS
|
| 19 |
+
from .base import ImageEditBackend
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _find_coeffs(dst: List[tuple], src: List[tuple]) -> List[float]:
|
| 23 |
+
matrix = []
|
| 24 |
+
for (dx, dy), (sx, sy) in zip(dst, src):
|
| 25 |
+
matrix.append([dx, dy, 1, 0, 0, 0, -sx * dx, -sx * dy])
|
| 26 |
+
matrix.append([0, 0, 0, dx, dy, 1, -sy * dx, -sy * dy])
|
| 27 |
+
a = np.array(matrix, dtype=float)
|
| 28 |
+
b = np.array(src, dtype=float).reshape(8)
|
| 29 |
+
res, *_ = np.linalg.lstsq(a, b, rcond=None)
|
| 30 |
+
return res.tolist()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _perspective(img: Image.Image, src_quad: List[tuple]) -> Image.Image:
|
| 34 |
+
w, h = img.size
|
| 35 |
+
dst = [(0, 0), (w, 0), (w, h), (0, h)]
|
| 36 |
+
coeffs = _find_coeffs(dst, src_quad)
|
| 37 |
+
return img.transform((w, h), Image.PERSPECTIVE, coeffs, resample=Image.BICUBIC)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _tilt(img: Image.Image, top_inset: float, bottom_inset: float) -> Image.Image:
|
| 41 |
+
w, h = img.size
|
| 42 |
+
src = [
|
| 43 |
+
(w * top_inset, 0),
|
| 44 |
+
(w * (1 - top_inset), 0),
|
| 45 |
+
(w * (1 - bottom_inset), h),
|
| 46 |
+
(w * bottom_inset, h),
|
| 47 |
+
]
|
| 48 |
+
return _perspective(img, src)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _zoom(img: Image.Image, factor: float) -> Image.Image:
|
| 52 |
+
w, h = img.size
|
| 53 |
+
if factor >= 1.0: # crop in, then scale back up
|
| 54 |
+
cw, ch = int(w / factor), int(h / factor)
|
| 55 |
+
left, top = (w - cw) // 2, (h - ch) // 2
|
| 56 |
+
return img.crop((left, top, left + cw, top + ch)).resize((w, h), Image.LANCZOS)
|
| 57 |
+
# zoom out: paste shrunk image onto a padded canvas
|
| 58 |
+
sw, sh = int(w * factor), int(h * factor)
|
| 59 |
+
small = img.resize((sw, sh), Image.LANCZOS)
|
| 60 |
+
canvas = Image.new("RGB", (w, h), (20, 20, 20))
|
| 61 |
+
canvas.paste(small, ((w - sw) // 2, (h - sh) // 2))
|
| 62 |
+
return canvas
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def _shift(img: Image.Image, dx_frac: float, dy_frac: float) -> Image.Image:
|
| 66 |
+
w, h = img.size
|
| 67 |
+
dx, dy = int(w * dx_frac), int(h * dy_frac)
|
| 68 |
+
return img.transform(
|
| 69 |
+
(w, h), Image.AFFINE, (1, 0, -dx, 0, 1, -dy), resample=Image.BICUBIC
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _transform_for_key(img: Image.Image, key: str) -> Image.Image:
|
| 74 |
+
if key in ("top_down", "birds_eye"):
|
| 75 |
+
return _tilt(img, top_inset=0.0, bottom_inset=0.20 if key == "top_down" else 0.12)
|
| 76 |
+
if key == "worms_eye":
|
| 77 |
+
return _tilt(img, top_inset=0.16, bottom_inset=0.0)
|
| 78 |
+
if key == "rotate_left_45":
|
| 79 |
+
return img.rotate(45, resample=Image.BICUBIC, expand=False)
|
| 80 |
+
if key == "rotate_right_45":
|
| 81 |
+
return img.rotate(-45, resample=Image.BICUBIC, expand=False)
|
| 82 |
+
if key == "rotate_left_90":
|
| 83 |
+
return img.rotate(90, resample=Image.BICUBIC, expand=False)
|
| 84 |
+
if key == "rotate_right_90":
|
| 85 |
+
return img.rotate(-90, resample=Image.BICUBIC, expand=False)
|
| 86 |
+
if key == "close_up":
|
| 87 |
+
return _zoom(img, 1.45)
|
| 88 |
+
if key == "wide_angle":
|
| 89 |
+
return _zoom(img, 0.7)
|
| 90 |
+
if key == "move_left":
|
| 91 |
+
return _shift(img, dx_frac=0.15, dy_frac=0.0)
|
| 92 |
+
if key == "move_right":
|
| 93 |
+
return _shift(img, dx_frac=-0.15, dy_frac=0.0)
|
| 94 |
+
if key == "move_forward":
|
| 95 |
+
return _zoom(img, 1.2)
|
| 96 |
+
if key == "move_down":
|
| 97 |
+
return _shift(img, dx_frac=0.0, dy_frac=-0.15)
|
| 98 |
+
return img # original / unknown
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class GeometricBackend(ImageEditBackend):
|
| 102 |
+
"""Token-free, CPU-only viewpoint approximation using Pillow transforms."""
|
| 103 |
+
|
| 104 |
+
source = "geometric_fallback"
|
| 105 |
+
|
| 106 |
+
def __init__(self, image_size: int = 512) -> None:
|
| 107 |
+
self.image_size = image_size
|
| 108 |
+
# Reverse map: bilingual prompt -> preset key.
|
| 109 |
+
self._prompt_to_key: Dict[str, str] = {
|
| 110 |
+
prompt: key for key, (_label, prompt) in ANGLE_PRESETS.items()
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
def prepare(self) -> None:
|
| 114 |
+
return None
|
| 115 |
+
|
| 116 |
+
def edit(
|
| 117 |
+
self,
|
| 118 |
+
image: Image.Image,
|
| 119 |
+
prompt: str,
|
| 120 |
+
seed: int,
|
| 121 |
+
num_inference_steps: int,
|
| 122 |
+
true_guidance_scale: float,
|
| 123 |
+
) -> Image.Image:
|
| 124 |
+
img = image.convert("RGB")
|
| 125 |
+
if not prompt or not prompt.strip():
|
| 126 |
+
return img
|
| 127 |
+
key = self._prompt_to_key.get(prompt.strip(), "original")
|
| 128 |
+
return _transform_for_key(img, key)
|