fantos commited on
Commit
b0053e2
ยท
verified ยท
1 Parent(s): 8de8647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -114
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
- def __init__(self):
15
- self.processor = WatermarkProcessor()
16
- self.create_interface()
17
 
18
- def process_watermark(self, image, watermark_text, author, purpose, opacity):
19
- """Process watermark with metadata"""
20
- if image is None or watermark_text.strip() == "":
21
- return None, "Please provide both image and watermark text"
22
-
23
- metadata = {
24
- "author": author,
25
- "purpose": purpose,
26
- "opacity": opacity
27
- }
28
-
29
- # Save temporary image
30
- temp_path = tempfile.mktemp(suffix='.png')
31
- Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
32
-
33
- # Add watermark
34
- result_path, message = self.processor.encode(temp_path, watermark_text, metadata)
35
-
36
- if "Error" in message:
37
- return None, message
38
-
39
- # Generate quality report
40
- quality_report = self.processor.analyze_quality(temp_path, result_path)
41
- quality_data = json.loads(quality_report)
42
-
43
- # Create formatted report
44
- report = f"""
 
 
 
 
 
 
 
 
 
 
45
  ### Watermark Quality Report
46
- - Quality Score: {quality_data['quality_score']}%
47
- - PSNR: {quality_data['psnr']} dB
48
- - Histogram Similarity: {quality_data['histogram_similarity'] * 100:.2f}%
49
- - Modified Pixels: {quality_data['modified_pixels']:,}
50
 
51
  ### Metadata
52
  - Author: {author}
53
  - Purpose: {purpose}
54
  - Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
55
- """
56
-
57
- os.remove(temp_path)
58
- return cv2.imread(result_path), report
 
 
 
 
59
 
60
- def detect_watermark(self, image):
61
- """Detect and extract watermark"""
62
- if image is None:
63
- return "Please provide an image"
64
-
65
- # Save temporary image
66
- temp_path = tempfile.mktemp(suffix='.png')
67
- Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
68
-
69
- # Extract watermark
70
- result = self.processor.decode(temp_path)
71
- os.remove(temp_path)
72
-
73
- try:
74
- # Parse JSON result
75
- data = json.loads(result)
76
- report = f"""
 
 
 
 
 
77
  ### Extracted Watermark
78
- Text: {data['text']}
79
 
80
  ### Metadata
81
- - Timestamp: {data['timestamp']}
82
- - Author: {data['metadata'].get('author', 'N/A')}
83
- - Purpose: {data['metadata'].get('purpose', 'N/A')}
84
- """
85
- return report
86
- except:
87
- return result
 
88
 
89
- def create_interface(self):
90
- """Create Gradio interface"""
91
- with gr.Blocks(css="footer {visibility: hidden}") as self.interface:
92
- gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Ffantos-SecureWatermark.hf.space"> <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffantos-SecureWatermark.hf.space&countColor=%23263759" /> </a>""")
93
-
94
- gr.Markdown("""# Enhanced Image Watermarking System
 
 
 
 
 
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
- with gr.Tabs():
115
- # Add Watermark Tab
116
- with gr.Tab("Add Watermark"):
117
- with gr.Row():
118
- with gr.Column():
119
- input_image = gr.Image(label="Input Image", type="numpy")
120
- watermark_text = gr.Textbox(label="Watermark Text")
121
- author = gr.Textbox(label="Author", placeholder="Enter author name")
122
- purpose = gr.Textbox(label="Purpose", placeholder="Enter watermark purpose")
123
- opacity = gr.Slider(minimum=0.1, maximum=1.0, value=0.3,
124
- label="Watermark Opacity")
125
-
126
- with gr.Row():
127
- process_btn = gr.Button("Add Watermark", variant="primary")
128
-
129
- with gr.Column():
130
- result_image = gr.Image(label="Watermarked Image")
131
- quality_report = gr.Markdown(label="Quality Report")
132
-
133
- # Detect Watermark Tab
134
- with gr.Tab("Detect Watermark"):
135
- with gr.Row():
136
- detect_image = gr.Image(label="Input Image", type="numpy")
137
- detect_result = gr.Markdown(label="Detected Watermark")
138
- detect_btn = gr.Button("Detect Watermark")
139
-
140
- # Event handlers
141
- process_btn.click(
142
- fn=self.process_watermark,
143
- inputs=[input_image, watermark_text, author, purpose, opacity],
144
- outputs=[result_image, quality_report]
145
- )
146
-
147
- detect_btn.click(
148
- fn=self.detect_watermark,
149
- inputs=[detect_image],
150
- outputs=detect_result
151
- )
152
 
153
- def launch(self, *args, **kwargs):
154
- """Launch the interface"""
155
- self.interface.launch(*args, **kwargs)
156
 
157
  if __name__ == "__main__":
158
- app = WatermarkGUI()
159
- app.launch()
 
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()