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.
- megadetector/__init__.py +0 -0
- megadetector/api/__init__.py +0 -0
- megadetector/api/batch_processing/integration/digiKam/setup.py +6 -0
- megadetector/api/batch_processing/integration/digiKam/xmp_integration.py +465 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/config_template.py +5 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/push_annotations_to_emammal.py +125 -0
- megadetector/api/batch_processing/integration/eMammal/test_scripts/select_images_for_testing.py +55 -0
- megadetector/classification/__init__.py +0 -0
- megadetector/classification/aggregate_classifier_probs.py +108 -0
- megadetector/classification/analyze_failed_images.py +227 -0
- megadetector/classification/cache_batchapi_outputs.py +198 -0
- megadetector/classification/create_classification_dataset.py +626 -0
- megadetector/classification/crop_detections.py +516 -0
- megadetector/classification/csv_to_json.py +226 -0
- megadetector/classification/detect_and_crop.py +853 -0
- megadetector/classification/efficientnet/__init__.py +9 -0
- megadetector/classification/efficientnet/model.py +415 -0
- megadetector/classification/efficientnet/utils.py +608 -0
- megadetector/classification/evaluate_model.py +520 -0
- megadetector/classification/identify_mislabeled_candidates.py +152 -0
- megadetector/classification/json_to_azcopy_list.py +63 -0
- megadetector/classification/json_validator.py +696 -0
- megadetector/classification/map_classification_categories.py +276 -0
- megadetector/classification/merge_classification_detection_output.py +509 -0
- megadetector/classification/prepare_classification_script.py +194 -0
- megadetector/classification/prepare_classification_script_mc.py +228 -0
- megadetector/classification/run_classifier.py +287 -0
- megadetector/classification/save_mislabeled.py +110 -0
- megadetector/classification/train_classifier.py +827 -0
- megadetector/classification/train_classifier_tf.py +725 -0
- megadetector/classification/train_utils.py +323 -0
- megadetector/data_management/__init__.py +0 -0
- megadetector/data_management/animl_to_md.py +161 -0
- megadetector/data_management/annotations/__init__.py +0 -0
- megadetector/data_management/annotations/annotation_constants.py +33 -0
- megadetector/data_management/camtrap_dp_to_coco.py +270 -0
- megadetector/data_management/cct_json_utils.py +566 -0
- megadetector/data_management/cct_to_md.py +184 -0
- megadetector/data_management/cct_to_wi.py +293 -0
- megadetector/data_management/coco_to_labelme.py +284 -0
- megadetector/data_management/coco_to_yolo.py +702 -0
- megadetector/data_management/databases/__init__.py +0 -0
- megadetector/data_management/databases/add_width_and_height_to_db.py +107 -0
- megadetector/data_management/databases/combine_coco_camera_traps_files.py +210 -0
- megadetector/data_management/databases/integrity_check_json_db.py +528 -0
- megadetector/data_management/databases/subset_json_db.py +195 -0
- megadetector/data_management/generate_crops_from_cct.py +200 -0
- megadetector/data_management/get_image_sizes.py +164 -0
- megadetector/data_management/labelme_to_coco.py +559 -0
- megadetector/data_management/labelme_to_yolo.py +349 -0
- megadetector/data_management/lila/__init__.py +0 -0
- megadetector/data_management/lila/create_lila_blank_set.py +556 -0
- megadetector/data_management/lila/create_lila_test_set.py +187 -0
- megadetector/data_management/lila/create_links_to_md_results_files.py +106 -0
- megadetector/data_management/lila/download_lila_subset.py +182 -0
- megadetector/data_management/lila/generate_lila_per_image_labels.py +777 -0
- megadetector/data_management/lila/get_lila_annotation_counts.py +174 -0
- megadetector/data_management/lila/get_lila_image_counts.py +112 -0
- megadetector/data_management/lila/lila_common.py +319 -0
- megadetector/data_management/lila/test_lila_metadata_urls.py +164 -0
- megadetector/data_management/mewc_to_md.py +344 -0
- megadetector/data_management/ocr_tools.py +873 -0
- megadetector/data_management/read_exif.py +964 -0
- megadetector/data_management/remap_coco_categories.py +195 -0
- megadetector/data_management/remove_exif.py +156 -0
- megadetector/data_management/rename_images.py +194 -0
- megadetector/data_management/resize_coco_dataset.py +663 -0
- megadetector/data_management/speciesnet_to_md.py +41 -0
- megadetector/data_management/wi_download_csv_to_coco.py +247 -0
- megadetector/data_management/yolo_output_to_md_output.py +594 -0
- megadetector/data_management/yolo_to_coco.py +876 -0
- megadetector/data_management/zamba_to_md.py +188 -0
- megadetector/detection/__init__.py +0 -0
- megadetector/detection/change_detection.py +840 -0
- megadetector/detection/process_video.py +479 -0
- megadetector/detection/pytorch_detector.py +1451 -0
- megadetector/detection/run_detector.py +1267 -0
- megadetector/detection/run_detector_batch.py +2159 -0
- megadetector/detection/run_inference_with_yolov5_val.py +1314 -0
- megadetector/detection/run_md_and_speciesnet.py +1494 -0
- megadetector/detection/run_tiled_inference.py +1038 -0
- megadetector/detection/tf_detector.py +209 -0
- megadetector/detection/video_utils.py +1379 -0
- megadetector/postprocessing/__init__.py +0 -0
- megadetector/postprocessing/add_max_conf.py +72 -0
- megadetector/postprocessing/categorize_detections_by_size.py +166 -0
- megadetector/postprocessing/classification_postprocessing.py +1752 -0
- megadetector/postprocessing/combine_batch_outputs.py +249 -0
- megadetector/postprocessing/compare_batch_results.py +2110 -0
- megadetector/postprocessing/convert_output_format.py +403 -0
- megadetector/postprocessing/create_crop_folder.py +629 -0
- megadetector/postprocessing/detector_calibration.py +570 -0
- megadetector/postprocessing/generate_csv_report.py +522 -0
- megadetector/postprocessing/load_api_results.py +223 -0
- megadetector/postprocessing/md_to_coco.py +428 -0
- megadetector/postprocessing/md_to_labelme.py +351 -0
- megadetector/postprocessing/md_to_wi.py +41 -0
- megadetector/postprocessing/merge_detections.py +392 -0
- megadetector/postprocessing/postprocess_batch_results.py +2077 -0
- megadetector/postprocessing/remap_detection_categories.py +226 -0
- megadetector/postprocessing/render_detection_confusion_matrix.py +677 -0
- megadetector/postprocessing/repeat_detection_elimination/find_repeat_detections.py +206 -0
- megadetector/postprocessing/repeat_detection_elimination/remove_repeat_detections.py +82 -0
- megadetector/postprocessing/repeat_detection_elimination/repeat_detections_core.py +1665 -0
- megadetector/postprocessing/separate_detections_into_folders.py +795 -0
- megadetector/postprocessing/subset_json_detector_output.py +964 -0
- megadetector/postprocessing/top_folders_to_bottom.py +238 -0
- megadetector/postprocessing/validate_batch_results.py +332 -0
- megadetector/taxonomy_mapping/__init__.py +0 -0
- megadetector/taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py +491 -0
- megadetector/taxonomy_mapping/map_new_lila_datasets.py +213 -0
- megadetector/taxonomy_mapping/prepare_lila_taxonomy_release.py +165 -0
- megadetector/taxonomy_mapping/preview_lila_taxonomy.py +543 -0
- megadetector/taxonomy_mapping/retrieve_sample_image.py +71 -0
- megadetector/taxonomy_mapping/simple_image_download.py +224 -0
- megadetector/taxonomy_mapping/species_lookup.py +1008 -0
- megadetector/taxonomy_mapping/taxonomy_csv_checker.py +159 -0
- megadetector/taxonomy_mapping/taxonomy_graph.py +346 -0
- megadetector/taxonomy_mapping/validate_lila_category_mappings.py +83 -0
- megadetector/tests/__init__.py +0 -0
- megadetector/tests/test_nms_synthetic.py +335 -0
- megadetector/utils/__init__.py +0 -0
- megadetector/utils/ct_utils.py +1857 -0
- megadetector/utils/directory_listing.py +199 -0
- megadetector/utils/extract_frames_from_video.py +307 -0
- megadetector/utils/gpu_test.py +125 -0
- megadetector/utils/md_tests.py +2072 -0
- megadetector/utils/path_utils.py +2832 -0
- megadetector/utils/process_utils.py +172 -0
- megadetector/utils/split_locations_into_train_val.py +237 -0
- megadetector/utils/string_utils.py +234 -0
- megadetector/utils/url_utils.py +825 -0
- megadetector/utils/wi_platform_utils.py +968 -0
- megadetector/utils/wi_taxonomy_utils.py +1759 -0
- megadetector/utils/write_html_image_list.py +239 -0
- megadetector/visualization/__init__.py +0 -0
- megadetector/visualization/plot_utils.py +309 -0
- megadetector/visualization/render_images_with_thumbnails.py +243 -0
- megadetector/visualization/visualization_utils.py +1940 -0
- megadetector/visualization/visualize_db.py +630 -0
- megadetector/visualization/visualize_detector_output.py +479 -0
- megadetector/visualization/visualize_video_output.py +705 -0
- megadetector-10.0.13.dist-info/METADATA +134 -0
- megadetector-10.0.13.dist-info/RECORD +147 -0
- megadetector-10.0.13.dist-info/WHEEL +5 -0
- megadetector-10.0.13.dist-info/licenses/LICENSE +19 -0
- 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()
|