35
35
def make_folder_video_dataset (
36
36
root : str | Path ,
37
37
normal_dir : str | Path = "" ,
38
- mask_dir : str | Path = "" ,
38
+ mask_dir : str | Path | None = "" ,
39
39
test_dir : str | Path = "" ,
40
40
split : str | Split | None = None ,
41
41
) -> DataFrame :
@@ -122,24 +122,27 @@ def _extract_samples(root: Path, path: Path) -> list:
122
122
root = validate_path (root )
123
123
normal_dir = validate_path (root / normal_dir )
124
124
test_dir = validate_path (root / test_dir )
125
- mask_dir = validate_path (root / mask_dir )
125
+
126
+ samples_list_labels = []
127
+ if mask_dir is not None :
128
+ mask_dir = validate_path (root / mask_dir )
129
+ samples_list_labels .extend (
130
+ [
131
+ filename .parts [- 1 ]
132
+ for filename in sorted (mask_dir .glob ("./*" ))
133
+ if (
134
+ _contains_files (path = filename , extensions = FOLDER_IMAGE_EXTENSIONS )
135
+ and not filename .name .startswith ("." )
136
+ )
137
+ or filename .suffix in [".npy" , ".pt" ]
138
+ ],
139
+ )
140
+
126
141
samples_list = []
127
142
samples_list .extend (_extract_samples (root , normal_dir ))
128
143
129
144
samples_list .extend (_extract_samples (root , test_dir ))
130
145
131
- samples_list_labels = []
132
- samples_list_labels .extend (
133
- [
134
- filename .parts [- 1 ]
135
- for filename in sorted (mask_dir .glob ("./*" ))
136
- if (
137
- _contains_files (path = filename , extensions = FOLDER_IMAGE_EXTENSIONS ) and not filename .name .startswith ("." )
138
- )
139
- or filename .suffix in [".npy" , ".pt" ]
140
- ],
141
- )
142
-
143
146
samples = DataFrame (samples_list , columns = ["root" , "folder" , "image_path" ])
144
147
145
148
# Remove DS_Store
@@ -150,10 +153,12 @@ def _extract_samples(root: Path, path: Path) -> list:
150
153
samples .loc [samples .folder == normal_dir .parts [- 1 ], "split" ] = "train"
151
154
samples .loc [samples .folder == test_dir .parts [- 1 ], "split" ] = "test"
152
155
samples_list_labels = [str (item ) for item in samples_list_labels if ".DS_Store" not in str (item )]
153
- samples .loc [samples .folder == test_dir .parts [- 1 ], "mask_path" ] = samples_list_labels
154
- if samples_list_labels == []:
156
+
157
+ if mask_dir is not None :
158
+ samples .loc [samples .folder == test_dir .parts [- 1 ], "mask_path" ] = samples_list_labels
159
+ samples ["mask_path" ] = str (mask_dir ) + "/" + samples .mask_path .astype (str )
160
+ else :
155
161
samples .loc [samples .folder == test_dir .parts [- 1 ], "mask_path" ] = ""
156
- samples ["mask_path" ] = str (mask_dir ) + "/" + samples .mask_path .astype (str )
157
162
samples .loc [samples .folder == normal_dir .parts [- 1 ], "mask_path" ] = ""
158
163
159
164
if split :
@@ -239,7 +244,7 @@ class FolderDataset(AnomalibVideoDataset):
239
244
root (Path | str): Path to the dataset.
240
245
normal_dir (Path | str): Path to the training videos of the dataset (.avi / .mp4 / imgages as frames).
241
246
test_dir (Path | str): Path to the testing videos of the dataset.
242
- mask_dir (Path | str): Path to the masks for the training videos of the dataset (.npy/.pt/ images)
247
+ mask_dir (Path | str | None ): Path to the masks for the training videos of the dataset (.npy/.pt/ images)
243
248
clip_length_in_frames (int, optional): Number of video frames in each clip.
244
249
frames_between_clips (int, optional): Number of frames between each consecutive video clip.
245
250
target_frame (VideoTargetFrame): Specifies the target frame in the video clip, used for ground truth retrieval.
@@ -252,7 +257,7 @@ def __init__(
252
257
task : TaskType ,
253
258
split : Split ,
254
259
root : Path | str ,
255
- mask_dir : Path | str = "test_labels" ,
260
+ mask_dir : Path | str | None = None ,
256
261
normal_dir : Path | str = "train_dir" ,
257
262
test_dir : Path | str = "test_dir" ,
258
263
clip_length_in_frames : int = 2 ,
@@ -307,11 +312,11 @@ class FolderVideo(AnomalibVideoDataModule):
307
312
root (Path | str): Path to the root of the dataset
308
313
normal_dir (Path | str): Path to the training videos of the dataset (.avi / .mp4 / imgages as frames).
309
314
test_dir (Path | str): Path to the testing videos of the dataset.
310
- mask_dir (Path | str): Path to the masks for the training videos of the dataset (.npy/.pt/ images)
315
+ mask_dir (Path | str | None ): Path to the masks for the training videos of the dataset (.npy/.pt/ images)
311
316
clip_length_in_frames (int, optional): Number of video frames in each clip.
312
317
frames_between_clips (int, optional): Number of frames between each consecutive video clip.
313
318
target_frame (VideoTargetFrame): Specifies the target frame in the video clip, used for ground truth retrieval
314
- task (TaskType): Task type, 'classification', 'detection' or 'segmentation'
319
+ task (TaskType | str ): Task type, 'classification', 'detection' or 'segmentation'
315
320
image_size (tuple[int, int], optional): Size to which input images should be resized.
316
321
Defaults to ``None``.
317
322
transform (Transform, optional): Transforms that should be applied to the input images.
@@ -331,13 +336,13 @@ class FolderVideo(AnomalibVideoDataModule):
331
336
def __init__ (
332
337
self ,
333
338
root : Path | str ,
334
- mask_dir : Path | str = "test_labels" ,
339
+ mask_dir : Path | str | None = None ,
335
340
normal_dir : Path | str = "train_vid" ,
336
341
test_dir : Path | str = "test_vid" ,
337
342
clip_length_in_frames : int = 2 ,
338
343
frames_between_clips : int = 1 ,
339
344
target_frame : VideoTargetFrame = VideoTargetFrame .LAST ,
340
- task : TaskType = TaskType .SEGMENTATION ,
345
+ task : TaskType | str = TaskType .SEGMENTATION ,
341
346
image_size : tuple [int , int ] | None = None ,
342
347
transform : Transform | None = None ,
343
348
train_transform : Transform | None = None ,
@@ -366,11 +371,7 @@ def __init__(
366
371
self .root = Path (root )
367
372
self .normal_dir = Path (normal_dir )
368
373
self .test_dir = Path (test_dir )
369
- if mask_dir is not None :
370
- self .mask_dir = Path (mask_dir )
371
- else :
372
- self .mask_dir = Path ()
373
-
374
+ self .mask_dir = mask_dir if mask_dir is None else Path (mask_dir )
374
375
self .clip_length_in_frames = clip_length_in_frames
375
376
self .frames_between_clips = frames_between_clips
376
377
self .target_frame = VideoTargetFrame (target_frame )
0 commit comments