Spaces:
Running
Running
File size: 7,493 Bytes
23f56a9 32b5c3d b0053e2 32b5c3d b0053e2 32b5c3d b0053e2 32b5c3d b0053e2 32b5c3d b0053e2 32b5c3d b0053e2 d5eb308 b0053e2 32b5c3d b0053e2 32b5c3d b0053e2 d5eb308 b0053e2 32b5c3d b0053e2 5eccf13 15ad7ae b0053e2 32b5c3d b0053e2 32b5c3d 2a90940 b0053e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import gradio as gr
from utils import WatermarkProcessor
import json
import tempfile
import os
from datetime import datetime
import cv2
from PIL import Image
import numpy as np
import logging
# ๋ก๊น
์ค์ (๋๋ฒ๊ทธ ์ ๋ณด ์ถ๋ ฅ)
logging.basicConfig(level=logging.DEBUG)
class WatermarkGUI:
def __init__(self):
self.processor = WatermarkProcessor()
self.create_interface()
def process_watermark(self, image, watermark_text, author, purpose, opacity):
"""๋ฉํ๋ฐ์ดํฐ์ ํจ๊ป ์ํฐ๋งํฌ๋ฅผ ์ด๋ฏธ์ง์ ์ถ๊ฐ"""
if image is None or watermark_text.strip() == "":
return None, "์ด๋ฏธ์ง์ ์ํฐ๋งํฌ ํ
์คํธ๋ฅผ ๋ชจ๋ ์ ๊ณตํด์ฃผ์ธ์."
metadata = {
"author": author,
"purpose": purpose,
"opacity": opacity
}
# ์์ ์ด๋ฏธ์ง ์ ์ฅ
temp_path = tempfile.mktemp(suffix='.png')
try:
Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
logging.debug("์์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: %s", temp_path)
except Exception as e:
logging.error("์์ ์ด๋ฏธ์ง ์ ์ฅ ์ค ์ค๋ฅ: %s", e)
return None, f"์ด๋ฏธ์ง ์ ์ฅ ์ค๋ฅ: {e}"
# ์ํฐ๋งํฌ ์ถ๊ฐ
result_path, message = self.processor.encode(temp_path, watermark_text, metadata)
logging.debug("์ํฐ๋งํฌ ์ธ์ฝ๋ฉ ๊ฒฐ๊ณผ - result_path: %s, message: %s", result_path, message)
if "Error" in message:
os.remove(temp_path)
return None, message
# ํ์ง ๋ณด๊ณ ์ ์์ฑ
quality_report = self.processor.analyze_quality(temp_path, result_path)
try:
quality_data = json.loads(quality_report)
except Exception as e:
logging.error("ํ์ง ๋ณด๊ณ ์ ํ์ฑ ์ค๋ฅ: %s", e)
quality_data = {"quality_score": "N/A", "psnr": "N/A", "histogram_similarity": 0, "modified_pixels": 0}
report = f"""
### Watermark Quality Report
- Quality Score: {quality_data.get('quality_score', 'N/A')}%
- PSNR: {quality_data.get('psnr', 'N/A')} dB
- Histogram Similarity: {quality_data.get('histogram_similarity', 0) * 100:.2f}%
- Modified Pixels: {quality_data.get('modified_pixels', 0):,}
### Metadata
- Author: {author}
- Purpose: {purpose}
- Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""
os.remove(temp_path)
watermarked_image = cv2.imread(result_path)
if watermarked_image is None:
logging.error("์ํฐ๋งํฌ๊ฐ ์ถ๊ฐ๋ ์ด๋ฏธ์ง ๋ก๋ ์คํจ: %s", result_path)
return None, "์ํฐ๋งํฌ ์ด๋ฏธ์ง ๋ก๋์ ์คํจํ์ต๋๋ค."
return watermarked_image, report
def detect_watermark(self, image):
"""์ํฐ๋งํฌ ๊ฒ์ถ ๋ฐ ์ถ์ถ"""
if image is None:
return "์ด๋ฏธ์ง๋ฅผ ์ ๊ณตํด์ฃผ์ธ์."
# ์์ ์ด๋ฏธ์ง ์ ์ฅ
temp_path = tempfile.mktemp(suffix='.png')
try:
Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).save(temp_path)
logging.debug("์ํฐ๋งํฌ ๊ฒ์ถ์ฉ ์์ ์ด๋ฏธ์ง ์ ์ฅ ์๋ฃ: %s", temp_path)
except Exception as e:
logging.error("๊ฒ์ถ์ฉ ์ด๋ฏธ์ง ์ ์ฅ ์ค ์ค๋ฅ: %s", e)
return f"์ด๋ฏธ์ง ์ ์ฅ ์ค๋ฅ: {e}"
# ์ํฐ๋งํฌ ์ถ์ถ
result = self.processor.decode(temp_path)
logging.debug("๋์ฝ๋ ๊ฒฐ๊ณผ: %s", result)
os.remove(temp_path)
try:
# JSON ํ์ฑ ์๋
data = json.loads(result)
report = f"""
### Extracted Watermark
Text: {data.get('text', 'N/A')}
### Metadata
- Timestamp: {data.get('timestamp', 'N/A')}
- Author: {data.get('metadata', {}).get('author', 'N/A')}
- Purpose: {data.get('metadata', {}).get('purpose', 'N/A')}
"""
return report
except Exception as e:
logging.error("์ํฐ๋งํฌ ๋์ฝ๋ฉ ๊ฒฐ๊ณผ ํ์ฑ ์ค๋ฅ: %s", e)
# JSON ํ์ฑ ์คํจ ์, ์๋ณธ ๊ฒฐ๊ณผ ๋ฐํ
return result
def create_interface(self):
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
with gr.Blocks(css="footer {visibility: hidden}") as self.interface:
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>"""
)
gr.Markdown(
"""# Enhanced Image Watermarking System
### Welcome to Secure Watermark - Advanced Image Protection System
๐ **Key Features:**
- **Dual Watermarking Technology**: Supports both steganography and PNG metadata
- **Secure Encryption**: Military-grade encryption for watermark data
- **Quality Assurance**: Real-time quality analysis and reporting
- **Metadata Support**: Track authorship, purpose, and timestamps
- **Integrity Verification**: Hash-based image tampering detection
๐ก **Perfect for:**
- Copyright Protection
- Digital Asset Management
- Document Authentication
- Creative Work Protection
Try our system by uploading an image and adding your watermark below!
"""
)
with gr.Tabs():
# ์ํฐ๋งํฌ ์ถ๊ฐ ํญ
with gr.Tab("Add Watermark"):
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input Image", type="numpy")
watermark_text = gr.Textbox(label="Watermark Text")
author = gr.Textbox(label="Author", placeholder="Enter author name")
purpose = gr.Textbox(label="Purpose", placeholder="Enter watermark purpose")
opacity = gr.Slider(minimum=0.1, maximum=1.0, value=0.3, label="Watermark Opacity")
with gr.Row():
process_btn = gr.Button("Add Watermark", variant="primary")
with gr.Column():
result_image = gr.Image(label="Watermarked Image")
quality_report = gr.Markdown(label="Quality Report")
# ์ํฐ๋งํฌ ๊ฒ์ถ ํญ
with gr.Tab("Detect Watermark"):
with gr.Row():
detect_image = gr.Image(label="Input Image", type="numpy")
detect_result = gr.Markdown(label="Detected Watermark")
detect_btn = gr.Button("Detect Watermark")
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
process_btn.click(
fn=self.process_watermark,
inputs=[input_image, watermark_text, author, purpose, opacity],
outputs=[result_image, quality_report]
)
detect_btn.click(
fn=self.detect_watermark,
inputs=[detect_image],
outputs=detect_result
)
def launch(self, *args, **kwargs):
"""์ธํฐํ์ด์ค ์คํ"""
self.interface.launch(*args, **kwargs)
if __name__ == "__main__":
app = WatermarkGUI()
app.launch()
|