Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,89 +9,118 @@ from datetime import datetime
|
|
9 |
import cv2
|
10 |
from PIL import Image
|
11 |
import numpy as np
|
|
|
|
|
|
|
|
|
12 |
|
13 |
class WatermarkGUI:
|
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 |
### Watermark Quality Report
|
46 |
-
- Quality Score: {quality_data
|
47 |
-
- PSNR: {quality_data
|
48 |
-
- Histogram Similarity: {quality_data
|
49 |
-
- Modified Pixels: {quality_data
|
50 |
|
51 |
### Metadata
|
52 |
- Author: {author}
|
53 |
- Purpose: {purpose}
|
54 |
- Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
77 |
### Extracted Watermark
|
78 |
-
Text: {data
|
79 |
|
80 |
### Metadata
|
81 |
-
- Timestamp: {data
|
82 |
-
- Author: {data
|
83 |
-
- Purpose: {data
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
### Welcome to Secure Watermark - Advanced Image Protection System
|
97 |
|
@@ -109,51 +138,50 @@ Text: {data['text']}
|
|
109 |
- Creative Work Protection
|
110 |
|
111 |
Try our system by uploading an image and adding your watermark below!
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
with gr.
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
)
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
|
157 |
if __name__ == "__main__":
|
158 |
-
|
159 |
-
|
|
|
9 |
import cv2
|
10 |
from PIL import Image
|
11 |
import numpy as np
|
12 |
+
import logging
|
13 |
+
|
14 |
+
# ๋ก๊น
์ค์ (๋๋ฒ๊ทธ ์ ๋ณด ์ถ๋ ฅ)
|
15 |
+
logging.basicConfig(level=logging.DEBUG)
|
16 |
|
17 |
class WatermarkGUI:
|
18 |
+
def __init__(self):
|
19 |
+
self.processor = WatermarkProcessor()
|
20 |
+
self.create_interface()
|
21 |
|
22 |
+
def process_watermark(self, image, watermark_text, author, purpose, opacity):
|
23 |
+
"""๋ฉํ๋ฐ์ดํฐ์ ํจ๊ป ์ํฐ๋งํฌ๋ฅผ ์ด๋ฏธ์ง์ ์ถ๊ฐ"""
|
24 |
+
if image is None or watermark_text.strip() == "":
|
25 |
+
return None, "์ด๋ฏธ์ง์ ์ํฐ๋งํฌ ํ
์คํธ๋ฅผ ๋ชจ๋ ์ ๊ณตํด์ฃผ์ธ์."
|
26 |
+
|
27 |
+
metadata = {
|
28 |
+
"author": author,
|
29 |
+
"purpose": purpose,
|
30 |
+
"opacity": opacity
|
31 |
+
}
|
32 |
+
|
33 |
+
# ์์ ์ด๋ฏธ์ง ์ ์ฅ
|
34 |
+
temp_path = tempfile.mktemp(suffix='.png')
|
35 |
+
try:
|
36 |
+
Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
|
37 |
+
logging.debug("์์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: %s", temp_path)
|
38 |
+
except Exception as e:
|
39 |
+
logging.error("์์ ์ด๋ฏธ์ง ์ ์ฅ ์ค ์ค๋ฅ: %s", e)
|
40 |
+
return None, f"์ด๋ฏธ์ง ์ ์ฅ ์ค๋ฅ: {e}"
|
41 |
+
|
42 |
+
# ์ํฐ๋งํฌ ์ถ๊ฐ
|
43 |
+
result_path, message = self.processor.encode(temp_path, watermark_text, metadata)
|
44 |
+
logging.debug("์ํฐ๋งํฌ ์ธ์ฝ๋ฉ ๊ฒฐ๊ณผ - result_path: %s, message: %s", result_path, message)
|
45 |
+
|
46 |
+
if "Error" in message:
|
47 |
+
os.remove(temp_path)
|
48 |
+
return None, message
|
49 |
+
|
50 |
+
# ํ์ง ๋ณด๊ณ ์ ์์ฑ
|
51 |
+
quality_report = self.processor.analyze_quality(temp_path, result_path)
|
52 |
+
try:
|
53 |
+
quality_data = json.loads(quality_report)
|
54 |
+
except Exception as e:
|
55 |
+
logging.error("ํ์ง ๋ณด๊ณ ์ ํ์ฑ ์ค๋ฅ: %s", e)
|
56 |
+
quality_data = {"quality_score": "N/A", "psnr": "N/A", "histogram_similarity": 0, "modified_pixels": 0}
|
57 |
+
|
58 |
+
report = f"""
|
59 |
### Watermark Quality Report
|
60 |
+
- Quality Score: {quality_data.get('quality_score', 'N/A')}%
|
61 |
+
- PSNR: {quality_data.get('psnr', 'N/A')} dB
|
62 |
+
- Histogram Similarity: {quality_data.get('histogram_similarity', 0) * 100:.2f}%
|
63 |
+
- Modified Pixels: {quality_data.get('modified_pixels', 0):,}
|
64 |
|
65 |
### Metadata
|
66 |
- Author: {author}
|
67 |
- Purpose: {purpose}
|
68 |
- Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
69 |
+
"""
|
70 |
+
|
71 |
+
os.remove(temp_path)
|
72 |
+
watermarked_image = cv2.imread(result_path)
|
73 |
+
if watermarked_image is None:
|
74 |
+
logging.error("์ํฐ๋งํฌ๊ฐ ์ถ๊ฐ๋ ์ด๋ฏธ์ง ๋ก๋ ์คํจ: %s", result_path)
|
75 |
+
return None, "์ํฐ๋งํฌ ์ด๋ฏธ์ง ๋ก๋์ ์คํจํ์ต๋๋ค."
|
76 |
+
return watermarked_image, report
|
77 |
|
78 |
+
def detect_watermark(self, image):
|
79 |
+
"""์ํฐ๋งํฌ ๊ฒ์ถ ๋ฐ ์ถ์ถ"""
|
80 |
+
if image is None:
|
81 |
+
return "์ด๋ฏธ์ง๋ฅผ ์ ๊ณตํด์ฃผ์ธ์."
|
82 |
+
|
83 |
+
# ์์ ์ด๋ฏธ์ง ์ ์ฅ
|
84 |
+
temp_path = tempfile.mktemp(suffix='.png')
|
85 |
+
try:
|
86 |
+
Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
|
87 |
+
logging.debug("์ํฐ๋งํฌ ๊ฒ์ถ์ฉ ์์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: %s", temp_path)
|
88 |
+
except Exception as e:
|
89 |
+
logging.error("๊ฒ์ถ์ฉ ์ด๋ฏธ์ง ์ ์ฅ ์ค ์ค๋ฅ: %s", e)
|
90 |
+
return f"์ด๋ฏธ์ง ์ ์ฅ ์ค๋ฅ: {e}"
|
91 |
+
|
92 |
+
# ์ํฐ๋งํฌ ์ถ์ถ
|
93 |
+
result = self.processor.decode(temp_path)
|
94 |
+
logging.debug("๋์ฝ๋ ๊ฒฐ๊ณผ: %s", result)
|
95 |
+
os.remove(temp_path)
|
96 |
+
|
97 |
+
try:
|
98 |
+
data = json.loads(result)
|
99 |
+
report = f"""
|
100 |
### Extracted Watermark
|
101 |
+
Text: {data.get('text', 'N/A')}
|
102 |
|
103 |
### Metadata
|
104 |
+
- Timestamp: {data.get('timestamp', 'N/A')}
|
105 |
+
- Author: {data.get('metadata', {}).get('author', 'N/A')}
|
106 |
+
- Purpose: {data.get('metadata', {}).get('purpose', 'N/A')}
|
107 |
+
"""
|
108 |
+
return report
|
109 |
+
except Exception as e:
|
110 |
+
logging.error("์ํฐ๋งํฌ ๋์ฝ๋ฉ ๊ฒฐ๊ณผ ํ์ฑ ์ค๋ฅ: %s", e)
|
111 |
+
return result
|
112 |
|
113 |
+
def create_interface(self):
|
114 |
+
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
115 |
+
with gr.Blocks(css="footer {visibility: hidden}") as self.interface:
|
116 |
+
gr.HTML(
|
117 |
+
"""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Ffantos-SecureWatermark.hf.space">
|
118 |
+
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffantos-SecureWatermark.hf.space&countColor=%23263759" />
|
119 |
+
</a>"""
|
120 |
+
)
|
121 |
+
|
122 |
+
gr.Markdown(
|
123 |
+
"""# Enhanced Image Watermarking System
|
124 |
|
125 |
### Welcome to Secure Watermark - Advanced Image Protection System
|
126 |
|
|
|
138 |
- Creative Work Protection
|
139 |
|
140 |
Try our system by uploading an image and adding your watermark below!
|
141 |
+
"""
|
142 |
+
)
|
143 |
+
|
144 |
+
with gr.Tabs():
|
145 |
+
# ์ํฐ๋งํฌ ์ถ๊ฐ ํญ
|
146 |
+
with gr.Tab("Add Watermark"):
|
147 |
+
with gr.Row():
|
148 |
+
with gr.Column():
|
149 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
150 |
+
watermark_text = gr.Textbox(label="Watermark Text")
|
151 |
+
author = gr.Textbox(label="Author", placeholder="Enter author name")
|
152 |
+
purpose = gr.Textbox(label="Purpose", placeholder="Enter watermark purpose")
|
153 |
+
opacity = gr.Slider(minimum=0.1, maximum=1.0, value=0.3, label="Watermark Opacity")
|
154 |
+
|
155 |
+
with gr.Row():
|
156 |
+
process_btn = gr.Button("Add Watermark", variant="primary")
|
157 |
+
with gr.Column():
|
158 |
+
result_image = gr.Image(label="Watermarked Image")
|
159 |
+
quality_report = gr.Markdown(label="Quality Report")
|
160 |
+
|
161 |
+
# ์ํฐ๋งํฌ ๊ฒ์ถ ํญ
|
162 |
+
with gr.Tab("Detect Watermark"):
|
163 |
+
with gr.Row():
|
164 |
+
detect_image = gr.Image(label="Input Image", type="numpy")
|
165 |
+
detect_result = gr.Markdown(label="Detected Watermark")
|
166 |
+
detect_btn = gr.Button("Detect Watermark")
|
167 |
+
|
168 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
|
169 |
+
process_btn.click(
|
170 |
+
fn=self.process_watermark,
|
171 |
+
inputs=[input_image, watermark_text, author, purpose, opacity],
|
172 |
+
outputs=[result_image, quality_report]
|
173 |
+
)
|
174 |
+
|
175 |
+
detect_btn.click(
|
176 |
+
fn=self.detect_watermark,
|
177 |
+
inputs=[detect_image],
|
178 |
+
outputs=detect_result
|
179 |
+
)
|
|
|
180 |
|
181 |
+
def launch(self, *args, **kwargs):
|
182 |
+
"""์ธํฐํ์ด์ค ์คํ"""
|
183 |
+
self.interface.launch(*args, **kwargs)
|
184 |
|
185 |
if __name__ == "__main__":
|
186 |
+
app = WatermarkGUI()
|
187 |
+
app.launch()
|