megadetector 10.0.15__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. megadetector/__init__.py +0 -0
  2. megadetector/api/__init__.py +0 -0
  3. megadetector/api/batch_processing/integration/digiKam/setup.py +6 -0
  4. megadetector/api/batch_processing/integration/digiKam/xmp_integration.py +465 -0
  5. megadetector/api/batch_processing/integration/eMammal/test_scripts/config_template.py +5 -0
  6. megadetector/api/batch_processing/integration/eMammal/test_scripts/push_annotations_to_emammal.py +125 -0
  7. megadetector/api/batch_processing/integration/eMammal/test_scripts/select_images_for_testing.py +55 -0
  8. megadetector/classification/__init__.py +0 -0
  9. megadetector/classification/aggregate_classifier_probs.py +108 -0
  10. megadetector/classification/analyze_failed_images.py +227 -0
  11. megadetector/classification/cache_batchapi_outputs.py +198 -0
  12. megadetector/classification/create_classification_dataset.py +626 -0
  13. megadetector/classification/crop_detections.py +516 -0
  14. megadetector/classification/csv_to_json.py +226 -0
  15. megadetector/classification/detect_and_crop.py +853 -0
  16. megadetector/classification/efficientnet/__init__.py +9 -0
  17. megadetector/classification/efficientnet/model.py +415 -0
  18. megadetector/classification/efficientnet/utils.py +608 -0
  19. megadetector/classification/evaluate_model.py +520 -0
  20. megadetector/classification/identify_mislabeled_candidates.py +152 -0
  21. megadetector/classification/json_to_azcopy_list.py +63 -0
  22. megadetector/classification/json_validator.py +696 -0
  23. megadetector/classification/map_classification_categories.py +276 -0
  24. megadetector/classification/merge_classification_detection_output.py +509 -0
  25. megadetector/classification/prepare_classification_script.py +194 -0
  26. megadetector/classification/prepare_classification_script_mc.py +228 -0
  27. megadetector/classification/run_classifier.py +287 -0
  28. megadetector/classification/save_mislabeled.py +110 -0
  29. megadetector/classification/train_classifier.py +827 -0
  30. megadetector/classification/train_classifier_tf.py +725 -0
  31. megadetector/classification/train_utils.py +323 -0
  32. megadetector/data_management/__init__.py +0 -0
  33. megadetector/data_management/animl_to_md.py +161 -0
  34. megadetector/data_management/annotations/__init__.py +0 -0
  35. megadetector/data_management/annotations/annotation_constants.py +33 -0
  36. megadetector/data_management/camtrap_dp_to_coco.py +270 -0
  37. megadetector/data_management/cct_json_utils.py +566 -0
  38. megadetector/data_management/cct_to_md.py +184 -0
  39. megadetector/data_management/cct_to_wi.py +293 -0
  40. megadetector/data_management/coco_to_labelme.py +284 -0
  41. megadetector/data_management/coco_to_yolo.py +701 -0
  42. megadetector/data_management/databases/__init__.py +0 -0
  43. megadetector/data_management/databases/add_width_and_height_to_db.py +107 -0
  44. megadetector/data_management/databases/combine_coco_camera_traps_files.py +210 -0
  45. megadetector/data_management/databases/integrity_check_json_db.py +563 -0
  46. megadetector/data_management/databases/subset_json_db.py +195 -0
  47. megadetector/data_management/generate_crops_from_cct.py +200 -0
  48. megadetector/data_management/get_image_sizes.py +164 -0
  49. megadetector/data_management/labelme_to_coco.py +559 -0
  50. megadetector/data_management/labelme_to_yolo.py +349 -0
  51. megadetector/data_management/lila/__init__.py +0 -0
  52. megadetector/data_management/lila/create_lila_blank_set.py +556 -0
  53. megadetector/data_management/lila/create_lila_test_set.py +192 -0
  54. megadetector/data_management/lila/create_links_to_md_results_files.py +106 -0
  55. megadetector/data_management/lila/download_lila_subset.py +182 -0
  56. megadetector/data_management/lila/generate_lila_per_image_labels.py +777 -0
  57. megadetector/data_management/lila/get_lila_annotation_counts.py +174 -0
  58. megadetector/data_management/lila/get_lila_image_counts.py +112 -0
  59. megadetector/data_management/lila/lila_common.py +319 -0
  60. megadetector/data_management/lila/test_lila_metadata_urls.py +164 -0
  61. megadetector/data_management/mewc_to_md.py +344 -0
  62. megadetector/data_management/ocr_tools.py +873 -0
  63. megadetector/data_management/read_exif.py +964 -0
  64. megadetector/data_management/remap_coco_categories.py +195 -0
  65. megadetector/data_management/remove_exif.py +156 -0
  66. megadetector/data_management/rename_images.py +194 -0
  67. megadetector/data_management/resize_coco_dataset.py +665 -0
  68. megadetector/data_management/speciesnet_to_md.py +41 -0
  69. megadetector/data_management/wi_download_csv_to_coco.py +247 -0
  70. megadetector/data_management/yolo_output_to_md_output.py +594 -0
  71. megadetector/data_management/yolo_to_coco.py +984 -0
  72. megadetector/data_management/zamba_to_md.py +188 -0
  73. megadetector/detection/__init__.py +0 -0
  74. megadetector/detection/change_detection.py +840 -0
  75. megadetector/detection/process_video.py +479 -0
  76. megadetector/detection/pytorch_detector.py +1451 -0
  77. megadetector/detection/run_detector.py +1267 -0
  78. megadetector/detection/run_detector_batch.py +2172 -0
  79. megadetector/detection/run_inference_with_yolov5_val.py +1314 -0
  80. megadetector/detection/run_md_and_speciesnet.py +1604 -0
  81. megadetector/detection/run_tiled_inference.py +1044 -0
  82. megadetector/detection/tf_detector.py +209 -0
  83. megadetector/detection/video_utils.py +1379 -0
  84. megadetector/postprocessing/__init__.py +0 -0
  85. megadetector/postprocessing/add_max_conf.py +72 -0
  86. megadetector/postprocessing/categorize_detections_by_size.py +166 -0
  87. megadetector/postprocessing/classification_postprocessing.py +1943 -0
  88. megadetector/postprocessing/combine_batch_outputs.py +249 -0
  89. megadetector/postprocessing/compare_batch_results.py +2110 -0
  90. megadetector/postprocessing/convert_output_format.py +403 -0
  91. megadetector/postprocessing/create_crop_folder.py +629 -0
  92. megadetector/postprocessing/detector_calibration.py +570 -0
  93. megadetector/postprocessing/generate_csv_report.py +522 -0
  94. megadetector/postprocessing/load_api_results.py +223 -0
  95. megadetector/postprocessing/md_to_coco.py +428 -0
  96. megadetector/postprocessing/md_to_labelme.py +351 -0
  97. megadetector/postprocessing/md_to_wi.py +41 -0
  98. megadetector/postprocessing/merge_detections.py +392 -0
  99. megadetector/postprocessing/postprocess_batch_results.py +2140 -0
  100. megadetector/postprocessing/remap_detection_categories.py +226 -0
  101. megadetector/postprocessing/render_detection_confusion_matrix.py +677 -0
  102. megadetector/postprocessing/repeat_detection_elimination/find_repeat_detections.py +206 -0
  103. megadetector/postprocessing/repeat_detection_elimination/remove_repeat_detections.py +82 -0
  104. megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py +1665 -0
  105. megadetector/postprocessing/separate_detections_into_folders.py +795 -0
  106. megadetector/postprocessing/subset_json_detector_output.py +964 -0
  107. megadetector/postprocessing/top_folders_to_bottom.py +238 -0
  108. megadetector/postprocessing/validate_batch_results.py +332 -0
  109. megadetector/taxonomy_mapping/__init__.py +0 -0
  110. megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py +491 -0
  111. megadetector/taxonomy_mapping/map_new_lila_datasets.py +211 -0
  112. megadetector/taxonomy_mapping/prepare_lila_taxonomy_release.py +165 -0
  113. megadetector/taxonomy_mapping/preview_lila_taxonomy.py +543 -0
  114. megadetector/taxonomy_mapping/retrieve_sample_image.py +71 -0
  115. megadetector/taxonomy_mapping/simple_image_download.py +231 -0
  116. megadetector/taxonomy_mapping/species_lookup.py +1008 -0
  117. megadetector/taxonomy_mapping/taxonomy_csv_checker.py +159 -0
  118. megadetector/taxonomy_mapping/taxonomy_graph.py +346 -0
  119. megadetector/taxonomy_mapping/validate_lila_category_mappings.py +83 -0
  120. megadetector/tests/__init__.py +0 -0
  121. megadetector/tests/test_nms_synthetic.py +335 -0
  122. megadetector/utils/__init__.py +0 -0
  123. megadetector/utils/ct_utils.py +1857 -0
  124. megadetector/utils/directory_listing.py +199 -0
  125. megadetector/utils/extract_frames_from_video.py +307 -0
  126. megadetector/utils/gpu_test.py +125 -0
  127. megadetector/utils/md_tests.py +2072 -0
  128. megadetector/utils/path_utils.py +2872 -0
  129. megadetector/utils/process_utils.py +172 -0
  130. megadetector/utils/split_locations_into_train_val.py +237 -0
  131. megadetector/utils/string_utils.py +234 -0
  132. megadetector/utils/url_utils.py +825 -0
  133. megadetector/utils/wi_platform_utils.py +968 -0
  134. megadetector/utils/wi_taxonomy_utils.py +1766 -0
  135. megadetector/utils/write_html_image_list.py +239 -0
  136. megadetector/visualization/__init__.py +0 -0
  137. megadetector/visualization/plot_utils.py +309 -0
  138. megadetector/visualization/render_images_with_thumbnails.py +243 -0
  139. megadetector/visualization/visualization_utils.py +1973 -0
  140. megadetector/visualization/visualize_db.py +630 -0
  141. megadetector/visualization/visualize_detector_output.py +498 -0
  142. megadetector/visualization/visualize_video_output.py +705 -0
  143. megadetector-10.0.15.dist-info/METADATA +115 -0
  144. megadetector-10.0.15.dist-info/RECORD +147 -0
  145. megadetector-10.0.15.dist-info/WHEEL +5 -0
  146. megadetector-10.0.15.dist-info/licenses/LICENSE +19 -0
  147. megadetector-10.0.15.dist-info/top_level.txt +1 -0
@@ -0,0 +1,665 @@
1
+ """
2
+
3
+ resize_coco_dataset.py
4
+
5
+ Given a COCO-formatted dataset, resizes all the images to a target size,
6
+ scaling bounding boxes accordingly.
7
+
8
+ """
9
+
10
+ #%% Imports and constants
11
+
12
+ import os
13
+ import json
14
+ import shutil
15
+ import argparse
16
+ import sys
17
+
18
+ from collections import defaultdict
19
+ from multiprocessing.pool import Pool, ThreadPool
20
+ from functools import partial
21
+
22
+ from PIL import Image
23
+ from tqdm import tqdm
24
+
25
+ from megadetector.utils.path_utils import insert_before_extension
26
+ from megadetector.visualization.visualization_utils import \
27
+ open_image, resize_image, exif_preserving_save
28
+ from megadetector.utils.ct_utils import make_test_folder
29
+ from megadetector.utils.ct_utils import write_json
30
+
31
+
32
+ #%% Functions
33
+
34
+ def _process_single_image_for_resize(image_data,
35
+ input_folder,
36
+ output_folder,
37
+ target_size,
38
+ correct_size_image_handling,
39
+ unavailable_image_handling,
40
+ no_enlarge_width,
41
+ verbose):
42
+ """
43
+ Processes a single image: loads, resizes/copies, updates metadata, and scales annotations.
44
+
45
+ [image_data] is a tuple of [im,annotations]
46
+ """
47
+
48
+ assert unavailable_image_handling in ('error','omit'), \
49
+ f'Illegal unavailable_image_handling {unavailable_image_handling}'
50
+
51
+ assert isinstance(image_data,tuple) and len(image_data) == 2
52
+ assert isinstance(image_data[0],dict)
53
+ assert isinstance(image_data[1],list)
54
+ im = image_data[0].copy()
55
+ annotations_this_image = [ann.copy() for ann in image_data[1]]
56
+
57
+ input_fn_relative = im['file_name']
58
+ input_fn_abs = os.path.join(input_folder, input_fn_relative)
59
+
60
+ if not os.path.isfile(input_fn_abs):
61
+ if unavailable_image_handling == 'error':
62
+ raise FileNotFoundError('Could not find file {}'.format(input_fn_abs))
63
+ else:
64
+ print("Can't find image {}, skipping".format(input_fn_relative))
65
+ return None, None
66
+
67
+ output_fn_abs = os.path.join(output_folder, input_fn_relative)
68
+ output_dir = os.path.dirname(output_fn_abs)
69
+ if len(output_dir) > 0:
70
+ os.makedirs(output_dir, exist_ok=True)
71
+
72
+ if verbose:
73
+ print('Resizing {} to {}'.format(input_fn_abs,output_fn_abs))
74
+
75
+ try:
76
+ pil_im = open_image(input_fn_abs)
77
+ input_w = pil_im.width
78
+ input_h = pil_im.height
79
+ except Exception as e:
80
+ if unavailable_image_handling == 'error':
81
+ raise Exception('Could not open image {}: {}'.format(
82
+ input_fn_relative, str(e)))
83
+ else:
84
+ print("Can't open image {}, skipping".format(input_fn_relative))
85
+ return None, None
86
+
87
+ image_is_already_target_size = \
88
+ (input_w == target_size[0]) and (input_h == target_size[1])
89
+ if no_enlarge_width and (input_w < target_size[0]):
90
+ image_is_already_target_size = True
91
+ preserve_original_size = \
92
+ (target_size[0] == -1) and (target_size[1] == -1)
93
+
94
+ # Do we need to resize, or can we try to get away with a copy?
95
+ if image_is_already_target_size or preserve_original_size:
96
+ output_w = input_w
97
+ output_h = input_h
98
+ if correct_size_image_handling == 'copy':
99
+ if input_fn_abs != output_fn_abs: # only copy if src and dst are different
100
+ shutil.copyfile(input_fn_abs, output_fn_abs)
101
+ elif correct_size_image_handling == 'rewrite':
102
+ exif_preserving_save(pil_im, output_fn_abs)
103
+ else:
104
+ raise ValueError(
105
+ f'Unrecognized value {correct_size_image_handling} for correct_size_image_handling')
106
+ else:
107
+ try:
108
+ pil_im = resize_image(pil_im, target_size[0], target_size[1],
109
+ no_enlarge_width=no_enlarge_width)
110
+ output_w = pil_im.width
111
+ output_h = pil_im.height
112
+ # We've already applied the rotation at the time we loaded the image, so don't
113
+ # write the orientation tag.
114
+ exif_preserving_save(pil_im, output_fn_abs, tags_to_exclude=('Orientation',))
115
+ except Exception as e:
116
+ if unavailable_image_handling == 'error':
117
+ raise Exception('Could not resize image {}: {}'.format(
118
+ input_fn_relative, str(e)))
119
+ else:
120
+ print("Can't resize image {}, skipping".format(input_fn_relative))
121
+ return None,None
122
+
123
+ im['width'] = output_w
124
+ im['height'] = output_h
125
+
126
+ for ann in annotations_this_image:
127
+
128
+ if 'bbox' in ann:
129
+ bbox = ann['bbox']
130
+ if (output_w != input_w) or (output_h != input_h):
131
+ width_scale = output_w / input_w
132
+ height_scale = output_h / input_h
133
+ bbox = [
134
+ bbox[0] * width_scale,
135
+ bbox[1] * height_scale,
136
+ bbox[2] * width_scale,
137
+ bbox[3] * height_scale
138
+ ]
139
+ ann['bbox'] = bbox
140
+
141
+ # ...for each annotation associated with this image
142
+
143
+ return im, annotations_this_image
144
+
145
+ # ...def _process_single_image_for_resize(...)
146
+
147
+
148
+ def resize_coco_dataset(input_folder,
149
+ input_filename,
150
+ output_folder,
151
+ output_filename=None,
152
+ target_size=(-1,-1),
153
+ correct_size_image_handling='copy',
154
+ unavailable_image_handling='error',
155
+ n_workers=1,
156
+ pool_type='thread',
157
+ no_enlarge_width=True,
158
+ verbose=False):
159
+ """
160
+ Given a COCO-formatted dataset (images in input_folder, data in input_filename), resizes
161
+ all the images to a target size (in output_folder) and scales bounding boxes accordingly.
162
+
163
+ Args:
164
+ input_folder (str): the folder where images live; filenames in [input_filename] should
165
+ be relative to [input_folder]
166
+ input_filename (str): the (input) COCO-formatted .json file containing annotations
167
+ output_folder (str): the folder to which we should write resized images; can be the
168
+ same as [input_folder], in which case images are over-written
169
+ output_filename (str, optional): the COCO-formatted .json file we should generate that refers
170
+ to the resized images
171
+ target_size (list or tuple of ints, optional): this should be tuple/list of ints, with length 2 (w,h).
172
+ If either dimension is -1, aspect ratio will be preserved. If both dimensions are -1, this means
173
+ "keep the original size". If both dimensions are -1 and correct_size_image_handling is copy, this
174
+ function is basically a no-op.
175
+ correct_size_image_handling (str, optional): what to do in the case where the original size
176
+ already matches the target size. Can be 'copy' (in which case the original image is just copied
177
+ to the output folder) or 'rewrite' (in which case the image is opened via PIL and re-written,
178
+ attempting to preserve the same quality). The only reason to do use 'rewrite' 'is the case where
179
+ you're superstitious about biases coming from images in a training set being written by different
180
+ image encoders.
181
+ unavailable_image_handling (str, optional): what to do when a file can't be opened. Can be
182
+ 'error' or 'omit'.
183
+ n_workers (int, optional): number of workers to use for parallel processing.
184
+ Defaults to 1 (no parallelization). If <= 1, processing is sequential.
185
+ pool_type (str, optional): type of multiprocessing pool to use ('thread' or 'process').
186
+ Defaults to 'thread'. Only used if n_workers > 1.
187
+ no_enlarge_width (bool, optional): if [no_enlarge_width] is True, and
188
+ [target width] is larger than the original image width, does not modify the image,
189
+ but still writes it
190
+ verbose (bool, optional): enable additional debug output
191
+
192
+ Returns:
193
+ dict: the COCO database with resized images, identical to the content of [output_filename]
194
+ """
195
+
196
+ # Validate arguments
197
+
198
+ assert unavailable_image_handling in ('error','omit'), \
199
+ f'Illegal unavailable_image_handling {unavailable_image_handling}'
200
+
201
+ # Read input data
202
+ with open(input_filename,'r') as f:
203
+ d = json.load(f)
204
+
205
+ # Map image IDs to annotations
206
+ image_id_to_annotations = defaultdict(list)
207
+ for ann in d['annotations']:
208
+ image_id_to_annotations[ann['image_id']].append(ann)
209
+
210
+ original_images = d['images']
211
+
212
+ # Our worker function will take tuples of images and their
213
+ # associated annotations
214
+ image_annotation_tuples = []
215
+ for im in original_images:
216
+ if im['id'] not in image_id_to_annotations:
217
+ annotations_this_image = []
218
+ else:
219
+ annotations_this_image = image_id_to_annotations[im['id']]
220
+ image_annotation_tuple = (im,annotations_this_image)
221
+ image_annotation_tuples.append(image_annotation_tuple)
222
+
223
+ processed_results = []
224
+
225
+ if n_workers <= 1:
226
+
227
+ for image_annotation_tuple in tqdm(image_annotation_tuples,
228
+ desc="Resizing images sequentially"):
229
+ result = _process_single_image_for_resize(
230
+ image_data=image_annotation_tuple,
231
+ input_folder=input_folder,
232
+ output_folder=output_folder,
233
+ target_size=target_size,
234
+ correct_size_image_handling=correct_size_image_handling,
235
+ unavailable_image_handling=unavailable_image_handling,
236
+ no_enlarge_width=no_enlarge_width,
237
+ verbose=verbose
238
+ )
239
+ processed_results.append(result)
240
+
241
+ else:
242
+
243
+ pool = None
244
+
245
+ try:
246
+
247
+ assert pool_type in ('process', 'thread'), f'Illegal pool type {pool_type}'
248
+ selected_pool = ThreadPool if (pool_type == 'thread') else Pool
249
+
250
+ print(f'Starting a {pool_type} pool of {n_workers} workers for image resizing')
251
+ pool = selected_pool(n_workers)
252
+
253
+ p_process_image = partial(_process_single_image_for_resize,
254
+ input_folder=input_folder,
255
+ output_folder=output_folder,
256
+ target_size=target_size,
257
+ correct_size_image_handling=correct_size_image_handling,
258
+ unavailable_image_handling=unavailable_image_handling,
259
+ no_enlarge_width=no_enlarge_width,
260
+ verbose=verbose)
261
+
262
+ processed_results = list(tqdm(pool.imap(p_process_image, image_annotation_tuples),
263
+ total=len(image_annotation_tuples),
264
+ desc=f"Resizing images with {pool_type} pool"))
265
+
266
+ finally:
267
+ if pool is not None:
268
+ pool.close()
269
+ pool.join()
270
+ print('Pool closed and joined for COCO dataset resizing')
271
+
272
+ new_images_list = []
273
+ new_annotations_list = []
274
+ for res_im_data, res_annotations in processed_results:
275
+ if res_im_data is None or res_annotations is None:
276
+ assert res_annotations is None and res_im_data is None
277
+ assert unavailable_image_handling == 'omit'
278
+ continue
279
+ new_images_list.append(res_im_data)
280
+ new_annotations_list.extend(res_annotations)
281
+
282
+ d['images'] = new_images_list
283
+ d['annotations'] = new_annotations_list
284
+
285
+ if output_filename is not None:
286
+ write_json(output_filename,d)
287
+
288
+ return d
289
+
290
+ # ...def resize_coco_dataset(...)
291
+
292
+
293
+ #%% Interactive driver
294
+
295
+ if False:
296
+
297
+ pass
298
+
299
+ #%% Test resizing
300
+
301
+ input_folder = 'i:/data/lila/ena24'
302
+ # input_filename = 'i:/data/lila/ena24.json'
303
+ input_filename = 'i:/data/lila/ena24-mini.json'
304
+
305
+ output_folder = 'i:/data/lila/ena24-resized'
306
+ output_filename = insert_before_extension(input_filename,'resized')
307
+
308
+ target_size = (640,-1)
309
+
310
+ correct_size_image_handling = 'rewrite'
311
+
312
+ _ = resize_coco_dataset(input_folder=input_folder,
313
+ input_filename=input_filename,
314
+ output_folder=output_folder,
315
+ output_filename=output_filename,
316
+ target_size=target_size,
317
+ correct_size_image_handling=correct_size_image_handling,
318
+ unavailable_image_handling='omit',
319
+ n_workers=10,
320
+ pool_type='process')
321
+
322
+
323
+ #%% Preview
324
+
325
+ from megadetector.visualization import visualize_db
326
+ options = visualize_db.DbVizOptions()
327
+ options.parallelize_rendering = True
328
+ options.viz_size = (640, -1)
329
+ options.num_to_visualize = 100
330
+
331
+ preview_folder = 'i:/data/lila/ena24-resized-preview'
332
+ html_file,_ = visualize_db.visualize_db(output_filename,
333
+ preview_folder,
334
+ output_folder,options)
335
+
336
+
337
+ from megadetector.utils import path_utils # noqa
338
+ path_utils.open_file(html_file)
339
+
340
+
341
+ #%% Command-line driver
342
+
343
+ def main():
344
+ """
345
+ Command-line driver for resize_coco_dataset
346
+ """
347
+
348
+ parser = argparse.ArgumentParser(
349
+ description='Resize images in a COCO dataset and scale annotations'
350
+ )
351
+ parser.add_argument(
352
+ 'input_folder',
353
+ type=str,
354
+ help='Path to the folder containing original images'
355
+ )
356
+ parser.add_argument(
357
+ 'input_filename',
358
+ type=str,
359
+ help='Path to the input COCO .json file'
360
+ )
361
+ parser.add_argument(
362
+ 'output_folder',
363
+ type=str,
364
+ help='Path to the folder where resized images will be saved'
365
+ )
366
+ parser.add_argument(
367
+ 'output_filename',
368
+ type=str,
369
+ help='Path to the output COCO .json file for resized data'
370
+ )
371
+ parser.add_argument(
372
+ '--target_size',
373
+ type=str,
374
+ default='-1,-1',
375
+ help='Target size as "width,height". Use -1 to preserve aspect ratio for a dimension. ' + \
376
+ 'E.g., "800,600" or "1024,-1".'
377
+ )
378
+ parser.add_argument(
379
+ '--correct_size_image_handling',
380
+ type=str,
381
+ default='copy',
382
+ choices=['copy', 'rewrite'],
383
+ help='How to handle images already at target size'
384
+ )
385
+ parser.add_argument(
386
+ '--n_workers',
387
+ type=int,
388
+ default=1,
389
+ help='Number of workers for parallel processing. <=1 for sequential'
390
+ )
391
+ parser.add_argument(
392
+ '--pool_type',
393
+ type=str,
394
+ default='thread',
395
+ choices=['thread', 'process'],
396
+ help='Type of multiprocessing pool if n_workers > 1'
397
+ )
398
+
399
+ if len(sys.argv[1:]) == 0:
400
+ parser.print_help()
401
+ parser.exit()
402
+
403
+ args = parser.parse_args()
404
+
405
+ try:
406
+ target_size_parts = args.target_size.split(',')
407
+ if len(target_size_parts) != 2:
408
+ raise ValueError("target_size must have two comma-separated parts (width,height).")
409
+ parsed_target_size = (int(target_size_parts[0]), int(target_size_parts[1]))
410
+ except ValueError as e:
411
+ print(f"Error parsing target_size: {e}")
412
+ parser.print_help()
413
+ parser.exit()
414
+
415
+ resize_coco_dataset(
416
+ args.input_folder,
417
+ args.input_filename,
418
+ args.output_folder,
419
+ args.output_filename,
420
+ target_size=parsed_target_size,
421
+ correct_size_image_handling=args.correct_size_image_handling,
422
+ n_workers=args.n_workers,
423
+ pool_type=args.pool_type
424
+ )
425
+ print("Dataset resizing complete")
426
+
427
+ if __name__ == '__main__':
428
+ main()
429
+
430
+
431
+ #%% Tests
432
+
433
+ class TestResizeCocoDataset:
434
+ """
435
+ Test class for the resize_coco_dataset function.
436
+ """
437
+
438
+ def set_up(self): # noqa
439
+ self.test_dir = make_test_folder(subfolder='resize_coco_tests')
440
+
441
+ self.input_images_dir_seq = os.path.join(self.test_dir, 'input_images_seq')
442
+ os.makedirs(self.input_images_dir_seq, exist_ok=True)
443
+
444
+ self.input_images_dir_par = os.path.join(self.test_dir, 'input_images_par')
445
+ os.makedirs(self.input_images_dir_par, exist_ok=True)
446
+
447
+ self.output_images_dir_seq = os.path.join(self.test_dir, 'output_images_seq')
448
+ os.makedirs(self.output_images_dir_seq, exist_ok=True)
449
+
450
+ self.output_images_dir_par = os.path.join(self.test_dir, 'output_images_par')
451
+ os.makedirs(self.output_images_dir_par, exist_ok=True)
452
+
453
+ def tear_down(self): # noqa
454
+
455
+ # Ensure shutil is imported if not already globally in the file
456
+ # (it is, under '#%% Imports and constants')
457
+ if hasattr(self, 'test_dir') and os.path.exists(self.test_dir):
458
+ shutil.rmtree(self.test_dir)
459
+
460
+ def _create_dummy_image_and_coco_json(self,
461
+ image_dir,
462
+ json_filename_base="input_coco.json",
463
+ num_images=2,
464
+ original_size=(100, 100),
465
+ num_annotations_per_image=2):
466
+ coco_data = {
467
+ "images": [],
468
+ "annotations": [],
469
+ "categories": [{"id": 1, "name": "test_category"}]
470
+ }
471
+
472
+ annotation_id_counter = 1
473
+
474
+ for i in range(num_images):
475
+ image_name = f"image_{i}.png"
476
+ image_path = os.path.join(image_dir, image_name)
477
+
478
+ # Create a dummy image
479
+ try:
480
+ img = Image.new('RGB', original_size, color='red')
481
+ img.save(image_path)
482
+ except Exception as e:
483
+ # In some environments, font loading for default PIL text might fail.
484
+ # For a simple color image, this shouldn't be an issue.
485
+ # If it is, consider a simpler save or pre-creating a tiny PNG.
486
+ print(f"Warning: Could not create dummy image {image_path}: {e}")
487
+ # Fallback: create an empty file, though this will fail later steps
488
+ # open(image_path, 'a').close()
489
+
490
+ image_entry = {
491
+ "id": i + 1,
492
+ "file_name": image_name, # Filename only, not path
493
+ "width": original_size[0],
494
+ "height": original_size[1]
495
+ }
496
+ coco_data["images"].append(image_entry)
497
+
498
+ for j in range(num_annotations_per_image):
499
+ annotation_entry = {
500
+ "id": annotation_id_counter,
501
+ "image_id": image_entry["id"],
502
+ "category_id": 1, # Corresponds to "test_category"
503
+ # Simple, non-overlapping bbox for testing scaling
504
+ "bbox": [10 + j*30, 10 + j*5, 20, 15]
505
+ }
506
+ coco_data["annotations"].append(annotation_entry)
507
+ annotation_id_counter += 1
508
+
509
+ json_file_path = os.path.join(self.test_dir, json_filename_base)
510
+ with open(json_file_path, 'w') as f:
511
+ json.dump(coco_data, f, indent=1)
512
+
513
+ return json_file_path, coco_data
514
+
515
+ def test_resize_sequential_vs_parallel(self):
516
+ """
517
+ Test driver for sequence vs. parallel COCO dataset resizing.
518
+ """
519
+
520
+ self.set_up()
521
+
522
+ try:
523
+ num_images_to_test = 3
524
+ original_w, original_h = 120, 80
525
+ target_w, target_h = 60, 40
526
+ target_size_test = (target_w, target_h)
527
+
528
+ # Sequential run
529
+ input_json_path_seq, _ = self._create_dummy_image_and_coco_json(
530
+ image_dir=self.input_images_dir_seq,
531
+ json_filename_base="input_coco_seq.json",
532
+ num_images=num_images_to_test,
533
+ original_size=(original_w, original_h)
534
+ )
535
+ output_json_path_seq = os.path.join(self.test_dir, 'output_coco_seq.json')
536
+
537
+ print("Test: starting sequential resize (1 worker)...")
538
+ resize_coco_dataset(
539
+ input_folder=self.input_images_dir_seq,
540
+ input_filename=input_json_path_seq,
541
+ output_folder=self.output_images_dir_seq,
542
+ output_filename=output_json_path_seq,
543
+ target_size=target_size_test,
544
+ n_workers=1
545
+ )
546
+ print(f"Test: Sequential resize complete. Output: {output_json_path_seq}")
547
+
548
+ # Parallel run
549
+ # For the parallel run, we use different input/output directories but can reuse the same logic
550
+ # for creating the dummy dataset structure. The image files will be new.
551
+ input_json_path_par, _ = self._create_dummy_image_and_coco_json(
552
+ image_dir=self.input_images_dir_par,
553
+ json_filename_base="input_coco_par.json",
554
+ num_images=num_images_to_test,
555
+ original_size=(original_w, original_h)
556
+ )
557
+ output_json_path_par = os.path.join(self.test_dir, 'output_coco_par.json')
558
+
559
+ print("Test: Starting parallel resize (2 workers, thread pool)...")
560
+ resize_coco_dataset(
561
+ input_folder=self.input_images_dir_par,
562
+ input_filename=input_json_path_par,
563
+ output_folder=self.output_images_dir_par,
564
+ output_filename=output_json_path_par,
565
+ target_size=target_size_test,
566
+ n_workers=2, # Using 2 workers for testing parallelism
567
+ pool_type='thread'
568
+ )
569
+ print(f"Test: Parallel resize complete. Output: {output_json_path_par}")
570
+
571
+ # Load results
572
+ with open(output_json_path_seq, 'r') as f:
573
+ data_seq = json.load(f)
574
+ with open(output_json_path_par, 'r') as f:
575
+ data_par = json.load(f)
576
+
577
+ # Compare COCO JSON data
578
+ # Compare images
579
+ assert len(data_seq['images']) == num_images_to_test
580
+ assert len(data_seq['images']) == len(data_par['images']), "Number of images differs"
581
+
582
+ sorted_images_seq = sorted(data_seq['images'], key=lambda x: x['id'])
583
+ sorted_images_par = sorted(data_par['images'], key=lambda x: x['id'])
584
+
585
+ for img_s, img_p in zip(sorted_images_seq, sorted_images_par, strict=True):
586
+ assert img_s['id'] == img_p['id'], \
587
+ f"Image IDs differ: {img_s['id']} vs {img_p['id']}"
588
+ # Filenames are generated independently, so we only check structure, not exact name matching
589
+ # across seq/par runs' inputs, but output structure should be consistent if input
590
+ # names were e.g. image_0, image_1
591
+ assert img_s['file_name'] == img_p['file_name']
592
+ assert img_s['width'] == target_w, \
593
+ f"Seq image {img_s['id']} width incorrect"
594
+ assert img_s['height'] == target_h, \
595
+ f"Seq image {img_s['id']} height incorrect"
596
+ assert img_p['width'] == target_w, \
597
+ f"Par image {img_p['id']} width incorrect"
598
+ assert img_p['height'] == target_h, \
599
+ f"Par image {img_p['id']} height incorrect"
600
+
601
+ # Compare annotations
602
+ assert len(data_seq['annotations']) == len(data_par['annotations']), \
603
+ "Number of annotations differs"
604
+ # Assuming _create_dummy_image_and_coco_json creates the same number of annotations for each test run
605
+
606
+ sorted_anns_seq = sorted(data_seq['annotations'], key=lambda x: x['id'])
607
+ sorted_anns_par = sorted(data_par['annotations'], key=lambda x: x['id'])
608
+
609
+ for ann_s, ann_p in zip(sorted_anns_seq, sorted_anns_par, strict=True):
610
+ assert ann_s['id'] == ann_p['id'], \
611
+ f"Annotation IDs differ: {ann_s['id']} vs {ann_p['id']}"
612
+ assert ann_s['image_id'] == ann_p['image_id'], \
613
+ f"Annotation image_ids differ for ann_id {ann_s['id']}"
614
+ assert ann_s['category_id'] == ann_p['category_id'], \
615
+ f"Annotation category_ids differ for ann_id {ann_s['id']}"
616
+
617
+ # Check bbox scaling (example: original width 120, target 60 -> scale 0.5)
618
+ # Original bbox: [10, 10, 20, 15] -> Scaled: [5, 5, 10, 7.5] (Floats possible)
619
+ # Need to compare with tolerance or ensure rounding is handled if expecting ints
620
+ # For this test, let's assume direct comparison works due to simple scaling.
621
+ # If PIL's resize causes slight pixel shifts affecting precise sub-pixel bbox calculations,
622
+ # then a tolerance (pytest.approx) would be better.
623
+ # Given the current resize_coco_dataset logic, it's direct multiplication.
624
+ for i in range(4):
625
+ assert abs(ann_s['bbox'][i] - ann_p['bbox'][i]) < 1e-5, \
626
+ f"Bbox element {i} differs for ann_id {ann_s['id']}: {ann_s['bbox']} vs {ann_p['bbox']}"
627
+
628
+ # Compare actual image files
629
+ seq_files = sorted(os.listdir(self.output_images_dir_seq))
630
+ par_files = sorted(os.listdir(self.output_images_dir_par))
631
+
632
+ assert len(seq_files) == num_images_to_test, "Incorrect number of output images (sequential)"
633
+ assert len(seq_files) == len(par_files), "Number of output image files differs"
634
+
635
+ for fname_s, fname_p in zip(seq_files, par_files, strict=True):
636
+ assert fname_s == fname_p, "Output image filenames differ between seq and par runs"
637
+ img_s_path = os.path.join(self.output_images_dir_seq, fname_s)
638
+ img_p_path = os.path.join(self.output_images_dir_par, fname_p)
639
+
640
+ with Image.open(img_s_path) as img_s_pil:
641
+ assert img_s_pil.size == target_size_test, \
642
+ f"Image {fname_s} (seq) has wrong dimensions: {img_s_pil.size}"
643
+ with Image.open(img_p_path) as img_p_pil:
644
+ assert img_p_pil.size == target_size_test, \
645
+ f"Image {fname_p} (par) has wrong dimensions: {img_p_pil.size}"
646
+
647
+ print("Test test_resize_sequential_vs_parallel PASSED")
648
+
649
+ finally:
650
+ self.tear_down()
651
+
652
+ # ...def test_resize_sequential_vs_parallel(...)
653
+
654
+ # ...class TestResizeCocoDataset
655
+
656
+
657
+ def test_resize_coco_dataset_main():
658
+ """
659
+ Driver for the TestResizeCocoDataset() class.
660
+ """
661
+
662
+ print("Starting TestResizeCocoDataset main runner...")
663
+ test_runner = TestResizeCocoDataset()
664
+ test_runner.test_resize_sequential_vs_parallel()
665
+ print("TestResizeCocoDataset main runner finished.")