File size: 5,634 Bytes
5626a1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
import pandas as pd
def delete_images_with_patterns(directory: str, patterns: list):
    """

    Deletes image files in the given directory if their filenames contain any of the specified patterns.



    Args:

        directory (str): The path to the directory containing images.

        patterns (list): A list of substrings to check in filenames.

    """
    if not os.path.exists(directory):
        print(f"Directory '{directory}' does not exist.")
        return
    
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        
        # Check if the filename contains any of the specified patterns
        if any("dr"+pattern in filename for pattern in patterns):
            try:
                os.remove(file_path)
                print(f"Deleted: {file_path}")
            except Exception as e:
                print(f"Error deleting {file_path}: {e}")

def clean_csv(csv_path: str, patterns: list):
    """

    Removes rows from the CSV if the first column contains filenames matching any pattern (e.g., "1_1" -> "dr1_1").

    Ensures that there are no additional digits after the pattern unless separated by an underscore `_`.

    """
    if not os.path.exists(csv_path):
        print(f"CSV file '{csv_path}' does not exist.")
        return
    
    # Load CSV into a DataFrame
    df = pd.read_csv(csv_path)

    # Ensure the first column is treated as a string
    df.iloc[:, 0] = df.iloc[:, 0].astype(str)

    # Create modified patterns to match filenames
    modified_patterns = [f"dr{p}" for p in patterns]

    # Build a regex pattern to match filenames exactly or with an underscore and additional digits
    regex_patterns = []
    for pattern in modified_patterns:
        # Match the pattern exactly or with an underscore and additional digits
        regex_patterns.append(f"^{pattern}(_\\d+)?$")

    # Combine all regex patterns into a single pattern
    combined_regex = '|'.join(regex_patterns)

    # Filter out rows where the first column matches any of the regex patterns
    df = df[~df.iloc[:, 0].str.match(combined_regex, na=False)]

    # Remove duplicates
    df.drop_duplicates(inplace=True)

    # Save cleaned data back to CSV
    df.to_csv(csv_path, index=False)
    print(f"Updated CSV saved: {csv_path}")

# List of text patterns to match in filenames
patterns_to_delete = [
    "1_1", 
    "4_1", 
    "4_2", 
    "4_3", 
    "4_4", 
    "4_5", 
    "4_6",
    "5_1", 
    "5_2", 
    "7_1", 
    "10_1", 
    "24_1", 
    "24_2", 
    "25_1", 
    "25_2", 
    "29_1", 
    "30_1", 
    "33_1", 
    "36_1", 
    "36_4", 
    "36_5", 
    "36_6", 
    "38_1", 
    "38_2", 
    "38_3", 
    "38_4", 
    "38_5", 
    "38_6", 
    "38_7", 
    "38_8", 
    "38_9", 
    "42_1", 
    "42_2", 
    "42_4", 
    "43_1", 
    "43_2", 
    "43_3", 
    "43_4", 
    "43_5", 
    "44_1", 
    "44_2", 
    "44_3", 
    "44_4", 
    "44_6", 
    "45_1", 
    "47_1", 
    "50_1", 
    "57_1", 
    "57_2", 
    "63_1", 
    "64_1", 
    "64_2", 
    "64_3", 
    "64_4", 
    "64_5", 
    "64_6", 
    "64_7", 
    "64_8", 
    "64_9", 
    "65_1", 
    "65_2", 
    "66_1", 
    "66_2", 
    "66_3", 
    "66_4", 
    "66_5", 
    "66_6", 
    "66_7", 
    "66_8", 
    "69_1", 
    "69_2", 
    "69_3", 
    "69_4", 
    "69_5", 
    "69_6", 
    "69_7", 
    "69_8", 
    "69_9", 
    "71_1", 
    "71_2", 
    "71_3", 
    "71_4", 
    "71_5", 
    "73_1", 
    "74_1", 
    "75_1", 
    "75_2", 
    "75_3", 
    "75_4", 
    "75_5", 
    "75_6", 
    "77_1", 
    "77_2", 
    "77_3", 
    "76_1", 
    "76_2", 
    "76_3", 
    "76_4", 
    "76_5",
    "80_1", 
    "80_2", 
    "82_1", 
    "86_1", 
    "86_2", 
    "86_3", 
    "86_4", 
    "86_5",
    "87_1", 
    "87_2", 
    "87_3", 
    "87_4", 
    "87_5", 
    "87_6",
    "89_1", 
    "92_1", 
    "92_2", 
    "93_1", 
    "94_2", 
    "94_1", 
    "95_1",
    "97_1", 
    "97_2",
    "102_1",
    "104_1",
    "108_1",
    "109_1",
    "112_1",
    "114_1",
    "114_2",
    "114_3",
    "114_4",
    "114_5",
    "114_6",
    "114_7",
    "114_8",
    "114_9",
    "115_1",
    "115_2",
    "116_1",
    "116_2",
    "116_3",
    "117_1",
    "128_1",
    "130_1",
    "132_1",
    "132_2",
    "132_3",
    "137_1",
    "137_2",
    "137_3",
    "137_4",
    "137_5",
    "137_6",
    "137_7",
    "137_8",
    "137_9",
    "140_5",
    "146_1",
    "146_2",
    "146_3",
    "151_1",
    "151_2",
    "163_1",
    "169_1",
    "173_1",
    "173_2",
    "100_1"
]

# Specify your target directory
target_directory = "./cropped_images"  # Change this to your actual directory

# Run the deletion function
# delete_images_with_patterns(target_directory, patterns_to_delete)
patterns_to_delete = [
    "dr80_2",
    "dr80_3",
    "dr81_1",
    "dr81_1",
    "dr81_2",
    "dr83_1",
    "dr86_1",
    "dr86_2",
    "dr86_3",
    "dr86_4",
    "dr86_5",
    "dr87_1",
    "dr87_2",
    "dr87_3",
    "dr87_4",
    "dr87_5",
    "dr87_6",
    "dr88_1",
    "dr89_1",
    "dr89_2",
    "dr9_1",
    "dr90_1",
    "dr92_1",
    "dr92_1",
    "dr92_2",
    "dr92_3",
    "dr93_1",
    "dr93_2",
    "dr94_1",
    "dr94_2",
    "dr94_3",
    "dr95_1",
    "dr95_2",
    "dr96_1",
    "dr97_1",
    "dr97_2",
    "dr97_3",
    "dr98_1",
]
clean_csv("all_cropped_data.csv",patterns=patterns_to_delete)