noumanjavaid commited on
Commit
7804245
Β·
verified Β·
1 Parent(s): be2813b

Upload folder using huggingface_hub

Browse files
Files changed (7) hide show
  1. README.md +4 -4
  2. app.py +103 -0
  3. detect.py +68 -0
  4. image.py +47 -0
  5. requirements.txt +4 -0
  6. utils.py +15 -0
  7. video.py +74 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
  title: Forensic
3
- emoji: πŸ†
4
- colorFrom: gray
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.25.0
8
  app_file: app.py
9
  pinned: false
10
  ---
 
1
  ---
2
  title: Forensic
3
+ emoji: πŸ‘€
4
+ colorFrom: blue
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.38.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from image import add_and_detect_watermark_image
3
+ from video import add_and_detect_watermark_video
4
+ from detect import detect_watermark_image, detect_watermark_video
5
+
6
+ # Image Interface
7
+ image_inputs = [
8
+ gr.Image(type="numpy", label="Upload Image"),
9
+ gr.Textbox(label="Watermark Text")
10
+ ]
11
+
12
+ image_outputs = [
13
+ gr.Image(type="numpy", label="Watermarked Image"),
14
+ gr.Image(type="numpy", label="Watermark Highlight"),
15
+ gr.File(label="Download Watermarked Image"),
16
+ gr.File(label="Download Watermark Highlight")
17
+ ]
18
+
19
+ def process_image(image, text):
20
+ watermarked_image, highlight, watermarked_image_path, highlight_path = add_and_detect_watermark_image(image, text)
21
+ return watermarked_image, highlight, watermarked_image_path, highlight_path
22
+
23
+ image_interface = gr.Interface(
24
+ fn=process_image,
25
+ inputs=image_inputs,
26
+ outputs=image_outputs,
27
+ title="Image Watermark Application",
28
+ description="Upload an image and add a watermark text. Detect watermark and highlight its position."
29
+ )
30
+
31
+ # Video Interface
32
+ video_inputs = [
33
+ gr.Video(label="Upload Video"),
34
+ gr.Textbox(label="Watermark Text")
35
+ ]
36
+
37
+ video_outputs = [
38
+ gr.Video(label="Watermarked Video"),
39
+ gr.Video(label="Watermark Highlight"),
40
+ gr.File(label="Download Watermarked Video"),
41
+ gr.File(label="Download Watermark Highlight")
42
+ ]
43
+
44
+ def process_video(video, text):
45
+ watermarked_video_path, highlight_video_path, _, _ = add_and_detect_watermark_video(video, text)
46
+ return watermarked_video_path, highlight_video_path, watermarked_video_path, highlight_video_path
47
+
48
+ video_interface = gr.Interface(
49
+ fn=process_video,
50
+ inputs=video_inputs,
51
+ outputs=video_outputs,
52
+ title="Video Watermark Application",
53
+ description="Upload a video and add a watermark text. Detect watermark and highlight its position."
54
+ )
55
+
56
+ # Forensic Watermark Detection Interface
57
+ detect_inputs = [
58
+ gr.Image(type="numpy", label="Upload Image")
59
+ ]
60
+
61
+ detect_outputs = [
62
+ gr.Image(type="numpy", label="Watermark Detection Result")
63
+ ]
64
+
65
+ detect_interface = gr.Interface(
66
+ fn=detect_watermark_image,
67
+ inputs=detect_inputs,
68
+ outputs=detect_outputs,
69
+ title="Forensic Watermark Detection",
70
+ description="Upload an image to detect forensic watermarks."
71
+ )
72
+
73
+ # Video Detection Interface
74
+ video_detect_inputs = [
75
+ gr.Video(label="Upload Video")
76
+ ]
77
+
78
+ video_detect_outputs = [
79
+ gr.Video(label="Watermark Detection Result")
80
+ ]
81
+
82
+ def detect_video(video):
83
+ detected_video_path = detect_watermark_video(video)
84
+ return detected_video_path
85
+
86
+ video_detect_interface = gr.Interface(
87
+ fn=detect_video,
88
+ inputs=video_detect_inputs,
89
+ outputs=video_detect_outputs,
90
+ title="Video Watermark Detection",
91
+ description="Upload a video to detect forensic watermarks."
92
+ )
93
+
94
+ # Combine interfaces in tabs
95
+ app = gr.TabbedInterface(
96
+ interface_list=[image_interface, video_interface, detect_interface, video_detect_interface],
97
+ tab_names=["Image", "Video", "Detect", "Video Detect"]
98
+ )
99
+
100
+ if __name__ == "__main__":
101
+ app.launch(
102
+ share=True
103
+ )
detect.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import random
4
+ import tempfile
5
+ from moviepy.video.io.VideoFileClip import VideoFileClip
6
+
7
+ def detect_watermark_image(image):
8
+ ycrcb_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
9
+ y_channel, _, _ = cv2.split(ycrcb_image)
10
+ dct_y = cv2.dct(np.float32(y_channel))
11
+
12
+ # Detecting the watermark
13
+ watermark = np.zeros_like(dct_y)
14
+ rows, cols = dct_y.shape
15
+ font = cv2.FONT_HERSHEY_SIMPLEX
16
+ text = "WATERMARK"
17
+ text_size = cv2.getTextSize(text, font, 0.5, 1)[0]
18
+ text_x = np.random.randint(0, cols - text_size[0])
19
+ text_y = np.random.randint(text_size[1], rows)
20
+ watermark = cv2.putText(watermark, text, (text_x, text_y), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
21
+
22
+ detected_image = cv2.idct(dct_y + watermark)
23
+ detected_image = np.uint8(np.clip(detected_image, 0, 255))
24
+
25
+ return detected_image
26
+
27
+ def detect_watermark_video(video_path):
28
+ """Detect and highlight watermarks in a video file.
29
+
30
+ Args:
31
+ video_path (str): Path to the video file
32
+
33
+ Returns:
34
+ str: Path to the video with detected watermarks
35
+ """
36
+ def process_frame(frame):
37
+ ycrcb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
38
+ y_channel, _, _ = cv2.split(ycrcb_image)
39
+ dct_y = cv2.dct(np.float32(y_channel))
40
+
41
+ # Detecting the watermark
42
+ watermark = np.zeros_like(dct_y)
43
+ rows, cols = dct_y.shape
44
+ font = cv2.FONT_HERSHEY_SIMPLEX
45
+ text = "WATERMARK"
46
+ text_size = cv2.getTextSize(text, font, 0.5, 1)[0]
47
+ text_x = np.random.randint(0, cols - text_size[0])
48
+ text_y = np.random.randint(text_size[1], rows)
49
+ watermark = cv2.putText(watermark, text, (text_x, text_y), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
50
+
51
+ detected_frame = cv2.idct(dct_y + watermark)
52
+ detected_frame = np.uint8(np.clip(detected_frame, 0, 255))
53
+
54
+ # Reconstruct the image with detected watermark
55
+ ycrcb_image[:, :, 0] = detected_frame
56
+ output_frame = cv2.cvtColor(ycrcb_image, cv2.COLOR_YCrCb2BGR)
57
+
58
+ return output_frame
59
+
60
+ # Process the video
61
+ video = moviepy.VideoFileClip(video_path)
62
+ processed_video = video.fl_image(process_frame)
63
+
64
+ # Save the result to a temporary file
65
+ _, output_path = tempfile.mkstemp(suffix=".mp4")
66
+ processed_video.write_videofile(output_path, codec='libx264')
67
+
68
+ return output_path
image.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import random
4
+ import tempfile
5
+ from utils import resize_image, text_to_image
6
+
7
+ def add_and_detect_watermark_image(image, watermark_text, num_watermarks=5):
8
+ watermark_positions = []
9
+
10
+ h, w, _ = image.shape
11
+ h_new = (h // 8) * 8
12
+ w_new = (w // 8) * 8
13
+ image_resized = cv2.resize(image, (w_new, h_new))
14
+
15
+ ycrcb_image = cv2.cvtColor(image_resized, cv2.COLOR_BGR2YCrCb)
16
+ y_channel, cr_channel, cb_channel = cv2.split(ycrcb_image)
17
+
18
+ dct_y = cv2.dct(np.float32(y_channel))
19
+
20
+ rows, cols = dct_y.shape
21
+ font = cv2.FONT_HERSHEY_SIMPLEX
22
+ for _ in range(num_watermarks):
23
+ text_size = cv2.getTextSize(watermark_text, font, 0.5, 1)[0]
24
+ text_x = random.randint(0, cols - text_size[0])
25
+ text_y = random.randint(text_size[1], rows)
26
+ watermark = np.zeros_like(dct_y)
27
+ watermark = cv2.putText(watermark, watermark_text, (text_x, text_y), font, 0.5, (1, 1, 1), 1, cv2.LINE_AA)
28
+ dct_y += watermark * 0.01
29
+ watermark_positions.append((text_x, text_y, text_size[0], text_size[1]))
30
+
31
+ idct_y = cv2.idct(dct_y)
32
+
33
+ ycrcb_image[:, :, 0] = idct_y
34
+ watermarked_image = cv2.cvtColor(ycrcb_image, cv2.COLOR_YCrCb2BGR)
35
+
36
+ watermark_highlight = watermarked_image.copy()
37
+ for (text_x, text_y, text_w, text_h) in watermark_positions:
38
+ cv2.putText(watermark_highlight, watermark_text, (text_x, text_y), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
39
+ cv2.rectangle(watermark_highlight, (text_x, text_y - text_h), (text_x + text_w, text_y), (0, 0, 255), 2)
40
+
41
+ # Save watermarked image and highlight to temporary files
42
+ _, watermarked_image_path = tempfile.mkstemp(suffix=".png")
43
+ _, watermark_highlight_path = tempfile.mkstemp(suffix=".png")
44
+ cv2.imwrite(watermarked_image_path, watermarked_image)
45
+ cv2.imwrite(watermark_highlight_path, watermark_highlight)
46
+
47
+ return watermarked_image, watermark_highlight, watermarked_image_path, watermark_highlight_path
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ numpy
3
+ opencv-python
4
+ moviepy
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+
4
+ def resize_image(img, shape):
5
+ return cv2.resize(img, (shape[1], shape[0]), interpolation=cv2.INTER_LINEAR)
6
+
7
+ def text_to_image(text, img_shape, font=cv2.FONT_HERSHEY_SIMPLEX, font_scale=3, thickness=5):
8
+ text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]
9
+ text_x = (img_shape[1] - text_size[0]) // 2
10
+ text_y = (img_shape[0] + text_size[1]) // 2
11
+
12
+ img_wm = np.zeros(img_shape, dtype=np.uint8)
13
+ cv2.putText(img_wm, text, (text_x, text_y), font, font_scale, (255, 255, 255), thickness)
14
+
15
+ return img_wm
video.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import random
4
+ import tempfile
5
+ import os
6
+ from moviepy.video.io.VideoFileClip import VideoFileClip # Updated import path
7
+
8
+ def add_and_detect_watermark_video(video_path, watermark_text, num_watermarks=5):
9
+ def add_watermark_to_frame(frame):
10
+ watermark_positions = []
11
+
12
+ # Resize frame to be divisible by 8 (required for DCT)
13
+ h, w, _ = frame.shape
14
+ h_new = (h // 8) * 8
15
+ w_new = (w // 8) * 8
16
+ frame_resized = cv2.resize(frame, (w_new, h_new))
17
+
18
+ # Convert to YCrCb color space and extract Y channel
19
+ ycrcb_image = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2YCrCb)
20
+ y_channel, cr_channel, cb_channel = cv2.split(ycrcb_image)
21
+
22
+ # Apply DCT to the Y channel
23
+ dct_y = cv2.dct(np.float32(y_channel))
24
+
25
+ # Add watermark in the DCT domain
26
+ rows, cols = dct_y.shape
27
+ font = cv2.FONT_HERSHEY_SIMPLEX
28
+ for _ in range(num_watermarks):
29
+ text_size = cv2.getTextSize(watermark_text, font, 0.5, 1)[0]
30
+ text_x = random.randint(0, cols - text_size[0])
31
+ text_y = random.randint(text_size[1], rows)
32
+ watermark = np.zeros_like(dct_y)
33
+ watermark = cv2.putText(watermark, watermark_text, (text_x, text_y), font, 0.5, (1, 1, 1), 1, cv2.LINE_AA)
34
+ dct_y += watermark * 0.01
35
+ watermark_positions.append((text_x, text_y, text_size[0], text_size[1]))
36
+
37
+ # Apply inverse DCT
38
+ idct_y = cv2.idct(dct_y)
39
+
40
+ # Merge channels and convert back to BGR
41
+ ycrcb_image[:, :, 0] = idct_y
42
+ watermarked_frame = cv2.cvtColor(ycrcb_image, cv2.COLOR_YCrCb2BGR)
43
+
44
+ # Highlight watermarks for visualization
45
+ watermark_highlight = watermarked_frame.copy()
46
+ for (text_x, text_y, text_w, text_h) in watermark_positions:
47
+ cv2.putText(watermark_highlight, watermark_text, (text_x, text_y), font, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
48
+ cv2.rectangle(watermark_highlight, (text_x, text_y - text_h), (text_x + text_w, text_y), (0, 0, 255), 2)
49
+
50
+ return watermarked_frame, watermark_highlight
51
+
52
+ try:
53
+ # Load video using MoviePy
54
+ video = VideoFileClip(video_path)
55
+
56
+ # Apply watermark to each frame
57
+ video_with_watermark = video.fl_image(lambda frame: add_watermark_to_frame(frame)[0])
58
+ video_with_highlight = video.fl_image(lambda frame: add_watermark_to_frame(frame)[1])
59
+
60
+ # Create temporary files for output videos
61
+ temp_fd, watermarked_video_path = tempfile.mkstemp(suffix=".mp4")
62
+ temp_fd_highlight, highlight_video_path = tempfile.mkstemp(suffix=".mp4")
63
+ os.close(temp_fd)
64
+ os.close(temp_fd_highlight)
65
+
66
+ # Write output videos
67
+ video_with_watermark.write_videofile(watermarked_video_path, codec='libx264')
68
+ video_with_highlight.write_videofile(highlight_video_path, codec='libx264')
69
+
70
+ return watermarked_video_path, highlight_video_path
71
+
72
+ except Exception as e:
73
+ print(f"An error occurred: {e}")
74
+ return None, None