Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -220,25 +220,12 @@ image_mask_list.sort()
|
|
220 |
|
221 |
|
222 |
@spaces.GPU
|
223 |
-
def run_local(base_image, base_mask, reference_image, ref_mask
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
else:
|
230 |
-
tar_image = base_image["background"]
|
231 |
-
tar_mask = base_mask["background"]
|
232 |
-
|
233 |
-
if ref_mask_option == "Draw Mask":
|
234 |
-
ref_image = reference_image["background"]
|
235 |
-
ref_mask = reference_image["layers"][0]
|
236 |
-
elif ref_mask_option == "Upload with Mask":
|
237 |
-
ref_image = reference_image["background"]
|
238 |
-
ref_mask = ref_mask["background"]
|
239 |
-
else:
|
240 |
-
ref_image = reference_image["background"]
|
241 |
-
ref_mask = get_mask(ref_image, text_prompt)
|
242 |
|
243 |
tar_image = tar_image.convert("RGB")
|
244 |
tar_mask = tar_mask.convert("L")
|
@@ -348,7 +335,7 @@ def run_local(base_image, base_mask, reference_image, ref_mask, seed, base_mask_
|
|
348 |
edited_image = Image.fromarray(edited_image)
|
349 |
|
350 |
|
351 |
-
return
|
352 |
|
353 |
def update_ui(option):
|
354 |
if option == "Draw Mask":
|
@@ -358,71 +345,21 @@ def update_ui(option):
|
|
358 |
|
359 |
|
360 |
with gr.Blocks() as demo:
|
|
|
361 |
|
|
|
|
|
|
|
|
|
|
|
362 |
|
363 |
-
gr.
|
364 |
-
gr.Markdown("### Make sure to select the correct mask button!!")
|
365 |
-
gr.Markdown("### Click the output image to toggle between Diptych and final results!!")
|
366 |
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
base_mask = gr.ImageEditor(label="Background Mask", sources="upload", type="pil", layers = False, brush=False, eraser=False)
|
375 |
-
|
376 |
-
with gr.Row():
|
377 |
-
base_mask_option = gr.Radio(["Draw Mask", "Upload with Mask"], label="Background Mask Input Option", value="Upload with Mask")
|
378 |
-
|
379 |
-
with gr.Row():
|
380 |
-
ref_image = gr.ImageEditor(label="Reference Image", sources="upload", type="pil", brush=gr.Brush(colors=["#FFFFFF"],default_size = 30,color_mode = "fixed"),
|
381 |
-
layers = False,
|
382 |
-
interactive=True)
|
383 |
-
|
384 |
-
ref_mask = gr.ImageEditor(label="Reference Mask", sources="upload", type="pil", layers = False, brush=False, eraser=False)
|
385 |
-
|
386 |
-
with gr.Row():
|
387 |
-
ref_mask_option = gr.Radio(["Draw Mask", "Upload with Mask", "Label to Mask"], label="Reference Mask Input Option", value="Upload with Mask")
|
388 |
-
|
389 |
-
with gr.Row():
|
390 |
-
text_prompt = gr.Textbox(label="Label", placeholder="Enter the category of the reference object, e.g., car, dress, toy, etc.")
|
391 |
-
|
392 |
-
with gr.Column(scale=1):
|
393 |
-
baseline_gallery = gr.Gallery(label='Output', show_label=True, elem_id="gallery", height=695, columns=1)
|
394 |
-
base64_box = gr.Textbox(label="Base64 Output", visible=False)
|
395 |
-
with gr.Accordion("Advanced Option", open=True):
|
396 |
-
seed = gr.Slider(label="Seed", minimum=-1, maximum=999999999, step=1, value=666)
|
397 |
-
gr.Markdown("### Guidelines")
|
398 |
-
gr.Markdown(" Users can try using different seeds. For example, seeds like 42 and 123456 may produce different effects.")
|
399 |
-
gr.Markdown(" Draw Mask means manually drawing a mask on the original image.")
|
400 |
-
gr.Markdown(" Upload with Mask means uploading a mask file.")
|
401 |
-
gr.Markdown(" Label to Mask means simply inputting a label to automatically extract the mask and obtain the result.")
|
402 |
-
|
403 |
-
|
404 |
-
run_local_button = gr.Button(value="Run")
|
405 |
-
|
406 |
-
# #### example #####
|
407 |
-
num_examples = len(image_list)
|
408 |
-
for i in range(num_examples):
|
409 |
-
with gr.Row():
|
410 |
-
if i == 0:
|
411 |
-
gr.Examples([image_list[i]], inputs=[base_image], label="Examples - Background Image", examples_per_page=1)
|
412 |
-
gr.Examples([image_mask_list[i]], inputs=[base_mask], label="Examples - Background Mask", examples_per_page=1)
|
413 |
-
gr.Examples([ref_list[i]], inputs=[ref_image], label="Examples - Reference Object", examples_per_page=1)
|
414 |
-
gr.Examples([ref_mask_list[i]], inputs=[ref_mask], label="Examples - Reference Mask", examples_per_page=1)
|
415 |
-
else:
|
416 |
-
gr.Examples([image_list[i]], inputs=[base_image], examples_per_page=1, label="")
|
417 |
-
gr.Examples([image_mask_list[i]], inputs=[base_mask], examples_per_page=1, label="")
|
418 |
-
gr.Examples([ref_list[i]], inputs=[ref_image], examples_per_page=1, label="")
|
419 |
-
gr.Examples([ref_mask_list[i]], inputs=[ref_mask], examples_per_page=1, label="")
|
420 |
-
if i < num_examples - 1:
|
421 |
-
gr.HTML("<hr>")
|
422 |
-
# #### example #####
|
423 |
-
|
424 |
-
run_local_button.click(fn=run_local,
|
425 |
-
inputs=[base_image, base_mask, ref_image, ref_mask, seed, base_mask_option, ref_mask_option, text_prompt],
|
426 |
-
outputs=[baseline_gallery, base64_box]
|
427 |
-
)
|
428 |
demo.launch()
|
|
|
220 |
|
221 |
|
222 |
@spaces.GPU
|
223 |
+
def run_local(base_image, base_mask, reference_image, ref_mask):
|
224 |
+
seed = 666
|
225 |
+
tar_image = base_image
|
226 |
+
tar_mask = base_mask
|
227 |
+
ref_image = reference_image
|
228 |
+
ref_mask = ref_mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
tar_image = tar_image.convert("RGB")
|
231 |
tar_mask = tar_mask.convert("L")
|
|
|
335 |
edited_image = Image.fromarray(edited_image)
|
336 |
|
337 |
|
338 |
+
return image_to_base64(edited_image)
|
339 |
|
340 |
def update_ui(option):
|
341 |
if option == "Draw Mask":
|
|
|
345 |
|
346 |
|
347 |
with gr.Blocks() as demo:
|
348 |
+
gr.Markdown("## Upload 4 Images to Combine into a 2x2 Grid and Return Base64")
|
349 |
|
350 |
+
with gr.Row():
|
351 |
+
base_image = gr.Image(type="pil", label="base_image")
|
352 |
+
base_mask = gr.Image(type="pil", label="base_mask")
|
353 |
+
ref_image = gr.Image(type="pil", label="ref_image")
|
354 |
+
ref_mask = gr.Image(type="pil", label="ref_mask")
|
355 |
|
356 |
+
output_base64 = gr.Textbox(label="Output Image (Base64 PNG)")
|
|
|
|
|
357 |
|
358 |
+
submit_btn = gr.Button("Process Images")
|
359 |
+
|
360 |
+
submit_btn.click(
|
361 |
+
fn=run_local,
|
362 |
+
inputs=[base_image, base_mask, ref_image, ref_mask, ],
|
363 |
+
outputs=output_base64
|
364 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
demo.launch()
|