-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-processing.py
More file actions
113 lines (68 loc) · 2.63 KB
/
Copy pathpre-processing.py
File metadata and controls
113 lines (68 loc) · 2.63 KB
1
2
3
4
5
6
7
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import pandas as pd
from pathlib import Path
# Step 1: Configuration parameters and paths
BASE_DIR = Path(r"D:\Personal\PHOTOS")
VALID_EXTENSIONS = {"jpg", "jpeg", "png"}
OUTPUT_DIR = Path("data")
OUTPUT_DIR.mkdir(exist_ok=True)
OUTPUT_FILE = OUTPUT_DIR / "images_path.csv"
# Step 2: Scan directory for image files
def scan_images(base_dir, valid_extensions):
print("Scanning directory for image files...")
image_paths = []
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.lower().split(".")[-1] in valid_extensions:
image_paths.append(Path(root) / file)
print(f"Total files found: {len(image_paths)}")
image_ids = [f"img_{i+1:05d}" for i in range(len(image_paths))]
df = pd.DataFrame({
"image_id": image_ids,
"image_path": [str(p) for p in image_paths]
})
return df
# Step 3: Extract group, subgroup and event from image path
def extract_group_info(path):
parts = Path(path).parts
if "PHOTOS" in parts:
base_index = parts.index("PHOTOS")
group = parts[base_index + 1] if len(parts) > base_index + 1 else "Unknown"
subgroup = parts[base_index + 2] if len(parts) > base_index + 2 else "Unknown"
event = parts[base_index + 3] if len(parts) > base_index + 3 else ""
else:
group = "Unknown"
subgroup = "Unknown"
event = ""
return group, subgroup, event
# Step 4: Clean event and subgroup values
def clean_event(event):
if pd.isna(event):
return "no_event"
event = str(event).strip().lower()
if event.endswith((".jpg", ".jpeg", ".png")) or event == "":
return "no_event"
return event
def clean_subgroup(subgroup):
if pd.isna(subgroup):
return "no_subgroup"
subgroup = str(subgroup).strip().lower()
if subgroup.endswith((".jpg", ".jpeg", ".png")) or subgroup == "":
return "no_subgroup"
return subgroup
# Step 5: Build final dataframe
def main():
df = scan_images(BASE_DIR, VALID_EXTENSIONS)
print("Extracting group, subgroup, and event name...")
group_data = df["image_path"].apply(extract_group_info)
df["group"] = group_data.map(lambda x: x[0])
df["subgroup"] = group_data.map(lambda x: x[1])
df["event_name"] = group_data.map(lambda x: x[2])
print("Cleaning subgroup and event...")
df["subgroup"] = df["subgroup"].apply(clean_subgroup)
df["event_name"] = df["event_name"].apply(clean_event)
df.to_csv(OUTPUT_FILE, index=False)
print(f"Final cleaned dataset saved to: {OUTPUT_FILE}")
# Step 6: Script entry point
if __name__ == "__main__":
main()