Spaces:
Running
Running
File size: 8,558 Bytes
40e52db fe01920 40e52db 88e4b1c a87a1d9 25a45ad 88e4b1c 6d9b79c 88e4b1c 6d9b79c 82e2c3a 88e4b1c 6d9b79c 82e2c3a 6d9b79c 88e4b1c 82e2c3a 88e4b1c a87a1d9 a6626fd 88e4b1c ed66e9f a6626fd 6e83c08 88e4b1c 8b40206 ed66e9f 82e2c3a ed66e9f b024c7c fe01920 40e52db 82e2c3a 88e4b1c a87a1d9 0ce80a6 ed66e9f 0ce80a6 e879be5 0ce80a6 a87a1d9 82e2c3a ed66e9f a87a1d9 82e2c3a fe01920 82e2c3a a87a1d9 fe01920 82e2c3a 40e52db |
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 |
import gradio as gr
import os
import tempfile
from PIL import Image
import numpy as np
css = """
p {
font-size: 120%;
}
li {
font-size: 110%;
}
video {
max-height: 400px;
}
.container {
height: initial;
}
.image-container {
width: 200px;
max-height: auto;
margin: auto;
}
img {
max-height: 400px;
}
"""
# Optional CSS stuff for the header example image:
#example {
# width: 80%;
# height: 60%
#}
#example img {
# width: 80%;
# height: 80%
#}
a = os.path.join(os.path.dirname(__file__), "files/barkley_balloon.mp4")
b = os.path.join(os.path.dirname(__file__), "files/eiffel_tower.mp4")
c = os.path.join(os.path.dirname(__file__), "files/bird.bmp")
d = os.path.join(os.path.dirname(__file__), "files/groot.jpeg")
w1 = os.path.join(os.path.dirname(__file__), "files/AI_generated.png")
w2 = os.path.join(os.path.dirname(__file__), "files/hf-logo.png")
w3 = os.path.join(os.path.dirname(__file__), "files/forest_qr_watermarking.png")
w4 = os.path.join(os.path.dirname(__file__), "files/cheetah1.jpg")
w5 = os.path.join(os.path.dirname(__file__), "files/frog.jpg")
w6 = os.path.join(os.path.dirname(__file__), "files/Human_generated.png")
w7 = os.path.join(os.path.dirname(__file__), "files/hf-logo_transpng.png")
def process_watermark(watermark_path, opacity, size, for_video=False):
"""Process watermark image with opacity and size adjustments"""
if watermark_path is None:
return None
# Load watermark image
if isinstance(watermark_path, str):
watermark = Image.open(watermark_path)
else:
watermark = Image.fromarray(watermark_path) if isinstance(watermark_path, np.ndarray) else watermark_path
# Convert to RGBA if not already
if watermark.mode != 'RGBA':
watermark = watermark.convert('RGBA')
# Resize watermark based on size parameter
original_size = watermark.size
new_size = (int(original_size[0] * size), int(original_size[1] * size))
watermark = watermark.resize(new_size, Image.Resampling.LANCZOS)
# Applying Opacity
# Get the alpha channel and multiply by opacity
r, g, b, a = watermark.split()
a = a.point(lambda x: int(x * opacity))
watermark = Image.merge('RGBA', (r, g, b, a))
# Return PIL Image for images, file path for videos
if for_video:
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
watermark.save(temp_file.name, 'PNG')
temp_file.close()
return temp_file.name
else:
return watermark
def generate_image(original_image, watermark, opacity, size):
if original_image is None:
return None
processed_watermark = process_watermark(watermark, opacity, size, for_video=False)
# Convert original_image to PIL Image if it's a numpy array
if isinstance(original_image, np.ndarray):
original_image = Image.fromarray(original_image)
return gr.Image(original_image, watermark=processed_watermark)
def generate_video(original_video, watermark, opacity, size):
if original_video is None:
return None
processed_watermark = process_watermark(watermark, opacity, size, for_video=True)
return gr.Video(original_video, watermark=processed_watermark)
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
gr.Markdown("# 🤗 Watermarking with Gradio: Example")
gr.Markdown("Watermarks can be **visible** or **invisible**.")
gr.Markdown("""They can provide information directly, or provide a link for more information.
- Visible watermarks are useful to disclose when content is AI-generated.
- Invisible watermarks can mark content as authentic.
- ...And vice versa! There are many possibilities for what watermarks can provide.
- Watermarks can also provide information about content created by people.""")
gr.Markdown("They are a useful tool for **AI provenance**.")
gr.Markdown("**Expert level:** One particularly useful form of watermarking provides a link with more information, such as with a QR code, [which you can further customize to suit the style of your imagery](https://huggingface.co/spaces/huggingface-projects/QR-code-AI-art-generator).")
gr.Markdown("""For more information on watermarking -- what watermarking is, why it's important, and the tools available on Hugging Face -- please check out [our blogpost on AI watermarking](https://huggingface.co/blog/watermarking).""")
gr.Markdown()
gr.Markdown("## Try it out below!")
with gr.Column():
with gr.Column():
gr.Image('files/watermark_example.png', visible=False)
with gr.Column():
gr.Image('files/watermark_example.png', show_label=False, show_download_button=False, elem_id='example', container=False, interactive=False)
gr.Markdown('**Image Watermark Code:**')
gr.Code('import gradio as gr\n\nwatermarked_image = gr.Image(original_image_file, watermark=watermark_file)', lines=3)
gr.Markdown('**Video Watermark Code:**')
gr.Code('import gradio as gr\n\nwatermarked_video = gr.Video(original_video_file, watermark=watermark_file)', lines=3)
with gr.Column():
gr.Image('files/watermark_example.png', visible=False)
with gr.Tab("Image Watermarking"):
with gr.Column():
gr.Markdown("**Inputs**: Image and watermark file")
with gr.Column():
gr.Markdown("**Output**: Watermarked image")
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Original Image")
watermark_image = gr.Image(type='filepath', image_mode=None, label="Watermark Image")
with gr.Accordion("Watermark settings", open=False):
opacity_slider = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.1, label="Watermark Opacity")
size_slider = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Watermark Size")
generate_btn = gr.Button("Generate Watermarked Image")
with gr.Column():
output_image = gr.Image(label="Watermarked Image")
# Examples
gr.Examples(
examples=[
[d, w7, 1.0, 1.0],
[w4, w5, 0.7, 0.8],
[c, w6, 0.9, 1.2]
],
inputs=[input_image, watermark_image, opacity_slider, size_slider],
outputs=output_image,
fn=generate_image,
cache_examples=False
)
generate_btn.click(
fn=generate_image,
inputs=[input_image, watermark_image, opacity_slider, size_slider],
outputs=output_image
)
with gr.Tab("Video Watermarking"):
with gr.Column():
gr.Markdown("**Inputs**: Video and watermark file")
with gr.Column():
gr.Markdown("**Output**: Watermarked video")
with gr.Row():
with gr.Column():
input_video = gr.Video(label="Original Video")
watermark_video = gr.Image(type='filepath', image_mode=None, label="Watermark Image")
with gr.Accordion("Watermark settings", open=False):
opacity_slider_video = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.1, label="Watermark Opacity")
size_slider_video = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Watermark Size")
generate_btn_video = gr.Button("Generate Watermarked Video")
with gr.Column():
output_video = gr.Video(label="Watermarked Video")
# Examples
gr.Examples(
examples=[
[a, w1, 1.0, 1.0],
[b, w2, 0.8, 0.9],
[a, w3, 0.6, 1.5],
[b, w4, 0.7, 0.8]
],
inputs=[input_video, watermark_video, opacity_slider_video, size_slider_video],
outputs=output_video,
fn=generate_video
)
generate_btn_video.click(
fn=generate_video,
inputs=[input_video, watermark_video, opacity_slider_video, size_slider_video],
outputs=output_video
)
if __name__ == "__main__":
demo.launch() |