Spaces:
Running
Running
xiaoyao9184
commited on
Synced repo using 'sync_with_huggingface' Github Action
Browse files- gradio_app.py +51 -22
gradio_app.py
CHANGED
@@ -303,39 +303,68 @@ with gr.Blocks(title="Watermark Anything Demo") as demo:
|
|
303 |
embedding_img = gr.Image(label="Input Image", type="numpy")
|
304 |
|
305 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
embedding_num = gr.Slider(1, 5, value=1, step=1, label="Number of Watermarks")
|
307 |
embedding_type = gr.Radio(["random", "input"], value="random", label="Type", info="Type of watermarks")
|
308 |
embedding_str = gr.Textbox(label="Watermark Text", visible=False, show_copy_button=True)
|
309 |
embedding_loc = gr.Radio(["random", "bounding"], value="random", label="Location", info="Location of watermarks")
|
310 |
|
311 |
-
@gr.render(inputs=embedding_loc)
|
312 |
-
def show_split(wm_loc):
|
313 |
-
if wm_loc == "bounding":
|
314 |
-
embedding_box = gr.AnnotatedImage(
|
315 |
-
label="ROI",
|
316 |
-
color_map={
|
317 |
-
"ROI of Watermark embedding": "#9987FF",
|
318 |
-
"Click second point for ROI": "#f44336"}
|
319 |
-
)
|
320 |
-
|
321 |
-
embedding_img.select(
|
322 |
-
fn=get_select_coordinates,
|
323 |
-
inputs=[embedding_img, embedding_num],
|
324 |
-
outputs=embedding_box)
|
325 |
-
embedding_box.select(
|
326 |
-
fn=del_select_coordinates,
|
327 |
-
inputs=embedding_box,
|
328 |
-
outputs=embedding_box
|
329 |
-
)
|
330 |
-
else:
|
331 |
-
embedding_img.select()
|
332 |
-
|
333 |
embedding_btn = gr.Button("Embed Watermark")
|
334 |
marked_msg = gr.JSON(label="Marked Messages")
|
335 |
with gr.Row():
|
336 |
marked_image = gr.Image(label="Watermarked Image")
|
337 |
marked_mask = gr.Image(label="Position of the watermark")
|
338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
def visible_text_label(embedding_type, embedding_num):
|
340 |
if embedding_type == "input":
|
341 |
tip = "-".join([f"FFFF-{_}{_}{_}{_}" for _ in range(embedding_num)])
|
|
|
303 |
embedding_img = gr.Image(label="Input Image", type="numpy")
|
304 |
|
305 |
with gr.Column():
|
306 |
+
embedding_box = gr.AnnotatedImage(
|
307 |
+
visible=False,
|
308 |
+
label="ROI: Click on left 'Input Image'",
|
309 |
+
color_map={
|
310 |
+
"ROI of Watermark embedding": "#9987FF",
|
311 |
+
"Click second point for ROI": "#f44336"}
|
312 |
+
)
|
313 |
+
|
314 |
embedding_num = gr.Slider(1, 5, value=1, step=1, label="Number of Watermarks")
|
315 |
embedding_type = gr.Radio(["random", "input"], value="random", label="Type", info="Type of watermarks")
|
316 |
embedding_str = gr.Textbox(label="Watermark Text", visible=False, show_copy_button=True)
|
317 |
embedding_loc = gr.Radio(["random", "bounding"], value="random", label="Location", info="Location of watermarks")
|
318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
319 |
embedding_btn = gr.Button("Embed Watermark")
|
320 |
marked_msg = gr.JSON(label="Marked Messages")
|
321 |
with gr.Row():
|
322 |
marked_image = gr.Image(label="Watermarked Image")
|
323 |
marked_mask = gr.Image(label="Position of the watermark")
|
324 |
|
325 |
+
embedding_img.select(
|
326 |
+
fn=get_select_coordinates,
|
327 |
+
inputs=[embedding_img, embedding_num],
|
328 |
+
outputs=embedding_box)
|
329 |
+
embedding_box.select(
|
330 |
+
fn=del_select_coordinates,
|
331 |
+
inputs=embedding_box,
|
332 |
+
outputs=embedding_box
|
333 |
+
)
|
334 |
+
|
335 |
+
# The inability to dynamically render `AnnotatedImage` is because,
|
336 |
+
# when placed inside `gr.Column()`, it prevents listeners from being added to controls outside the column.
|
337 |
+
# Dynamically adding a select listener will not change the cursor shape of the Image.
|
338 |
+
# So `render` cannot work properly in this scenario.
|
339 |
+
#
|
340 |
+
# @gr.render(inputs=embedding_loc)
|
341 |
+
# def show_split(wm_loc):
|
342 |
+
# if wm_loc == "bounding":
|
343 |
+
# embedding_img.select(
|
344 |
+
# fn=get_select_coordinates,
|
345 |
+
# inputs=[embedding_img, embedding_num],
|
346 |
+
# outputs=embedding_box)
|
347 |
+
# embedding_box.select(
|
348 |
+
# fn=del_select_coordinates,
|
349 |
+
# inputs=embedding_box,
|
350 |
+
# outputs=embedding_box
|
351 |
+
# )
|
352 |
+
# else:
|
353 |
+
# embedding_img.select()
|
354 |
+
|
355 |
+
def visible_box_image(img, wm_loc):
|
356 |
+
if wm_loc == "bounding":
|
357 |
+
return gr.update(visible=True, value=(img,sections))
|
358 |
+
else:
|
359 |
+
sections.clear()
|
360 |
+
ROI_coordinates['clicks'] = 0
|
361 |
+
return gr.update(visible=False, value=(img,sections))
|
362 |
+
embedding_loc.change(
|
363 |
+
fn=visible_box_image,
|
364 |
+
inputs=[embedding_img, embedding_loc],
|
365 |
+
outputs=[embedding_box]
|
366 |
+
)
|
367 |
+
|
368 |
def visible_text_label(embedding_type, embedding_num):
|
369 |
if embedding_type == "input":
|
370 |
tip = "-".join([f"FFFF-{_}{_}{_}{_}" for _ in range(embedding_num)])
|