@@ -29,11 +29,9 @@ def get_image_filenames(path: str | Path) -> list[Path]:
29
29
list[Path]: List of image filenames
30
30
31
31
"""
32
+ path = Path (path ).resolve ()
32
33
image_filenames : list [Path ]
33
34
34
- if isinstance (path , str ):
35
- path = Path (path )
36
-
37
35
if path .is_file () and path .suffix in IMG_EXTENSIONS :
38
36
image_filenames = [path ]
39
37
@@ -67,8 +65,10 @@ def duplicate_filename(path: str | Path) -> Path:
67
65
Returns:
68
66
Path: Duplicated output path.
69
67
"""
70
- if isinstance (path , str ):
71
- path = Path (path )
68
+ path = Path (path )
69
+
70
+ if not path .exists ():
71
+ return path
72
72
73
73
i = 0
74
74
while True :
@@ -114,32 +114,29 @@ def generate_output_image_filename(input_path: str | Path, output_path: str | Pa
114
114
Returns:
115
115
Path: The output filename to save the output predictions from the inferencer.
116
116
"""
117
- if isinstance (input_path , str ):
118
- input_path = Path (input_path )
117
+ input_path = Path (input_path )
118
+ output_path = Path (output_path )
119
119
120
- if isinstance (output_path , str ):
121
- output_path = Path (output_path )
122
-
123
- # This function expects an ``input_path`` that is a file. This is to check if output_path
120
+ # Input validation: Check if input_path is a valid directory or file
124
121
if input_path .is_file () is False :
125
122
msg = "input_path is expected to be a file to generate a proper output filename."
126
123
raise ValueError (msg )
127
124
128
125
# If the output is a directory, then add parent directory name
129
126
# and filename to the path. This is to ensure we do not overwrite
130
127
# images and organize based on the categories.
131
- file_path = output_path / input_path .parent .name / input_path .name if output_path .is_dir () else output_path
132
-
133
- # This new ``file_path`` might contain a directory path yet to be created.
134
- # Create the parent directory to avoid such cases.
135
- file_path .parent .mkdir (parents = True , exist_ok = True )
136
-
137
- if file_path .is_file ():
128
+ if output_path .is_dir ():
129
+ output_image_filename = output_path / input_path .parent .name / input_path .name
130
+ elif output_path .is_file () and output_path .exists ():
138
131
msg = f"{ output_path } already exists. Renaming the file to avoid overwriting."
139
132
logger .warning (msg )
140
- file_path = duplicate_filename (file_path )
133
+ output_image_filename = duplicate_filename (output_path )
134
+ else :
135
+ output_image_filename = output_path
136
+
137
+ output_image_filename .parent .mkdir (parents = True , exist_ok = True )
141
138
142
- return file_path
139
+ return output_image_filename
143
140
144
141
145
142
def get_image_height_and_width (image_size : int | Sequence [int ]) -> tuple [int , int ]:
0 commit comments