reedmayhew commited on
Commit
9fbd930
1 Parent(s): 65b549e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -44,11 +44,11 @@ def upscale_chunk(chunk, model, processor, device):
44
  return Image.fromarray(output_image)
45
 
46
  @spaces.GPU
47
- def main(image, model_choice, save_as_jpg=True):
48
  # Resize the input image
49
  image = resize_image(image)
50
 
51
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
52
 
53
  model_paths = {
54
  "Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
@@ -58,20 +58,24 @@ def main(image, model_choice, save_as_jpg=True):
58
  processor = AutoImageProcessor.from_pretrained(model_paths[model_choice])
59
  model = Swin2SRForImageSuperResolution.from_pretrained(model_paths[model_choice]).to(device)
60
 
61
- # Split the image into chunks
62
- chunks = split_image(image)
63
-
64
- # Process each chunk
65
- upscaled_chunks = []
66
- for chunk, x, y in chunks:
67
- upscaled_chunk = upscale_chunk(chunk, model, processor, device)
68
- # Remove 32 pixels from bottom and right edges
69
- upscaled_chunk = upscaled_chunk.crop((0, 0, upscaled_chunk.width - 32, upscaled_chunk.height - 32))
70
- upscaled_chunks.append((upscaled_chunk, x * 4, y * 4)) # Multiply coordinates by 4 due to 4x upscaling
71
-
72
- # Stitch the chunks back together
73
- final_size = (image.width * 4 - 32, image.height * 4 - 32) # Adjust for removed pixels
74
- upscaled_image = stitch_image(upscaled_chunks, final_size)
 
 
 
 
75
 
76
  if save_as_jpg:
77
  upscaled_image.save("upscaled_image.jpg", quality=95)
@@ -80,9 +84,9 @@ def main(image, model_choice, save_as_jpg=True):
80
  upscaled_image.save("upscaled_image.png")
81
  return "upscaled_image.png"
82
 
83
- def gradio_interface(image, model_choice, save_as_jpg):
84
  try:
85
- result = main(image, model_choice, save_as_jpg)
86
  return result, None
87
  except Exception as e:
88
  return None, str(e)
@@ -97,13 +101,15 @@ interface = gr.Interface(
97
  value="PSNR Match (Recommended)"
98
  ),
99
  gr.Checkbox(value=True, label="Save as JPEG"),
 
 
100
  ],
101
  outputs=[
102
  gr.File(label="Download Upscaled Image"),
103
  gr.Textbox(label="Error Message", visible=True)
104
  ],
105
  title="Image Upscaler",
106
- description="Upload an image, select a model, and upscale it. Images larger than 2048x2048 will be resized while maintaining aspect ratio. The image will be processed in 512x512 pixel chunks for efficient handling.",
107
  )
108
 
109
  interface.launch()
 
44
  return Image.fromarray(output_image)
45
 
46
  @spaces.GPU
47
+ def main(image, model_choice, save_as_jpg=True, use_tiling=True, auto_cpu=True):
48
  # Resize the input image
49
  image = resize_image(image)
50
 
51
+ device = torch.device("cuda" if torch.cuda.is_available() and not auto_cpu else "cpu")
52
 
53
  model_paths = {
54
  "Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
 
58
  processor = AutoImageProcessor.from_pretrained(model_paths[model_choice])
59
  model = Swin2SRForImageSuperResolution.from_pretrained(model_paths[model_choice]).to(device)
60
 
61
+ if use_tiling:
62
+ # Split the image into chunks
63
+ chunks = split_image(image)
64
+
65
+ # Process each chunk
66
+ upscaled_chunks = []
67
+ for chunk, x, y in chunks:
68
+ upscaled_chunk = upscale_chunk(chunk, model, processor, device)
69
+ # Remove 32 pixels from bottom and right edges
70
+ upscaled_chunk = upscaled_chunk.crop((0, 0, upscaled_chunk.width - 32, upscaled_chunk.height - 32))
71
+ upscaled_chunks.append((upscaled_chunk, x * 4, y * 4)) # Multiply coordinates by 4 due to 4x upscaling
72
+
73
+ # Stitch the chunks back together
74
+ final_size = (image.width * 4 - 32, image.height * 4 - 32) # Adjust for removed pixels
75
+ upscaled_image = stitch_image(upscaled_chunks, final_size)
76
+ else:
77
+ # Process the entire image at once
78
+ upscaled_image = upscale_chunk(image, model, processor, device)
79
 
80
  if save_as_jpg:
81
  upscaled_image.save("upscaled_image.jpg", quality=95)
 
84
  upscaled_image.save("upscaled_image.png")
85
  return "upscaled_image.png"
86
 
87
+ def gradio_interface(image, model_choice, save_as_jpg, use_tiling, auto_cpu):
88
  try:
89
+ result = main(image, model_choice, save_as_jpg, use_tiling, auto_cpu)
90
  return result, None
91
  except Exception as e:
92
  return None, str(e)
 
101
  value="PSNR Match (Recommended)"
102
  ),
103
  gr.Checkbox(value=True, label="Save as JPEG"),
104
+ gr.Checkbox(value=True, label="Use Tiling"),
105
+ gr.Checkbox(value=True, label="Auto CPU"),
106
  ],
107
  outputs=[
108
  gr.File(label="Download Upscaled Image"),
109
  gr.Textbox(label="Error Message", visible=True)
110
  ],
111
  title="Image Upscaler",
112
+ description="Upload an image, select a model, and upscale it. Images larger than 2048x2048 will be resized while maintaining aspect ratio. Use tiling for efficient processing of large images. Auto CPU will use CPU if GPU is not available.",
113
  )
114
 
115
  interface.launch()