onehowon commited on
Commit
83a4e09
ยท
1 Parent(s): ceee1cb

requirement

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -7,10 +7,9 @@ from art.attacks.evasion import FastGradientMethod
7
  from art.estimators.classification import PyTorchClassifier
8
  from PIL import Image
9
  import numpy as np
 
10
  import io
11
- import base64
12
  from blind_watermark import WaterMark
13
- import matplotlib.pyplot as plt
14
 
15
  # Pretrained ResNet50 ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ (ImageNet ์‚ฌ์ „ ํ›ˆ๋ จ)
16
  model = models.resnet50(pretrained=True)
@@ -73,26 +72,37 @@ def generate_adversarial_image(image, eps_value):
73
  def apply_watermark(image_pil):
74
  wm_text = "123"
75
  bwm = WaterMark(password_img=123, password_wm=456)
76
-
77
- # ์ด๋ฏธ์ง€์— ์›Œํ„ฐ๋งˆํฌ ์‚ฝ์ž…
78
- img_bytes = io.BytesIO()
79
- image_pil.save(img_bytes, format='PNG')
80
- bwm.read_img(img_bytes)
 
 
81
  bwm.read_wm(wm_text, mode='str')
82
 
83
  # ์›Œํ„ฐ๋งˆํฌ ์‚ฝ์ž…
84
- bwm.embed(img_bytes)
85
- return img_bytes
 
 
 
 
 
 
 
 
 
86
 
87
- # Gradio ํ•จ์ˆ˜ (์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๋ฐ ์ ๋Œ€์  ์˜ˆ์ œ ์ƒ์„ฑ)
88
  def process_image(image, eps_value):
89
  # ์ ๋Œ€์  ์ด๋ฏธ์ง€ ์ƒ์„ฑ
90
  adv_image = generate_adversarial_image(image, eps_value)
91
 
92
  # ์›Œํ„ฐ๋งˆํฌ ์ ์šฉ
93
- result_image = apply_watermark(adv_image)
94
 
95
- return result_image.getvalue()
96
 
97
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
98
  gr.Interface(
 
7
  from art.estimators.classification import PyTorchClassifier
8
  from PIL import Image
9
  import numpy as np
10
+ import os
11
  import io
 
12
  from blind_watermark import WaterMark
 
13
 
14
  # Pretrained ResNet50 ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ (ImageNet ์‚ฌ์ „ ํ›ˆ๋ จ)
15
  model = models.resnet50(pretrained=True)
 
72
  def apply_watermark(image_pil):
73
  wm_text = "123"
74
  bwm = WaterMark(password_img=123, password_wm=456)
75
+
76
+ # ์ด๋ฏธ์ง€ ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
77
+ temp_image_path = "temp_image.png"
78
+ image_pil.save(temp_image_path)
79
+
80
+ # temp_image_path ๊ฒฝ๋กœ๋กœ ์›Œํ„ฐ๋งˆํฌ ์‚ฝ์ž… ์ฒ˜๋ฆฌ
81
+ bwm.read_img(temp_image_path)
82
  bwm.read_wm(wm_text, mode='str')
83
 
84
  # ์›Œํ„ฐ๋งˆํฌ ์‚ฝ์ž…
85
+ output_path = "watermarked_image.png"
86
+ bwm.embed(output_path)
87
+
88
+ # ์‚ฝ์ž…๋œ ์›Œํ„ฐ๋งˆํฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋‹ค์‹œ ์ฝ์–ด์„œ PIL ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜
89
+ result_image = Image.open(output_path)
90
+
91
+ # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
92
+ os.remove(temp_image_path)
93
+ os.remove(output_path)
94
+
95
+ return result_image
96
 
97
+ # ์ „์ฒด ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
98
  def process_image(image, eps_value):
99
  # ์ ๋Œ€์  ์ด๋ฏธ์ง€ ์ƒ์„ฑ
100
  adv_image = generate_adversarial_image(image, eps_value)
101
 
102
  # ์›Œํ„ฐ๋งˆํฌ ์ ์šฉ
103
+ watermarked_image = apply_watermark(adv_image)
104
 
105
+ return watermarked_image
106
 
107
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
108
  gr.Interface(