Closed lizhigen-max closed 1 year ago
最好用原版代码来恢复,默认的那个命令行不好用,重点是“要把图片恢复成原始尺寸”,成功率在80%以上:
最重要的是要把图片恢复成原始尺寸后解密:
# recover from attack:
recover_crop(template_file='output/2022-09-30_15-20.png', output_file_name='output/截屏攻击2_还原.png',
loc=(x1, y1, x2, y2), image_o_shape=image_o_shape)
https://github.com/guofei9987/blind_watermark/blob/master/examples/example_str.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# embed string
import numpy as np
from blind_watermark import WaterMark
from blind_watermark import att
from blind_watermark.recover import estimate_crop_parameters, recover_crop
import cv2
bwm = WaterMark(password_img=77782589, password_wm=7758258)
bwm.read_img('pic/1648799417649ee7d05.png')
wm = '1998-2022 Yafe Co., Limited'
bwm.read_wm(wm, mode='str')
bwm.embed('output/embedded.png')
len_wm = len(bwm.wm_bit) # 解水印需要用到长度
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))
ori_img_shape = cv2.imread('pic/1648799417649ee7d05.png').shape[:2] # 抗攻击有时需要知道原图的shape
# %% 解水印
bwm1 = WaterMark(password_img=77782589, password_wm=7758258)
wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str')
print("不攻击的提取结果:", wm_extract)
assert wm == wm_extract, '提取水印和原水印不一致'
# %% 截屏攻击 = 剪切攻击 + 缩放攻击 + 不知道攻击参数
'''
loc_r = ((0.1, 0.1), (0.7, 0.6))
scale = 0.7
_, (x1, y1, x2, y2) = att.cut_att2(input_filename='output/embedded.png', output_file_name='output/截屏攻击2.png',
loc_r=loc_r, scale=scale)
print(f'Crop attack\'s real parameters: x1={x1},y1={y1},x2={x2},y2={y2}')
'''
# estimate crop attack parameters:
(x1, y1, x2, y2), image_o_shape, score, scale_infer = estimate_crop_parameters(original_file='pic/1648799417649ee7d05.png',
template_file='output/2022-09-30_15-20.png',
scale=(0.5, 2), search_num=200)
print(f'Crop attack\'s estimate parameters: x1={x1},y1={y1},x2={x2},y2={y2}. score={score}')
# recover from attack:
recover_crop(template_file='output/2022-09-30_15-20.png', output_file_name='output/截屏攻击2_还原.png',
loc=(x1, y1, x2, y2), image_o_shape=image_o_shape)
bwm1 = WaterMark(password_wm=7758258, password_img=77782589)
wm_extract = bwm1.extract('output/截屏攻击2_还原.png', wm_shape=len_wm, mode='str')
print("截屏攻击,不知道攻击参数。提取结果:", wm_extract)
assert wm == wm_extract, '提取水印和原水印不一致'
'''
# %%缩放攻击
#att.resize_att(input_filename='output/embedded.png', output_file_name='output/缩放攻击.png', out_shape=(400, 300))
att.resize_att(input_filename='output/embedded1.png', output_file_name='output/缩放攻击_还原.png',
out_shape=ori_img_shape[::-1])
# out_shape 是分辨率,需要颠倒一下
bwm1 = WaterMark(password_wm=7758258, password_img=77782589)
wm_extract = bwm1.extract('output/缩放攻击_还原.png', wm_shape=len_wm, mode='str')
print("缩放攻击后的提取结果:", wm_extract)
assert np.all(wm == wm_extract), '提取水印和原水印不一致'
# %%
'''
旋转或者截取图片后解码失败