Spaces:
Running
Running
Update detect.py
Browse files
detect.py
CHANGED
@@ -1,68 +1,107 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from moviepy.video.io.VideoFileClip import VideoFileClip
|
6 |
-
|
7 |
-
def detect_watermark_image(image):
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
from moviepy.video.io.VideoFileClip import VideoFileClip
|
6 |
+
|
7 |
+
def detect_watermark_image(image, watermark_text="WATERMARK", threshold=0.7):
|
8 |
+
"""Detect if an image contains a DCT-domain watermark.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
image: Input image
|
12 |
+
watermark_text: The text to look for
|
13 |
+
threshold: Detection sensitivity threshold
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
tuple: (detected (bool), highlighted_image)
|
17 |
+
"""
|
18 |
+
# Ensure image dimensions are divisible by 8 for DCT
|
19 |
+
h, w = image.shape[:2]
|
20 |
+
h_new = (h // 8) * 8
|
21 |
+
w_new = (w // 8) * 8
|
22 |
+
image = cv2.resize(image, (w_new, h_new))
|
23 |
+
|
24 |
+
# Convert to YCrCb and extract Y channel
|
25 |
+
ycrcb_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
|
26 |
+
y_channel, cr, cb = cv2.split(ycrcb_image)
|
27 |
+
|
28 |
+
# Apply DCT
|
29 |
+
dct_y = cv2.dct(np.float32(y_channel))
|
30 |
+
|
31 |
+
# Create known pattern for detection (systematic approach)
|
32 |
+
h_blocks, w_blocks = dct_y.shape[0] // 8, dct_y.shape[1] // 8
|
33 |
+
detection_score = 0
|
34 |
+
max_location = None
|
35 |
+
highlighted_image = image.copy()
|
36 |
+
|
37 |
+
# Search for watermark pattern in blocks
|
38 |
+
for y_block in range(h_blocks):
|
39 |
+
for x_block in range(w_blocks):
|
40 |
+
# Extract block
|
41 |
+
block = dct_y[y_block*8:(y_block+1)*8, x_block*8:(x_block+1)*8]
|
42 |
+
|
43 |
+
# Calculate variance to detect unusual patterns
|
44 |
+
block_variance = np.var(block)
|
45 |
+
# Check for anomalies in mid-frequency coefficients
|
46 |
+
mid_freq_mean = np.mean(np.abs(block[2:6, 2:6]))
|
47 |
+
|
48 |
+
# Simple detection heuristic
|
49 |
+
if mid_freq_mean > 2.0 and block_variance > 100:
|
50 |
+
detection_score += 1
|
51 |
+
max_location = (x_block*8, y_block*8)
|
52 |
+
|
53 |
+
detected = detection_score > (h_blocks * w_blocks * 0.01) # Detect if >1% of blocks show watermark characteristics
|
54 |
+
|
55 |
+
# Highlight detected region if found
|
56 |
+
if detected and max_location:
|
57 |
+
x, y = max_location
|
58 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
59 |
+
text_size = cv2.getTextSize(watermark_text, font, 0.5, 1)[0]
|
60 |
+
cv2.putText(highlighted_image, "WATERMARK DETECTED", (x, y + text_size[1]),
|
61 |
+
font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
|
62 |
+
cv2.rectangle(highlighted_image, (x, y), (x + 64, y + 64), (0, 0, 255), 2)
|
63 |
+
|
64 |
+
return detected, highlighted_image
|
65 |
+
|
66 |
+
def detect_watermark_video(video_path, watermark_text="WATERMARK"):
|
67 |
+
"""Detect and highlight watermarks in a video file.
|
68 |
+
|
69 |
+
Args:
|
70 |
+
video_path (str): Path to the video file
|
71 |
+
watermark_text (str): The watermark text to detect
|
72 |
+
|
73 |
+
Returns:
|
74 |
+
tuple: (detection_result (bool), output_video_path)
|
75 |
+
"""
|
76 |
+
try:
|
77 |
+
# Process the video
|
78 |
+
video = VideoFileClip(video_path)
|
79 |
+
|
80 |
+
# Track detection across frames
|
81 |
+
frame_count = 0
|
82 |
+
detected_frames = 0
|
83 |
+
|
84 |
+
def process_frame(frame):
|
85 |
+
nonlocal frame_count, detected_frames
|
86 |
+
frame_count += 1
|
87 |
+
detected, highlighted_frame = detect_watermark_image(frame, watermark_text)
|
88 |
+
if detected:
|
89 |
+
detected_frames += 1
|
90 |
+
return highlighted_frame
|
91 |
+
|
92 |
+
processed_video = video.fl_image(process_frame)
|
93 |
+
|
94 |
+
# Save the result to a temporary file
|
95 |
+
temp_fd, output_path = tempfile.mkstemp(suffix=".mp4")
|
96 |
+
os.close(temp_fd) # Close the file descriptor properly
|
97 |
+
|
98 |
+
processed_video.write_videofile(output_path, codec='libx264')
|
99 |
+
|
100 |
+
# Determine if watermark is detected in the video
|
101 |
+
detection_result = detected_frames > (frame_count * 0.1) # >10% of frames have watermark
|
102 |
+
|
103 |
+
return detection_result, output_path
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
print(f"Error detecting watermark in video: {e}")
|
107 |
+
return False, None
|