Skip to content

Commit 3ce68a2

Browse files
committed
optional non-resizing of small images in resize_coco_dataset
1 parent b5c0d80 commit 3ce68a2

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

megadetector/data_management/resize_coco_dataset.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def _process_single_image_for_resize(image_data,
3737
target_size,
3838
correct_size_image_handling,
3939
unavailable_image_handling,
40+
no_enlarge_width,
4041
verbose):
4142
"""
4243
Processes a single image: loads, resizes/copies, updates metadata, and scales annotations.
@@ -83,6 +84,8 @@ def _process_single_image_for_resize(image_data,
8384

8485
image_is_already_target_size = \
8586
(input_w == target_size[0]) and (input_h == target_size[1])
87+
if no_enlarge_width and (input_w < target_size[0]):
88+
image_is_already_target_size = True
8689
preserve_original_size = \
8790
(target_size[0] == -1) and (target_size[1] == -1)
8891

@@ -100,7 +103,8 @@ def _process_single_image_for_resize(image_data,
100103
f'Unrecognized value {correct_size_image_handling} for correct_size_image_handling')
101104
else:
102105
try:
103-
pil_im = resize_image(pil_im, target_size[0], target_size[1])
106+
pil_im = resize_image(pil_im, target_size[0], target_size[1],
107+
no_enlarge_width=no_enlarge_width)
104108
output_w = pil_im.width
105109
output_h = pil_im.height
106110
exif_preserving_save(pil_im, output_fn_abs)
@@ -146,6 +150,7 @@ def resize_coco_dataset(input_folder,
146150
unavailable_image_handling='error',
147151
n_workers=1,
148152
pool_type='thread',
153+
no_enlarge_width=True,
149154
verbose=False):
150155
"""
151156
Given a COCO-formatted dataset (images in input_folder, data in input_filename), resizes
@@ -175,6 +180,9 @@ def resize_coco_dataset(input_folder,
175180
Defaults to 1 (no parallelization). If <= 1, processing is sequential.
176181
pool_type (str, optional): type of multiprocessing pool to use ('thread' or 'process').
177182
Defaults to 'thread'. Only used if n_workers > 1.
183+
no_enlarge_width (bool, optional): if [no_enlarge_width] is True, and
184+
[target width] is larger than the original image width, does not modify the image,
185+
but still writes it
178186
verbose (bool, optional): enable additional debug output
179187
180188
Returns:
@@ -221,6 +229,7 @@ def resize_coco_dataset(input_folder,
221229
target_size=target_size,
222230
correct_size_image_handling=correct_size_image_handling,
223231
unavailable_image_handling=unavailable_image_handling,
232+
no_enlarge_width=no_enlarge_width,
224233
verbose=verbose
225234
)
226235
processed_results.append(result)
@@ -240,6 +249,7 @@ def resize_coco_dataset(input_folder,
240249
target_size=target_size,
241250
correct_size_image_handling=correct_size_image_handling,
242251
unavailable_image_handling=unavailable_image_handling,
252+
no_enlarge_width=no_enlarge_width,
243253
verbose=verbose)
244254

245255
processed_results = list(tqdm(pool.imap(p_process_image, image_annotation_tuples),

megadetector/visualization/visualization_utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ def load_image(input_file, ignore_exif_rotation=False):
247247
return image
248248

249249

250-
def resize_image(image, target_width=-1, target_height=-1, output_file=None,
251-
no_enlarge_width=False, verbose=False, quality='keep'):
250+
def resize_image(image,
251+
target_width=-1,
252+
target_height=-1,
253+
output_file=None,
254+
no_enlarge_width=False,
255+
verbose=False,
256+
quality='keep'):
252257
"""
253258
Resizes a PIL Image object to the specified width and height; does not resize
254259
in place. If either width or height are -1, resizes with aspect ratio preservation.
@@ -1373,8 +1378,11 @@ def _resize_absolute_image(input_output_files,
13731378
try:
13741379
_ = resize_image(input_fn_abs,
13751380
output_file=output_fn_abs,
1376-
target_width=target_width, target_height=target_height,
1377-
no_enlarge_width=no_enlarge_width, verbose=verbose, quality=quality)
1381+
target_width=target_width,
1382+
target_height=target_height,
1383+
no_enlarge_width=no_enlarge_width,
1384+
verbose=verbose,
1385+
quality=quality)
13781386
status = 'success'
13791387
error = None
13801388
except Exception as e:

0 commit comments

Comments
 (0)