Arrcttacsrks commited on
Commit
5fd62d4
·
verified ·
1 Parent(s): 67f8d32

Upload app(26).py

Browse files
Files changed (1) hide show
  1. app(26).py +153 -0
app(26).py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ Gradio demo (almost the same code as the one used in Huggingface space)
3
+ '''
4
+ import os, sys
5
+ import cv2
6
+ import time
7
+ import datetime, pytz
8
+ import gradio as gr
9
+ import torch
10
+ import numpy as np
11
+ from torchvision.utils import save_image
12
+
13
+
14
+ # Import files from the local folder
15
+ root_path = os.path.abspath('.')
16
+ sys.path.append(root_path)
17
+ from test_code.inference import super_resolve_img
18
+ from test_code.test_utils import load_grl, load_rrdb, load_dat
19
+
20
+
21
+ def auto_download_if_needed(weight_path):
22
+ if os.path.exists(weight_path):
23
+ return
24
+
25
+ if not os.path.exists("pretrained"):
26
+ os.makedirs("pretrained")
27
+
28
+ if weight_path == "pretrained/4x_APISR_RRDB_GAN_generator.pth":
29
+ os.system("wget https://github.com/Kiteretsu77/APISR/releases/download/v0.2.0/4x_APISR_RRDB_GAN_generator.pth")
30
+ os.system("mv 4x_APISR_RRDB_GAN_generator.pth pretrained")
31
+
32
+ if weight_path == "pretrained/4x_APISR_GRL_GAN_generator.pth":
33
+ os.system("wget https://github.com/Kiteretsu77/APISR/releases/download/v0.1.0/4x_APISR_GRL_GAN_generator.pth")
34
+ os.system("mv 4x_APISR_GRL_GAN_generator.pth pretrained")
35
+
36
+ if weight_path == "pretrained/2x_APISR_RRDB_GAN_generator.pth":
37
+ os.system("wget https://github.com/Kiteretsu77/APISR/releases/download/v0.1.0/2x_APISR_RRDB_GAN_generator.pth")
38
+ os.system("mv 2x_APISR_RRDB_GAN_generator.pth pretrained")
39
+
40
+ if weight_path == "pretrained/4x_APISR_DAT_GAN_generator.pth":
41
+ os.system("wget https://github.com/Kiteretsu77/APISR/releases/download/v0.3.0/4x_APISR_DAT_GAN_generator.pth")
42
+ os.system("mv 4x_APISR_DAT_GAN_generator.pth pretrained")
43
+
44
+
45
+
46
+ def inference(img_path, model_name):
47
+
48
+ try:
49
+ weight_dtype = torch.float32
50
+
51
+ # Load the model
52
+ if model_name == "4xGRL":
53
+ weight_path = "pretrained/4x_APISR_GRL_GAN_generator.pth"
54
+ auto_download_if_needed(weight_path)
55
+ generator = load_grl(weight_path, scale=4) # Directly use default way now
56
+
57
+ elif model_name == "4xRRDB":
58
+ weight_path = "pretrained/4x_APISR_RRDB_GAN_generator.pth"
59
+ auto_download_if_needed(weight_path)
60
+ generator = load_rrdb(weight_path, scale=4) # Directly use default way now
61
+
62
+ elif model_name == "2xRRDB":
63
+ weight_path = "pretrained/2x_APISR_RRDB_GAN_generator.pth"
64
+ auto_download_if_needed(weight_path)
65
+ generator = load_rrdb(weight_path, scale=2) # Directly use default way now
66
+
67
+ elif model_name == "4xDAT":
68
+ weight_path = "pretrained/4x_APISR_DAT_GAN_generator.pth"
69
+ auto_download_if_needed(weight_path)
70
+ generator = load_dat(weight_path, scale=4) # Directly use default way now
71
+
72
+ else:
73
+ raise gr.Error("We don't support such Model")
74
+
75
+ generator = generator.to(dtype=weight_dtype)
76
+
77
+
78
+ print("We are processing ", img_path)
79
+ print("The time now is ", datetime.datetime.now(pytz.timezone('US/Eastern')))
80
+
81
+ # In default, we will automatically use crop to match 4x size
82
+ super_resolved_img = super_resolve_img(generator, img_path, output_path=None, weight_dtype=weight_dtype, downsample_threshold=720, crop_for_4x=True)
83
+ store_name = str(time.time()) + ".png"
84
+ save_image(super_resolved_img, store_name)
85
+ outputs = cv2.imread(store_name)
86
+ outputs = cv2.cvtColor(outputs, cv2.COLOR_RGB2BGR)
87
+ os.remove(store_name)
88
+
89
+ return outputs
90
+
91
+
92
+ except Exception as error:
93
+ raise gr.Error(f"global exception: {error}")
94
+
95
+
96
+
97
+ if __name__ == '__main__':
98
+
99
+ MARKDOWN = \
100
+ """
101
+ ## <p style='text-align: center'> APISR: Anime Production Inspired Real-World Anime Super-Resolution (CVPR 2024) </p>
102
+
103
+ [GitHub](https://github.com/Kiteretsu77/APISR) | [Paper](https://arxiv.org/abs/2403.01598)
104
+
105
+ APISR aims at restoring and enhancing low-quality low-resolution **anime** images and video sources with various degradations from real-world scenarios.
106
+
107
+ ### Note: Due to memory restriction, all images whose short side is over 720 pixel will be downsampled to 720 pixel with the same aspect ratio. E.g., 1920x1080 -> 1280x720
108
+ ### Note: Please check [Model Zoo](https://github.com/Kiteretsu77/APISR/blob/main/docs/model_zoo.md) for the description of each weight and [Here](https://imgsli.com/MjU0MjI0) for model comparisons.
109
+
110
+ ### If APISR is helpful, please help star the [GitHub Repo](https://github.com/Kiteretsu77/APISR). Thanks! ###
111
+ """
112
+
113
+ block = gr.Blocks().queue(max_size=10)
114
+ with block:
115
+ with gr.Row():
116
+ gr.Markdown(MARKDOWN)
117
+ with gr.Row(elem_classes=["container"]):
118
+ with gr.Column(scale=2):
119
+ input_image = gr.Image(type="filepath", label="Input")
120
+ model_name = gr.Dropdown(
121
+ [
122
+ "2xRRDB",
123
+ "4xRRDB",
124
+ "4xGRL",
125
+ "4xDAT",
126
+ ],
127
+ type="value",
128
+ value="4xGRL",
129
+ label="model",
130
+ )
131
+ run_btn = gr.Button(value="Submit")
132
+
133
+ with gr.Column(scale=3):
134
+ output_image = gr.Image(type="numpy", label="Output image")
135
+
136
+ with gr.Row(elem_classes=["container"]):
137
+ gr.Examples(
138
+ [
139
+ ["__assets__/lr_inputs/image-00277.png"],
140
+ ["__assets__/lr_inputs/image-00542.png"],
141
+ ["__assets__/lr_inputs/41.png"],
142
+ ["__assets__/lr_inputs/f91.jpg"],
143
+ ["__assets__/lr_inputs/image-00440.png"],
144
+ ["__assets__/lr_inputs/image-00164.jpg"],
145
+ ["__assets__/lr_inputs/img_eva.jpeg"],
146
+ ["__assets__/lr_inputs/naruto.jpg"],
147
+ ],
148
+ [input_image],
149
+ )
150
+
151
+ run_btn.click(inference, inputs=[input_image, model_name], outputs=[output_image])
152
+
153
+ block.launch()