megadetector 10.0.13__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.

Potentially problematic release.


This version of megadetector might be problematic. Click here for more details.

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 +702 -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 +528 -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 +187 -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 +663 -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 +876 -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 +2159 -0
  79. megadetector/detection/run_inference_with_yolov5_val.py +1314 -0
  80. megadetector/detection/run_md_and_speciesnet.py +1494 -0
  81. megadetector/detection/run_tiled_inference.py +1038 -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 +1752 -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 +2077 -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 +213 -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 +224 -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 +2832 -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 +1759 -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 +1940 -0
  140. megadetector/visualization/visualize_db.py +630 -0
  141. megadetector/visualization/visualize_detector_output.py +479 -0
  142. megadetector/visualization/visualize_video_output.py +705 -0
  143. megadetector-10.0.13.dist-info/METADATA +134 -0
  144. megadetector-10.0.13.dist-info/RECORD +147 -0
  145. megadetector-10.0.13.dist-info/WHEEL +5 -0
  146. megadetector-10.0.13.dist-info/licenses/LICENSE +19 -0
  147. megadetector-10.0.13.dist-info/top_level.txt +1 -0
@@ -0,0 +1,243 @@
1
+ """
2
+
3
+ render_images_with_thumbnails.py
4
+
5
+ Renders an output image with one primary image and crops from many secondary images,
6
+ used primarily to check whether candidate repeat detections are actually false positives or not.
7
+
8
+ """
9
+
10
+ #%% Imports
11
+
12
+ import math
13
+ import os
14
+ import random
15
+
16
+ from PIL import Image
17
+
18
+ from megadetector.visualization import visualization_utils as vis_utils
19
+ from megadetector.utils import path_utils
20
+
21
+
22
+ #%% Support functions
23
+
24
+ def crop_image_with_normalized_coordinates(
25
+ image,
26
+ bounding_box):
27
+ """
28
+ Args:
29
+ image (PIL.Image): image to crop
30
+ bounding_box (tuple): tuple formatted as (x,y,w,h), where (0,0) is the
31
+ upper-left of the image, and coordinates are normalized
32
+ (so (0,0,1,1) is a box containing the entire image).
33
+
34
+ Returns:
35
+ PIL.Image: cropped image
36
+ """
37
+
38
+ im_width, im_height = image.size
39
+ (x_norm, y_norm, w_norm, h_norm) = bounding_box
40
+ (x, y, w, h) = (x_norm * im_width,
41
+ y_norm * im_height,
42
+ w_norm * im_width,
43
+ h_norm * im_height)
44
+ return image.crop((x, y, x+w, y+h))
45
+
46
+
47
+ #%% Main function
48
+
49
+ def render_images_with_thumbnails(
50
+ primary_image_filename,
51
+ primary_image_width,
52
+ secondary_image_filename_list,
53
+ secondary_image_bounding_box_list,
54
+ cropped_grid_width,
55
+ output_image_filename,
56
+ primary_image_location='right'):
57
+ """
58
+ Given a primary image filename and a list of secondary images, writes to
59
+ the provided output_image_filename an image where the one
60
+ side is the primary image, and the other side is a grid of the
61
+ secondary images, cropped according to the provided list of bounding
62
+ boxes.
63
+
64
+ The output file will be primary_image_width + cropped_grid_width pixels
65
+ wide.
66
+
67
+ The height of the output image will be determined by the original aspect
68
+ ratio of the primary image.
69
+
70
+ Args:
71
+ primary_image_filename (str): filename of the primary image to load as str
72
+ primary_image_width (int): width at which to render the primary image; if this is
73
+ None, will render at the original image width
74
+ secondary_image_filename_list (list): list of filenames of the secondary images
75
+ secondary_image_bounding_box_list (list): list of tuples, one per secondary
76
+ image. Each tuple is a bounding box of the secondary image,
77
+ formatted as (x,y,w,h), where (0,0) is the upper-left of the image,
78
+ and coordinates are normalized (so (0,0,1,1) is a box containing
79
+ the entire image.
80
+ cropped_grid_width (int): width of the cropped-image area
81
+ output_image_filename (str): filename to write the output image
82
+ primary_image_location (str, optional): 'right' or left'; reserving 'top', 'bottom', etc.
83
+ for future use
84
+ """
85
+
86
+ # Check to make sure the arguments are reasonable
87
+ assert(len(secondary_image_filename_list) ==
88
+ len(secondary_image_bounding_box_list)), \
89
+ 'Length of secondary image list and bounding box list should be equal'
90
+
91
+ assert primary_image_location in ['left','right']
92
+
93
+ # Load primary image and resize to desired width
94
+ primary_image = vis_utils.load_image(primary_image_filename)
95
+ if primary_image_width is not None:
96
+ primary_image = vis_utils.resize_image(primary_image, primary_image_width,
97
+ target_height=-1)
98
+
99
+ # Compute the number of grid elements for the secondary images
100
+ # to best fit the available aspect ratio
101
+ grid_width = cropped_grid_width
102
+ grid_height = primary_image.size[1]
103
+ grid_aspect = grid_width / grid_height
104
+
105
+ sample_crop_width = secondary_image_bounding_box_list[0][2]
106
+ sample_crop_height = secondary_image_bounding_box_list[0][3]
107
+
108
+ n_crops = len(secondary_image_filename_list)
109
+
110
+ optimal_n_rows = None
111
+ optimal_aspect_error = None
112
+
113
+ for candidate_n_rows in range(1,n_crops+1):
114
+ candidate_n_cols = math.ceil(n_crops / candidate_n_rows)
115
+ candidate_grid_aspect = (candidate_n_cols*sample_crop_width) / \
116
+ (candidate_n_rows*sample_crop_height)
117
+ aspect_error = abs(grid_aspect-candidate_grid_aspect)
118
+ if optimal_n_rows is None or aspect_error < optimal_aspect_error:
119
+ optimal_n_rows = candidate_n_rows
120
+ optimal_aspect_error = aspect_error
121
+
122
+ assert optimal_n_rows is not None
123
+ grid_rows = optimal_n_rows
124
+ grid_columns = math.ceil(n_crops/grid_rows)
125
+
126
+ # Compute the width of each grid cell
127
+ grid_cell_width = math.floor(grid_width / grid_columns)
128
+ grid_cell_height = math.floor(grid_height / grid_rows)
129
+
130
+ # Load secondary images and their associated bounding boxes. Iterate
131
+ # through them, crop them, and save them to a list of cropped_images
132
+ cropped_images = []
133
+ for (name, box) in zip(secondary_image_filename_list,
134
+ secondary_image_bounding_box_list,
135
+ strict=True):
136
+
137
+ other_image = vis_utils.load_image(name)
138
+ cropped_image = crop_image_with_normalized_coordinates(
139
+ other_image, box)
140
+
141
+ # Rescale this crop to fit within the desired grid cell size
142
+ width_scale_factor = grid_cell_width / cropped_image.size[0]
143
+ height_scale_factor = grid_cell_height / cropped_image.size[1]
144
+ scale_factor = min(width_scale_factor,height_scale_factor)
145
+
146
+ # Resize the cropped image, whether we're making it larger or smaller
147
+ cropped_image = cropped_image.resize(
148
+ ((int)(cropped_image.size[0] * scale_factor),
149
+ (int)(cropped_image.size[1] * scale_factor)))
150
+
151
+ cropped_images.append(cropped_image)
152
+
153
+ # ...for each crop
154
+
155
+ # Compute the final output image size. This will depend upon the aspect
156
+ # ratio of the crops.
157
+ output_image_width = primary_image.size[0] + grid_width
158
+ output_image_height = primary_image.size[1]
159
+
160
+ # Create blank output image
161
+ output_image = Image.new('RGB', (output_image_width, output_image_height))
162
+
163
+ # Copy resized primary image to output image
164
+ if primary_image_location == 'right':
165
+ primary_image_x = grid_width
166
+ else:
167
+ primary_image_x = 0
168
+
169
+ output_image.paste(primary_image, (primary_image_x, 0))
170
+
171
+ # Compute the final locations of the secondary images in the output image
172
+ i_row = 0; i_col = 0
173
+ for image in cropped_images:
174
+
175
+ x = i_col * grid_cell_width
176
+ if primary_image_location == 'left':
177
+ x += primary_image.size[0]
178
+ y = i_row * grid_cell_height
179
+ output_image.paste(image, (x,y))
180
+ i_col += 1
181
+ if i_col >= grid_columns:
182
+ i_col = 0
183
+ i_row += 1
184
+
185
+ # ...for each crop
186
+
187
+ # Write output image to disk
188
+ parent_dir = os.path.dirname(output_image_filename)
189
+ if len(parent_dir) > 0:
190
+ os.makedirs(parent_dir,exist_ok=True)
191
+ output_image.save(output_image_filename)
192
+
193
+ # ...def render_images_with_thumbnails(...)
194
+
195
+
196
+ #%% Command-line driver
197
+
198
+ # This is just a test driver, this module is not meant to be run from the command line.
199
+
200
+ def main(): # noqa
201
+
202
+ # Load images from a test directory.
203
+ #
204
+ # Make the first image in the directory the primary image,
205
+ # the remaining ones the comparison images.
206
+ test_input_folder = os.path.expanduser('~/data/KRU-test')
207
+ output_image_filename = os.path.expanduser('~/tmp/thumbnail_test.jpg')
208
+
209
+ files = path_utils.find_images(test_input_folder)
210
+
211
+ random.seed(0); random.shuffle(files)
212
+ primary_image_filename = files[0]
213
+
214
+ secondary_image_filename_list = []
215
+ secondary_image_bounding_box_list = []
216
+
217
+ # Initialize the x,y location of the bounding box
218
+ box = (random.uniform(0.25, 0.75), random.uniform(0.25, 0.75))
219
+
220
+ # Create the list of secondary images and their bounding boxes
221
+ for file in files[1:]:
222
+ secondary_image_filename_list.append(file)
223
+ secondary_image_bounding_box_list.append(
224
+ (box[0] + random.uniform(-0.001, 0.001),
225
+ box[1] + random.uniform(-0.001, 0.001),
226
+ 0.2,
227
+ 0.2))
228
+
229
+ primary_image_width = 1000
230
+ cropped_grid_width = 1000
231
+
232
+ render_images_with_thumbnails(
233
+ primary_image_filename,
234
+ primary_image_width,
235
+ secondary_image_filename_list,
236
+ secondary_image_bounding_box_list,
237
+ cropped_grid_width,
238
+ output_image_filename, 'right')
239
+
240
+ path_utils.open_file(output_image_filename)
241
+
242
+ if __name__ == '__main__':
243
+ main()