Spaces:
Running
Running
File size: 11,475 Bytes
6710c89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
import os
import cv2
import gradio as gr
import numpy as np
import sys
import io
import torch
class Logger:
def __init__(self):
self.terminal = sys.stdout
self.log = io.BytesIO()
def write(self, message):
self.terminal.write(message)
self.log.write(bytes(message, encoding='utf-8'))
def flush(self):
self.terminal.flush()
self.log.flush()
def isatty(self):
return False
log = Logger()
sys.stdout = log
def read_logs():
out = log.log.getvalue().decode()
if out.count("\n") >= 30:
log.log = io.BytesIO()
sys.stdout.flush()
return out
with gr.Blocks() as app:
gr.Markdown("""
# HINet (or INR-Harmonization) - A novel image Harmonization method based on Implicit neural Networks
## Harmonize any image you want! Arbitrary resolution, and arbitrary aspect ratio!
### Official Gradio Demo
**Since Gradio Space only support CPU, the speed may kind of slow. You may better download the code to run locally with a GPU.**
<a href="https://huggingface.co/spaces/WindVChen/INR-Harmon?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
<img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a> for no queue on your own hardware.</p>
* Official Repo: [INR-Harmonization](https://github.com/WindVChen/INR-Harmonization)
""")
valid_checkpoints_dict = {"Resolution_256_iHarmony4": "Resolution_256_iHarmony4.pth",
"Resolution_1024_HAdobe5K": "Resolution_1024_HAdobe5K.pth",
"Resolution_2048_HAdobe5K": "Resolution_2048_HAdobe5K.pth",
"Resolution_RAW_HAdobe5K": "Resolution_RAW_HAdobe5K.pth",
"Resolution_RAW_iHarmony4": "Resolution_RAW_iHarmony4.pth"}
global_state = gr.State({
'pretrained_weight': valid_checkpoints_dict["Resolution_RAW_iHarmony4"],
})
with gr.Row():
form_composite_image = gr.Image(label='Input Composite image', type='pil').style(height="auto")
form_mask_image = gr.Image(label='Input Mask image', type='pil', interactive=False).style(
height="auto")
with gr.Row():
with gr.Column(scale=4):
with gr.Row():
with gr.Column(scale=2, min_width=10):
gr.Markdown(value='Model Selection', show_label=False)
with gr.Column(scale=4, min_width=10):
form_pretrained_dropdown = gr.Dropdown(
choices=list(valid_checkpoints_dict.values()),
label="Pretrained Model",
value=valid_checkpoints_dict["Resolution_RAW_iHarmony4"],
interactive=True
)
with gr.Row():
with gr.Column(scale=2, min_width=10):
gr.Markdown(value='Inference Mode', show_label=False)
with gr.Column(scale=4, min_width=10):
form_inference_mode = gr.Radio(
['Square Image', 'Arbitrary Image'],
value='Arbitrary Image',
interactive=False,
label='Mode',
)
with gr.Row():
with gr.Column(scale=2, min_width=10):
gr.Markdown(value='Split Parameter', show_label=False)
with gr.Column(scale=4, min_width=10):
form_split_res = gr.Slider(
minimum=0,
maximum=2048,
step=128,
value=256,
interactive=False,
label="Split Resolution",
)
form_split_num = gr.Number(
value=8,
interactive=False,
label="Split Number")
with gr.Row():
form_log = gr.Textbox(read_logs, label="Logs", interactive=False, type="text", every=1)
with gr.Column(scale=4):
form_harmonized_image = gr.Image(label='Harmonized Result', type='numpy', interactive=False).style(
height="auto")
form_start_btn = gr.Button("Start Harmonization", interactive=False)
form_reset_btn = gr.Button("Reset", interactive=True)
def on_change_form_composite_image(form_composite_image):
if form_composite_image is None:
return gr.update(interactive=False, value=None), gr.update(value=None)
return gr.update(interactive=True), gr.update(value=None)
def on_change_form_mask_image(form_composite_image, form_mask_image):
if form_mask_image is None:
return gr.update(interactive=False if form_composite_image is None else True), gr.update(
interactive=False), gr.update(interactive=False), gr.update(
interactive=False), gr.update(interactive=False), gr.update(value=None)
if form_composite_image.size[:2] != form_mask_image.size[:2]:
raise gr.Error("Composite image and mask image should have the same resolution!")
else:
w, h = form_composite_image.size[:2]
if h != w or (h % 16 != 0):
return gr.update(value='Arbitrary Image', interactive=False), gr.update(interactive=True), gr.update(
interactive=True), gr.update(interactive=True), gr.update(interactive=False,
value=-1), gr.update(value=None)
else:
return gr.update(value='Square Image', interactive=True), gr.update(interactive=True), gr.update(
interactive=True), gr.update(interactive=False), gr.update(interactive=True,
value=h // 16,
maximum=h,
minimum=h // 16,
step=h // 16), gr.update(value=None)
form_composite_image.change(
on_change_form_composite_image,
inputs=[form_composite_image],
outputs=[form_mask_image, form_harmonized_image]
)
form_mask_image.change(
on_change_form_mask_image,
inputs=[form_composite_image, form_mask_image],
outputs=[form_inference_mode, form_mask_image, form_start_btn, form_split_num, form_split_res,
form_harmonized_image]
)
def on_change_form_split_num(form_composite_image, form_split_num):
w, h = form_composite_image.size[:2]
if form_split_num < 1:
return gr.update(value=1)
elif form_split_num > min(w, h):
return gr.update(value=min(w, h))
else:
return gr.update(value=form_split_num)
form_split_num.change(
on_change_form_split_num,
inputs=[form_composite_image, form_split_num],
outputs=[form_split_num]
)
def on_change_form_inference_mode(form_inference_mode):
if form_inference_mode == "Square Image":
return gr.update(interactive=True), gr.update(interactive=False)
else:
return gr.update(interactive=False), gr.update(interactive=True)
form_inference_mode.change(on_change_form_inference_mode, inputs=[form_inference_mode],
outputs=[form_split_res, form_split_num])
def on_click_form_start_btn(form_composite_image, form_mask_image, form_pretrained_dropdown, form_inference_mode,
form_split_res, form_split_num):
log.log = io.BytesIO()
if form_inference_mode == "Square Image":
from efficient_inference_for_square_image import parse_args, main_process
opt = parse_args()
opt.transform_mean = [.5, .5, .5]
opt.transform_var = [.5, .5, .5]
opt.pretrained = os.path.join("./pretrained_models", form_pretrained_dropdown)
opt.split_resolution = form_split_res
opt.save_path = None
opt.workers = 0
opt.device = "cuda" if torch.cuda.is_available() else "cpu"
composite_image = np.asarray(form_composite_image)
mask = np.asarray(form_mask_image)
try:
return cv2.cvtColor(
main_process(opt, composite_image=composite_image, mask=mask),
cv2.COLOR_BGR2RGB)
except:
raise gr.Error("Patches too big. Try to reduce the `split_res`!")
else:
from inference_for_arbitrary_resolution_image import parse_args, main_process
opt = parse_args()
opt.transform_mean = [.5, .5, .5]
opt.transform_var = [.5, .5, .5]
opt.pretrained = os.path.join("./pretrained_models", form_pretrained_dropdown)
opt.split_num = int(form_split_num)
opt.save_path = None
opt.workers = 0
opt.device = "cuda" if torch.cuda.is_available() else "cpu"
composite_image = np.asarray(form_composite_image)
mask = np.asarray(form_mask_image)
try:
return cv2.cvtColor(
main_process(opt, composite_image=composite_image, mask=mask),
cv2.COLOR_BGR2RGB)
except:
raise gr.Error("Patches too big. Try to increase the `split_num`!")
form_start_btn.click(on_click_form_start_btn,
inputs=[form_composite_image, form_mask_image, form_pretrained_dropdown, form_inference_mode,
form_split_res, form_split_num], outputs=[form_harmonized_image])
def on_click_form_reset_btn():
log.log = io.BytesIO()
return gr.update(value=None), gr.update(value=None, interactive=True), gr.update(value=None,
interactive=False), gr.update(
interactive=False)
form_reset_btn.click(on_click_form_reset_btn,
inputs=None, outputs=[form_log, form_composite_image, form_mask_image, form_start_btn])
gr.Markdown("""
## Quick Start
1. Select desired `Pretrained Model`.
2. Select a composite image, and then a mask with the same size.
3. Select the inference mode (for non-square image, only `Arbitrary Image` support).
4. Set `Split Resolution` (Patches' resolution) or `Split Number` (How many patches, about N*N) according to the inference mode.
3. Click `Start` and enjoy it!
""")
gr.HTML("""
<style>
.container {
position: absolute;
height: 50px;
text-align: center;
line-height: 50px;
width: 100%;
}
</style>
<div class="container">
Gradio demo supported by
<a href="https://github.com/WindVChen">WindVChen</a>
</div>
""")
gr.close_all()
app.queue(concurrency_count=1, max_size=200, api_open=False)
app.launch(show_api=False, server_port=12345)
|