优化了抗哈希检测的方法
This commit is contained in:
parent
92a60af402
commit
0a1ba3e980
Binary file not shown.
@ -475,6 +475,127 @@ class PosterNotesCreator:
|
|||||||
image.save(data, format=image.format if image.format else 'PNG')
|
image.save(data, format=image.format if image.format else 'PNG')
|
||||||
return Image.open(data)
|
return Image.open(data)
|
||||||
|
|
||||||
|
def optimize_anti_hash_methods(self, image: Image.Image, strength: str = "medium") -> Image.Image:
|
||||||
|
"""
|
||||||
|
综合优化的哈希对抗方法,针对性处理aHash、pHash和dHash
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image: 输入图像
|
||||||
|
strength: 处理强度 - "low", "medium", "high"
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
处理后的图像
|
||||||
|
"""
|
||||||
|
# 根据强度设置参数
|
||||||
|
if strength == "low":
|
||||||
|
ahash_intensity = 0.03
|
||||||
|
phash_intensity = 0.04
|
||||||
|
dhash_intensity = 0.03
|
||||||
|
elif strength == "high":
|
||||||
|
ahash_intensity = 0.09
|
||||||
|
phash_intensity = 0.08
|
||||||
|
dhash_intensity = 0.09
|
||||||
|
else: # medium
|
||||||
|
ahash_intensity = 0.06
|
||||||
|
phash_intensity = 0.06
|
||||||
|
dhash_intensity = 0.06
|
||||||
|
|
||||||
|
# 1. 针对aHash (平均哈希)的处理 - 专注于整体亮度变化
|
||||||
|
# aHash对整体亮度敏感,创建局部亮度变化可有效对抗
|
||||||
|
img_array = np.array(image)
|
||||||
|
h, w = img_array.shape[0], img_array.shape[1]
|
||||||
|
|
||||||
|
# 创建10-20个随机块,并调整其亮度
|
||||||
|
num_blocks = random.randint(10, 20)
|
||||||
|
for _ in range(num_blocks):
|
||||||
|
# 随机选择块的位置和大小
|
||||||
|
block_w = random.randint(w//20, w//10)
|
||||||
|
block_h = random.randint(h//20, h//10)
|
||||||
|
x = random.randint(0, w - block_w)
|
||||||
|
y = random.randint(0, h - block_h)
|
||||||
|
|
||||||
|
# 随机调整块的亮度
|
||||||
|
delta = int(random.uniform(-25, 25) * ahash_intensity)
|
||||||
|
if len(img_array.shape) == 3: # 彩色图像
|
||||||
|
img_array[y:y+block_h, x:x+block_w, :] = np.clip(
|
||||||
|
img_array[y:y+block_h, x:x+block_w, :] + delta, 0, 255)
|
||||||
|
else: # 灰度图像
|
||||||
|
img_array[y:y+block_h, x:x+block_w] = np.clip(
|
||||||
|
img_array[y:y+block_h, x:x+block_w] + delta, 0, 255)
|
||||||
|
|
||||||
|
# 转回PIL图像
|
||||||
|
image = Image.fromarray(img_array.astype(np.uint8))
|
||||||
|
|
||||||
|
# 2. 调用现有的pHash对抗方法
|
||||||
|
image = self.add_phash_noise(image, intensity=phash_intensity)
|
||||||
|
|
||||||
|
# 3. 针对dHash (差值哈希)的处理 - 添加细微梯度扰动
|
||||||
|
# dHash对相邻像素梯度敏感,添加小梯度干扰非常有效
|
||||||
|
img_array = np.array(image)
|
||||||
|
|
||||||
|
# 计算图像尺寸并创建掩码
|
||||||
|
mask = np.zeros_like(img_array, dtype=bool)
|
||||||
|
grid_size = 8 # dHash通常是8x8
|
||||||
|
|
||||||
|
# 在图像中选择8-12条边缘线进行干扰
|
||||||
|
num_lines = random.randint(8, 12)
|
||||||
|
for _ in range(num_lines):
|
||||||
|
# 随机决定是水平线还是垂直线
|
||||||
|
if random.random() < 0.5: # 水平线
|
||||||
|
y = random.randint(0, h - 1)
|
||||||
|
line_width = random.randint(1, 3)
|
||||||
|
if len(mask.shape) == 3: # 彩色图像
|
||||||
|
mask[max(0, y-line_width//2):min(h, y+line_width//2+1), :, :] = True
|
||||||
|
else: # 灰度图像
|
||||||
|
mask[max(0, y-line_width//2):min(h, y+line_width//2+1), :] = True
|
||||||
|
else: # 垂直线
|
||||||
|
x = random.randint(0, w - 1)
|
||||||
|
line_width = random.randint(1, 3)
|
||||||
|
if len(mask.shape) == 3: # 彩色图像
|
||||||
|
mask[:, max(0, x-line_width//2):min(w, x+line_width//2+1), :] = True
|
||||||
|
else: # 灰度图像
|
||||||
|
mask[:, max(0, x-line_width//2):min(w, x+line_width//2+1)] = True
|
||||||
|
|
||||||
|
# 应用梯度干扰
|
||||||
|
if len(img_array.shape) == 3: # 彩色图像
|
||||||
|
delta = (np.random.random(img_array.shape) * 2 - 1) * dhash_intensity * 25
|
||||||
|
for c in range(img_array.shape[2]):
|
||||||
|
img_array[:,:,c][mask[:,:,c]] += delta[:,:,c][mask[:,:,c]]
|
||||||
|
else: # 灰度图像
|
||||||
|
delta = (np.random.random(img_array.shape) * 2 - 1) * dhash_intensity * 25
|
||||||
|
img_array[mask] += delta[mask]
|
||||||
|
|
||||||
|
img_array = np.clip(img_array, 0, 255).astype(np.uint8)
|
||||||
|
|
||||||
|
# 4. 颜色直方图扰动
|
||||||
|
image = Image.fromarray(img_array)
|
||||||
|
image = self.perturb_color_histogram(image, strength=dhash_intensity)
|
||||||
|
|
||||||
|
# 5. 区域翻转 - 特别有效对抗所有哈希算法
|
||||||
|
if strength != "low" and random.random() < 0.5:
|
||||||
|
img_array = np.array(image)
|
||||||
|
# 随机选择一个小区域
|
||||||
|
region_w = random.randint(w//30, w//20)
|
||||||
|
region_h = random.randint(h//30, h//20)
|
||||||
|
x = random.randint(0, w - region_w)
|
||||||
|
y = random.randint(0, h - region_h)
|
||||||
|
|
||||||
|
# 对区域进行水平或垂直翻转
|
||||||
|
if random.random() < 0.5: # 水平翻转
|
||||||
|
if len(img_array.shape) == 3: # 彩色图像
|
||||||
|
img_array[y:y+region_h, x:x+region_w, :] = img_array[y:y+region_h, x:x+region_w, :][:, ::-1, :]
|
||||||
|
else: # 灰度图像
|
||||||
|
img_array[y:y+region_h, x:x+region_w] = img_array[y:y+region_h, x:x+region_w][:, ::-1]
|
||||||
|
else: # 垂直翻转
|
||||||
|
if len(img_array.shape) == 3: # 彩色图像
|
||||||
|
img_array[y:y+region_h, x:x+region_w, :] = img_array[y:y+region_h, x:x+region_w, :][::-1, :, :]
|
||||||
|
else: # 灰度图像
|
||||||
|
img_array[y:y+region_h, x:x+region_w] = img_array[y:y+region_h, x:x+region_w][::-1, :]
|
||||||
|
|
||||||
|
image = Image.fromarray(img_array)
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
def optimized_process_image(
|
def optimized_process_image(
|
||||||
self,
|
self,
|
||||||
image: Image.Image,
|
image: Image.Image,
|
||||||
@ -583,20 +704,8 @@ class PosterNotesCreator:
|
|||||||
# 5. 新增 - 应用反查重技术
|
# 5. 新增 - 应用反查重技术
|
||||||
# 根据变化强度选择性应用
|
# 根据变化强度选择性应用
|
||||||
if use_extra:
|
if use_extra:
|
||||||
# 随机决定应用哪些反查重技术
|
# 使用综合优化的哈希对抗方法
|
||||||
apply_phash = random.random() < 0.7
|
processed_image = self.optimize_anti_hash_methods(processed_image, variation_strength)
|
||||||
apply_color = random.random() < 0.7
|
|
||||||
|
|
||||||
# 感知哈希干扰 (在中高强度变化时应用)
|
|
||||||
if apply_phash and variation_strength != "low":
|
|
||||||
phash_intensity = 0.05 if variation_strength == "medium" else 0.08
|
|
||||||
processed_image = self.add_phash_noise(processed_image, phash_intensity)
|
|
||||||
|
|
||||||
# 颜色直方图扰动
|
|
||||||
if apply_color:
|
|
||||||
color_strength = 0.02 if variation_strength == "low" else \
|
|
||||||
0.04 if variation_strength == "medium" else 0.06
|
|
||||||
processed_image = self.perturb_color_histogram(processed_image, color_strength)
|
|
||||||
|
|
||||||
# 应用额外效果 (只在需要时)
|
# 应用额外效果 (只在需要时)
|
||||||
if use_extra:
|
if use_extra:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user