Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,93 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
-
import cv2
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from torchvision import transforms
|
| 7 |
-
from transformers import SegformerForSemanticSegmentation, AutoImageProcessor
|
| 8 |
-
|
| 9 |
-
# Device
|
| 10 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
-
|
| 12 |
-
# Load model and processor once
|
| 13 |
-
processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b2-finetuned-ade-512-512")
|
| 14 |
-
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b2-finetuned-ade-512-512").to(device)
|
| 15 |
-
|
| 16 |
-
def process(room_img, tile_img):
|
| 17 |
-
room_img = room_img.convert("RGB")
|
| 18 |
-
tile_img = tile_img.convert("RGB")
|
| 19 |
-
room_np = np.array(room_img)
|
| 20 |
-
|
| 21 |
-
# Segmentation
|
| 22 |
-
inputs = processor(images=room_img, return_tensors="pt").to(device)
|
| 23 |
-
with torch.no_grad():
|
| 24 |
-
outputs = model(**inputs)
|
| 25 |
-
segmentation = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
|
| 26 |
-
segmentation_resized = cv2.resize(segmentation.astype(np.uint8), (room_np.shape[1], room_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 27 |
-
|
| 28 |
-
# Mask for floor (ADE20K class index 3)
|
| 29 |
-
floor_class_index = 3
|
| 30 |
-
mask_bin = (segmentation_resized == floor_class_index).astype(np.uint8)
|
| 31 |
-
|
| 32 |
-
# Largest contour
|
| 33 |
-
contours, _ = cv2.findContours(mask_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 34 |
-
if not contours:
|
| 35 |
-
return room_img, Image.fromarray(mask_bin*255), tile_img, room_img
|
| 36 |
-
contour = max(contours, key=cv2.contourArea)
|
| 37 |
-
if len(contour) < 4:
|
| 38 |
-
return room_img, Image.fromarray(mask_bin*255), tile_img, room_img
|
| 39 |
-
|
| 40 |
-
x, y, w, h = cv2.boundingRect(contour)
|
| 41 |
-
src_pts = np.array([[x, y + h], [x + w, y + h], [x + w, y], [x, y]], dtype=np.float32)
|
| 42 |
-
offset = h * 0.5
|
| 43 |
-
dst_pts = np.array([[x - offset, y + h], [x + w + offset, y + h], [x + w, y], [x, y]], dtype=np.float32)
|
| 44 |
-
H = cv2.getPerspectiveTransform(src_pts, dst_pts)
|
| 45 |
-
|
| 46 |
-
# Resize tile
|
| 47 |
-
target_tile_width = room_np.shape[1] // 10
|
| 48 |
-
tile_aspect_ratio = tile_img.height / tile_img.width
|
| 49 |
-
target_tile_height = int(target_tile_width * tile_aspect_ratio)
|
| 50 |
-
tile_resized = tile_img.resize((target_tile_width, target_tile_height), Image.LANCZOS)
|
| 51 |
-
tile_np = np.array(tile_resized)
|
| 52 |
-
|
| 53 |
-
# Tile texture
|
| 54 |
-
tile_h, tile_w = tile_np.shape[:2]
|
| 55 |
-
room_h, room_w = room_np.shape[:2]
|
| 56 |
-
reps_y = room_h // tile_h + 2
|
| 57 |
-
reps_x = room_w // tile_w + 2
|
| 58 |
-
tiled_texture = np.tile(tile_np, (reps_y, reps_x, 1))[:room_h, :room_w]
|
| 59 |
-
warped_texture = cv2.warpPerspective(tiled_texture, H, (room_w, room_h))
|
| 60 |
-
|
| 61 |
-
# Blend
|
| 62 |
-
room_float = room_np.astype(np.float32) / 255.0
|
| 63 |
-
texture_float = warped_texture.astype(np.float32) / 255.0
|
| 64 |
-
room_gray = cv2.cvtColor(room_float, cv2.COLOR_RGB2GRAY)
|
| 65 |
-
lighting = np.stack([room_gray]*3, axis=-1)
|
| 66 |
-
lighting = np.clip(lighting * 1.2, 0, 1)
|
| 67 |
-
lit_texture = np.clip(texture_float * lighting, 0, 1)
|
| 68 |
-
mask_3ch = np.stack([mask_bin]*3, axis=-1)
|
| 69 |
-
blended = np.where(mask_3ch == 1, lit_texture, room_float)
|
| 70 |
-
blended_img = (blended * 255).astype(np.uint8)
|
| 71 |
-
|
| 72 |
-
return Image.fromarray(room_np), Image.fromarray(mask_bin * 255), Image.fromarray(warped_texture), Image.fromarray(blended_img)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
demo = gr.Interface(
|
| 76 |
-
fn=process,
|
| 77 |
-
inputs=[gr.Image(label="Room Image", type="pil"), gr.Image(label="Tile Image", type="pil")],
|
| 78 |
-
outputs=[
|
| 79 |
-
gr.Image(label="Original Room"),
|
| 80 |
-
gr.Image(label="Floor Mask"),
|
| 81 |
-
gr.Image(label="Warped Texture"),
|
| 82 |
-
gr.Image(label="Final Overlay")
|
| 83 |
-
],
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
from transformers import SegformerForSemanticSegmentation, AutoImageProcessor
|
| 8 |
+
|
| 9 |
+
# Device
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Load model and processor once
|
| 13 |
+
processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b2-finetuned-ade-512-512")
|
| 14 |
+
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b2-finetuned-ade-512-512").to(device)
|
| 15 |
+
|
| 16 |
+
def process(room_img, tile_img):
|
| 17 |
+
room_img = room_img.convert("RGB")
|
| 18 |
+
tile_img = tile_img.convert("RGB")
|
| 19 |
+
room_np = np.array(room_img)
|
| 20 |
+
|
| 21 |
+
# Segmentation
|
| 22 |
+
inputs = processor(images=room_img, return_tensors="pt").to(device)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
segmentation = outputs.logits.argmax(dim=1).squeeze().cpu().numpy()
|
| 26 |
+
segmentation_resized = cv2.resize(segmentation.astype(np.uint8), (room_np.shape[1], room_np.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 27 |
+
|
| 28 |
+
# Mask for floor (ADE20K class index 3)
|
| 29 |
+
floor_class_index = 3
|
| 30 |
+
mask_bin = (segmentation_resized == floor_class_index).astype(np.uint8)
|
| 31 |
+
|
| 32 |
+
# Largest contour
|
| 33 |
+
contours, _ = cv2.findContours(mask_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 34 |
+
if not contours:
|
| 35 |
+
return room_img, Image.fromarray(mask_bin*255), tile_img, room_img
|
| 36 |
+
contour = max(contours, key=cv2.contourArea)
|
| 37 |
+
if len(contour) < 4:
|
| 38 |
+
return room_img, Image.fromarray(mask_bin*255), tile_img, room_img
|
| 39 |
+
|
| 40 |
+
x, y, w, h = cv2.boundingRect(contour)
|
| 41 |
+
src_pts = np.array([[x, y + h], [x + w, y + h], [x + w, y], [x, y]], dtype=np.float32)
|
| 42 |
+
offset = h * 0.5
|
| 43 |
+
dst_pts = np.array([[x - offset, y + h], [x + w + offset, y + h], [x + w, y], [x, y]], dtype=np.float32)
|
| 44 |
+
H = cv2.getPerspectiveTransform(src_pts, dst_pts)
|
| 45 |
+
|
| 46 |
+
# Resize tile
|
| 47 |
+
target_tile_width = room_np.shape[1] // 10
|
| 48 |
+
tile_aspect_ratio = tile_img.height / tile_img.width
|
| 49 |
+
target_tile_height = int(target_tile_width * tile_aspect_ratio)
|
| 50 |
+
tile_resized = tile_img.resize((target_tile_width, target_tile_height), Image.LANCZOS)
|
| 51 |
+
tile_np = np.array(tile_resized)
|
| 52 |
+
|
| 53 |
+
# Tile texture
|
| 54 |
+
tile_h, tile_w = tile_np.shape[:2]
|
| 55 |
+
room_h, room_w = room_np.shape[:2]
|
| 56 |
+
reps_y = room_h // tile_h + 2
|
| 57 |
+
reps_x = room_w // tile_w + 2
|
| 58 |
+
tiled_texture = np.tile(tile_np, (reps_y, reps_x, 1))[:room_h, :room_w]
|
| 59 |
+
warped_texture = cv2.warpPerspective(tiled_texture, H, (room_w, room_h))
|
| 60 |
+
|
| 61 |
+
# Blend
|
| 62 |
+
room_float = room_np.astype(np.float32) / 255.0
|
| 63 |
+
texture_float = warped_texture.astype(np.float32) / 255.0
|
| 64 |
+
room_gray = cv2.cvtColor(room_float, cv2.COLOR_RGB2GRAY)
|
| 65 |
+
lighting = np.stack([room_gray]*3, axis=-1)
|
| 66 |
+
lighting = np.clip(lighting * 1.2, 0, 1)
|
| 67 |
+
lit_texture = np.clip(texture_float * lighting, 0, 1)
|
| 68 |
+
mask_3ch = np.stack([mask_bin]*3, axis=-1)
|
| 69 |
+
blended = np.where(mask_3ch == 1, lit_texture, room_float)
|
| 70 |
+
blended_img = (blended * 255).astype(np.uint8)
|
| 71 |
+
|
| 72 |
+
return Image.fromarray(room_np), Image.fromarray(mask_bin * 255), Image.fromarray(warped_texture), Image.fromarray(blended_img)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
demo = gr.Interface(
|
| 76 |
+
fn=process,
|
| 77 |
+
inputs=[gr.Image(label="Room Image", type="pil"), gr.Image(label="Tile Image", type="pil")],
|
| 78 |
+
outputs=[
|
| 79 |
+
gr.Image(label="Original Room"),
|
| 80 |
+
gr.Image(label="Floor Mask"),
|
| 81 |
+
gr.Image(label="Warped Texture"),
|
| 82 |
+
gr.Image(label="Final Overlay")
|
| 83 |
+
],
|
| 84 |
+
examples=[
|
| 85 |
+
["https://www.thespruce.com/thmb/GtlHim5EsWERYoVi62TnWpu6JTA=/5472x3648/filters:fill(auto,1)/GettyImages-9261821821-5c69c1b7c9e77c0001675a49.jpg", "https://renovlange.de/images/marbles/arabescato.jpg"],
|
| 86 |
+
["https://st.hzcdn.com/simgs/57717d160282e776_9-0651/home-design.jpg", "https://renovlange.de/images/marbles/grigio-cenere.jpg"]
|
| 87 |
+
],
|
| 88 |
+
title="Room Floor Tiler",
|
| 89 |
+
description="Upload a room image and a tile texture. The floor is automatically detected and overlaid with your selected tile using SegFormer and perspective warping."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.launch()
|