maitri01 commited on
Commit
d75b880
·
verified ·
1 Parent(s): 3ed2c0f

Update task_template.py

Browse files
Files changed (1) hide show
  1. task_template.py +106 -0
task_template.py CHANGED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import zipfile
4
+ from pathlib import Path
5
+
6
+ import numpy as np
7
+ import requests
8
+ from PIL import Image
9
+
10
+ # CONFIG
11
+ ZIP_FILE = "Dataset.zip" # Path to the downloaded dataset zip
12
+ DATASET_DIR = Path("Dataset") # Unzipped folder
13
+ TEMP_OUT_DIR = Path("submission_temp") # Temporary folder for forged images
14
+ FILE_PATH = "submission.zip" # Final file to upload
15
+
16
+ # Leaderboard submission
17
+ BASE_URL = "http://35.192.205.84:80"
18
+ API_KEY = "YOUR_API_KEY_HERE" # REPLACE WITH YOUR API KEY
19
+ TASK_ID = "22-forging-task"
20
+
21
+ # 1. UNZIP DATASET
22
+ if not DATASET_DIR.exists():
23
+ if not os.path.exists(ZIP_FILE):
24
+ raise FileNotFoundError(f"Could not find {ZIP_FILE}. Please download the dataset first.")
25
+
26
+ print(f"Unzipping {ZIP_FILE}...")
27
+ with zipfile.ZipFile(ZIP_FILE, "r") as zip_ref:
28
+ zip_ref.extractall(".")
29
+ else:
30
+ print("Dataset already extracted.")
31
+
32
+ # Ensure output directory exists
33
+ TEMP_OUT_DIR.mkdir(exist_ok=True)
34
+
35
+
36
+ # 2. NAIVE FORGERY ATTACK (IMAGE AVERAGING)
37
+ print("Building forgery submission...")
38
+
39
+ # Map the Dataset structure: (Source_Folder, Size_Subfolder, Target_Folder)
40
+ CATEGORIES = [
41
+ ("WM_1", 1, 25),
42
+ ("WM_2", 26, 50),
43
+ ("WM_3", 51, 75),
44
+ ("WM_4", 76, 100),
45
+ ("WM_5", 101, 125),
46
+ ("WM_6", 126, 150),
47
+ ("WM_7", 151, 175),
48
+ ("WM_8", 176, 200),
49
+ ]
50
+
51
+
52
+ total_processed = 0
53
+
54
+ for source_wm, target_start, target_stop in CATEGORIES:
55
+ print(f"Processing {source_wm} dataset -> Forging onto images {target_start}.png to {target_stop}.png ...")
56
+
57
+ source_dir = DATASET_DIR / "watermarked_sources" / source_wm
58
+ source_images = list(source_dir.glob("*.png"))
59
+
60
+ if not source_images:
61
+ print(f" [Warning] No source images found in {source_dir}")
62
+ continue
63
+
64
+ target_dir = DATASET_DIR / "clean_targets"
65
+ target_images = []
66
+
67
+ for number in range(target_start, target_stop + 1, 1):
68
+ temp = target_dir / f"{number}.png"
69
+ target_images.append(temp)
70
+
71
+ for target_path, source_path in zip(target_images, source_images):
72
+ # Load target clean image
73
+ target_pil = Image.open(target_path).convert("RGB")
74
+
75
+ # Load target source image
76
+ source_pil = Image.open(source_path).convert("RGB")
77
+
78
+ # Convert to numpy arrays for the math
79
+ target_arr = np.array(target_pil).astype(np.float32)
80
+ source_arr = np.array(source_pil).astype(np.float32)
81
+
82
+ # Blend the Image with a Watermarked Image (Alpha Blending)
83
+ forged_img = (target_arr * 0.5) + (source_arr * 0.5)
84
+
85
+ # Clip values to valid pixel range [0, 255] and convert to uint8
86
+ forged_img = np.clip(forged_img, 0, 255).astype(np.uint8)
87
+
88
+ # Save to our temporary flat directory using the exact original filename (e.g., "104.png")
89
+ out_path = TEMP_OUT_DIR / target_path.name
90
+ Image.fromarray(forged_img).save(out_path)
91
+ total_processed += 1
92
+
93
+ print(f"\nSuccessfully forged {total_processed} images.")
94
+ if total_processed != 200:
95
+ print(f"[WARNING] Expected 200 images, but processed {total_processed}. Your submission may be rejected!")
96
+
97
+
98
+ # 3. PACKAGE INTO FLAT ZIP FILE
99
+ print(f"Packaging images into {FILE_PATH}...")
100
+ with zipfile.ZipFile(FILE_PATH, "w", zipfile.ZIP_DEFLATED) as zipf:
101
+ for img_path in TEMP_OUT_DIR.glob("*.png"):
102
+ zipf.write(img_path, arcname=img_path.name)
103
+
104
+ print(f"Saved submission file to {FILE_PATH}")
105
+
106
+