File size: 15,843 Bytes
d849382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import sys
if "APP_PATH" in os.environ:
    os.chdir(os.environ["APP_PATH"])
    # fix sys.path for import
    sys.path.append(os.getcwd())

import gradio as gr

import re
import string
import random
import os
import numpy as np
from PIL import Image
import torch
import torch.nn.functional as F
from torchvision import transforms


from watermark_anything.data.metrics import msg_predict_inference
from notebooks.inference_utils import (
    load_model_from_checkpoint,
    default_transform,
    unnormalize_img,
    create_random_mask,
    plot_outputs,
    msg2str,
    torch_to_np,
    multiwm_dbscan
)

# Device configuration
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Constants
proportion_masked = 0.5  # Proportion of image to be watermarked
epsilon = 1  # min distance between decoded messages in a cluster
min_samples = 500  # min number of pixels in a 256x256 image to form a cluster

# Color map for visualization
color_map = {
    -1: [0, 0, 0],      # Black for -1
    0: [255, 0, 255],   # ? for 0
    1: [255, 0, 0],     # Red for 1
    2: [0, 255, 0],     # Green for 2
    3: [0, 0, 255],     # Blue for 3
    4: [255, 255, 0],   # Yellow for 4
}

def load_wam():
    # Load the model from the specified checkpoint
    exp_dir = "checkpoints"
    json_path = os.path.join(exp_dir, "params.json")
    ckpt_path = os.path.join(exp_dir, 'checkpoint.pth')
    wam = load_model_from_checkpoint(json_path, ckpt_path).to(device).eval()
    return wam

def image_detect(img_pil: Image.Image) -> (torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor):
    img_pt = default_transform(img_pil).unsqueeze(0).to(device)  # [1, 3, H, W]

    # Detect the watermark in the multi-watermarked image
    preds = wam.detect(img_pt)["preds"]  # [1, 33, 256, 256]
    mask_preds = F.sigmoid(preds[:, 0, :, :])  # [1, 256, 256], predicted mask
    mask_preds_res = F.interpolate(mask_preds.unsqueeze(1), size=(img_pt.shape[-2], img_pt.shape[-1]), mode="bilinear", align_corners=False)  # [1, 1, H, W]
    bit_preds = preds[:, 1:, :, :]  # [1, 32, 256, 256], predicted bits

    # positions has the cluster number at each pixel. can be upsaled back to the original size.
    try:
        centroids, positions = multiwm_dbscan(bit_preds, mask_preds, epsilon=epsilon, min_samples=min_samples)
        centroids_pt = torch.stack(list(centroids.values()))
    except (UnboundLocalError) as e:
        print(f"Error while detecting watermark: {e}")
        positions = None
        centroids = None
        centroids_pt = None

    return img_pt, (mask_preds_res>0.5).float(), positions, centroids, centroids_pt

def image_embed(img_pil: Image.Image, wm_msgs: torch.Tensor, wm_masks: torch.Tensor) -> (torch.Tensor, torch.Tensor, torch.Tensor):
    img_pt = default_transform(img_pil).unsqueeze(0).to(device)  # [1, 3, H, W]

    # Embed the watermark message into the image
    # Mask to use. 1 values correspond to pixels where the watermark will be placed.
    multi_wm_img = img_pt.clone()
    for ii in range(len(wm_msgs)):
        wm_msg, mask = wm_msgs[ii].unsqueeze(0), wm_masks[ii]
        outputs = wam.embed(img_pt, wm_msg)
        multi_wm_img = outputs['imgs_w'] * mask + multi_wm_img * (1 - mask)

    torch.cuda.empty_cache()
    return img_pt, multi_wm_img, wm_masks.sum(0)

def create_bounding_mask(img_size, boxes):
    """Create a binary mask from bounding boxes.

    Args:
        img_size (tuple): Image size (height, width)
        boxes (list): List of tuples (x1, y1, x2, y2) defining bounding boxes

    Returns:
        torch.Tensor: Binary mask tensor
    """
    mask = torch.zeros(img_size)
    for x1, y1, x2, y2 in boxes:
        mask[y1:y2, x1:x2] = 1
    return mask

def centroid_to_hex(centroid):
    binary_int = 0
    for bit in centroid:
        binary_int = (binary_int << 1) | int(bit.item())
    return format(binary_int, '08x')

# Load the model
wam = load_wam()

def detect_watermark(image):
    if image is None:
        return None, None, None, {"status": "error", "messages": [], "error": "No image provided"}

    img_pil = Image.fromarray(image).convert("RGB")
    det_img, pred, positions, centroids, centroids_pt = image_detect(img_pil)

    # Convert tensor images to numpy for display
    detected_img = torch_to_np(det_img.detach())
    pred_mask = torch_to_np(pred.detach().repeat(1, 3, 1, 1))

    # Create cluster visualization
    if positions is not None:
        resize_ori = transforms.Resize(det_img.shape[-2:])
        rgb_image = torch.zeros((3, positions.shape[-1], positions.shape[-2]), dtype=torch.uint8)
        for value, color in color_map.items():
            mask_ = positions == value
            for channel, color_value in enumerate(color):
                rgb_image[channel][mask_.squeeze()] = color_value
        rgb_image = resize_ori(rgb_image.float()/255)
        cluster_viz = rgb_image.permute(1, 2, 0).numpy()

        # Create message output as JSON
        messages = []
        for key in centroids.keys():
            centroid_hex = centroid_to_hex(centroids[key])
            centroid_hex_array = "-".join([centroid_hex[i:i+4] for i in range(0, len(centroid_hex), 4)])
            messages.append({
                "id": int(key),
                "message": centroid_hex_array,
                "color": color_map[key]
            })
        message_json = {
            "status": "success",
            "messages": messages,
            "count": len(messages)
        }
    else:
        cluster_viz = np.zeros_like(detected_img)
        message_json = {
            "status": "no_detection",
            "messages": [],
            "count": 0
        }

    return pred_mask, cluster_viz, message_json

def embed_watermark(image, wm_num, wm_type, wm_str, wm_loc):
    if image is None:
        return None, None, {
            "status": "failure",
            "messages": "No image provided"
        }

    if wm_type == "input":
        if not re.match(r"^([0-9A-F]{4}-[0-9A-F]{4}-){%d}[0-9A-F]{4}-[0-9A-F]{4}$" % (wm_num-1), wm_str):
            tip = "-".join([f"FFFF-{_}{_}{_}{_}" for _ in range(wm_num)])
            return None, None, {
                "status": "failure",
                "messages": f"Invalid type input. Please use {tip}"
            }

    if wm_loc == "bounding":
        if ROI_coordinates['clicks'] != wm_num * 2:
            return None, None, {
                "status": "failure",
                "messages": "Invalid location input. Please draw at least %d bounding ROI" % (wm_num)
            }

    img_pil = Image.fromarray(image).convert("RGB")

    # Generate watermark messages based on type
    wm_msgs = []
    if wm_type == "random":
        chars = '-'.join(''.join(random.choice(string.hexdigits) for _ in range(4)) for _ in range(wm_num * 2))
        wm_str = chars.lower()
    wm_hex = wm_str.replace("-", "")
    for i in range(0, len(wm_hex), 8):
        chunk = wm_hex[i:i+8]
        binary = bin(int(chunk, 16))[2:].zfill(32)
        wm_msgs.append([int(b) for b in binary])
    # Define a 32-bit message to be embedded into the images
    wm_msgs = torch.tensor(wm_msgs, dtype=torch.float32).to(device)

    # Create mask based on location type
    wm_masks = None
    if wm_loc == "random":
        img_pt = default_transform(img_pil).unsqueeze(0).to(device)
        # To ensure at least `proportion_masked %` of the width is randomly usable,
        # otherwise, it is easy to enter an infinite loop and fail to find a usable width.
        mask_percentage = img_pil.height / img_pil.width * proportion_masked / wm_num
        wm_masks = create_random_mask(img_pt, num_masks=wm_num, mask_percentage=mask_percentage)
    elif wm_loc == "bounding" and sections:
        wm_masks = torch.zeros((len(sections), 1, img_pil.height, img_pil.width), dtype=torch.float32).to(device)
        for idx, ((x_start, y_start, x_end, y_end), _) in enumerate(sections):
            left = min(x_start, x_end)
            right = max(x_start, x_end)
            top = min(y_start, y_end)
            bottom = max(y_start, y_end)
            wm_masks[idx, 0, top:bottom, left:right] = 1


    img_pt, embed_img_pt, embed_mask_pt = image_embed(img_pil, wm_msgs, wm_masks)

    # Convert to numpy for display
    img_np = torch_to_np(embed_img_pt.detach())
    mask_np = torch_to_np(embed_mask_pt.detach().expand(3, -1, -1))
    message_json = {
        "status": "success",
        "messages": wm_str
    }
    return img_np, mask_np, message_json



# ROI means Region Of Interest. It is the region where the user clicks
# to specify the location of the watermark.
ROI_coordinates = {
    'x_temp': 0,
    'y_temp': 0,
    'x_new': 0,
    'y_new': 0,
    'clicks': 0,
}

sections = []

def get_select_coordinates(img, evt: gr.SelectData, num):
    if ROI_coordinates['clicks'] >= num * 2:
        gr.Warning(f"Cant add more than {num} of Watermarks.")
        return (img, sections)

    # update new coordinates
    ROI_coordinates['clicks'] += 1
    ROI_coordinates['x_temp'] = ROI_coordinates['x_new']
    ROI_coordinates['y_temp'] = ROI_coordinates['y_new']
    ROI_coordinates['x_new'] = evt.index[0]
    ROI_coordinates['y_new'] = evt.index[1]
    # compare start end coordinates
    x_start = ROI_coordinates['x_new'] if (ROI_coordinates['x_new'] < ROI_coordinates['x_temp']) else ROI_coordinates['x_temp']
    y_start = ROI_coordinates['y_new'] if (ROI_coordinates['y_new'] < ROI_coordinates['y_temp']) else ROI_coordinates['y_temp']
    x_end = ROI_coordinates['x_new'] if (ROI_coordinates['x_new'] > ROI_coordinates['x_temp']) else ROI_coordinates['x_temp']
    y_end = ROI_coordinates['y_new'] if (ROI_coordinates['y_new'] > ROI_coordinates['y_temp']) else ROI_coordinates['y_temp']
    if ROI_coordinates['clicks'] % 2 == 0:
        sections[len(sections) - 1] = ((x_start, y_start, x_end, y_end), f"Mask {len(sections)}")
        # both start and end point get
        return (img, sections)
    else:
        point_width = int(img.shape[0]*0.05)
        sections.append(((ROI_coordinates['x_new'], ROI_coordinates['y_new'],
                          ROI_coordinates['x_new'] + point_width, ROI_coordinates['y_new'] + point_width),
                        f"Click second point for Mask {len(sections) + 1}"))
        return (img, sections)

def del_select_coordinates(img, evt: gr.SelectData):
    del sections[evt.index]
    # recreate section names
    for i in range(len(sections)):
        sections[i] = (sections[i][0], f"Mask {i + 1}")

    # last section clicking second point not complete
    if ROI_coordinates['clicks'] % 2 != 0:
        if len(sections) == evt.index:
            # delete last section
            ROI_coordinates['clicks'] -= 1
        else:
            # recreate last section name for second point
            ROI_coordinates['clicks'] -= 2
            sections[len(sections) - 1] = (sections[len(sections) - 1][0], f"Click second point for Mask {len(sections) + 1}")
    else:
        ROI_coordinates['clicks'] -= 2

    return (img[0], sections)

with gr.Blocks(title="Watermark Anything Demo") as demo:
    gr.Markdown("""
    # Watermark Anything Demo
    This app demonstrates watermark detection and embedding using the Watermark Anything model.
    Find the project [here](https://github.com/facebookresearch/watermark-anything).
    """)

    with gr.Tabs():
        with gr.TabItem("Embed Watermark"):
            with gr.Row():
                with gr.Column():
                    embedding_img = gr.Image(label="Input Image", type="numpy")

                with gr.Column():
                    embedding_num = gr.Slider(1, 5, value=1, step=1, label="Number of Watermarks")
                    embedding_type = gr.Radio(["random", "input"], value="random", label="Type", info="Type of watermarks")
                    embedding_str = gr.Textbox(label="Watermark Text", visible=False, show_copy_button=True)
                    embedding_loc = gr.Radio(["random", "bounding"], value="random", label="Location", info="Location of watermarks")

                    @gr.render(inputs=embedding_loc)
                    def show_split(wm_loc):
                        if wm_loc == "bounding":
                            embedding_box = gr.AnnotatedImage(
                                label="ROI",
                                color_map={
                                    "ROI of Watermark embedding": "#9987FF",
                                    "Click second point for ROI": "#f44336"}
                            )

                            embedding_img.select(
                                fn=get_select_coordinates,
                                inputs=[embedding_img, embedding_num],
                                outputs=embedding_box)
                            embedding_box.select(
                                fn=del_select_coordinates,
                                inputs=embedding_box,
                                outputs=embedding_box
                            )
                        else:
                            embedding_img.select()

                    embedding_btn = gr.Button("Embed Watermark")
                    marked_msg = gr.JSON(label="Marked Messages")
            with gr.Row():
                marked_image = gr.Image(label="Watermarked Image")
                marked_mask = gr.Image(label="Position of the watermark")

            def visible_text_label(embedding_type, embedding_num):
                if embedding_type == "input":
                    tip = "-".join([f"FFFF-{_}{_}{_}{_}" for _ in range(embedding_num)])
                    return gr.update(visible=True, label=f"Watermark Text (Format: {tip})")
                else:
                    return gr.update(visible=False)

            def check_embedding_str(embedding_str, embedding_num):
                if not re.match(r"^([0-9A-F]{4}-[0-9A-F]{4}-){%d}[0-9A-F]{4}-[0-9A-F]{4}$" % (embedding_num-1), embedding_str):
                    tip = "-".join([f"FFFF-{_}{_}{_}{_}" for _ in range(embedding_num)])
                    gr.Warning(f"Invalid format. Please use {tip}", duration=0)
                    return gr.update(interactive=False)
                else:
                    return gr.update(interactive=True)

            embedding_num.change(
                fn=visible_text_label,
                inputs=[embedding_type, embedding_num],
                outputs=[embedding_str]
            )
            embedding_type.change(
                fn=visible_text_label,
                inputs=[embedding_type, embedding_num],
                outputs=[embedding_str]
            )
            embedding_str.change(
                fn=check_embedding_str,
                inputs=[embedding_str, embedding_num],
                outputs=[embedding_btn]
            )

            embedding_btn.click(
                fn=embed_watermark,
                inputs=[embedding_img, embedding_num, embedding_type, embedding_str, embedding_loc],
                outputs=[marked_image, marked_mask, marked_msg]
            )

        with gr.TabItem("Detect Watermark"):
            with gr.Row():
                with gr.Column():
                    detecting_img = gr.Image(label="Input Image", type="numpy")
                with gr.Column():
                    detecting_btn = gr.Button("Detect Watermark")
                    predicted_messages = gr.JSON(label="Detected Messages")
            with gr.Row():
                predicted_mask = gr.Image(label="Predicted Watermark Position")
                predicted_cluster = gr.Image(label="Watermark Clusters")

            detecting_btn.click(
                fn=detect_watermark,
                inputs=[detecting_img],
                outputs=[predicted_mask, predicted_cluster, predicted_messages]
            )

demo.launch()