Vivien Chappelier commited on
Commit
2342c94
Β·
1 Parent(s): e9d8edd
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -19,6 +19,8 @@ import json
19
  from PIL import Image, ImageEnhance
20
  import base64
21
  import io
 
 
22
 
23
  class BZHStableSignatureDemo(object):
24
 
@@ -53,7 +55,7 @@ class BZHStableSignatureDemo(object):
53
  output = self.pipe(prompt, num_inference_steps=4, guidance_scale=0.0, output_type="pil")
54
  return output.images[0] #{ "background": output.images[0], "layers": [], "composite": None }
55
 
56
- def attack_detect(self, img, jpeg_compression, downscale, saturation):
57
 
58
  #img = img_edit["composite"]
59
  img = img.convert("RGB")
@@ -63,6 +65,21 @@ class BZHStableSignatureDemo(object):
63
  size = img.size
64
  size = (int(size[0] / downscale), int(size[1] / downscale))
65
  img = img.resize(size, Image.BICUBIC)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  converter = ImageEnhance.Color(img)
68
  img = converter.enhance(saturation)
@@ -119,15 +136,17 @@ def interface():
119
  with gr.Row():
120
  watermarked_image = gr.Image(type="pil", width=512, height=512)
121
  with gr.Column():
 
122
  downscale = gr.Slider(1, 3, value=1, step=0.1, label="Downscale ratio")
 
123
  saturation = gr.Slider(0, 2, value=1, step=0.1, label="Color saturation")
124
  jpeg_compression = gr.Slider(value=100, step=5, label="JPEG quality")
125
- btn2 = gr.Button("Attack & Detect")
126
  with gr.Row():
127
  attacked_image = gr.Image(type="pil", width=256)
128
  detection_label = gr.Label(label="Detection info")
129
  btn1.click(fn=backend.generate, inputs=[mode, seed, inp], outputs=[watermarked_image], api_name="generate")
130
- btn2.click(fn=backend.attack_detect, inputs=[watermarked_image, jpeg_compression, downscale, saturation], outputs=[attacked_image, detection_label], api_name="detect")
131
 
132
  return demo
133
 
 
19
  from PIL import Image, ImageEnhance
20
  import base64
21
  import io
22
+ import random
23
+ import math
24
 
25
  class BZHStableSignatureDemo(object):
26
 
 
55
  output = self.pipe(prompt, num_inference_steps=4, guidance_scale=0.0, output_type="pil")
56
  return output.images[0] #{ "background": output.images[0], "layers": [], "composite": None }
57
 
58
+ def attack_detect(self, img, jpeg_compression, downscale, crop, saturation):
59
 
60
  #img = img_edit["composite"]
61
  img = img.convert("RGB")
 
65
  size = img.size
66
  size = (int(size[0] / downscale), int(size[1] / downscale))
67
  img = img.resize(size, Image.BICUBIC)
68
+ if crop != 0:
69
+ width, height = img.size
70
+ area = width * height
71
+ log_rmin = math.log(0.5)
72
+ log_rmax = math.log(2.0)
73
+ for _ in range(10):
74
+ target_area = area * (1 - crop)
75
+ aspect_ratio = math.exp(random.random() * (log_rmax - log_rmin) + log_rmin)
76
+ w = int(round(math.sqrt(target_area * aspect_ratio)))
77
+ h = int(round(math.sqrt(target_area / aspect_ratio)))
78
+ if 0 < w <= width and 0 < h <= height:
79
+ top = random.randint(0, height - h + 1)
80
+ left = random.randint(0, width - w + 1)
81
+ img = img.crop((left, top, left+w, top+h))
82
+ break
83
 
84
  converter = ImageEnhance.Color(img)
85
  img = converter.enhance(saturation)
 
136
  with gr.Row():
137
  watermarked_image = gr.Image(type="pil", width=512, height=512)
138
  with gr.Column():
139
+ gr.Markdown("""With these controls you may alter the generated image before detection. You may also upload your own edited image instead.""")
140
  downscale = gr.Slider(1, 3, value=1, step=0.1, label="Downscale ratio")
141
+ crop = gr.Slider(0, 0.9, value=0, step=0.01, label="Random crop ratio")
142
  saturation = gr.Slider(0, 2, value=1, step=0.1, label="Color saturation")
143
  jpeg_compression = gr.Slider(value=100, step=5, label="JPEG quality")
144
+ btn2 = gr.Button("Modify & Detect")
145
  with gr.Row():
146
  attacked_image = gr.Image(type="pil", width=256)
147
  detection_label = gr.Label(label="Detection info")
148
  btn1.click(fn=backend.generate, inputs=[mode, seed, inp], outputs=[watermarked_image], api_name="generate")
149
+ btn2.click(fn=backend.attack_detect, inputs=[watermarked_image, jpeg_compression, downscale, crop, saturation], outputs=[attacked_image, detection_label], api_name="detect")
150
 
151
  return demo
152