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,392 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
merge_detections.py
|
|
4
|
+
|
|
5
|
+
Merge high-confidence detections from one or more results files into another
|
|
6
|
+
file. Typically used to combine results from MDv5b and/or MDv4 into a "primary"
|
|
7
|
+
results file from MDv5a.
|
|
8
|
+
|
|
9
|
+
Detection categories must be the same in both files; if you want to first remap
|
|
10
|
+
one file's category mapping to be the same as another's, see remap_detection_categories.
|
|
11
|
+
|
|
12
|
+
If you want to literally merge two .json files, see combine_batch_outputs.py.
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
#%% Constants and imports
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import sys
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
|
|
23
|
+
from tqdm import tqdm
|
|
24
|
+
|
|
25
|
+
from megadetector.utils.ct_utils import get_iou
|
|
26
|
+
from megadetector.utils.ct_utils import write_json
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
#%% Structs
|
|
30
|
+
|
|
31
|
+
class MergeDetectionsOptions:
|
|
32
|
+
"""
|
|
33
|
+
Class defining options for merge_detections().
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
|
|
38
|
+
#: Maximum detection size to include in the merged output
|
|
39
|
+
self.max_detection_size = 1.01
|
|
40
|
+
|
|
41
|
+
#: Minimum detection size to include in the merged output
|
|
42
|
+
self.min_detection_size = 0
|
|
43
|
+
|
|
44
|
+
#: Exclude detections whose confidence in the source file(s) is less
|
|
45
|
+
#: than this. Should have the same length as the number of source files.
|
|
46
|
+
self.source_confidence_thresholds = [0.05]
|
|
47
|
+
|
|
48
|
+
#: Don't bother merging into target images if there is a similar detection
|
|
49
|
+
#: above this threshold (or if there is *any* detection above this threshold,
|
|
50
|
+
#: and merge_empty_only is True)
|
|
51
|
+
self.target_confidence_threshold = 0.2
|
|
52
|
+
|
|
53
|
+
#: If you want to merge only certain categories, specify one
|
|
54
|
+
#: (but not both) of these. These are category IDs, not names.
|
|
55
|
+
self.categories_to_include = None
|
|
56
|
+
|
|
57
|
+
#: If you want to merge only certain categories, specify one
|
|
58
|
+
#: (but not both) of these. These are category IDs, not names.
|
|
59
|
+
self.categories_to_exclude = None
|
|
60
|
+
|
|
61
|
+
#: Only merge detections into images that have *no* detections in the
|
|
62
|
+
#: target results file.
|
|
63
|
+
self.merge_empty_only = False
|
|
64
|
+
|
|
65
|
+
#: IoU threshold above which two detections are considered the same
|
|
66
|
+
self.iou_threshold = 0.65
|
|
67
|
+
|
|
68
|
+
#: Error if this is False and the output file exists
|
|
69
|
+
self.overwrite = False
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
#%% Main function
|
|
73
|
+
|
|
74
|
+
def merge_detections(source_files,target_file,output_file,options=None):
|
|
75
|
+
"""
|
|
76
|
+
Merge high-confidence detections from one or more results files into another
|
|
77
|
+
file. Typically used to combine results from MDv5b and/or MDv4 into a "primary"
|
|
78
|
+
results file from MDv5a.
|
|
79
|
+
|
|
80
|
+
[source_files] (a list of files or a single filename) specifies the set of
|
|
81
|
+
results files that will be merged into [target_file]. The difference between a
|
|
82
|
+
"source file" and the "target file" is that if no merging is necessary, either because
|
|
83
|
+
two boxes are nearly identical or because merge_only_empty is True and the target
|
|
84
|
+
file already has above-threshold detection for an image+category, the output file gets
|
|
85
|
+
the results of the "target" file. I.e., the "target" file wins all ties.
|
|
86
|
+
|
|
87
|
+
The results are written to [output_file].
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
source_files (list of str): list of files to merge into the results in [target_file]
|
|
91
|
+
target_file (str): filename that is treated as the primary source of results
|
|
92
|
+
output_file (str): file to which we should write merged results
|
|
93
|
+
options (MergeDetectionsOptions, optional): see MergeDetectionsOptions
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
if isinstance(source_files,str):
|
|
97
|
+
source_files = [source_files]
|
|
98
|
+
|
|
99
|
+
if options is None:
|
|
100
|
+
options = MergeDetectionsOptions()
|
|
101
|
+
|
|
102
|
+
if (not options.overwrite) and (os.path.isfile(output_file)):
|
|
103
|
+
print('File {} exists, bypassing merge'.format(output_file))
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
assert not ((options.categories_to_exclude is not None) and \
|
|
107
|
+
(options.categories_to_include is not None)), \
|
|
108
|
+
'categories_to_include and categories_to_exclude are mutually exclusive'
|
|
109
|
+
|
|
110
|
+
if options.categories_to_exclude is not None:
|
|
111
|
+
options.categories_to_exclude = [int(c) for c in options.categories_to_exclude]
|
|
112
|
+
|
|
113
|
+
if options.categories_to_include is not None:
|
|
114
|
+
options.categories_to_include = [int(c) for c in options.categories_to_include]
|
|
115
|
+
|
|
116
|
+
assert len(source_files) == len(options.source_confidence_thresholds), \
|
|
117
|
+
'{} source files provided, but {} source confidence thresholds provided'.format(
|
|
118
|
+
len(source_files),len(options.source_confidence_thresholds))
|
|
119
|
+
|
|
120
|
+
for fn in source_files:
|
|
121
|
+
assert os.path.isfile(fn), 'Could not find source file {}'.format(fn)
|
|
122
|
+
|
|
123
|
+
assert os.path.isfile(target_file)
|
|
124
|
+
|
|
125
|
+
with open(target_file,'r') as f:
|
|
126
|
+
output_data = json.load(f)
|
|
127
|
+
|
|
128
|
+
print('Loaded results for {} images'.format(len(output_data['images'])))
|
|
129
|
+
|
|
130
|
+
fn_to_image = {}
|
|
131
|
+
|
|
132
|
+
# im = output_data['images'][0]
|
|
133
|
+
for im in output_data['images']:
|
|
134
|
+
fn_to_image[im['file']] = im
|
|
135
|
+
|
|
136
|
+
if 'detections_transferred_from' not in output_data['info']:
|
|
137
|
+
output_data['info']['detections_transferred_from'] = []
|
|
138
|
+
|
|
139
|
+
if 'detector' not in output_data['info']:
|
|
140
|
+
output_data['info']['detector'] = 'MDv4 (assumed)'
|
|
141
|
+
|
|
142
|
+
detection_categories_raw = output_data['detection_categories'].keys()
|
|
143
|
+
|
|
144
|
+
# Determine whether we should be processing all categories, or just a subset
|
|
145
|
+
# of categories.
|
|
146
|
+
detection_categories = []
|
|
147
|
+
|
|
148
|
+
if options.categories_to_exclude is not None:
|
|
149
|
+
for c in detection_categories_raw:
|
|
150
|
+
if int(c) not in options.categories_to_exclude:
|
|
151
|
+
detection_categories.append(c)
|
|
152
|
+
else:
|
|
153
|
+
print('Excluding category {}'.format(c))
|
|
154
|
+
elif options.categories_to_include is not None:
|
|
155
|
+
for c in detection_categories_raw:
|
|
156
|
+
if int(c) in options.categories_to_include:
|
|
157
|
+
print('Including category {}'.format(c))
|
|
158
|
+
detection_categories.append(c)
|
|
159
|
+
else:
|
|
160
|
+
detection_categories = detection_categories_raw
|
|
161
|
+
|
|
162
|
+
# i_source_file = 0; source_file = source_files[i_source_file]
|
|
163
|
+
for i_source_file,source_file in enumerate(source_files):
|
|
164
|
+
|
|
165
|
+
print('Processing detections from file {}'.format(source_file))
|
|
166
|
+
|
|
167
|
+
with open(source_file,'r') as f:
|
|
168
|
+
source_data = json.load(f)
|
|
169
|
+
|
|
170
|
+
if 'detector' in source_data['info']:
|
|
171
|
+
source_detector_name = source_data['info']['detector']
|
|
172
|
+
else:
|
|
173
|
+
source_detector_name = os.path.basename(source_file)
|
|
174
|
+
|
|
175
|
+
output_data['info']['detections_transferred_from'].append(os.path.basename(source_file))
|
|
176
|
+
output_data['info']['detector'] = output_data['info']['detector'] + ' + ' + source_detector_name
|
|
177
|
+
|
|
178
|
+
assert source_data['detection_categories'] == output_data['detection_categories'], \
|
|
179
|
+
'Cannot merge files with different detection category maps'
|
|
180
|
+
|
|
181
|
+
source_confidence_threshold = options.source_confidence_thresholds[i_source_file]
|
|
182
|
+
|
|
183
|
+
# source_im = source_data['images'][0]
|
|
184
|
+
for source_im in tqdm(source_data['images']):
|
|
185
|
+
|
|
186
|
+
image_filename = source_im['file']
|
|
187
|
+
|
|
188
|
+
assert image_filename in fn_to_image, \
|
|
189
|
+
'Image {} not in target image set'.format(image_filename)
|
|
190
|
+
target_im = fn_to_image[image_filename]
|
|
191
|
+
|
|
192
|
+
if 'detections' not in source_im or source_im['detections'] is None:
|
|
193
|
+
continue
|
|
194
|
+
|
|
195
|
+
if 'detections' not in target_im or target_im['detections'] is None:
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
source_detections_this_image = source_im['detections']
|
|
199
|
+
target_detections_this_image = target_im['detections']
|
|
200
|
+
|
|
201
|
+
detections_to_transfer = []
|
|
202
|
+
|
|
203
|
+
# detection_category = list(detection_categories)[0]
|
|
204
|
+
for detection_category in detection_categories:
|
|
205
|
+
|
|
206
|
+
target_detections_this_category = \
|
|
207
|
+
[det for det in target_detections_this_image if det['category'] == \
|
|
208
|
+
detection_category]
|
|
209
|
+
|
|
210
|
+
max_target_confidence_this_category = 0.0
|
|
211
|
+
|
|
212
|
+
if len(target_detections_this_category) > 0:
|
|
213
|
+
max_target_confidence_this_category = max([det['conf'] for \
|
|
214
|
+
det in target_detections_this_category])
|
|
215
|
+
|
|
216
|
+
# If we have a valid detection in the target file, and we're only merging
|
|
217
|
+
# into images that have no detections at all, we don't need to review the individual
|
|
218
|
+
# detections in the source file.
|
|
219
|
+
if options.merge_empty_only and \
|
|
220
|
+
(max_target_confidence_this_category >= options.target_confidence_threshold):
|
|
221
|
+
continue
|
|
222
|
+
|
|
223
|
+
source_detections_this_category_raw = [det for det in \
|
|
224
|
+
source_detections_this_image if det['category'] == detection_category]
|
|
225
|
+
|
|
226
|
+
# Boxes are x/y/w/h
|
|
227
|
+
# source_sizes = [det['bbox'][2]*det['bbox'][3] for det in source_detections_this_category_raw]
|
|
228
|
+
|
|
229
|
+
# Only look at source boxes within the size range
|
|
230
|
+
source_detections_this_category_filtered = [
|
|
231
|
+
det for det in source_detections_this_category_raw if \
|
|
232
|
+
(det['bbox'][2]*det['bbox'][3] <= options.max_detection_size) and \
|
|
233
|
+
(det['bbox'][2]*det['bbox'][3] >= options.min_detection_size) \
|
|
234
|
+
]
|
|
235
|
+
|
|
236
|
+
# det = source_detections_this_category_filtered[0]
|
|
237
|
+
for det in source_detections_this_category_filtered:
|
|
238
|
+
|
|
239
|
+
if det['conf'] >= source_confidence_threshold:
|
|
240
|
+
|
|
241
|
+
# Check only whole images
|
|
242
|
+
if options.merge_empty_only:
|
|
243
|
+
|
|
244
|
+
# We verified this above, asserting here for clarity
|
|
245
|
+
assert max_target_confidence_this_category < options.target_confidence_threshold
|
|
246
|
+
det['transferred_from'] = source_detector_name
|
|
247
|
+
detections_to_transfer.append(det)
|
|
248
|
+
|
|
249
|
+
# Check individual detections
|
|
250
|
+
else:
|
|
251
|
+
|
|
252
|
+
# Does this source detection match any existing above-threshold
|
|
253
|
+
# target category detections?
|
|
254
|
+
matches_existing_box = False
|
|
255
|
+
|
|
256
|
+
# target_detection = target_detections_this_category[0]
|
|
257
|
+
for target_detection in target_detections_this_category:
|
|
258
|
+
|
|
259
|
+
if (target_detection['conf'] >= options.target_confidence_threshold) \
|
|
260
|
+
and \
|
|
261
|
+
(get_iou(det['bbox'],target_detection['bbox']) >= options.iou_threshold):
|
|
262
|
+
matches_existing_box = True
|
|
263
|
+
break
|
|
264
|
+
|
|
265
|
+
if (not matches_existing_box):
|
|
266
|
+
det['transferred_from'] = source_detector_name
|
|
267
|
+
detections_to_transfer.append(det)
|
|
268
|
+
|
|
269
|
+
# ...if this source detection is above the confidence threshold
|
|
270
|
+
|
|
271
|
+
# ...for each source detection within category
|
|
272
|
+
|
|
273
|
+
# ...for each detection category
|
|
274
|
+
|
|
275
|
+
if len(detections_to_transfer) > 0:
|
|
276
|
+
|
|
277
|
+
# print('Adding {} detections to image {}'.format(len(detections_to_transfer),image_filename))
|
|
278
|
+
detections = fn_to_image[image_filename]['detections']
|
|
279
|
+
detections.extend(detections_to_transfer)
|
|
280
|
+
|
|
281
|
+
# Update the max_detection_conf field (if present)
|
|
282
|
+
if 'max_detection_conf' in fn_to_image[image_filename]:
|
|
283
|
+
fn_to_image[image_filename]['max_detection_conf'] = \
|
|
284
|
+
max([d['conf'] for d in detections])
|
|
285
|
+
|
|
286
|
+
# ...if we have any detections to transfer
|
|
287
|
+
|
|
288
|
+
# ...for each image
|
|
289
|
+
|
|
290
|
+
# ...for each source file
|
|
291
|
+
|
|
292
|
+
write_json(output_file,output_data)
|
|
293
|
+
|
|
294
|
+
print('Saved merged results to {}'.format(output_file))
|
|
295
|
+
|
|
296
|
+
# ...def merge_detections(...)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
#%% Command-line driver
|
|
300
|
+
|
|
301
|
+
def main():
|
|
302
|
+
"""
|
|
303
|
+
Command-line driver for merge_detections.py
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
default_options = MergeDetectionsOptions()
|
|
307
|
+
|
|
308
|
+
parser = argparse.ArgumentParser(
|
|
309
|
+
description='Merge detections from one or more MegaDetector results files into an existing results file')
|
|
310
|
+
parser.add_argument(
|
|
311
|
+
'source_files',
|
|
312
|
+
nargs='+',
|
|
313
|
+
help='Path to source .json file(s) to merge from')
|
|
314
|
+
parser.add_argument(
|
|
315
|
+
'target_file',
|
|
316
|
+
help='Path to a .json file to merge detections into')
|
|
317
|
+
parser.add_argument(
|
|
318
|
+
'output_file',
|
|
319
|
+
help='Path to output .json results file')
|
|
320
|
+
parser.add_argument(
|
|
321
|
+
'--max_detection_size',
|
|
322
|
+
type=float,
|
|
323
|
+
default=default_options.max_detection_size,
|
|
324
|
+
help='Ignore detections with an area larger than this (as a fraction of ' + \
|
|
325
|
+
'image size) (default {})'.format(
|
|
326
|
+
default_options.max_detection_size))
|
|
327
|
+
parser.add_argument(
|
|
328
|
+
'--min_detection_size',
|
|
329
|
+
default=default_options.min_detection_size,
|
|
330
|
+
type=float,
|
|
331
|
+
help='Ignore detections with an area smaller than this (as a fraction of ' + \
|
|
332
|
+
'image size) (default {})'.format(
|
|
333
|
+
default_options.min_detection_size))
|
|
334
|
+
parser.add_argument(
|
|
335
|
+
'--source_confidence_thresholds',
|
|
336
|
+
nargs='+',
|
|
337
|
+
type=float,
|
|
338
|
+
default=default_options.source_confidence_thresholds,
|
|
339
|
+
help='List of thresholds for each source file (default {}). '.format(
|
|
340
|
+
default_options.source_confidence_thresholds) + \
|
|
341
|
+
'Merge only if the source file\'s detection confidence is higher than its ' + \
|
|
342
|
+
'corresponding threshold. Should be the same length as the number of source files.')
|
|
343
|
+
parser.add_argument(
|
|
344
|
+
'--target_confidence_threshold',
|
|
345
|
+
type=float,
|
|
346
|
+
default=default_options.target_confidence_threshold,
|
|
347
|
+
help="Do not merge if target file detection confidence is already higher " + \
|
|
348
|
+
"than this (default {})".format(default_options.target_confidence_threshold))
|
|
349
|
+
parser.add_argument(
|
|
350
|
+
'--categories_to_include',
|
|
351
|
+
type=int,
|
|
352
|
+
nargs='+',
|
|
353
|
+
default=None,
|
|
354
|
+
help='List of numeric detection category IDs to include')
|
|
355
|
+
parser.add_argument(
|
|
356
|
+
'--categories_to_exclude',
|
|
357
|
+
type=int,
|
|
358
|
+
nargs='+',
|
|
359
|
+
default=None,
|
|
360
|
+
help='List of numeric detection categories to exclude')
|
|
361
|
+
parser.add_argument(
|
|
362
|
+
'--merge_empty_only',
|
|
363
|
+
action='store_true',
|
|
364
|
+
help='Ignore individual detections and only merge images for which the target ' + \
|
|
365
|
+
'file contains no detections')
|
|
366
|
+
parser.add_argument(
|
|
367
|
+
'--iou_threshold',
|
|
368
|
+
type=float,
|
|
369
|
+
default=default_options.iou_threshold,
|
|
370
|
+
help='Sets the minimum IoU for a source detection to be considered the same as ' + \
|
|
371
|
+
'a target detection (default {})'.format(default_options.iou_threshold))
|
|
372
|
+
|
|
373
|
+
if len(sys.argv[1:]) == 0:
|
|
374
|
+
parser.print_help()
|
|
375
|
+
parser.exit()
|
|
376
|
+
|
|
377
|
+
args = parser.parse_args()
|
|
378
|
+
|
|
379
|
+
options = MergeDetectionsOptions()
|
|
380
|
+
options.max_detection_size = args.max_detection_size
|
|
381
|
+
options.min_detection_size = args.min_detection_size
|
|
382
|
+
options.source_confidence_thresholds = args.source_confidence_thresholds
|
|
383
|
+
options.target_confidence_threshold = args.target_confidence_threshold
|
|
384
|
+
options.categories_to_include = args.categories_to_include
|
|
385
|
+
options.categories_to_exclude = args.categories_to_exclude
|
|
386
|
+
options.merge_empty_only = args.merge_empty_only
|
|
387
|
+
options.iou_threshold = args.iou_threshold
|
|
388
|
+
|
|
389
|
+
merge_detections(args.source_files, args.target_file, args.output_file, options)
|
|
390
|
+
|
|
391
|
+
if __name__ == '__main__':
|
|
392
|
+
main()
|