Vijish commited on
Commit
1ad1081
Β·
verified Β·
1 Parent(s): 7907b06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -18,6 +18,16 @@ def clear_memory():
18
  torch.cuda.empty_cache()
19
  torch.cuda.ipc_collect()
20
 
 
 
 
 
 
 
 
 
 
 
21
  # Global variable definitions
22
  controlnet_pipe = None
23
  reference_pipe = None
@@ -298,6 +308,7 @@ def generate_images_with_progress(prompt, negative_prompt, batch_count, use_cont
298
 
299
  preprocessed_images = []
300
  for img in tqdm(control_images, desc="Preprocessing images"):
 
301
  if controlnet_type == "Canny":
302
  preprocessed_images.append(preprocess_canny(img))
303
  elif controlnet_type == "Depth":
@@ -356,6 +367,7 @@ def load_images_from_folder(folder_path):
356
  if filename.endswith(('.png', '.jpg', '.jpeg')):
357
  img_path = os.path.join(folder_path, filename)
358
  img = Image.open(img_path).convert("RGB")
 
359
  images.append((filename, img))
360
  return images
361
 
@@ -460,7 +472,7 @@ with gr.Blocks() as demo:
460
  else:
461
  if not batch_images_input:
462
  raise ValueError("No images uploaded for batch processing.")
463
- selected_images = [Image.open(img).convert("RGB") for img in batch_images_input]
464
  else:
465
  selected_images = [img[1] for img in selected_folder_images]
466
  return generate_images_with_progress(prompt, negative_prompt, batch_count, use_controlnet, controlnet_type, mode, selected_images, progress)
@@ -486,4 +498,4 @@ with gr.Blocks() as demo:
486
  if __name__ == "__main__":
487
  # Your Gradio interface setup here
488
  demo.launch(auth=("roland", "roland"), debug=True)
489
- clear_memory()
 
18
  torch.cuda.empty_cache()
19
  torch.cuda.ipc_collect()
20
 
21
+ # Function to resize images while preserving the aspect ratio
22
+ def resize_image(image, max_size=1024):
23
+ width, height = image.size
24
+ if max(width, height) > max_size:
25
+ ratio = max_size / max(width, height)
26
+ new_width = int(width * ratio)
27
+ new_height = int(height * ratio)
28
+ image = image.resize((new_width, new_height), Image.ANTIALIAS)
29
+ return image
30
+
31
  # Global variable definitions
32
  controlnet_pipe = None
33
  reference_pipe = None
 
308
 
309
  preprocessed_images = []
310
  for img in tqdm(control_images, desc="Preprocessing images"):
311
+ img = resize_image(img) # Resize the image before preprocessing
312
  if controlnet_type == "Canny":
313
  preprocessed_images.append(preprocess_canny(img))
314
  elif controlnet_type == "Depth":
 
367
  if filename.endswith(('.png', '.jpg', '.jpeg')):
368
  img_path = os.path.join(folder_path, filename)
369
  img = Image.open(img_path).convert("RGB")
370
+ img = resize_image(img) # Resize the image before adding to the list
371
  images.append((filename, img))
372
  return images
373
 
 
472
  else:
473
  if not batch_images_input:
474
  raise ValueError("No images uploaded for batch processing.")
475
+ selected_images = [resize_image(Image.open(img).convert("RGB")) for img in batch_images_input]
476
  else:
477
  selected_images = [img[1] for img in selected_folder_images]
478
  return generate_images_with_progress(prompt, negative_prompt, batch_count, use_controlnet, controlnet_type, mode, selected_images, progress)
 
498
  if __name__ == "__main__":
499
  # Your Gradio interface setup here
500
  demo.launch(auth=("roland", "roland"), debug=True)
501
+ clear_memory()