Spaces:
Sleeping
Sleeping
| 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() | |