File size: 12,025 Bytes
6fce8cc
 
6678b47
 
 
11c0865
6678b47
 
 
 
 
 
 
 
 
 
6fce8cc
6678b47
 
 
 
 
 
 
11c0865
 
 
6678b47
 
 
722e880
6678b47
 
 
 
 
 
722e880
 
 
 
 
6678b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
722e880
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
 
11c0865
6678b47
 
 
 
11c0865
6678b47
11c0865
6678b47
 
 
 
 
11c0865
6678b47
11c0865
6678b47
 
 
 
11c0865
6678b47
 
11c0865
 
6678b47
 
 
722e880
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
722e880
 
 
 
6678b47
 
722e880
 
 
6678b47
 
722e880
6678b47
 
 
722e880
 
6678b47
 
 
11c0865
6678b47
 
11c0865
 
6678b47
 
 
722e880
 
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
 
 
 
 
 
 
11c0865
6678b47
722e880
 
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
4b2ec41
6678b47
 
 
 
 
 
 
 
11c0865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
 
11c0865
6678b47
 
 
 
 
 
 
 
 
 
 
 
 
 
11c0865
 
 
 
 
 
 
 
 
 
 
6678b47
4b2ec41
6678b47
11c0865
6678b47
 
 
 
 
 
 
11c0865
 
 
 
6678b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c0865
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6678b47
 
 
11c0865
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import spaces

import os
import pickle
from time import perf_counter
import tempfile

import cv2
import gradio as gr
import numpy as np
import torch
from PIL import Image
from diffusers import AutoPipelineForInpainting, AutoencoderTiny, LCMScheduler

from utils.drag import bi_warp



__all__ = [
    'clear_all', 'resize',
    'visualize_user_drag', 'preview_out_image', 'inpaint',
    'add_point', 'undo_point', 'clear_point',
]

# Global variables for lazy loading
pipe = None

# UI functions
def clear_all(length):
    """Reset UI by clearing all input images and parameters."""
    return (gr.Image(value=None, height=length, width=length),) * 3 + ([], 5, None)

def resize(canvas, gen_length, canvas_length):
    """Resize canvas while maintaining aspect ratio."""
    if not canvas:
        return (gr.Image(value=None, width=canvas_length, height=canvas_length),) * 3

    result = process_canvas(canvas)
    if result[0] is None:  # Check if image is None
        return (gr.Image(value=None, width=canvas_length, height=canvas_length),) * 3
    
    image = result[0]
    aspect_ratio = image.shape[1] / image.shape[0]
    is_landscape = aspect_ratio >= 1

    new_dims = (
        (gen_length, round(gen_length / aspect_ratio / 8) * 8) if is_landscape
        else (round(gen_length * aspect_ratio / 8) * 8, gen_length)
    )
    canvas_dims = (
        (canvas_length, round(canvas_length / aspect_ratio)) if is_landscape
        else (round(canvas_length * aspect_ratio), canvas_length)
    )

    return (gr.Image(value=cv2.resize(image, new_dims), width=canvas_dims[0], height=canvas_dims[1]),) * 3

def process_canvas(canvas):
    """Extracts the image (H, W, 3) and the mask (H, W) from a Gradio canvas object."""
    # Handle None canvas
    if canvas is None:
        return None, None
    
    # Handle new ImageEditor format
    if isinstance(canvas, dict):
        if 'background' in canvas and 'layers' in canvas:
            # New ImageEditor format
            if canvas["background"] is None:
                return None, None
            image = canvas["background"].copy()
            
            # Ensure image is 3-channel RGB
            if len(image.shape) == 3 and image.shape[2] == 4:
                image = image[:, :, :3]  # Remove alpha channel
            elif len(image.shape) == 2:
                image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
            
            # Try to extract mask from layers
            mask = np.zeros(image.shape[:2], dtype=np.uint8)
            if canvas["layers"]:
                for layer in canvas["layers"]:
                    if isinstance(layer, np.ndarray) and len(layer.shape) >= 2:
                        layer_mask = np.uint8(layer[:, :, 0] > 0) if len(layer.shape) == 3 else np.uint8(layer > 0)
                        mask = np.logical_or(mask, layer_mask).astype(np.uint8)
        elif 'image' in canvas and 'mask' in canvas:
            # Old format
            if canvas["image"] is None:
                return None, None
            image = canvas["image"].copy()
            
            # Ensure image is 3-channel RGB
            if len(image.shape) == 3 and image.shape[2] == 4:
                image = image[:, :, :3]  # Remove alpha channel
            elif len(image.shape) == 2:
                image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
            
            mask = np.uint8(canvas["mask"][:, :, 0] > 0).copy() if canvas["mask"] is not None else np.zeros(image.shape[:2], dtype=np.uint8)
        else:
            # Fallback
            return None, None
    else:
        # Direct numpy array
        if canvas is None:
            return None, None
        image = canvas.copy() if isinstance(canvas, np.ndarray) else np.array(canvas)
        
        # Ensure image is 3-channel RGB
        if len(image.shape) == 3 and image.shape[2] == 4:
            image = image[:, :, :3]  # Remove alpha channel
        elif len(image.shape) == 2:
            image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
        
        mask = np.zeros(image.shape[:2], dtype=np.uint8)
    
    return image, mask

# Point manipulation functions
def add_point(canvas, points, inpaint_ks, evt: gr.SelectData):
    """Add selected point to points list and update image."""
    if canvas is None:
        return None 
    points.append(evt.index)
    return visualize_user_drag(canvas, points)

def undo_point(canvas, points, inpaint_ks):
    """Remove last point and update image."""
    if canvas is None:
        return None 
    if len(points) > 0:
        points.pop()
    return visualize_user_drag(canvas, points)

def clear_point(canvas, points, inpaint_ks):
    """Clear all points and update image."""
    if canvas is None:
        return None 
    points.clear()
    return visualize_user_drag(canvas, points)

# Visualization tools
def visualize_user_drag(canvas, points):
    """Visualize control points and motion vectors on the input image."""
    if canvas is None:
        return None
    
    result = process_canvas(canvas)
    if result[0] is None:  # Check if image is None
        return None
    
    image, mask = result

    # Ensure image is uint8 and 3-channel
    if image.dtype != np.uint8:
        image = (image * 255).astype(np.uint8) if image.max() <= 1.0 else image.astype(np.uint8)
    
    if len(image.shape) != 3 or image.shape[2] != 3:
        return None

    # Apply colored mask overlay
    result_img = image.copy()
    if np.any(mask == 1):
        result_img[mask == 1] = [255, 0, 0]  # Red color
        image = cv2.addWeighted(result_img, 0.3, image, 0.7, 0)
    
    # Draw mask outline
    if np.any(mask > 0):
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(image, contours, -1, (255, 255, 255), 2)
    
    # Draw control points and motion vectors
    prev_point = None
    for idx, point in enumerate(points, 1):
        if idx % 2 == 0:
            cv2.circle(image, tuple(point), 10, (0, 0, 255), -1)  # End point
            if prev_point is not None:
                cv2.arrowedLine(image, prev_point, point, (255, 255, 255), 4, tipLength=0.5)
        else:
            cv2.circle(image, tuple(point), 10, (255, 0, 0), -1)  # Start point
            prev_point = point
    
    return image

def preview_out_image(canvas, points, inpaint_ks):
    """Preview warped image result and generate inpainting mask."""
    if canvas is None:
        return None, None
    
    result = process_canvas(canvas)
    if result[0] is None:  # Check if image is None
        return None, None
    
    image, mask = result
    
    # Ensure image is uint8 and 3-channel
    if image.dtype != np.uint8:
        image = (image * 255).astype(np.uint8) if image.max() <= 1.0 else image.astype(np.uint8)
    
    if len(image.shape) != 3 or image.shape[2] != 3:
        return image, None
    
    if len(points) < 2:
        return image, None
    
    # ensure H, W divisible by 8 and longer edge 512
    shapes_valid = all(s % 8 == 0 for s in mask.shape + image.shape[:2])
    size_valid = all(max(x.shape[:2] if len(x.shape) > 2 else x.shape) == 512 for x in (image, mask))
    if not (shapes_valid and size_valid):
        gr.Warning('Click Resize Image Button first.')
        return image, None
    
    try:
        handle_pts, target_pts, inpaint_mask = bi_warp(mask, points, inpaint_ks)
        image[target_pts[:, 1], target_pts[:, 0]] = image[handle_pts[:, 1], handle_pts[:, 0]]

        # Add grid pattern to highlight inpainting regions
        background = np.ones_like(mask) * 255
        background[::10] = background[:, ::10] = 0
        image = np.where(inpaint_mask[..., np.newaxis]==1, background[..., np.newaxis], image)
        
        return image, (inpaint_mask * 255).astype(np.uint8)
    except Exception as e:
        gr.Warning(f"Preview failed: {str(e)}")
        return image, None

# Inpaint tools
@spaces.GPU
def setup_pipeline(device='cuda', model_version='v1-5'):
    """Initialize optimized inpainting pipeline with specified model configuration."""
    MODEL_CONFIGS = {
        'v1-5': ('runwayml/stable-diffusion-inpainting', 'latent-consistency/lcm-lora-sdv1-5', 'madebyollin/taesd'),
        'xl': ('diffusers/stable-diffusion-xl-1.0-inpainting-0.1', 'latent-consistency/lcm-lora-sdxl', 'madebyollin/taesdxl')
    }
    model_id, lora_id, vae_id = MODEL_CONFIGS[model_version]

    # Check if CUDA is available, fallback to CPU
    if not torch.cuda.is_available():
        device = 'cpu'
        torch_dtype = torch.float32
        variant = None
    else:
        torch_dtype = torch.float16
        variant = "fp16"

    gr.Info('Loading inpainting pipeline...')
    pipe = AutoPipelineForInpainting.from_pretrained(
        model_id, 
        torch_dtype=torch_dtype, 
        variant=variant, 
        safety_checker=None
    )
    pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
    pipe.load_lora_weights(lora_id)
    pipe.fuse_lora()
    pipe.vae = AutoencoderTiny.from_pretrained(vae_id, torch_dtype=torch_dtype)
    pipe = pipe.to(device)
    
    # Pre-compute prompt embeddings during setup
    if model_version == 'v1-5':
        pipe.cached_prompt_embeds = pipe.encode_prompt(
            '', device=device, num_images_per_prompt=1,
            do_classifier_free_guidance=False)[0]
    else:
        pipe.cached_prompt_embeds, pipe.cached_pooled_prompt_embeds = pipe.encode_prompt(
            '', device=device, num_images_per_prompt=1,
            do_classifier_free_guidance=False)[0::2]
            
    return pipe

def get_pipeline():
    """Lazy load pipeline only when needed."""
    global pipe
    if pipe is None:
        device = 'cuda' if torch.cuda.is_available() else 'cpu'
        pipe = setup_pipeline(device=device, model_version='v1-5')
        if device == 'cuda':
            pipe.cached_prompt_embeds = pipe.encode_prompt('', 'cuda', 1, False)[0]
        else:
            pipe.cached_prompt_embeds = pipe.encode_prompt('', 'cpu', 1, False)[0]
    return pipe

@spaces.GPU
def inpaint(image, inpaint_mask):
    """Perform efficient inpainting on masked regions using Stable Diffusion."""
    if image is None:
        return None

    if inpaint_mask is None:
        return image
    
    start = perf_counter()
    
    # Get pipeline (lazy loading)
    pipe = get_pipeline()
    
    pipe_id = 'xl' if 'xl' in pipe.config._name_or_path else 'v1-5'
    inpaint_strength = 0.99 if pipe_id == 'xl' else 1.0

    # Convert inputs to PIL
    image_pil = Image.fromarray(image)
    inpaint_mask_pil = Image.fromarray(inpaint_mask) 

    width, height = inpaint_mask_pil.size
    if width % 8 != 0 or height % 8 != 0:
        width, height = round(width / 8) * 8, round(height / 8) * 8
        image_pil = image_pil.resize((width, height))
        image = np.array(image_pil)
        inpaint_mask_pil = inpaint_mask_pil.resize((width, height), Image.NEAREST)
        inpaint_mask = np.array(inpaint_mask_pil)

    # Common pipeline parameters
    common_params = {
        'image': image_pil,
        'mask_image': inpaint_mask_pil,
        'height': height,
        'width': width,
        'guidance_scale': 1.0,
        'num_inference_steps': 8,
        'strength': inpaint_strength,
        'output_type': 'np'
    }

    # Run pipeline
    try:
        if pipe_id == 'v1-5':
            inpainted = pipe(
                prompt_embeds=pipe.cached_prompt_embeds,
                **common_params
            ).images[0]
        else:
            inpainted = pipe(
                prompt_embeds=pipe.cached_prompt_embeds,
                pooled_prompt_embeds=pipe.cached_pooled_prompt_embeds,
                **common_params
            ).images[0]
    except Exception as e:
        gr.Warning(f"Inpainting failed: {str(e)}")
        return image

    # Post-process results
    inpaint_mask = (inpaint_mask[..., np.newaxis] / 255).astype(np.uint8)
    return (inpainted * 255).astype(np.uint8) * inpaint_mask + image * (1 - inpaint_mask)