HikariDawn commited on
Commit
8f3d49d
1 Parent(s): 6d00ac8

feat: update queue

Browse files
Files changed (2) hide show
  1. app.py +11 -7
  2. test_code/inference.py +9 -2
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os, sys
2
  import cv2
 
3
  import gradio as gr
4
  import torch
5
  import numpy as np
@@ -47,15 +48,16 @@ def inference(img_path, model_name):
47
  generator = load_rrdb(weight_path, scale=2) # Directly use default way now
48
 
49
  else:
50
- raise gr.Error(error)
51
 
52
  generator = generator.to(dtype=weight_dtype)
53
 
54
 
55
  # In default, we will automatically use crop to match 4x size
56
  super_resolved_img = super_resolve_img(generator, img_path, output_path=None, weight_dtype=weight_dtype, crop_for_4x=True)
57
- save_image(super_resolved_img, "SR_result.png")
58
- outputs = cv2.imread("SR_result.png")
 
59
  outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)
60
 
61
  return outputs
@@ -70,14 +72,16 @@ if __name__ == '__main__':
70
 
71
  MARKDOWN = \
72
  """
73
- ## APISR: Anime Production Inspired Real-World Anime Super-Resolution (CVPR 2024)
74
-
75
  [GitHub](https://github.com/Kiteretsu77/APISR) | [Paper](https://arxiv.org/abs/2403.01598)
76
 
77
- If APISR is helpful for you, please help star the GitHub Repo. Thanks!
 
 
78
  """
79
 
80
- block = gr.Blocks().queue()
81
  with block:
82
  with gr.Row():
83
  gr.Markdown(MARKDOWN)
 
1
  import os, sys
2
  import cv2
3
+ import time
4
  import gradio as gr
5
  import torch
6
  import numpy as np
 
48
  generator = load_rrdb(weight_path, scale=2) # Directly use default way now
49
 
50
  else:
51
+ raise gr.Error("We don't support such Model")
52
 
53
  generator = generator.to(dtype=weight_dtype)
54
 
55
 
56
  # In default, we will automatically use crop to match 4x size
57
  super_resolved_img = super_resolve_img(generator, img_path, output_path=None, weight_dtype=weight_dtype, crop_for_4x=True)
58
+ store_name = str(time.time()) + ".png"
59
+ save_image(super_resolved_img, store_name)
60
+ outputs = cv2.imread(store_name)
61
  outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)
62
 
63
  return outputs
 
72
 
73
  MARKDOWN = \
74
  """
75
+ ## <p style='text-align: center'> APISR: Anime Production Inspired Real-World Anime Super-Resolution (CVPR 2024) </p>
76
+
77
  [GitHub](https://github.com/Kiteretsu77/APISR) | [Paper](https://arxiv.org/abs/2403.01598)
78
 
79
+ APISR aims at restoring and enhancing low-quality low-resolution anime images and video sources with various degradations from real-world scenarios.
80
+
81
+ If APISR is helpful, please help star the GitHub Repo. Thanks!
82
  """
83
 
84
+ block = gr.Blocks().queue(max_size=10)
85
  with block:
86
  with gr.Row():
87
  gr.Markdown(MARKDOWN)
test_code/inference.py CHANGED
@@ -4,6 +4,7 @@
4
  import argparse
5
  import os, sys, cv2, shutil, warnings
6
  import torch
 
7
  from torchvision.transforms import ToTensor
8
  from torchvision.utils import save_image
9
  warnings.simplefilter("default")
@@ -38,6 +39,12 @@ def super_resolve_img(generator, input_path, output_path=None, weight_dtype=torc
38
  img_lr = img_lr[:4*(h//4),:,:]
39
  if w % 4 != 0:
40
  img_lr = img_lr[:,:4*(w//4),:]
 
 
 
 
 
 
41
 
42
  # Transform to tensor
43
  img_lr = cv2.cvtColor(img_lr, cv2.COLOR_BGR2RGB)
@@ -54,7 +61,7 @@ def super_resolve_img(generator, input_path, output_path=None, weight_dtype=torc
54
  if output_path is not None:
55
  save_image(super_resolved_img, output_path)
56
 
57
- # Empty the cache everytime you finish processing one image
58
  torch.cuda.empty_cache()
59
 
60
  return super_resolved_img
@@ -71,7 +78,7 @@ if __name__ == "__main__":
71
  parser.add_argument('--scale', type = int, default = 4, help="Up scaler factor")
72
  parser.add_argument('--weight_path', type = str, default = 'pretrained/4x_APISR_GRL_GAN_generator.pth', help="Weight path directory, usually under saved_models folder")
73
  parser.add_argument('--store_dir', type = str, default = 'sample_outputs', help="The folder to store the super-resolved images")
74
- parser.add_argument('--float16_inference', type = bool, default = False, help="The folder to store the super-resolved images") # Currently, this is only supported in RRDB, there is some bug with GRL model
75
  args = parser.parse_args()
76
 
77
  # Sample Command
 
4
  import argparse
5
  import os, sys, cv2, shutil, warnings
6
  import torch
7
+ import gradio as gr
8
  from torchvision.transforms import ToTensor
9
  from torchvision.utils import save_image
10
  warnings.simplefilter("default")
 
39
  img_lr = img_lr[:4*(h//4),:,:]
40
  if w % 4 != 0:
41
  img_lr = img_lr[:,:4*(w//4),:]
42
+
43
+ # Check if the size is out of the boundary
44
+ h, w, c = img_lr.shape
45
+ if h*w > 720*1280:
46
+ raise gr.Error("The input image size is too large. The largest area we support is 720x1280=921600 pixel!")
47
+
48
 
49
  # Transform to tensor
50
  img_lr = cv2.cvtColor(img_lr, cv2.COLOR_BGR2RGB)
 
61
  if output_path is not None:
62
  save_image(super_resolved_img, output_path)
63
 
64
+ # Empty the cache every time you finish processing one image
65
  torch.cuda.empty_cache()
66
 
67
  return super_resolved_img
 
78
  parser.add_argument('--scale', type = int, default = 4, help="Up scaler factor")
79
  parser.add_argument('--weight_path', type = str, default = 'pretrained/4x_APISR_GRL_GAN_generator.pth', help="Weight path directory, usually under saved_models folder")
80
  parser.add_argument('--store_dir', type = str, default = 'sample_outputs', help="The folder to store the super-resolved images")
81
+ parser.add_argument('--float16_inference', type = bool, default = False, help="Float16 inference, only useful in RRDB now") # Currently, this is only supported in RRDB, there is some bug with GRL model
82
  args = parser.parse_args()
83
 
84
  # Sample Command