Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -116,8 +116,7 @@ class WatermarkProcessor:
|
|
116 |
except Exception as e:
|
117 |
return image_path, f"Error in encoding: {str(e)}"
|
118 |
|
119 |
-
|
120 |
-
def decode(self, image_path):
|
121 |
"""Decode watermark using simple LSB steganography"""
|
122 |
try:
|
123 |
# Try PNG metadata method first
|
@@ -186,3 +185,40 @@ class WatermarkProcessor:
|
|
186 |
except Exception as e:
|
187 |
return f"Error in decoding: {str(e)}"
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
except Exception as e:
|
117 |
return image_path, f"Error in encoding: {str(e)}"
|
118 |
|
119 |
+
def decode(self, image_path):
|
|
|
120 |
"""Decode watermark using simple LSB steganography"""
|
121 |
try:
|
122 |
# Try PNG metadata method first
|
|
|
185 |
except Exception as e:
|
186 |
return f"Error in decoding: {str(e)}"
|
187 |
|
188 |
+
def analyze_quality(self, original_path, watermarked_path):
|
189 |
+
"""Analyze watermark quality"""
|
190 |
+
try:
|
191 |
+
original = cv2.imread(original_path)
|
192 |
+
watermarked = cv2.imread(watermarked_path)
|
193 |
+
|
194 |
+
if original is None or watermarked is None:
|
195 |
+
raise ValueError("Could not read image files")
|
196 |
+
|
197 |
+
# Calculate PSNR
|
198 |
+
mse = np.mean((original - watermarked) ** 2)
|
199 |
+
if mse == 0:
|
200 |
+
psnr = float('inf')
|
201 |
+
else:
|
202 |
+
psnr = 20 * np.log10(255.0 / np.sqrt(mse))
|
203 |
+
|
204 |
+
# Calculate histogram similarity
|
205 |
+
hist_original = cv2.calcHist([original], [0], None, [256], [0, 256])
|
206 |
+
hist_watermarked = cv2.calcHist([watermarked], [0], None, [256], [0, 256])
|
207 |
+
hist_correlation = cv2.compareHist(hist_original, hist_watermarked, cv2.HISTCMP_CORREL)
|
208 |
+
|
209 |
+
# Count modified pixels
|
210 |
+
diff = cv2.bitwise_xor(original, watermarked)
|
211 |
+
modified_pixels = np.count_nonzero(diff)
|
212 |
+
|
213 |
+
report = {
|
214 |
+
'psnr': round(psnr, 2),
|
215 |
+
'histogram_similarity': round(hist_correlation, 4),
|
216 |
+
'modified_pixels': modified_pixels,
|
217 |
+
'image_size': original.shape,
|
218 |
+
'quality_score': round((psnr / 50) * 100, 2) if psnr != float('inf') else 100
|
219 |
+
}
|
220 |
+
|
221 |
+
return json.dumps(report, indent=2)
|
222 |
+
|
223 |
+
except Exception as e:
|
224 |
+
return f"Error in quality analysis: {str(e)}"
|