Spaces:
Runtime error
Runtime error
UI improvements and easier photo download
Browse files
app.py
CHANGED
@@ -4,6 +4,9 @@ import util
|
|
4 |
from run_cmd import run_cmd
|
5 |
from random import randint
|
6 |
from PIL import Image
|
|
|
|
|
|
|
7 |
|
8 |
is_colab = util.is_google_colab()
|
9 |
|
@@ -11,10 +14,10 @@ run_cmd("pip install pngquant")
|
|
11 |
|
12 |
def inference(img, size, type):
|
13 |
_id = randint(1, 10000)
|
14 |
-
INPUT_DIR = "
|
15 |
-
OUTPUT_DIR = "
|
16 |
img_in_path = os.path.join(INPUT_DIR, "1.jpg")
|
17 |
-
img_out_path = os.path.join(OUTPUT_DIR, "
|
18 |
run_cmd(f"rm -rf {INPUT_DIR}")
|
19 |
run_cmd(f"rm -rf {OUTPUT_DIR}")
|
20 |
run_cmd(f"mkdir {INPUT_DIR}")
|
@@ -36,37 +39,58 @@ def inference(img, size, type):
|
|
36 |
|
37 |
# Remove input and output image
|
38 |
run_cmd(f"rm -f {img_in_path}")
|
39 |
-
run_cmd(f"rm -f {img_out_path}")
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
title = "ESRGAN Upscaling With Custom Models"
|
49 |
description = "This space uses old ESRGAN architecture to upscale images, using models made by the community."
|
50 |
article = "<p><a href='https://upscale.wiki/wiki/Model_Database'>Model Database</a></p>"
|
51 |
|
52 |
-
with gr.Blocks() as demo:
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
demo.queue()
|
72 |
demo.launch(debug=is_colab, share=is_colab, inline=is_colab)
|
|
|
4 |
from run_cmd import run_cmd
|
5 |
from random import randint
|
6 |
from PIL import Image
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
temp_path = tempfile.gettempdir()
|
10 |
|
11 |
is_colab = util.is_google_colab()
|
12 |
|
|
|
14 |
|
15 |
def inference(img, size, type):
|
16 |
_id = randint(1, 10000)
|
17 |
+
INPUT_DIR = os.path.join(temp_path, f"input_image{str(_id)}")
|
18 |
+
OUTPUT_DIR = os.path.join(temp_path, f"output_image{str(_id)}")
|
19 |
img_in_path = os.path.join(INPUT_DIR, "1.jpg")
|
20 |
+
img_out_path = os.path.join(OUTPUT_DIR, f"1_{size}.png")
|
21 |
run_cmd(f"rm -rf {INPUT_DIR}")
|
22 |
run_cmd(f"rm -rf {OUTPUT_DIR}")
|
23 |
run_cmd(f"mkdir {INPUT_DIR}")
|
|
|
39 |
|
40 |
# Remove input and output image
|
41 |
run_cmd(f"rm -f {img_in_path}")
|
42 |
+
#run_cmd(f"rm -f {img_out_path}")
|
43 |
+
|
44 |
+
out_file.update(value=img_out_path, visible=True)
|
45 |
+
|
46 |
+
return img_out, gr.File.update(value=img_out_path, visible=True)
|
47 |
|
48 |
+
css = '''
|
49 |
+
.file-preview {
|
50 |
+
overflow: hidden !important;
|
51 |
+
margin: 5px 0 !important;
|
52 |
+
padding: 0 10px !important;
|
53 |
+
}
|
54 |
|
55 |
+
.file-preview div div:nth-child(2) {
|
56 |
+
flex-grow: 1 !important;
|
57 |
+
}
|
58 |
|
59 |
+
.file-preview div div:nth-child(3) {
|
60 |
+
text-align: right !important;
|
61 |
+
}
|
62 |
+
'''
|
63 |
|
64 |
title = "ESRGAN Upscaling With Custom Models"
|
65 |
description = "This space uses old ESRGAN architecture to upscale images, using models made by the community."
|
66 |
article = "<p><a href='https://upscale.wiki/wiki/Model_Database'>Model Database</a></p>"
|
67 |
|
68 |
+
with gr.Blocks(title=title, css=css) as demo:
|
69 |
+
gr.Markdown(
|
70 |
+
f"""
|
71 |
+
# {title}
|
72 |
+
{description}
|
73 |
+
""")
|
74 |
+
|
75 |
+
with gr.Box():
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
input_image = gr.Image(type="pil", label="Input")
|
79 |
+
upscale_size = gr.Radio(["x4", "x2"], label="Upscale by:", value="x4")
|
80 |
+
upscale_type = gr.Radio(["Manga", "Anime", "General"], label="Select the type of picture you want to upscale:", value="Manga")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
upscale_btn = gr.Button(value="Upscale", variant="primary")
|
84 |
+
|
85 |
+
with gr.Column():
|
86 |
+
output_image = gr.Image(type="filepath", interactive=False, label="Upscaled image", )
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
out_file = gr.File(interactive=False, show_label=False, visible=False)
|
90 |
+
|
91 |
+
gr.HTML(value=article)
|
92 |
+
|
93 |
+
upscale_btn.click(inference, inputs=[input_image, upscale_size, upscale_type], outputs=[output_image, out_file])
|
94 |
|
95 |
demo.queue()
|
96 |
demo.launch(debug=is_colab, share=is_colab, inline=is_colab)
|
inference.py
CHANGED
@@ -14,9 +14,9 @@ def is_cuda():
|
|
14 |
model_type = sys.argv[3]
|
15 |
|
16 |
if model_type == "Anime":
|
17 |
-
model_path = "4x-AnimeSharp.pth"
|
18 |
else:
|
19 |
-
model_path = "4x-UniScaleV2_Sharp.pth"
|
20 |
|
21 |
img_path = sys.argv[1]
|
22 |
output_dir = sys.argv[2]
|
@@ -39,8 +39,7 @@ model = model.to(device)
|
|
39 |
|
40 |
base = os.path.splitext(os.path.basename(img_path))[0]
|
41 |
|
42 |
-
#
|
43 |
-
print(img_path);
|
44 |
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
45 |
img = img * 1.0 / 255
|
46 |
img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
|
|
|
14 |
model_type = sys.argv[3]
|
15 |
|
16 |
if model_type == "Anime":
|
17 |
+
model_path = "models/4x-AnimeSharp.pth"
|
18 |
else:
|
19 |
+
model_path = "models/4x-UniScaleV2_Sharp.pth"
|
20 |
|
21 |
img_path = sys.argv[1]
|
22 |
output_dir = sys.argv[2]
|
|
|
39 |
|
40 |
base = os.path.splitext(os.path.basename(img_path))[0]
|
41 |
|
42 |
+
# Read image
|
|
|
43 |
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
44 |
img = img * 1.0 / 255
|
45 |
img = torch.from_numpy(np.transpose(img[:, :, [2, 1, 0]], (2, 0, 1))).float()
|
inference_manga_v2.py
CHANGED
@@ -11,7 +11,7 @@ def is_cuda():
|
|
11 |
else:
|
12 |
return False
|
13 |
|
14 |
-
model_path = '4x_eula_digimanga_bw_v2_nc1_307k.pth'
|
15 |
img_path = sys.argv[1]
|
16 |
output_dir = sys.argv[2]
|
17 |
device = torch.device('cuda' if is_cuda() else 'cpu')
|
@@ -33,8 +33,7 @@ model = model.to(device)
|
|
33 |
|
34 |
base = os.path.splitext(os.path.basename(img_path))[0]
|
35 |
|
36 |
-
#
|
37 |
-
print(img_path);
|
38 |
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
39 |
img = img * 1.0 / 255
|
40 |
img = torch.from_numpy(img[np.newaxis, :, :]).float()
|
|
|
11 |
else:
|
12 |
return False
|
13 |
|
14 |
+
model_path = 'models/4x_eula_digimanga_bw_v2_nc1_307k.pth'
|
15 |
img_path = sys.argv[1]
|
16 |
output_dir = sys.argv[2]
|
17 |
device = torch.device('cuda' if is_cuda() else 'cpu')
|
|
|
33 |
|
34 |
base = os.path.splitext(os.path.basename(img_path))[0]
|
35 |
|
36 |
+
# Read image
|
|
|
37 |
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
38 |
img = img * 1.0 / 255
|
39 |
img = torch.from_numpy(img[np.newaxis, :, :]).float()
|
4x-AnimeSharp.pth β models/4x-AnimeSharp.pth
RENAMED
File without changes
|
4x-UniScaleV2_Sharp.pth β models/4x-UniScaleV2_Sharp.pth
RENAMED
File without changes
|
4x_eula_digimanga_bw_v2_nc1_307k.pth β models/4x_eula_digimanga_bw_v2_nc1_307k.pth
RENAMED
File without changes
|