-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathui.py
More file actions
316 lines (299 loc) · 13.5 KB
/
ui.py
File metadata and controls
316 lines (299 loc) · 13.5 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import torch
if not torch.cuda.is_available():
raise RuntimeError('CUDA is not available, but required.')
import gradio as gr
from region_utils.ui_utils import *
from region_utils.drag import sd_version
LENGTH = 400 # length of image in Gradio App, you can adjust it according to your screen size
GEN_SIZE = {'v1-5': 512, 'v2-1': 768, 'xl': 1024}[sd_version] # Default generated image size
def main():
with gr.Blocks() as demo:
gr_length = gr.Number(value=LENGTH, visible=False, precision=0)
gr_gen_size = gr.Number(value=GEN_SIZE, visible=False, precision=0)
selected_masks = gr.State(value=[])
src_points_m = gr.State(value=None); trg_points_m = gr.State(value=None)
selected_points = gr.State(value=[])
selected_shapes = gr.State(value=[])
src_points = gr.State(value=None); trg_points = gr.State(value=None)
selected_points_r = gr.State(value=[])
src_points_r = gr.State(value=None); trg_points_r = gr.State(value=None)
seed = gr.Number(value=42, label="Generation Seed", precision=0, visible=False)
start_t = gr.Number(value=0.5, visible=False)
end_t = gr.Number(value=0.2, visible=False)
# layout definition
with gr.Row():
gr.Markdown("""
# Official Implementation of [RegionDrag](https://arxiv.org/abs/2407.18247)
#### Explore our detailed [User Guide](https://github.com/LuJingyi-John/RegionDrag/blob/main/README.md) for interface instructions.
""")
with gr.Row():
with gr.Tab(label='Region pairs'):
with gr.Row():
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">1. Upload image and add regions</p>""")
canvas_m = gr.Image(type="numpy", tool="sketch", label=" ", height=LENGTH, width=LENGTH)
with gr.Row():
resize_button_m = gr.Button("Fit Canvas")
add_mask_button = gr.Button("Add Region")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">2. View Input</p>""")
input_image_m = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=False)
with gr.Row():
undo_mask_button = gr.Button("Undo Region")
clear_mask_button = gr.Button("Clear Region")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Results</p>""")
output_image_m = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=False)
with gr.Row():
run_button_m = gr.Button("Run Drag")
clear_all_button_m = gr.Button("Clear All")
with gr.Tab(label='Polygon pairs'):
with gr.Row():
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">1. Upload image</p>""")
canvas = gr.Image(type="numpy", tool="sketch", label=" ", height=LENGTH, width=LENGTH, interactive=True)
with gr.Row():
resize_button = gr.Button("Fit Canvas")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">2. Click vertices</p>""")
input_image = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=True)
with gr.Row():
undo_point_button = gr.Button("Undo Point")
clear_point_button = gr.Button("Clear Point")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Results</p>""")
output_image = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=False)
with gr.Row():
run_button = gr.Button("Run Drag")
clear_all_button = gr.Button("Clear All")
shape = gr.Radio(choices=['▲ Tri', '■ Quad'], value='■ Quad', type='index', label='Mask Shape', interactive=True)
with gr.Tab(label='Region + points'):
with gr.Row():
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">1. Upload image and draw regions</p>""")
canvas_r = gr.Image(type="numpy", tool="sketch", label=" ", height=LENGTH, width=LENGTH)
with gr.Row():
resize_button_r = gr.Button("Fit Canvas")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">2. Click points to control regions</p>""")
input_image_r = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=True)
with gr.Row():
undo_point_button_r = gr.Button("Undo Point")
clear_point_button_r = gr.Button("Clear Point")
with gr.Column():
gr.Markdown("""<p style="text-align: center; font-size: 20px">Results</p>""")
output_image_r = gr.Image(type="numpy", label=" ", height=LENGTH, width=LENGTH, interactive=False)
with gr.Row():
run_button_r = gr.Button("Run Drag")
clear_all_button_r = gr.Button("Clear All")
with gr.Tab("Generation Parameters"):
with gr.Row():
prompt = gr.Textbox(label="Prompt describing output image (Optional)", value='A photo of an object.')
data_path = gr.Textbox(value='output/default', label="Output path")
with gr.Row():
steps = gr.Slider(minimum=20, maximum=100, value=20, step=20, label='Sampling steps', interactive=True)
noise_scale = gr.Slider(minimum=0, maximum=1.6, value=0.6, step=0.2, label='Handle Noise Scale', interactive=True) # alpha
method = gr.Dropdown(choices=['InstantDrag', 'Encode then CP', 'CP then Encode'], value='InstantDrag', label='Method', interactive=True)
clear_all_button_m.click(
clear_all_m,
[gr_length],
[canvas_m, input_image_m, output_image_m, selected_masks, prompt, data_path, steps, noise_scale, src_points_m, trg_points_m]
)
canvas_m.clear(
clear_all_m,
[gr_length],
[canvas_m, input_image_m, output_image_m, selected_masks, prompt, data_path, steps, noise_scale, src_points_m, trg_points_m]
)
resize_button_m.click(
clear_masks,
[canvas_m, selected_masks],
[input_image_m]
).then(
resize_image,
[canvas_m, gr_length, gr_gen_size],
[canvas_m, input_image_m, output_image_m]
)
add_mask_button.click(
add_mask,
[canvas_m, selected_masks],
[input_image_m]
).then(
preview_out_image_m,
[canvas_m, selected_masks],
[output_image_m, src_points_m, trg_points_m]
)
undo_mask_button.click(
undo_mask,
[canvas_m, selected_masks],
[input_image_m]
).then(
preview_out_image_m,
[canvas_m, selected_masks],
[output_image_m, src_points_m, trg_points_m]
)
clear_mask_button.click(
clear_masks,
[canvas_m, selected_masks],
[input_image_m]
).then(
preview_out_image_m,
[canvas_m, selected_masks],
[output_image_m, src_points_m, trg_points_m]
)
run_button_m.click(
preview_out_image_m,
[canvas_m, selected_masks],
[output_image_m, src_points_m, trg_points_m]
).then(
run_process,
[canvas_m, input_image_m, output_image_m, src_points_m, trg_points_m, prompt, start_t, end_t, steps, noise_scale, data_path, method, seed],
[output_image_m]
)
clear_all_button.click(
clear_all,
[gr_length],
[canvas, input_image, output_image, selected_points, selected_shapes, prompt, data_path, steps, noise_scale, src_points, trg_points]
)
canvas.clear(
clear_all,
[gr_length],
[canvas, input_image, output_image, selected_points, selected_shapes, prompt, data_path, steps, noise_scale, src_points, trg_points]
)
resize_button.click(
clear_points,
[canvas, selected_points, selected_shapes],
[input_image]
).then(
resize_image,
[canvas, gr_length, gr_gen_size],
[canvas, input_image, output_image]
)
canvas.edit(
draw_input_image,
[canvas, selected_points, selected_shapes],
input_image
).then(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
)
shape.change(
update_shape,
[canvas, shape, selected_points, selected_shapes],
[input_image]
).then(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
)
input_image.upload(
wrong_upload,
outputs=[input_image]
)
input_image.select(
add_point,
[canvas, shape, selected_points, selected_shapes],
[input_image]
).then(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
)
undo_point_button.click(
undo_point,
[canvas, shape, selected_points, selected_shapes],
[input_image]
).then(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
)
clear_point_button.click(
clear_points,
[canvas, selected_points, selected_shapes],
[input_image]
).then(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
)
run_button.click(
preview_out_image,
[canvas, selected_points, selected_shapes],
[output_image, src_points, trg_points]
).then(
run_process,
[canvas, input_image, output_image, src_points, trg_points, prompt, start_t, end_t, steps, noise_scale, data_path, method, seed],
[output_image]
)
clear_all_button_r.click(
clear_all_m,
[gr_length],
[canvas_r, input_image_r, output_image_r, selected_points_r, prompt, data_path, steps, noise_scale, src_points_r, trg_points_r]
)
canvas_r.clear(
clear_all_m,
[gr_length],
[canvas_r, input_image_r, output_image_r, selected_points_r, prompt, data_path, steps, noise_scale, src_points_r, trg_points_r]
)
resize_button_r.click(
clear_points_r,
[canvas_r, selected_points_r],
[input_image_r]
).then(
resize_image,
[canvas_r, gr_length, gr_gen_size],
[canvas_r, input_image_r, output_image_r]
)
canvas_r.edit(
draw_input_image_r,
[canvas_r, selected_points_r],
[input_image_r]
).then(
preview_out_image_r,
[canvas_r, selected_points_r],
[output_image_r, src_points_r, trg_points_r]
)
input_image_r.upload(
wrong_upload,
outputs=[input_image_r]
)
input_image_r.select(
add_point_r,
[canvas_r, selected_points_r],
[input_image_r]
).then(
preview_out_image_r,
[canvas_r, selected_points_r],
[output_image_r, src_points_r, trg_points_r]
)
undo_point_button_r.click(
undo_point_r,
[canvas_r, selected_points_r],
[input_image_r]
).then(
preview_out_image_r,
[canvas_r, selected_points_r],
[output_image_r, src_points_r, trg_points_r]
)
clear_point_button_r.click(
clear_points_r,
[canvas_r, selected_points_r],
[input_image_r]
).then(
preview_out_image_r,
[canvas_r, selected_points_r],
[output_image_r, src_points_r, trg_points_r]
)
run_button_r.click(
preview_out_image_r,
[canvas_r, selected_points_r],
[output_image_r, src_points_r, trg_points_r]
).then(
run_process,
[canvas_r, input_image_r, output_image_r, src_points_r, trg_points_r, prompt, start_t, end_t, steps, noise_scale, data_path, method, seed],
[output_image_r]
)
demo.queue().launch(share=True, debug=True)
if __name__ == '__main__':
main()