Spaces:
Sleeping
Upload 2 files
Browse files# ๐ฑ ๋ชจ๋ฐ์ผ ํ๊ฒฝ CNN ๊ธฐ๋ฐ ์ํฐ๋งํน ์์คํ
์ค์๊ฐ ์ด๋ฏธ์ง ์ํฐ๋งํน ๋ฐ ๊ฒ์ฆ์ ์ํ ๊ฒฝ๋ํ๋ ๋ฅ๋ฌ๋ ์์คํ
์
๋๋ค.
## ๐ ์ฃผ์ ๊ธฐ๋ฅ
### ๐ ์ํฐ๋งํฌ ์ฝ์
- **์ค์๊ฐ ์ฒ๋ฆฌ**: ๊ฒฝ๋ํ๋ CNN ๋ชจ๋ธ๋ก ๋น ๋ฅธ ์ํฐ๋งํฌ ์ฝ์
- **์ ์ํ ํด์๋**: ๋ค์ํ ์ด๋ฏธ์ง ํฌ๊ธฐ์ ์๋ ์ ์
- **์ฌ์ฉ์๋ณ ๊ณ ์ ํจํด**: ์ฌ์ฉ์ ID ๊ธฐ๋ฐ ๊ฐ์ธํ๋ ์ํฐ๋งํฌ ์์ฑ
### ๐ ์ํฐ๋งํฌ ๊ฒ์ฆ
- **ํจํด ์ถ์ถ**: ์ด๋ฏธ์ง์์ ์ํฐ๋งํฌ ํจํด ์ถ์ถ ๋ฐ ์๊ฐํ
- **์ ๋ขฐ๋ ๋ถ์**: ์ํฐ๋งํฌ ์กด์ฌ ์ฌ๋ถ์ ํ๋ฅ ์ ํ๋จ
- **๋ฉํ๋ฐ์ดํฐ ๊ฒ์ฆ**: ์๋ณธ ์ ๋ณด์์ ๊ต์ฐจ ๊ฒ์ฆ
### ๐ ํ์ง ๋ถ์
- **PSNR ์ธก์ **: ์ํฐ๋งํฌ ์ฝ์
ํ ํ์ง ๋ณํ ์ ๋ ๋ถ์
- **์ฐจ์ด ์๊ฐํ**: ์๋ณธ๊ณผ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง์ ์ฐจ์ด์ ํ์
## ๐๏ธ ์์คํ
์ํคํ
์ฒ
### CNN ๋ชจ๋ธ ๊ตฌ์กฐ
- **์ธ์ฝ๋**: MobileNet ๊ธฐ๋ฐ ๊ฒฝ๋ํ ์ํคํ
์ฒ
- Depthwise Separable Convolution ํ์ฉ
- ๋ชจ๋ฐ์ผ ํ๊ฒฝ ์ต์ ํ
- **๋์ฝ๋**: ์ํฐ๋งํฌ ํจํด ์ถ์ถ์ฉ CNN
- Adaptive Pooling์ผ๋ก ํด์๋ ๋
๋ฆฝ์ฑ ๋ณด์ฅ
### ๊ธฐ์ ์ ํน์ง
- **๋๋ฐ์ด์ค**: CPU/GPU ์๋ ๊ฐ์ง ๋ฐ ํ์ฉ
- **์ํฐ๋งํฌ ํฌ๊ธฐ**: 32x32 ํฝ์
ํจํด
- **์ง์ ํฌ๋งท**: JPG, PNG, BMP
- **์ฒ๋ฆฌ ์๋**: < 1์ด (๋ชฉํ)
## ๐ฏ ์ฌ์ฉ ๋ฐฉ๋ฒ
### 1๋จ๊ณ: ์ํฐ๋งํฌ ์ฝ์
1. ์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋
2. ์ฌ์ฉ์ ID ์
๋ ฅ (์: user123)
3. ์ถ๋ ฅ ํฌ๋งท ์ ํ (PNG/JPG)
4. "์ํฐ๋งํฌ ์ฝ์
" ๋ฒํผ ํด๋ฆญ
### 2๋จ๊ณ: ์ํฐ๋งํฌ ๊ฒ์ฆ
1. ์์ฌ์ค๋ฌ์ด ์ด๋ฏธ์ง ์
๋ก๋
2. (์ ํ์ฌํญ) ์๋ณธ ๋ฉํ๋ฐ์ดํฐ ์
๋ ฅ
3. "์ํฐ๋งํฌ ์ถ์ถ" ๋ฒํผ ํด๋ฆญ
4. ์ ๋ขฐ๋ ๋ฐ ๊ฒ์ฆ ๊ฒฐ๊ณผ ํ์ธ
### 3๋จ๊ณ: ํ์ง ๋ถ์
1. ์๋ณธ ์ด๋ฏธ์ง์ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง ์
๋ก๋
2. "ํ์ง ๋น๊ต" ๋ฒํผ ํด๋ฆญ
3. PSNR ๊ฐ ๋ฐ ์ฐจ์ด ๋ถ์ ํ์ธ
## ๐ ์ ๋ขฐ๋ ํด์ ๊ฐ์ด๋
| ์ ๋ขฐ๋ ๋ฒ์ | ํ์ | ์ค๋ช
|
|------------|------|------|
| 0.8 ์ด์ | โ
๋์ | ์ํฐ๋งํฌ ํ์คํ ์กด์ฌ |
| 0.6~0.8 | โ ๏ธ ๋ณดํต | ์ํฐ๋งํฌ ์กด์ฌ ๊ฐ๋ฅ์ฑ ๋์ |
| 0.3~0.6 | โ ๋ฎ์ | ์ํฐ๋งํฌ ์กด์ฌ ๋ถํ์ค |
| 0.3 ๋ฏธ๋ง | โ ๋งค์ฐ ๋ฎ์ | ์ํฐ๋งํฌ ์์ |
## โ ๏ธ ์ฃผ์์ฌํญ
- **์คํ์ฉ ํ๋กํ ํ์
**: ์ฐ๊ตฌ ๋ฐ ๊ต์ก ๋ชฉ์ ์ผ๋ก ๊ฐ๋ฐ๋จ
- **๋ณด์ ๊ฐํ ํ์**: ์์ฉ ํ๊ฒฝ์์๋ ์ถ๊ฐ ์ํธํ ๊ธฐ๋ฒ ์ ์ฉ ๊ถ์ฅ
- **์ฑ๋ฅ ์ต์ ํ**: ์ค์ ๋ฐฐํฌ์ ๋ชจ๋ธ ๊ฒฝ๋ํ ๋ฐ ํ๋์จ์ด ๊ฐ์ ํ์
## ๐ ๏ธ ๊ธฐ์ ์คํ
- **Framework**: PyTorch, Gradio
- **Computer Vision**: OpenCV, PIL
- **Data Processing**: NumPy, Matplotlib
- **Deployment**: Hugging Face Spaces
## ๐ ๋ผ์ด์ ์ค
์ด ํ๋ก์ ํธ๋ ๊ต์ก ๋ฐ ์ฐ๊ตฌ ๋ชฉ์ ์ผ๋ก ์ ๊ณต๋ฉ๋๋ค.
---
**๊ฐ๋ฐ์**: AI ์ํฐ๋งํน ์ฐ๊ตฌํ
**๋ฒ์ **: 1.0.0
**์ต์ข
์
๋ฐ์ดํธ**: 2025๋
6์
- app.py +682 -0
-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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""์ํฐ๋งํน์์คํ
.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/17EOpDL6hwJ6f3G_-7bpUspm-1-XcgL_w
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import numpy as np
|
| 12 |
+
import cv2
|
| 13 |
+
import torch
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import io
|
| 18 |
+
import base64
|
| 19 |
+
import json
|
| 20 |
+
import time
|
| 21 |
+
from typing import Tuple, Optional
|
| 22 |
+
import matplotlib.pyplot as plt
|
| 23 |
+
import tempfile
|
| 24 |
+
import os
|
| 25 |
+
|
| 26 |
+
# ===== ๊ฒฝ๋ํ CNN ์ํฐ๋งํน ๋ชจ๋ธ =====
|
| 27 |
+
class MobileWatermarkEncoder(nn.Module):
|
| 28 |
+
"""๋ชจ๋ฐ์ผ ์ต์ ํ ์ํฐ๋งํฌ ์ธ์ฝ๋"""
|
| 29 |
+
def __init__(self, watermark_size=32):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.watermark_size = watermark_size
|
| 32 |
+
|
| 33 |
+
# ๊ฒฝ๋ํ ์ธ์ฝ๋ (MobileNet ์คํ์ผ)
|
| 34 |
+
self.encoder = nn.Sequential(
|
| 35 |
+
# ์ด๊ธฐ ํน์ง ์ถ์ถ
|
| 36 |
+
nn.Conv2d(3, 32, 3, padding=1),
|
| 37 |
+
nn.BatchNorm2d(32),
|
| 38 |
+
nn.ReLU(inplace=True),
|
| 39 |
+
|
| 40 |
+
# Depthwise Separable Convolution
|
| 41 |
+
nn.Conv2d(32, 32, 3, padding=1, groups=32), # Depthwise
|
| 42 |
+
nn.Conv2d(32, 64, 1), # Pointwise
|
| 43 |
+
nn.BatchNorm2d(64),
|
| 44 |
+
nn.ReLU(inplace=True),
|
| 45 |
+
|
| 46 |
+
nn.Conv2d(64, 64, 3, padding=1, groups=64),
|
| 47 |
+
nn.Conv2d(64, 128, 1),
|
| 48 |
+
nn.BatchNorm2d(128),
|
| 49 |
+
nn.ReLU(inplace=True),
|
| 50 |
+
|
| 51 |
+
# ์ถ๋ ฅ์ธต
|
| 52 |
+
nn.Conv2d(128, 3, 3, padding=1),
|
| 53 |
+
nn.Tanh()
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# ์ํฐ๋งํฌ ์๋ฒ ๋ฉ ๊ฐ๋ ์กฐ์
|
| 57 |
+
self.alpha = nn.Parameter(torch.tensor(0.1))
|
| 58 |
+
|
| 59 |
+
def forward(self, image, watermark_pattern):
|
| 60 |
+
# ์ํฐ๋งํฌ ํจํด์ ์ด๋ฏธ์ง ํฌ๊ธฐ์ ๋ง๊ฒ ํ์ฅ
|
| 61 |
+
h, w = image.shape[2], image.shape[3]
|
| 62 |
+
watermark = F.interpolate(watermark_pattern, size=(h, w), mode='bilinear')
|
| 63 |
+
|
| 64 |
+
# ์ํฐ๋งํฌ ์๋ฒ ๋ฉ
|
| 65 |
+
watermark_noise = self.encoder(image)
|
| 66 |
+
watermarked = image + self.alpha * watermark_noise * watermark
|
| 67 |
+
|
| 68 |
+
return torch.clamp(watermarked, 0, 1)
|
| 69 |
+
|
| 70 |
+
class MobileWatermarkDecoder(nn.Module):
|
| 71 |
+
"""๋ชจ๋ฐ์ผ ์ต์ ํ ์ํฐ๋งํฌ ๋์ฝ๋"""
|
| 72 |
+
def __init__(self, watermark_size=32):
|
| 73 |
+
super().__init__()
|
| 74 |
+
self.watermark_size = watermark_size
|
| 75 |
+
|
| 76 |
+
self.decoder = nn.Sequential(
|
| 77 |
+
nn.Conv2d(3, 32, 3, padding=1),
|
| 78 |
+
nn.ReLU(inplace=True),
|
| 79 |
+
nn.Conv2d(32, 64, 3, padding=1),
|
| 80 |
+
nn.ReLU(inplace=True),
|
| 81 |
+
nn.AdaptiveAvgPool2d((watermark_size, watermark_size)),
|
| 82 |
+
nn.Conv2d(64, 1, 1),
|
| 83 |
+
nn.Sigmoid()
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def forward(self, image):
|
| 87 |
+
return self.decoder(image)
|
| 88 |
+
|
| 89 |
+
# ===== ์ํฐ๋งํน ์์คํ
ํด๋์ค =====
|
| 90 |
+
class MobileWatermarkingSystem:
|
| 91 |
+
def __init__(self):
|
| 92 |
+
self.encoder = MobileWatermarkEncoder()
|
| 93 |
+
self.decoder = MobileWatermarkDecoder()
|
| 94 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 95 |
+
|
| 96 |
+
# ๋ชจ๋ธ์ device๋ก ์ด๋
|
| 97 |
+
self.encoder.to(self.device)
|
| 98 |
+
self.decoder.to(self.device)
|
| 99 |
+
|
| 100 |
+
# ๊ฐ๋จํ ํ๋ จ์ฉ ๋๋ฏธ ๋ฐ์ดํฐ๋ก ์ด๊ธฐํ
|
| 101 |
+
self._initialize_models()
|
| 102 |
+
|
| 103 |
+
def _initialize_models(self):
|
| 104 |
+
"""๋ชจ๋ธ ์ด๊ธฐํ (์ค์ ๋ก๋ ์ฌ์ ํ๋ จ๋ ๊ฐ์ค์น ๋ก๋)"""
|
| 105 |
+
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ ์ด๊ธฐํ๋ง ์ํ
|
| 106 |
+
# ์ค์ ๊ตฌํ์์๋ ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ ๋ก๋
|
| 107 |
+
pass
|
| 108 |
+
|
| 109 |
+
def generate_watermark_pattern(self, user_id: str, timestamp: str) -> torch.Tensor:
|
| 110 |
+
"""์ฌ์ฉ์ ID์ ํ์์คํฌํ๋ก ๊ณ ์ ์ํฐ๋งํฌ ํจํด ์์ฑ"""
|
| 111 |
+
# ๊ฐ๋จํ ํจํด ์์ฑ (์ค์ ๋ก๋ ๋ ๋ณต์กํ ๋ฐฉ๋ฒ ์ฌ์ฉ)
|
| 112 |
+
seed = hash(user_id + timestamp) % 10000
|
| 113 |
+
torch.manual_seed(seed)
|
| 114 |
+
pattern = torch.randn(1, 1, 32, 32)
|
| 115 |
+
return torch.sigmoid(pattern)
|
| 116 |
+
|
| 117 |
+
def embed_watermark(self, image: np.ndarray, user_id: str) -> Tuple[np.ndarray, dict]:
|
| 118 |
+
"""์ด๋ฏธ์ง์ ์ํฐ๋งํฌ ์ฝ์
"""
|
| 119 |
+
start_time = time.time()
|
| 120 |
+
|
| 121 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 122 |
+
if len(image.shape) == 3:
|
| 123 |
+
image_tensor = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255.0
|
| 124 |
+
else:
|
| 125 |
+
image_tensor = torch.from_numpy(image).float() / 255.0
|
| 126 |
+
image_tensor = image_tensor.unsqueeze(0).repeat(3, 1, 1)
|
| 127 |
+
|
| 128 |
+
image_tensor = image_tensor.unsqueeze(0).to(self.device)
|
| 129 |
+
|
| 130 |
+
# ์ํฐ๋งํฌ ํจํด ์์ฑ
|
| 131 |
+
timestamp = str(int(time.time()))
|
| 132 |
+
watermark_pattern = self.generate_watermark_pattern(user_id, timestamp)
|
| 133 |
+
watermark_pattern = watermark_pattern.to(self.device)
|
| 134 |
+
|
| 135 |
+
# ์ํฐ๋งํฌ ์ฝ์
|
| 136 |
+
with torch.no_grad():
|
| 137 |
+
watermarked_tensor = self.encoder(image_tensor, watermark_pattern)
|
| 138 |
+
|
| 139 |
+
# ํ์ฒ๋ฆฌ
|
| 140 |
+
watermarked_image = watermarked_tensor.squeeze(0).cpu().numpy()
|
| 141 |
+
watermarked_image = (watermarked_image.transpose(1, 2, 0) * 255).astype(np.uint8)
|
| 142 |
+
|
| 143 |
+
processing_time = time.time() - start_time
|
| 144 |
+
|
| 145 |
+
# ๋ฉํ๋ฐ์ดํฐ
|
| 146 |
+
metadata = {
|
| 147 |
+
'user_id': user_id,
|
| 148 |
+
'timestamp': timestamp,
|
| 149 |
+
'processing_time': processing_time,
|
| 150 |
+
'image_size': image.shape,
|
| 151 |
+
'watermark_strength': float(self.encoder.alpha.item())
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
return watermarked_image, metadata
|
| 155 |
+
|
| 156 |
+
def extract_watermark(self, image: np.ndarray) -> Tuple[np.ndarray, float]:
|
| 157 |
+
"""์ด๋ฏธ์ง์์ ์ํฐ๋งํฌ ์ถ์ถ ๋ฐ ๊ฒ์ฆ"""
|
| 158 |
+
start_time = time.time()
|
| 159 |
+
|
| 160 |
+
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
|
| 161 |
+
if len(image.shape) == 3:
|
| 162 |
+
image_tensor = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255.0
|
| 163 |
+
else:
|
| 164 |
+
image_tensor = torch.from_numpy(image).float() / 255.0
|
| 165 |
+
image_tensor = image_tensor.unsqueeze(0).repeat(3, 1, 1)
|
| 166 |
+
|
| 167 |
+
image_tensor = image_tensor.unsqueeze(0).to(self.device)
|
| 168 |
+
|
| 169 |
+
# ์ํฐ๋งํฌ ์ถ์ถ
|
| 170 |
+
with torch.no_grad():
|
| 171 |
+
extracted_watermark = self.decoder(image_tensor)
|
| 172 |
+
|
| 173 |
+
# ํ์ฒ๋ฆฌ
|
| 174 |
+
watermark_array = extracted_watermark.squeeze().cpu().numpy()
|
| 175 |
+
confidence = np.mean(watermark_array) # ๊ฐ๋จํ ์ ๋ขฐ๋ ๊ณ์ฐ
|
| 176 |
+
|
| 177 |
+
processing_time = time.time() - start_time
|
| 178 |
+
|
| 179 |
+
return watermark_array, confidence
|
| 180 |
+
|
| 181 |
+
def verify_watermark(self, original_metadata: dict, extracted_confidence: float) -> dict:
|
| 182 |
+
"""์ํฐ๋งํฌ ๊ฒ์ฆ"""
|
| 183 |
+
threshold = 0.3 # ๊ฒ์ฆ ์๊ณ๊ฐ
|
| 184 |
+
is_valid = bool(extracted_confidence > threshold) # bool() ๋ช
์์ ๋ณํ
|
| 185 |
+
|
| 186 |
+
return {
|
| 187 |
+
'is_valid': is_valid,
|
| 188 |
+
'confidence': float(extracted_confidence), # float() ๋ช
์์ ๋ณํ
|
| 189 |
+
'threshold': float(threshold),
|
| 190 |
+
'original_metadata': original_metadata
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
# ===== ์ ์ญ ์์คํ
์ธ์คํด์ค =====
|
| 194 |
+
watermarking_system = MobileWatermarkingSystem()
|
| 195 |
+
|
| 196 |
+
# ===== Gradio ์ธํฐํ์ด์ค ํจ์๋ค =====
|
| 197 |
+
def embed_watermark_interface(image, user_id, output_format):
|
| 198 |
+
"""์ํฐ๋งํฌ ์ฝ์
์ธํฐํ์ด์ค"""
|
| 199 |
+
if image is None:
|
| 200 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None, None
|
| 201 |
+
|
| 202 |
+
if not user_id.strip():
|
| 203 |
+
return None, "์ฌ์ฉ์ ID๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", None, None
|
| 204 |
+
|
| 205 |
+
try:
|
| 206 |
+
# ์ํฐ๋งํฌ ์ฝ์
|
| 207 |
+
watermarked_image, metadata = watermarking_system.embed_watermark(image, user_id)
|
| 208 |
+
|
| 209 |
+
# ๋ฉํ๋ฐ์ดํฐ๋ฅผ JSON์ผ๋ก ๋ณํ
|
| 210 |
+
metadata_json = json.dumps(metadata, indent=2)
|
| 211 |
+
|
| 212 |
+
# ๋ค์ด๋ก๋์ฉ ํ์ผ ์์ฑ
|
| 213 |
+
download_file = create_download_file(watermarked_image, user_id, metadata, output_format)
|
| 214 |
+
|
| 215 |
+
# ๊ฒฐ๊ณผ ๋ฉ์์ง
|
| 216 |
+
result_msg = f"""
|
| 217 |
+
โ
์ํฐ๋งํฌ ์ฝ์
์๋ฃ!
|
| 218 |
+
๐ ์ฒ๋ฆฌ ์๊ฐ: {metadata['processing_time']:.3f}์ด
|
| 219 |
+
๐ ์ด๋ฏธ์ง ํฌ๊ธฐ: {metadata['image_size']}
|
| 220 |
+
๐ช ์ํฐ๋งํฌ ๊ฐ๋: {metadata['watermark_strength']:.3f}
|
| 221 |
+
๐ ์ฌ์ฉ์ ID: {metadata['user_id']}
|
| 222 |
+
โฐ ํ์์คํฌํ: {metadata['timestamp']}
|
| 223 |
+
๐ ํฌ๋งท: {output_format.upper()}
|
| 224 |
+
"""
|
| 225 |
+
|
| 226 |
+
return watermarked_image, result_msg, metadata_json, download_file
|
| 227 |
+
|
| 228 |
+
except Exception as e:
|
| 229 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", None, None
|
| 230 |
+
|
| 231 |
+
def create_download_file(image, user_id, metadata, output_format):
|
| 232 |
+
"""๋ค์ด๋ก๋์ฉ ํ์ผ ์์ฑ"""
|
| 233 |
+
try:
|
| 234 |
+
# PIL Image๋ก ๋ณํ
|
| 235 |
+
if isinstance(image, np.ndarray):
|
| 236 |
+
pil_image = Image.fromarray(image)
|
| 237 |
+
else:
|
| 238 |
+
pil_image = image
|
| 239 |
+
|
| 240 |
+
# ํ์ผ๋ช
์์ฑ
|
| 241 |
+
timestamp = metadata['timestamp']
|
| 242 |
+
filename = f"watermarked_{user_id}_{timestamp}.{output_format.lower()}"
|
| 243 |
+
|
| 244 |
+
# ์์ ํ์ผ ์์ฑ
|
| 245 |
+
temp_dir = tempfile.mkdtemp()
|
| 246 |
+
temp_path = os.path.join(temp_dir, filename)
|
| 247 |
+
|
| 248 |
+
if output_format.lower() == 'jpg':
|
| 249 |
+
# JPG๋ RGB ๋ชจ๋ ํ์
|
| 250 |
+
if pil_image.mode in ('RGBA', 'LA', 'P'):
|
| 251 |
+
# ํฌ๋ช
๋๊ฐ ์๋ ๊ฒฝ์ฐ ํฐ์ ๋ฐฐ๊ฒฝ๊ณผ ํฉ์ฑ
|
| 252 |
+
background = Image.new('RGB', pil_image.size, (255, 255, 255))
|
| 253 |
+
if pil_image.mode == 'P':
|
| 254 |
+
pil_image = pil_image.convert('RGBA')
|
| 255 |
+
background.paste(pil_image, mask=pil_image.split()[-1] if pil_image.mode == 'RGBA' else None)
|
| 256 |
+
pil_image = background
|
| 257 |
+
|
| 258 |
+
pil_image.save(temp_path, format='JPEG', quality=95, optimize=True)
|
| 259 |
+
|
| 260 |
+
elif output_format.lower() == 'png':
|
| 261 |
+
# PNG ๋ฉํ๋ฐ์ดํฐ
|
| 262 |
+
pnginfo = Image.PngImagePlugin.PngInfo()
|
| 263 |
+
pnginfo.add_text("User_ID", user_id)
|
| 264 |
+
pnginfo.add_text("Timestamp", timestamp)
|
| 265 |
+
pnginfo.add_text("Watermark_Strength", str(metadata['watermark_strength']))
|
| 266 |
+
pnginfo.add_text("Software", "Mobile Watermarking System")
|
| 267 |
+
|
| 268 |
+
pil_image.save(temp_path, format='PNG', pnginfo=pnginfo, optimize=True)
|
| 269 |
+
|
| 270 |
+
return temp_path
|
| 271 |
+
|
| 272 |
+
except Exception as e:
|
| 273 |
+
print(f"๋ค์ด๋ก๋ ํ์ผ ์์ฑ ์ค๋ฅ: {e}")
|
| 274 |
+
return None
|
| 275 |
+
|
| 276 |
+
def extract_watermark_interface(image, metadata_json):
|
| 277 |
+
"""์ํฐ๋งํฌ ์ถ์ถ ๋ฐ ๊ฒ์ฆ ์ธํฐํ์ด์ค"""
|
| 278 |
+
if image is None:
|
| 279 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", None
|
| 280 |
+
|
| 281 |
+
try:
|
| 282 |
+
# ์ํฐ๋งํฌ ์ถ์ถ
|
| 283 |
+
watermark_pattern, confidence = watermarking_system.extract_watermark(image)
|
| 284 |
+
|
| 285 |
+
# ์ํฐ๋งํฌ ํจํด ์๊ฐํ (์ ๋ชฉ๋ง ์์ด, ํฐํธ ์ค์ )
|
| 286 |
+
plt.figure(figsize=(6, 6))
|
| 287 |
+
plt.rcParams['font.family'] = 'DejaVu Sans' # ์์ด ํฐํธ ์ค์
|
| 288 |
+
plt.rcParams['font.size'] = 10
|
| 289 |
+
|
| 290 |
+
plt.imshow(watermark_pattern, cmap='viridis')
|
| 291 |
+
plt.title(f'Extracted Watermark Pattern (Confidence: {confidence:.3f})',
|
| 292 |
+
fontsize=12, fontweight='bold')
|
| 293 |
+
plt.colorbar(label='Pattern Intensity')
|
| 294 |
+
plt.axis('off')
|
| 295 |
+
|
| 296 |
+
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 297 |
+
watermark_viz = plt.gcf()
|
| 298 |
+
|
| 299 |
+
# ์ ๋ขฐ๋๋ณ ํด์ ๋ฉ์์ง ์์ฑ (ํ๊ธ)
|
| 300 |
+
def get_confidence_interpretation(conf):
|
| 301 |
+
if conf >= 0.8:
|
| 302 |
+
return {
|
| 303 |
+
'level': '๋์',
|
| 304 |
+
'emoji': 'โ
',
|
| 305 |
+
'message': '์ํฐ๋งํฌ๊ฐ ๋ช
ํํ ๊ฐ์ง๋์์ต๋๋ค.',
|
| 306 |
+
'detail': '์ด ์ด๋ฏธ์ง๋ ์ํฐ๋งํฌ๊ฐ ์ฝ์
๋ ์ด๋ฏธ์ง๋ก ํ๋จ๋ฉ๋๋ค.',
|
| 307 |
+
'color': '๐ข'
|
| 308 |
+
}
|
| 309 |
+
elif conf >= 0.6:
|
| 310 |
+
return {
|
| 311 |
+
'level': '๋ณดํต',
|
| 312 |
+
'emoji': 'โ ๏ธ',
|
| 313 |
+
'message': '์ํฐ๋งํฌ ํจํด์ด ๊ฐ์ง๋์์ต๋๋ค.',
|
| 314 |
+
'detail': '์ํฐ๋งํฌ๊ฐ ์์ ๊ฐ๋ฅ์ฑ์ด ๋์ง๋ง ์ถ๊ฐ ๊ฒ์ฆ์ด ๊ถ์ฅ๋ฉ๋๋ค.',
|
| 315 |
+
'color': '๐ก'
|
| 316 |
+
}
|
| 317 |
+
elif conf >= 0.3:
|
| 318 |
+
return {
|
| 319 |
+
'level': '๋ฎ์',
|
| 320 |
+
'emoji': 'โ',
|
| 321 |
+
'message': '์ฝํ ์ํฐ๋งํฌ ์ ํธ๊ฐ ๊ฐ์ง๋์์ต๋๋ค.',
|
| 322 |
+
'detail': '์ํฐ๋งํฌ๊ฐ ์์ ์ ์์ง๋ง ๋
ธ์ด์ฆ์ผ ๊ฐ๋ฅ์ฑ๋ ์์ต๋๋ค.',
|
| 323 |
+
'color': '๐ '
|
| 324 |
+
}
|
| 325 |
+
else:
|
| 326 |
+
return {
|
| 327 |
+
'level': '๋งค์ฐ ๋ฎ์',
|
| 328 |
+
'emoji': 'โ',
|
| 329 |
+
'message': '์ํฐ๋งํฌ๊ฐ ๊ฐ์ง๋์ง ์์์ต๋๋ค.',
|
| 330 |
+
'detail': '์ด ์ด๋ฏธ์ง์๋ ์ํฐ๋งํฌ๊ฐ ์๊ฑฐ๋ ์์๋์์ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค.',
|
| 331 |
+
'color': '๐ด'
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
confidence_info = get_confidence_interpretation(confidence)
|
| 335 |
+
|
| 336 |
+
# ๊ฒ์ฆ ์ํ
|
| 337 |
+
verification_result = None
|
| 338 |
+
if metadata_json and metadata_json.strip():
|
| 339 |
+
try:
|
| 340 |
+
original_metadata = json.loads(metadata_json)
|
| 341 |
+
verification_result = watermarking_system.verify_watermark(
|
| 342 |
+
original_metadata, confidence
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
# JSON ์ง๋ ฌํ ๊ฐ๋ฅํ๋๋ก ๋ฐ์ดํฐ ํ์
๋ณด์ฅ
|
| 346 |
+
verification_result = {
|
| 347 |
+
'is_valid': bool(verification_result['is_valid']),
|
| 348 |
+
'confidence': float(verification_result['confidence']),
|
| 349 |
+
'threshold': float(verification_result['threshold']),
|
| 350 |
+
'original_metadata': verification_result['original_metadata'],
|
| 351 |
+
'confidence_level': confidence_info['level'],
|
| 352 |
+
'interpretation': confidence_info['message']
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
except json.JSONDecodeError as e:
|
| 356 |
+
verification_result = {
|
| 357 |
+
'error': f'๋ฉํ๋ฐ์ดํฐ ํ์ฑ ์ค๋ฅ: {str(e)}',
|
| 358 |
+
'confidence': float(confidence),
|
| 359 |
+
'threshold': 0.3,
|
| 360 |
+
'confidence_level': confidence_info['level'],
|
| 361 |
+
'interpretation': confidence_info['message']
|
| 362 |
+
}
|
| 363 |
+
except Exception as e:
|
| 364 |
+
verification_result = {
|
| 365 |
+
'error': f'๊ฒ์ฆ ์ค ์ค๋ฅ: {str(e)}',
|
| 366 |
+
'confidence': float(confidence),
|
| 367 |
+
'threshold': 0.3,
|
| 368 |
+
'confidence_level': confidence_info['level'],
|
| 369 |
+
'interpretation': confidence_info['message']
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
# ๊ฒฐ๊ณผ ๋ฉ์์ง (ํ๊ธ)
|
| 373 |
+
result_msg = f"""
|
| 374 |
+
๐ ์ํฐ๋งํฌ ์ถ์ถ ์๋ฃ!
|
| 375 |
+
๐ ์ ๋ขฐ๋: {confidence:.3f}
|
| 376 |
+
{confidence_info['color']} ์ ๋ขฐ๋ ์์ค: {confidence_info['level']}
|
| 377 |
+
|
| 378 |
+
{confidence_info['emoji']} {confidence_info['message']}
|
| 379 |
+
๐ก {confidence_info['detail']}
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
+
if verification_result and 'error' not in verification_result:
|
| 383 |
+
status = "โ
์ ํจ" if verification_result['is_valid'] else "โ ๋ฌดํจ"
|
| 384 |
+
result_msg += f"""
|
| 385 |
+
|
| 386 |
+
๐ก๏ธ ๊ฒ์ฆ ๊ฒฐ๊ณผ: {status}
|
| 387 |
+
๐ ์๊ณ๊ฐ: {verification_result['threshold']}
|
| 388 |
+
"""
|
| 389 |
+
|
| 390 |
+
# ๋ฉํ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ ์ถ๊ฐ ์ ๋ณด
|
| 391 |
+
if 'original_metadata' in verification_result:
|
| 392 |
+
orig_meta = verification_result['original_metadata']
|
| 393 |
+
result_msg += f"""
|
| 394 |
+
๐ ์๋ณธ ์ฌ์ฉ์: {orig_meta.get('user_id', 'N/A')}
|
| 395 |
+
โฐ ์์ฑ ์๊ฐ: {orig_meta.get('timestamp', 'N/A')}
|
| 396 |
+
๐ช ์๋ณธ ๊ฐ๋: {orig_meta.get('watermark_strength', 'N/A')}
|
| 397 |
+
"""
|
| 398 |
+
elif verification_result and 'error' in verification_result:
|
| 399 |
+
result_msg += f"""
|
| 400 |
+
|
| 401 |
+
โ ๏ธ ๊ฒ์ฆ ์ค๋ฅ: {verification_result['error']}
|
| 402 |
+
"""
|
| 403 |
+
|
| 404 |
+
# ์ถ๊ฐ ํด์ ๊ฐ์ด๋ (ํ๊ธ)
|
| 405 |
+
result_msg += f"""
|
| 406 |
+
|
| 407 |
+
๐ ํด์ ๊ฐ์ด๋:
|
| 408 |
+
โข 0.8 ์ด์: ์ํฐ๋งํฌ ํ์คํ ์กด์ฌ โ
|
| 409 |
+
โข 0.6~0.8: ์ํฐ๋งํฌ ์กด์ฌ ๊ฐ๋ฅ์ฑ ๋์ โ ๏ธ
|
| 410 |
+
โข 0.3~0.6: ์ํฐ๋งํฌ ์กด์ฌ ๋ถํ์ค โ
|
| 411 |
+
โข 0.3 ๋ฏธ๋ง: ์ํฐ๋งํฌ ์์ โ
|
| 412 |
+
"""
|
| 413 |
+
|
| 414 |
+
plt.close()
|
| 415 |
+
|
| 416 |
+
# JSON ์ง๋ ฌํ ๊ฒ์ฆ
|
| 417 |
+
verification_json = None
|
| 418 |
+
if verification_result:
|
| 419 |
+
try:
|
| 420 |
+
verification_json = json.dumps(verification_result, indent=2, ensure_ascii=False)
|
| 421 |
+
except Exception as e:
|
| 422 |
+
verification_json = json.dumps({
|
| 423 |
+
'error': f'JSON ์ง๋ ฌํ ์ค๋ฅ: {str(e)}',
|
| 424 |
+
'confidence': float(confidence),
|
| 425 |
+
'confidence_level': confidence_info['level'],
|
| 426 |
+
'interpretation': confidence_info['message']
|
| 427 |
+
}, indent=2, ensure_ascii=False)
|
| 428 |
+
|
| 429 |
+
return watermark_viz, result_msg, verification_json
|
| 430 |
+
|
| 431 |
+
except Exception as e:
|
| 432 |
+
plt.close()
|
| 433 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", json.dumps({
|
| 434 |
+
'error': f'์ถ์ถ ์ค ์ค๋ฅ: {str(e)}'
|
| 435 |
+
}, indent=2, ensure_ascii=False)
|
| 436 |
+
|
| 437 |
+
def compare_images(original, watermarked):
|
| 438 |
+
"""์๋ณธ๊ณผ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง ๋น๊ต"""
|
| 439 |
+
if original is None or watermarked is None:
|
| 440 |
+
return None, "๋ ์ด๋ฏธ์ง๊ฐ ๋ชจ๋ ํ์ํฉ๋๋ค."
|
| 441 |
+
|
| 442 |
+
try:
|
| 443 |
+
# ์ด๋ฏธ์ง ํฌ๊ธฐ ๋ง์ถ๊ธฐ
|
| 444 |
+
h1, w1 = original.shape[:2]
|
| 445 |
+
h2, w2 = watermarked.shape[:2]
|
| 446 |
+
|
| 447 |
+
if (h1, w1) != (h2, w2):
|
| 448 |
+
watermarked = cv2.resize(watermarked, (w1, h1))
|
| 449 |
+
|
| 450 |
+
# PSNR ๊ณ์ฐ
|
| 451 |
+
mse = np.mean((original.astype(float) - watermarked.astype(float)) ** 2)
|
| 452 |
+
if mse == 0:
|
| 453 |
+
psnr = float('inf')
|
| 454 |
+
else:
|
| 455 |
+
psnr = 20 * np.log10(255.0 / np.sqrt(mse))
|
| 456 |
+
|
| 457 |
+
# ์ฐจ์ด ์ด๋ฏธ์ง ์์ฑ
|
| 458 |
+
diff = np.abs(original.astype(float) - watermarked.astype(float))
|
| 459 |
+
diff = (diff / diff.max() * 255).astype(np.uint8)
|
| 460 |
+
|
| 461 |
+
# ์๊ฐํ (์ ๋ชฉ๋ง ์์ด๋ก, ํฐํธ ์ค์ )
|
| 462 |
+
plt.rcParams['font.family'] = 'DejaVu Sans'
|
| 463 |
+
plt.rcParams['font.size'] = 10
|
| 464 |
+
|
| 465 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
| 466 |
+
|
| 467 |
+
axes[0].imshow(original)
|
| 468 |
+
axes[0].set_title('Original Image', fontweight='bold')
|
| 469 |
+
axes[0].axis('off')
|
| 470 |
+
|
| 471 |
+
axes[1].imshow(watermarked)
|
| 472 |
+
axes[1].set_title('Watermarked Image', fontweight='bold')
|
| 473 |
+
axes[1].axis('off')
|
| 474 |
+
|
| 475 |
+
axes[2].imshow(diff, cmap='hot')
|
| 476 |
+
axes[2].set_title(f'Difference (PSNR: {psnr:.2f}dB)', fontweight='bold')
|
| 477 |
+
axes[2].axis('off')
|
| 478 |
+
|
| 479 |
+
plt.tight_layout()
|
| 480 |
+
|
| 481 |
+
result_msg = f"""
|
| 482 |
+
๐ ์ด๋ฏธ์ง ํ์ง ๋ถ์:
|
| 483 |
+
- PSNR: {psnr:.2f} dB
|
| 484 |
+
- MSE: {mse:.2f}
|
| 485 |
+
- ์ด๋ฏธ์ง ํฌ๊ธฐ: {original.shape}
|
| 486 |
+
"""
|
| 487 |
+
|
| 488 |
+
comparison_fig = plt.gcf()
|
| 489 |
+
plt.close()
|
| 490 |
+
|
| 491 |
+
return comparison_fig, result_msg
|
| 492 |
+
|
| 493 |
+
except Exception as e:
|
| 494 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 495 |
+
|
| 496 |
+
# ===== Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ =====
|
| 497 |
+
def create_gradio_interface():
|
| 498 |
+
"""Gradio ์ธํฐํ์ด์ค ์์ฑ"""
|
| 499 |
+
|
| 500 |
+
with gr.Blocks(title="๋ชจ๋ฐ์ผ ์ํฐ๋งํน ์คํ ์์คํ
", theme=gr.themes.Soft()) as demo:
|
| 501 |
+
gr.Markdown("""
|
| 502 |
+
# ๐ฑ ๋ชจ๋ฐ์ผ ํ๊ฒฝ CNN ๊ธฐ๋ฐ ์ํฐ๋งํน ์์คํ
|
| 503 |
+
|
| 504 |
+
์ด ์์คํ
์ ๋ชจ๋ฐ์ผ ํ๊ฒฝ์ ์ต์ ํ๋ ์ค์๊ฐ ์ด๋ฏธ์ง ์ํฐ๋งํน ๊ธฐ์ ์ ์คํํ ์ ์์ต๋๋ค.
|
| 505 |
+
|
| 506 |
+
## ๐ฌ ์ฃผ์ ๊ธฐ๋ฅ
|
| 507 |
+
- **์ค์๊ฐ ์ํฐ๋งํฌ ์ฝ์
**: ๊ฒฝ๋ํ๋ CNN ๋ชจ๋ธ๋ก ๋น ๋ฅธ ์ฒ๋ฆฌ
|
| 508 |
+
- **์ ์ํ ํด์๋ ์ง์**: ๋ค์ํ ์ด๋ฏธ์ง ํฌ๊ธฐ์ ์๋ ์ ์
|
| 509 |
+
- **์ํฐ๋งํฌ ๊ฒ์ฆ**: ์ฝ์
๋ ์ํฐ๋งํฌ์ ์ถ์ถ ๋ฐ ๊ฒ์ฆ
|
| 510 |
+
- **ํ์ง ๋ถ์**: ์๋ณธ ๋๋น ํ์ง ๋ณํ ์ธก์
|
| 511 |
+
""")
|
| 512 |
+
|
| 513 |
+
with gr.Tabs():
|
| 514 |
+
# ํญ 1: ์ํฐ๋งํฌ ์ฝ์
|
| 515 |
+
with gr.Tab("๐ ์ํฐ๋งํฌ ์ฝ์
"):
|
| 516 |
+
with gr.Row():
|
| 517 |
+
with gr.Column():
|
| 518 |
+
embed_input_image = gr.Image(
|
| 519 |
+
label="๐ท ์๋ณธ ์ด๋ฏธ์ง ์
๋ก๋",
|
| 520 |
+
type="numpy"
|
| 521 |
+
)
|
| 522 |
+
embed_user_id = gr.Textbox(
|
| 523 |
+
label="๐ ์ฌ์ฉ์ ID",
|
| 524 |
+
placeholder="์: user123",
|
| 525 |
+
value="demo_user"
|
| 526 |
+
)
|
| 527 |
+
output_format = gr.Radio(
|
| 528 |
+
label="๐ ์ถ๋ ฅ ํฌ๋งท",
|
| 529 |
+
choices=["PNG", "JPG"],
|
| 530 |
+
value="PNG"
|
| 531 |
+
)
|
| 532 |
+
embed_btn = gr.Button("๐ ์ํฐ๋งํฌ ์ฝ์
", variant="primary")
|
| 533 |
+
|
| 534 |
+
with gr.Column():
|
| 535 |
+
embed_output_image = gr.Image(label="๐ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง")
|
| 536 |
+
embed_result_text = gr.Textbox(
|
| 537 |
+
label="๐ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ",
|
| 538 |
+
lines=8,
|
| 539 |
+
interactive=False
|
| 540 |
+
)
|
| 541 |
+
download_btn = gr.DownloadButton(
|
| 542 |
+
label="๐พ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง ๋ค์ด๋ก๋",
|
| 543 |
+
variant="secondary"
|
| 544 |
+
)
|
| 545 |
+
|
| 546 |
+
embed_metadata = gr.JSON(label="๐ ๋ฉํ๋ฐ์ดํฐ", visible=False)
|
| 547 |
+
|
| 548 |
+
embed_btn.click(
|
| 549 |
+
fn=embed_watermark_interface,
|
| 550 |
+
inputs=[embed_input_image, embed_user_id, output_format],
|
| 551 |
+
outputs=[embed_output_image, embed_result_text, embed_metadata, download_btn]
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
# ํญ 2: ์ํฐ๋งํฌ ์ถ์ถ ๋ฐ ๊ฒ์ฆ
|
| 555 |
+
with gr.Tab("๐ ์ํฐ๋งํฌ ๊ฒ์ฆ"):
|
| 556 |
+
with gr.Row():
|
| 557 |
+
with gr.Column():
|
| 558 |
+
extract_input_image = gr.Image(
|
| 559 |
+
label="๐ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง ์
๋ก๋",
|
| 560 |
+
type="numpy"
|
| 561 |
+
)
|
| 562 |
+
extract_metadata = gr.Textbox(
|
| 563 |
+
label="๐ ์๋ณธ ๋ฉํ๋ฐ์ดํฐ (์ ํ์ฌํญ)",
|
| 564 |
+
placeholder="์ํฐ๋งํฌ ์ฝ์
์ ์์ฑ๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ถ์ฌ๋ฃ์ผ์ธ์",
|
| 565 |
+
lines=5
|
| 566 |
+
)
|
| 567 |
+
extract_btn = gr.Button("๐ ์ํฐ๋งํฌ ์ถ์ถ", variant="primary")
|
| 568 |
+
|
| 569 |
+
with gr.Column():
|
| 570 |
+
extract_output_viz = gr.Plot(label="๐จ ์ถ์ถ๋ ์ํฐ๋งํฌ ํจํด")
|
| 571 |
+
extract_result_text = gr.Textbox(
|
| 572 |
+
label="๐ ์ถ์ถ ๊ฒฐ๊ณผ",
|
| 573 |
+
lines=5,
|
| 574 |
+
interactive=False
|
| 575 |
+
)
|
| 576 |
+
|
| 577 |
+
extract_verification = gr.JSON(label="๐ก๏ธ ๊ฒ์ฆ ๊ฒฐ๊ณผ", visible=True)
|
| 578 |
+
|
| 579 |
+
extract_btn.click(
|
| 580 |
+
fn=extract_watermark_interface,
|
| 581 |
+
inputs=[extract_input_image, extract_metadata],
|
| 582 |
+
outputs=[extract_output_viz, extract_result_text, extract_verification]
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
# ํญ 3: ์ด๋ฏธ์ง ํ์ง ๋น๊ต
|
| 586 |
+
with gr.Tab("๐ ํ์ง ๋ถ์"):
|
| 587 |
+
with gr.Row():
|
| 588 |
+
with gr.Column():
|
| 589 |
+
compare_original = gr.Image(
|
| 590 |
+
label="๐ท ์๋ณธ ์ด๋ฏธ์ง",
|
| 591 |
+
type="numpy"
|
| 592 |
+
)
|
| 593 |
+
compare_watermarked = gr.Image(
|
| 594 |
+
label="๐ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง",
|
| 595 |
+
type="numpy"
|
| 596 |
+
)
|
| 597 |
+
compare_btn = gr.Button("๐ ํ์ง ๋น๊ต", variant="primary")
|
| 598 |
+
|
| 599 |
+
with gr.Column():
|
| 600 |
+
compare_output_plot = gr.Plot(label="๐ฌ ๋น๊ต ๋ถ์ ๊ฒฐ๊ณผ")
|
| 601 |
+
compare_result_text = gr.Textbox(
|
| 602 |
+
label="๐ ๋ถ์ ๊ฒฐ๊ณผ",
|
| 603 |
+
lines=6,
|
| 604 |
+
interactive=False
|
| 605 |
+
)
|
| 606 |
+
|
| 607 |
+
compare_btn.click(
|
| 608 |
+
fn=compare_images,
|
| 609 |
+
inputs=[compare_original, compare_watermarked],
|
| 610 |
+
outputs=[compare_output_plot, compare_result_text]
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
# ํญ 4: ์์คํ
์ ๋ณด
|
| 614 |
+
with gr.Tab("โน๏ธ ์์คํ
์ ๋ณด"):
|
| 615 |
+
gr.Markdown(f"""
|
| 616 |
+
## ๐ง ์์คํ
์ฌ์
|
| 617 |
+
|
| 618 |
+
- **๋๋ฐ์ด์ค**: {watermarking_system.device}
|
| 619 |
+
- **CNN ์ํคํ
์ฒ**: MobileNet ๊ธฐ๋ฐ ๊ฒฝ๋ํ ๋ชจ๋ธ
|
| 620 |
+
- **์ํฐ๋งํฌ ํฌ๊ธฐ**: 32x32 ํฝ์
|
| 621 |
+
- **์ง์ ํฌ๋งท**: JPG, PNG, BMP
|
| 622 |
+
|
| 623 |
+
## ๐ ์ฑ๋ฅ ํน์ง
|
| 624 |
+
|
| 625 |
+
- **์ฒ๋ฆฌ ์๋**: < 1์ด (๋ชฉํ)
|
| 626 |
+
- **๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ**: ๋ชจ๋ฐ์ผ ์ต์ ํ
|
| 627 |
+
- **ํด์๋ ์ ์**: ๋์ ํฌ๊ธฐ ์กฐ์
|
| 628 |
+
- **๊ฒฌ๊ณ ์ฑ**: ์์ถ/๋ณํ ๊ณต๊ฒฉ ์ ํญ
|
| 629 |
+
|
| 630 |
+
## ๐ฏ ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 631 |
+
|
| 632 |
+
1. **์ํฐ๋งํฌ ์ฝ์
**: ์๋ณธ ์ด๋ฏธ์ง์ ์ฌ์ฉ์ ID ์
๋ ฅ
|
| 633 |
+
2. **์ํฐ๋งํฌ ๊ฒ์ฆ**: ์์ฌ๋๋ ์ด๋ฏธ์ง ์
๋ก๋ ํ ์ถ์ถ
|
| 634 |
+
3. **ํ์ง ๋ถ์**: ์๋ณธ๊ณผ ์ํฐ๋งํฌ๋ ์ด๋ฏธ์ง ๋น๊ต
|
| 635 |
+
|
| 636 |
+
## โ ๏ธ ์ฃผ์์ฌํญ
|
| 637 |
+
|
| 638 |
+
- ์ด๋ ์คํ์ฉ ํ๋กํ ํ์
์
๋๋ค
|
| 639 |
+
- ์ค์ ์์ฉ ํ๊ฒฝ์์๋ ์ถ๊ฐ ์ต์ ํ๊ฐ ํ์ํฉ๋๋ค
|
| 640 |
+
- ๋ณด์ ๊ฐํ๋ฅผ ์ํด ๋ ๋ณต์กํ ์ํธํ ๊ธฐ๋ฒ ์ ์ฉ ๊ถ์ฅ
|
| 641 |
+
""")
|
| 642 |
+
|
| 643 |
+
# ์ฐ๊ฒฐ ๊ธฐ๋ฅ: ์ํฐ๋งํฌ ์ฝ์
๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆ ํญ์ผ๋ก ์ ๋ฌ
|
| 644 |
+
embed_output_image.change(
|
| 645 |
+
fn=lambda x: x,
|
| 646 |
+
inputs=[embed_output_image],
|
| 647 |
+
outputs=[extract_input_image]
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
embed_metadata.change(
|
| 651 |
+
fn=lambda x: json.dumps(x, indent=2) if x else "",
|
| 652 |
+
inputs=[embed_metadata],
|
| 653 |
+
outputs=[extract_metadata]
|
| 654 |
+
)
|
| 655 |
+
|
| 656 |
+
# ์ฐ๊ฒฐ ๊ธฐ๋ฅ: ๋น๊ต ๋ถ์์ ์ํ ์ด๋ฏธ์ง ์ ๋ฌ
|
| 657 |
+
embed_input_image.change(
|
| 658 |
+
fn=lambda x: x,
|
| 659 |
+
inputs=[embed_input_image],
|
| 660 |
+
outputs=[compare_original]
|
| 661 |
+
)
|
| 662 |
+
|
| 663 |
+
embed_output_image.change(
|
| 664 |
+
fn=lambda x: x,
|
| 665 |
+
inputs=[embed_output_image],
|
| 666 |
+
outputs=[compare_watermarked]
|
| 667 |
+
)
|
| 668 |
+
|
| 669 |
+
return demo
|
| 670 |
+
|
| 671 |
+
# ===== ๋ฉ์ธ ์คํ =====
|
| 672 |
+
if __name__ == "__main__":
|
| 673 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ
|
| 674 |
+
demo = create_gradio_interface()
|
| 675 |
+
|
| 676 |
+
# Colab ํ๊ฒฝ์์ ์คํ
|
| 677 |
+
demo.launch(
|
| 678 |
+
share=True, # ๊ณต๊ฐ ๋งํฌ ์์ฑ
|
| 679 |
+
debug=True, # ๋๋ฒ๊ทธ ๋ชจ๋
|
| 680 |
+
server_name="0.0.0.0", # ๋ชจ๋ IP์์ ์ ๊ทผ ๊ฐ๋ฅ
|
| 681 |
+
server_port=7860 # ํฌํธ ์ง์
|
| 682 |
+
)
|
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.12.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
torchvision>=0.15.0
|
| 4 |
+
numpy>=1.24.0
|
| 5 |
+
opencv-python>=4.8.0
|
| 6 |
+
Pillow>=10.0.0
|
| 7 |
+
matplotlib>=3.7.0
|