Skip to content

Commit 267c0e6

Browse files
committed
wip border stripes fix
from #74 sets border = 'smart' by default
1 parent cb33994 commit 267c0e6

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

scripts/deforum_helpers/animation.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,28 @@ def anim_frame_warp_2d(prev_img_cv2, args, anim_args, keys, frame_idx):
247247
else:
248248
xform = np.matmul(rot_mat, trans_mat)
249249

250-
return cv2.warpPerspective(
251-
prev_img_cv2,
252-
xform,
253-
(prev_img_cv2.shape[1], prev_img_cv2.shape[0]),
254-
borderMode=cv2.BORDER_WRAP if anim_args.border == 'wrap' else cv2.BORDER_REPLICATE
255-
)
250+
borderMode = cv2.BORDER_CONSTANT #zeros
251+
252+
if anim_args.border == 'wrap':
253+
borderMode = cv2.BORDER_WRAP
254+
elif anim_args.border == 'replicate':
255+
borderMode = cv2.BORDER_REPLICATE
256+
257+
if borderMode == 'smart':
258+
return cv2.warpPerspective(
259+
prev_img_cv2,
260+
xform,
261+
(prev_img_cv2.shape[1], prev_img_cv2.shape[0]),
262+
borderMode=borderMode,
263+
borderValue=(0, 0, 0,),
264+
)
265+
else:
266+
return cv2.warpPerspective(
267+
prev_img_cv2,
268+
xform,
269+
(prev_img_cv2.shape[1], prev_img_cv2.shape[0]),
270+
borderMode=borderMode,
271+
)
256272

257273
def anim_frame_warp_3d(device, prev_img_cv2, depth, anim_args, keys, frame_idx):
258274
TRANSLATION_SCALE = 1.0/200.0 # matches Disco
@@ -305,7 +321,7 @@ def transform_image_3d(device, prev_img_cv2, depth_tensor, rot_mat, translate, a
305321
image_tensor.add(1/512 - 0.0001).unsqueeze(0),
306322
offset_coords_2d,
307323
mode=anim_args.sampling_mode,
308-
padding_mode=anim_args.padding_mode,
324+
padding_mode=anim_args.padding_mode if anim_args.border is not 'smart' else 'zeros', # border overrides padding_mode without changing the settings saved
309325
align_corners=False
310326
)
311327

scripts/deforum_helpers/args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def DeforumAnimArgs():
2222
#@markdown ####**Animation:**
2323
animation_mode = '2D' #@param ['None', '2D', '3D', 'Video Input', 'Interpolation'] {type:'string'}
2424
max_frames = 120 #@param {type:"number"}
25-
border = 'replicate' #@param ['wrap', 'replicate', 'smart'] {type:'string'}
25+
border = 'smart' #@param ['wrap', 'replicate', 'smart'] {type:'string'}
2626

2727
#@markdown ####**Motion Parameters:**
2828
angle = "0:(0)"#@param {type:"string"}
@@ -327,7 +327,7 @@ def show_vid():
327327
with gr.Row():
328328
animation_mode = gr.Dropdown(label="animation_mode", choices=['2D', '3D', 'Video Input', 'Interpolation'], value=da.animation_mode, type="value", elem_id="animation_mode", interactive=True)
329329
max_frames = gr.Number(label="max_frames", value=da.max_frames, interactive=True, precision=0)
330-
border = gr.Dropdown(label="border", choices=['replicate', 'wrap'], value=da.border, type="value", elem_id="border", interactive=True)
330+
border = gr.Dropdown(label="border", choices=['replicate', 'wrap', 'smart'], value=da.border, type="value", elem_id="border", interactive=True)
331331

332332

333333
i10 = gr.HTML("<p style=\"margin-bottom:0.75em\">Motion parameters:</p>")

scripts/deforum_helpers/generate.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,39 @@ def generate(args, anim_args, root, frame = 0, return_sample=False):
160160
open_cv_image = sample_to_cv2(args.init_sample)
161161
img = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB)
162162
init_image = Image.fromarray(img)
163+
164+
if anim_args.border == 'smart':
165+
166+
# Inpaint changed parts of the image
167+
# that's, to say, zeros we got after the transformations
168+
169+
mask_image = init_image.convert('L')
170+
for x in range(mask_image.width):
171+
for y in range(mask_image.height):
172+
if mask_image.getpixel((x,y)) < 1:
173+
mask_image.putpixel((x,y), 255)
174+
else:
175+
mask_image.putpixel((x,y), 0)
176+
177+
mask = prepare_mask(mask_image,
178+
(args.W, args.H),
179+
args.mask_contrast_adjust,
180+
args.mask_brightness_adjust,
181+
invert_mask=False)
182+
183+
p.inpainting_fill = args.fill # need to come up with better name.
184+
p.inpaint_full_res= args.full_res_mask
185+
p.inpaint_full_res_padding = args.full_res_mask_padding
186+
187+
p.init_images = [init_image]
188+
p.image_mask = mask
189+
190+
processed = processing.process_images(p)
191+
192+
init_image = processed.images[0].convert('RGB')
193+
p.image_mask = None
194+
mask_image = None
195+
processed = None
163196
elif args.use_init and args.init_image != None and args.init_image != '':
164197
init_image, mask_image = load_img(args.init_image,
165198
shape=(args.W, args.H),
@@ -218,7 +251,7 @@ def generate(args, anim_args, root, frame = 0, return_sample=False):
218251
assert not ( (args.use_mask and args.overlay_mask) and (args.init_sample is None and init_image is None)), "Need an init image when use_mask == True and overlay_mask == True"
219252

220253
p.init_images = [init_image]
221-
p.image_mask = mask
254+
p.image_mask = mask_image
222255

223256
print(f"seed={p.seed}; subseed={p.subseed}; subseed_strength={p.subseed_strength}; denoising_strength={p.denoising_strength}; steps={p.steps}; cfg_scale={p.cfg_scale}")
224257
processed = processing.process_images(p)

0 commit comments

Comments
 (0)