fantos commited on
Commit
5eccf13
·
verified ·
1 Parent(s): e6b6ac7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -103
app.py CHANGED
@@ -11,37 +11,37 @@ 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
@@ -52,28 +52,28 @@ class WatermarkGUI:
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
 
@@ -81,18 +81,18 @@ Text: {data['text']}
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%2Fgunship999-SecureWatermark.hf.space"> <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgunship999-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
 
98
  🔒 **Key Features:**
@@ -109,47 +109,44 @@ 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
-
117
- with gr.Tab("Add Watermark"):
118
- with gr.Row():
119
- with gr.Column():
120
- input_image = gr.Image(label="Input Image", type="numpy")
121
- watermark_text = gr.Textbox(label="Watermark Text")
122
- author = gr.Textbox(label="Author", placeholder="Enter author name")
123
- purpose = gr.Textbox(label="Purpose", placeholder="Enter watermark purpose")
124
- opacity = gr.Slider(minimum=0.1, maximum=1.0, value=0.3,
125
- label="Watermark Opacity")
126
-
127
- with gr.Row():
128
- process_btn = gr.Button("Add Watermark", variant="primary")
129
-
130
- with gr.Column():
131
- result_image = gr.Image(label="Watermarked Image")
132
- quality_report = gr.Markdown(label="Quality Report")
133
-
134
- # Detect Watermark Tab
135
- with gr.Tab("Detect Watermark"):
136
- with gr.Row():
137
- detect_image = gr.Image(label="Input Image", type="numpy")
138
- detect_result = gr.Markdown(label="Detected Watermark")
139
- detect_btn = gr.Button("Detect Watermark")
140
-
141
- # Event handlers
142
- process_btn.click(
143
- fn=self.process_watermark,
144
- inputs=[input_image, watermark_text, author, purpose, opacity],
145
- outputs=[result_image, quality_report]
146
- )
147
-
148
- detect_btn.click(
149
- fn=self.detect_watermark,
150
- inputs=[detect_image],
151
- outputs=detect_result
152
- )
153
 
154
  def launch(self, *args, **kwargs):
155
  """Launch the interface"""
 
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
 
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
 
 
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%2Fgunship999-SecureWatermark.hf.space"> <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgunship999-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
 
98
  🔒 **Key Features:**
 
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
 
151
  def launch(self, *args, **kwargs):
152
  """Launch the interface"""