Spaces:
Running on Zero
Running on Zero
Upload: EXIF-rotate + center-crop non-square; add usage notes
Browse files- app_demo.py +25 -1
app_demo.py
CHANGED
|
@@ -72,7 +72,19 @@ class WildRayZerDemo:
|
|
| 72 |
print(f" Motion mask predictor: {self.model.motion_mask_predictor is not None}")
|
| 73 |
|
| 74 |
def preprocess_image(self, image_input):
|
| 75 |
-
"""Preprocess image to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if isinstance(image_input, tuple):
|
| 77 |
image_input = image_input[0]
|
| 78 |
|
|
@@ -83,9 +95,21 @@ class WildRayZerDemo:
|
|
| 83 |
else:
|
| 84 |
pil_image = Image.fromarray(image_input)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if pil_image.mode != "RGB":
|
| 87 |
pil_image = pil_image.convert("RGB")
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
pil_image = pil_image.resize((256, 256), Image.LANCZOS)
|
| 90 |
img_array = np.array(pil_image) / 255.0
|
| 91 |
img_tensor = torch.from_numpy(img_array).permute(2, 0, 1).float()
|
|
|
|
| 72 |
print(f" Motion mask predictor: {self.model.motion_mask_predictor is not None}")
|
| 73 |
|
| 74 |
def preprocess_image(self, image_input):
|
| 75 |
+
"""Preprocess image to the 256x256 RGB tensor the model expects.
|
| 76 |
+
|
| 77 |
+
Applied in order:
|
| 78 |
+
1. Open from whatever Gradio hands us (path, PIL.Image, or numpy).
|
| 79 |
+
2. Respect EXIF orientation (iPhone photos come in rotated).
|
| 80 |
+
3. Convert to RGB (handles RGBA, grayscale, CMYK, palette).
|
| 81 |
+
4. *Center-crop to a square* — the model was trained on square crops.
|
| 82 |
+
Naive `.resize((256, 256))` squishes 16:9 / portrait images.
|
| 83 |
+
5. Resize to 256x256 with LANCZOS.
|
| 84 |
+
6. Normalize to [0, 1] and return as CHW float tensor.
|
| 85 |
+
"""
|
| 86 |
+
from PIL import ImageOps
|
| 87 |
+
|
| 88 |
if isinstance(image_input, tuple):
|
| 89 |
image_input = image_input[0]
|
| 90 |
|
|
|
|
| 95 |
else:
|
| 96 |
pil_image = Image.fromarray(image_input)
|
| 97 |
|
| 98 |
+
# iPhone / camera EXIF orientation — rotate in-place to the viewer's
|
| 99 |
+
# orientation before any geometric ops.
|
| 100 |
+
pil_image = ImageOps.exif_transpose(pil_image)
|
| 101 |
+
|
| 102 |
if pil_image.mode != "RGB":
|
| 103 |
pil_image = pil_image.convert("RGB")
|
| 104 |
|
| 105 |
+
# Center-crop to square (matches training's center_crop=true).
|
| 106 |
+
w, h = pil_image.size
|
| 107 |
+
if w != h:
|
| 108 |
+
side = min(w, h)
|
| 109 |
+
left = (w - side) // 2
|
| 110 |
+
top = (h - side) // 2
|
| 111 |
+
pil_image = pil_image.crop((left, top, left + side, top + side))
|
| 112 |
+
|
| 113 |
pil_image = pil_image.resize((256, 256), Image.LANCZOS)
|
| 114 |
img_array = np.array(pil_image) / 255.0
|
| 115 |
img_tensor = torch.from_numpy(img_array).permute(2, 0, 1).float()
|