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,795 @@
|
|
|
1
|
+
r"""
|
|
2
|
+
|
|
3
|
+
separate_detections_into_folders.py
|
|
4
|
+
|
|
5
|
+
**Overview**
|
|
6
|
+
|
|
7
|
+
Given a .json file with batch processing results, separate the files in that
|
|
8
|
+
set of results into folders that contain animals/people/vehicles/nothing,
|
|
9
|
+
according to per-class thresholds.
|
|
10
|
+
|
|
11
|
+
Image files are copied, not moved.
|
|
12
|
+
|
|
13
|
+
**Output structure**
|
|
14
|
+
|
|
15
|
+
Preserves relative paths within each of those folders; cannot be used with .json
|
|
16
|
+
files that have absolute paths in them.
|
|
17
|
+
|
|
18
|
+
For example, if your .json file has these images:
|
|
19
|
+
|
|
20
|
+
* a/b/c/1.jpg
|
|
21
|
+
* a/b/d/2.jpg
|
|
22
|
+
* a/b/e/3.jpg
|
|
23
|
+
* a/b/f/4.jpg
|
|
24
|
+
* a/x/y/5.jpg
|
|
25
|
+
|
|
26
|
+
And let's say:
|
|
27
|
+
|
|
28
|
+
* The results say that the first three images are empty/person/vehicle, respectively
|
|
29
|
+
* The fourth image is above threshold for "animal" and "person"
|
|
30
|
+
* The fifth image contains an animal
|
|
31
|
+
|
|
32
|
+
* You specify an output base folder of c:/out
|
|
33
|
+
|
|
34
|
+
You will get the following files:
|
|
35
|
+
|
|
36
|
+
* c:/out/empty/a/b/c/1.jpg
|
|
37
|
+
* c:/out/people/a/b/d/2.jpg
|
|
38
|
+
* c:/out/vehicles/a/b/e/3.jpg
|
|
39
|
+
* c:/out/animal_person/a/b/f/4.jpg
|
|
40
|
+
* c:/out/animals/a/x/y/5.jpg
|
|
41
|
+
|
|
42
|
+
**Rendering bounding boxes**
|
|
43
|
+
|
|
44
|
+
By default, images are just copied to the target output folder. If you specify --render_boxes,
|
|
45
|
+
bounding boxes will be rendered on the output images. Because this is no longer strictly
|
|
46
|
+
a copy operation, this may result in the loss of metadata. More accurately, this *may*
|
|
47
|
+
result in the loss of some EXIF metadata; this *will* result in the loss of IPTC/XMP metadata.
|
|
48
|
+
|
|
49
|
+
Rendering boxes also makes this script a lot slower.
|
|
50
|
+
|
|
51
|
+
**Classification-based separation**
|
|
52
|
+
|
|
53
|
+
If you have a results file with classification data, you can also specify classes to put
|
|
54
|
+
in their own folders, within the "animals" folder, like this:
|
|
55
|
+
|
|
56
|
+
``--classification_thresholds "deer=0.75,cow=0.75"``
|
|
57
|
+
|
|
58
|
+
So, e.g., you might get:
|
|
59
|
+
|
|
60
|
+
c:/out/animals/deer/a/x/y/5.jpg
|
|
61
|
+
|
|
62
|
+
In this scenario, the folders within "animals" will be:
|
|
63
|
+
|
|
64
|
+
deer, cow, multiple, unclassified
|
|
65
|
+
|
|
66
|
+
"multiple" in this case only means "deer and cow"; if an image is classified as containing a
|
|
67
|
+
bird and a bear, that would end up in "unclassified", since the folder separation is based only
|
|
68
|
+
on the categories you provide at the command line.
|
|
69
|
+
|
|
70
|
+
No classification-based separation is done within the animal_person, animal_vehicle, or
|
|
71
|
+
animal_person_vehicle folders.
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
#%% Constants and imports
|
|
76
|
+
|
|
77
|
+
import argparse
|
|
78
|
+
import json
|
|
79
|
+
import os
|
|
80
|
+
import shutil
|
|
81
|
+
import sys
|
|
82
|
+
import itertools
|
|
83
|
+
|
|
84
|
+
from multiprocessing.pool import ThreadPool
|
|
85
|
+
from functools import partial
|
|
86
|
+
from tqdm import tqdm
|
|
87
|
+
|
|
88
|
+
from megadetector.utils.ct_utils import args_to_object, is_float
|
|
89
|
+
from megadetector.utils.path_utils import remove_empty_folders
|
|
90
|
+
from megadetector.detection.run_detector import get_typical_confidence_threshold_from_results
|
|
91
|
+
from megadetector.visualization import visualization_utils as vis_utils
|
|
92
|
+
from megadetector.visualization.visualization_utils import blur_detections
|
|
93
|
+
|
|
94
|
+
friendly_folder_names = {'animal':'animals','person':'people','vehicle':'vehicles'}
|
|
95
|
+
|
|
96
|
+
# Occasionally we have near-zero confidence detections associated with COCO classes that
|
|
97
|
+
# didn't quite get squeezed out of the model in training. As long as they're near zero
|
|
98
|
+
# confidence, we just ignore them.
|
|
99
|
+
invalid_category_epsilon = 0.00001
|
|
100
|
+
|
|
101
|
+
default_line_thickness = 8
|
|
102
|
+
default_box_expansion = 3
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
#%% Options class
|
|
106
|
+
|
|
107
|
+
class SeparateDetectionsIntoFoldersOptions:
|
|
108
|
+
"""
|
|
109
|
+
Options used to parameterize separate_detections_into_folders()
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
def __init__(self,threshold=None):
|
|
113
|
+
|
|
114
|
+
#: Default threshold for categories not specified in category_name_to_threshold
|
|
115
|
+
self.threshold = None
|
|
116
|
+
|
|
117
|
+
#: Dict mapping category names to thresholds; for example, an image with only a detection of class
|
|
118
|
+
#: "animal" whose confidence is greater than or equal to category_name_to_threshold['animal']
|
|
119
|
+
#: will be put in the "animal" folder.
|
|
120
|
+
self.category_name_to_threshold = {
|
|
121
|
+
'animal': self.threshold,
|
|
122
|
+
'person': self.threshold,
|
|
123
|
+
'vehicle': self.threshold
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#: Number of workers to use, set to <= 1 to disable parallelization
|
|
127
|
+
self.n_threads = 1
|
|
128
|
+
|
|
129
|
+
#: By default, this function errors if you try to output to an existing folder
|
|
130
|
+
self.allow_existing_directory = False
|
|
131
|
+
|
|
132
|
+
#: By default, this function errors if any of the images specified in the results file don't
|
|
133
|
+
#: exist in the source folder.
|
|
134
|
+
self.allow_missing_files = False
|
|
135
|
+
|
|
136
|
+
#: Whether to overwrite images that already exist in the target folder; only relevant if
|
|
137
|
+
#: [allow_existing_directory] is True
|
|
138
|
+
self.overwrite = True
|
|
139
|
+
|
|
140
|
+
#: Whether to skip empty images; if this is False, empty images (i.e., images with no detections
|
|
141
|
+
#: above the corresponding threshold) will be copied to an "empty" folder.
|
|
142
|
+
self.skip_empty_images = False
|
|
143
|
+
|
|
144
|
+
#: The MD results .json file to process
|
|
145
|
+
self.results_file = None
|
|
146
|
+
|
|
147
|
+
#: The folder containing source images; filenames in [results_file] should be relative to this
|
|
148
|
+
#: folder.
|
|
149
|
+
self.base_input_folder = None
|
|
150
|
+
|
|
151
|
+
#: The folder to which we should write output images; see the module header comment for information
|
|
152
|
+
#: about how that folder will be structured.
|
|
153
|
+
self.base_output_folder = None
|
|
154
|
+
|
|
155
|
+
#: Should we move rather than copy?
|
|
156
|
+
self.move_images = False
|
|
157
|
+
|
|
158
|
+
#: Should we render boxes on the output images? Makes everything a lot slower.
|
|
159
|
+
self.render_boxes = False
|
|
160
|
+
|
|
161
|
+
#: Line thickness in pixels; only relevant if [render_boxes] is True
|
|
162
|
+
self.line_thickness = default_line_thickness
|
|
163
|
+
|
|
164
|
+
#: Box expansion in pixels; only relevant if [render_boxes] is True
|
|
165
|
+
self.box_expansion = default_box_expansion
|
|
166
|
+
|
|
167
|
+
#: Originally specified as a string that looks like this:
|
|
168
|
+
#:
|
|
169
|
+
#: deer=0.75,cow=0.75
|
|
170
|
+
#:
|
|
171
|
+
#: String, converted internally to a dict mapping name:threshold
|
|
172
|
+
self.classification_thresholds = None
|
|
173
|
+
|
|
174
|
+
## Debug or internal attributes
|
|
175
|
+
|
|
176
|
+
#: Do not set explicitly; populated from data when using classification results
|
|
177
|
+
self.classification_category_id_to_name = None
|
|
178
|
+
|
|
179
|
+
#: Do not set explicitly; populated from data when using classification results
|
|
180
|
+
self.classification_categories = None
|
|
181
|
+
|
|
182
|
+
#: Used to test this script; sets a limit on the number of images to process.
|
|
183
|
+
self.debug_max_images = None
|
|
184
|
+
|
|
185
|
+
#: Do not set explicitly; this gets created based on [results_file]
|
|
186
|
+
#:
|
|
187
|
+
#:Dictionary mapping categories (plus combinations of categories, and 'empty') to output folders
|
|
188
|
+
self.category_name_to_folder = None
|
|
189
|
+
|
|
190
|
+
#: Do not set explicitly; this gets loaded from [results_file]
|
|
191
|
+
self.category_id_to_category_name = None
|
|
192
|
+
|
|
193
|
+
#: List of category names for which we should blur detections, most commonly ['person']
|
|
194
|
+
#:
|
|
195
|
+
#: Can also be a comma-separated list.
|
|
196
|
+
self.category_names_to_blur = None
|
|
197
|
+
|
|
198
|
+
#: Remove all empty folders from the target folder at the end of the process,
|
|
199
|
+
#: whether or not they were created by this script
|
|
200
|
+
self.remove_empty_folders = False
|
|
201
|
+
|
|
202
|
+
# ...__init__()
|
|
203
|
+
|
|
204
|
+
# ...class SeparateDetectionsIntoFoldersOptions
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
#%% Support functions
|
|
208
|
+
|
|
209
|
+
def _path_is_abs(p): return (len(p) > 1) and (p[0] == '/' or p[1] == ':')
|
|
210
|
+
|
|
211
|
+
printed_missing_file_warning = False
|
|
212
|
+
|
|
213
|
+
def _process_detections(im,options):
|
|
214
|
+
"""
|
|
215
|
+
Process all detections for a single image
|
|
216
|
+
|
|
217
|
+
May modify *im*.
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
global printed_missing_file_warning
|
|
221
|
+
|
|
222
|
+
relative_filename = im['file']
|
|
223
|
+
|
|
224
|
+
detections = None
|
|
225
|
+
if 'detections' in im:
|
|
226
|
+
detections = im['detections']
|
|
227
|
+
|
|
228
|
+
categories_above_threshold = None
|
|
229
|
+
|
|
230
|
+
if detections is None:
|
|
231
|
+
|
|
232
|
+
assert im['failure'] is not None and len(im['failure']) > 0
|
|
233
|
+
target_folder = options.category_name_to_folder['failure']
|
|
234
|
+
|
|
235
|
+
else:
|
|
236
|
+
|
|
237
|
+
category_name_to_max_confidence = {}
|
|
238
|
+
category_names = options.category_id_to_category_name.values()
|
|
239
|
+
for category_name in category_names:
|
|
240
|
+
category_name_to_max_confidence[category_name] = 0.0
|
|
241
|
+
|
|
242
|
+
# Find the maximum confidence for each category
|
|
243
|
+
#
|
|
244
|
+
# det = detections[0]
|
|
245
|
+
for det in detections:
|
|
246
|
+
|
|
247
|
+
category_id = det['category']
|
|
248
|
+
|
|
249
|
+
# For zero-confidence detections, we occasionally have leftover goop
|
|
250
|
+
# from COCO classes
|
|
251
|
+
if category_id not in options.category_id_to_category_name:
|
|
252
|
+
print('Warning: unrecognized category {} in file {}'.format(
|
|
253
|
+
category_id,relative_filename))
|
|
254
|
+
# assert det['conf'] < invalid_category_epsilon
|
|
255
|
+
continue
|
|
256
|
+
|
|
257
|
+
category_name = options.category_id_to_category_name[category_id]
|
|
258
|
+
if det['conf'] > category_name_to_max_confidence[category_name]:
|
|
259
|
+
category_name_to_max_confidence[category_name] = det['conf']
|
|
260
|
+
|
|
261
|
+
# ...for each detection on this image
|
|
262
|
+
|
|
263
|
+
# Count the number of thresholds exceeded
|
|
264
|
+
categories_above_threshold = []
|
|
265
|
+
for category_name in category_names:
|
|
266
|
+
|
|
267
|
+
threshold = options.category_name_to_threshold[category_name]
|
|
268
|
+
assert threshold is not None
|
|
269
|
+
|
|
270
|
+
max_confidence_this_category = category_name_to_max_confidence[category_name]
|
|
271
|
+
if max_confidence_this_category >= threshold:
|
|
272
|
+
categories_above_threshold.append(category_name)
|
|
273
|
+
|
|
274
|
+
# ...for each category
|
|
275
|
+
|
|
276
|
+
categories_above_threshold.sort()
|
|
277
|
+
|
|
278
|
+
using_classification_folders = (options.classification_thresholds is not None and \
|
|
279
|
+
len(options.classification_thresholds) > 0)
|
|
280
|
+
|
|
281
|
+
# If this is above multiple thresholds
|
|
282
|
+
if len(categories_above_threshold) > 1:
|
|
283
|
+
|
|
284
|
+
# Currently "animal_person" images get put into the "animal_person" folder, even if we're
|
|
285
|
+
# doing species-based separation. Ideally, we would optionally put these in either the "deer"
|
|
286
|
+
# folder or a "deer_person" folder, but this is pretty esoteric, so not worrying about this
|
|
287
|
+
# for now.
|
|
288
|
+
target_folder = options.category_name_to_folder['_'.join(categories_above_threshold)]
|
|
289
|
+
|
|
290
|
+
elif len(categories_above_threshold) == 0:
|
|
291
|
+
|
|
292
|
+
target_folder = options.category_name_to_folder['empty']
|
|
293
|
+
|
|
294
|
+
else:
|
|
295
|
+
|
|
296
|
+
assert len(categories_above_threshold) == 1
|
|
297
|
+
|
|
298
|
+
target_folder = options.category_name_to_folder[categories_above_threshold[0]]
|
|
299
|
+
|
|
300
|
+
# Are we making species classification folders, and is this an animal?
|
|
301
|
+
if ('animal' in categories_above_threshold) and (using_classification_folders):
|
|
302
|
+
|
|
303
|
+
# Do we need to put this into a specific species folder?
|
|
304
|
+
|
|
305
|
+
# Find the animal-class detections that are above threshold
|
|
306
|
+
category_name_to_id = {v: k for k, v in options.category_id_to_category_name.items()}
|
|
307
|
+
animal_category_id = category_name_to_id['animal']
|
|
308
|
+
valid_animal_detections = [d for d in detections if \
|
|
309
|
+
(d['category'] == animal_category_id and \
|
|
310
|
+
d['conf'] >= options.category_name_to_threshold['animal'])]
|
|
311
|
+
|
|
312
|
+
# Count the number of classification categories that are above threshold for at
|
|
313
|
+
# least one detection
|
|
314
|
+
classification_categories_above_threshold = set()
|
|
315
|
+
|
|
316
|
+
# d = valid_animal_detections[0]
|
|
317
|
+
for d in valid_animal_detections:
|
|
318
|
+
|
|
319
|
+
if 'classifications' not in d or d['classifications'] is None:
|
|
320
|
+
continue
|
|
321
|
+
|
|
322
|
+
# classification = d['classifications'][0]
|
|
323
|
+
for classification in d['classifications']:
|
|
324
|
+
|
|
325
|
+
classification_category_id = classification[0]
|
|
326
|
+
classification_confidence = classification[1]
|
|
327
|
+
|
|
328
|
+
# Do we have a threshold for this category, and if so, is
|
|
329
|
+
# this classification above threshold?
|
|
330
|
+
assert options.classification_category_id_to_name is not None
|
|
331
|
+
classification_category_name = \
|
|
332
|
+
options.classification_category_id_to_name[classification_category_id]
|
|
333
|
+
if (classification_category_name in options.classification_thresholds) and \
|
|
334
|
+
(classification_confidence > \
|
|
335
|
+
options.classification_thresholds[classification_category_name]):
|
|
336
|
+
classification_categories_above_threshold.add(classification_category_name)
|
|
337
|
+
|
|
338
|
+
# ...for each classification
|
|
339
|
+
|
|
340
|
+
# ...for each detection
|
|
341
|
+
|
|
342
|
+
if len(classification_categories_above_threshold) == 0:
|
|
343
|
+
classification_folder_name = 'unclassified'
|
|
344
|
+
|
|
345
|
+
elif len(classification_categories_above_threshold) > 1:
|
|
346
|
+
classification_folder_name = 'multiple'
|
|
347
|
+
|
|
348
|
+
else:
|
|
349
|
+
assert len(classification_categories_above_threshold) == 1
|
|
350
|
+
classification_folder_name = list(classification_categories_above_threshold)[0]
|
|
351
|
+
|
|
352
|
+
target_folder = os.path.join(target_folder,classification_folder_name)
|
|
353
|
+
|
|
354
|
+
# ...if we have to deal with classification subfolders
|
|
355
|
+
|
|
356
|
+
# ...if we have 0/1/more categories above threshold
|
|
357
|
+
|
|
358
|
+
# ...if this is/isn't a failure case
|
|
359
|
+
|
|
360
|
+
source_path = os.path.join(options.base_input_folder,relative_filename)
|
|
361
|
+
if not os.path.isfile(source_path):
|
|
362
|
+
if not options.allow_missing_files:
|
|
363
|
+
raise ValueError('Cannot find file {}'.format(source_path))
|
|
364
|
+
else:
|
|
365
|
+
if not printed_missing_file_warning:
|
|
366
|
+
print('Warning: cannot find at least one file ({})'.format(source_path))
|
|
367
|
+
printed_missing_file_warning = True
|
|
368
|
+
return
|
|
369
|
+
|
|
370
|
+
target_path = os.path.join(target_folder,relative_filename)
|
|
371
|
+
if (not options.overwrite) and (os.path.isfile(target_path)):
|
|
372
|
+
return
|
|
373
|
+
|
|
374
|
+
target_dir = os.path.dirname(target_path)
|
|
375
|
+
os.makedirs(target_dir,exist_ok=True)
|
|
376
|
+
|
|
377
|
+
# Skip this image if it's empty and we're not processing empty images
|
|
378
|
+
if ((categories_above_threshold is None) or (len(categories_above_threshold) == 0)) and \
|
|
379
|
+
options.skip_empty_images:
|
|
380
|
+
return
|
|
381
|
+
|
|
382
|
+
# At this point, this image is getting copied; we may or may not also need to
|
|
383
|
+
# draw bounding boxes or blur pixels.
|
|
384
|
+
|
|
385
|
+
# Do a simple copy operation if we don't need to manipulate the images (render boxes, blur pixels)
|
|
386
|
+
if (not options.render_boxes and (options.category_names_to_blur is None)) or \
|
|
387
|
+
(categories_above_threshold is None) or \
|
|
388
|
+
(len(categories_above_threshold) == 0):
|
|
389
|
+
|
|
390
|
+
if options.move_images:
|
|
391
|
+
shutil.move(source_path,target_path)
|
|
392
|
+
else:
|
|
393
|
+
shutil.copyfile(source_path,target_path)
|
|
394
|
+
|
|
395
|
+
else:
|
|
396
|
+
|
|
397
|
+
# Open the source image
|
|
398
|
+
pil_image = vis_utils.load_image(source_path)
|
|
399
|
+
|
|
400
|
+
# Blur regions in the image if necessary
|
|
401
|
+
category_names_to_blur = options.category_names_to_blur
|
|
402
|
+
|
|
403
|
+
if category_names_to_blur is not None:
|
|
404
|
+
|
|
405
|
+
if isinstance(category_names_to_blur,str):
|
|
406
|
+
category_names_to_blur = category_names_to_blur.split(',')
|
|
407
|
+
category_names_to_blur = [s.strip() for s in category_names_to_blur]
|
|
408
|
+
|
|
409
|
+
detections_to_blur = []
|
|
410
|
+
for d in detections:
|
|
411
|
+
category_name = options.category_id_to_category_name[d['category']]
|
|
412
|
+
category_threshold = options.category_name_to_threshold[category_name]
|
|
413
|
+
if (d['conf'] >= category_threshold) and (category_name in category_names_to_blur):
|
|
414
|
+
detections_to_blur.append(d)
|
|
415
|
+
if len(detections_to_blur) > 0:
|
|
416
|
+
blur_detections(pil_image,detections_to_blur)
|
|
417
|
+
|
|
418
|
+
# Render bounding boxes for each category separately, because
|
|
419
|
+
# we allow different thresholds for each category.
|
|
420
|
+
|
|
421
|
+
category_name_to_id = {v: k for k, v in options.category_id_to_category_name.items()}
|
|
422
|
+
assert len(category_name_to_id) == len(options.category_id_to_category_name)
|
|
423
|
+
|
|
424
|
+
classification_label_map = None
|
|
425
|
+
if using_classification_folders:
|
|
426
|
+
classification_label_map = options.classification_categories
|
|
427
|
+
|
|
428
|
+
for category_name in categories_above_threshold:
|
|
429
|
+
|
|
430
|
+
category_id = category_name_to_id[category_name]
|
|
431
|
+
category_threshold = options.category_name_to_threshold[category_name]
|
|
432
|
+
assert category_threshold is not None
|
|
433
|
+
category_detections = [d for d in detections if d['category'] == category_id]
|
|
434
|
+
|
|
435
|
+
# When we're not using classification folders, remove classification
|
|
436
|
+
# information to maintain standard detection colors.
|
|
437
|
+
if not using_classification_folders:
|
|
438
|
+
for d in category_detections:
|
|
439
|
+
if 'classifications' in d:
|
|
440
|
+
del d['classifications']
|
|
441
|
+
|
|
442
|
+
vis_utils.render_detection_bounding_boxes(
|
|
443
|
+
category_detections,
|
|
444
|
+
pil_image,
|
|
445
|
+
label_map=options.detection_categories,
|
|
446
|
+
classification_label_map=classification_label_map,
|
|
447
|
+
confidence_threshold=category_threshold,
|
|
448
|
+
thickness=options.line_thickness,
|
|
449
|
+
expansion=options.box_expansion)
|
|
450
|
+
|
|
451
|
+
# ...for each category
|
|
452
|
+
|
|
453
|
+
# Try to preserve EXIF data and image quality when saving
|
|
454
|
+
vis_utils.exif_preserving_save(pil_image,target_path)
|
|
455
|
+
|
|
456
|
+
# ...if we don't/do need to render boxes
|
|
457
|
+
|
|
458
|
+
# ...def _process_detections()
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
#%% Main function
|
|
462
|
+
|
|
463
|
+
def separate_detections_into_folders(options):
|
|
464
|
+
"""
|
|
465
|
+
Given a .json file with batch processing results, separate the files in that
|
|
466
|
+
set of results into folders that contain animals/people/vehicles/nothing,
|
|
467
|
+
according to per-class thresholds. See the header comment of this module for
|
|
468
|
+
more details about the output folder structure.
|
|
469
|
+
|
|
470
|
+
Args:
|
|
471
|
+
options (SeparateDetectionsIntoFoldersOptions): parameters guiding image
|
|
472
|
+
separation, see the SeparateDetectionsIntoFoldersOptions documentation for specific
|
|
473
|
+
options.
|
|
474
|
+
"""
|
|
475
|
+
|
|
476
|
+
# Input validation
|
|
477
|
+
|
|
478
|
+
# Currently we don't support moving (instead of copying) when we're also rendering
|
|
479
|
+
# bounding boxes or blurring humans.
|
|
480
|
+
assert not (options.render_boxes and options.move_images), \
|
|
481
|
+
'Cannot specify both render_boxes and move_images'
|
|
482
|
+
assert not ((options.category_names_to_blur is not None) and options.move_images), \
|
|
483
|
+
'Cannot specify both category_names_to_blur and move_images'
|
|
484
|
+
|
|
485
|
+
# Create output folder if necessary
|
|
486
|
+
if (os.path.isdir(options.base_output_folder)) and \
|
|
487
|
+
(len(os.listdir(options.base_output_folder) ) > 0):
|
|
488
|
+
if options.allow_existing_directory:
|
|
489
|
+
print('Warning: target folder exists and is not empty... did ' + \
|
|
490
|
+
'you mean to delete an old version?')
|
|
491
|
+
else:
|
|
492
|
+
raise ValueError('Target folder exists and is not empty')
|
|
493
|
+
os.makedirs(options.base_output_folder,exist_ok=True)
|
|
494
|
+
|
|
495
|
+
# Load detection results
|
|
496
|
+
print('Loading detection results')
|
|
497
|
+
with open(options.results_file,'r') as f:
|
|
498
|
+
results = json.load(f)
|
|
499
|
+
images = results['images']
|
|
500
|
+
|
|
501
|
+
for im in images:
|
|
502
|
+
fn = im['file']
|
|
503
|
+
assert not _path_is_abs(fn), 'Cannot process results with absolute image paths'
|
|
504
|
+
|
|
505
|
+
print('Processing detections for {} images'.format(len(images)))
|
|
506
|
+
|
|
507
|
+
default_threshold = options.threshold
|
|
508
|
+
|
|
509
|
+
if default_threshold is None:
|
|
510
|
+
default_threshold = get_typical_confidence_threshold_from_results(results)
|
|
511
|
+
|
|
512
|
+
detection_categories = results['detection_categories']
|
|
513
|
+
options.detection_categories = detection_categories
|
|
514
|
+
options.category_id_to_category_name = detection_categories
|
|
515
|
+
|
|
516
|
+
# Map class names to output folders
|
|
517
|
+
options.category_name_to_folder = {}
|
|
518
|
+
options.category_name_to_folder['empty'] = os.path.join(options.base_output_folder,'empty')
|
|
519
|
+
options.category_name_to_folder['failure'] =\
|
|
520
|
+
os.path.join(options.base_output_folder,'processing_failure')
|
|
521
|
+
|
|
522
|
+
# Create all combinations of categories
|
|
523
|
+
category_names = list(detection_categories.values())
|
|
524
|
+
category_names.sort()
|
|
525
|
+
|
|
526
|
+
# category_name = category_names[0]
|
|
527
|
+
for category_name in category_names:
|
|
528
|
+
|
|
529
|
+
# Do we have a custom threshold for this category?
|
|
530
|
+
if category_name not in options.category_name_to_threshold:
|
|
531
|
+
print('Warning: category {} in detection file, but not in threshold mapping'.format(
|
|
532
|
+
category_name))
|
|
533
|
+
options.category_name_to_threshold[category_name] = None
|
|
534
|
+
|
|
535
|
+
if options.category_name_to_threshold[category_name] is None:
|
|
536
|
+
options.category_name_to_threshold[category_name] = default_threshold
|
|
537
|
+
|
|
538
|
+
category_threshold = options.category_name_to_threshold[category_name]
|
|
539
|
+
print('Processing category {} at threshold {}'.format(category_name,category_threshold))
|
|
540
|
+
|
|
541
|
+
target_category_names = []
|
|
542
|
+
for c in category_names:
|
|
543
|
+
|
|
544
|
+
target_category_names.append(c)
|
|
545
|
+
|
|
546
|
+
for combination_length in range(2,len(category_names)+1):
|
|
547
|
+
|
|
548
|
+
combined_category_names = list(itertools.combinations(category_names,combination_length))
|
|
549
|
+
|
|
550
|
+
for combination in combined_category_names:
|
|
551
|
+
combined_name = '_'.join(combination)
|
|
552
|
+
target_category_names.append(combined_name)
|
|
553
|
+
|
|
554
|
+
# Create folder mappings for each category
|
|
555
|
+
for category_name in target_category_names:
|
|
556
|
+
|
|
557
|
+
folder_name = category_name
|
|
558
|
+
|
|
559
|
+
if category_name in friendly_folder_names:
|
|
560
|
+
folder_name = friendly_folder_names[category_name]
|
|
561
|
+
|
|
562
|
+
options.category_name_to_folder[category_name] = \
|
|
563
|
+
os.path.join(options.base_output_folder,folder_name)
|
|
564
|
+
|
|
565
|
+
# Create the actual folders
|
|
566
|
+
for folder in options.category_name_to_folder.values():
|
|
567
|
+
os.makedirs(folder,exist_ok=True)
|
|
568
|
+
|
|
569
|
+
# Handle species classification thresholds, if specified
|
|
570
|
+
if options.classification_thresholds is not None:
|
|
571
|
+
|
|
572
|
+
assert 'classification_categories' in results and \
|
|
573
|
+
results['classification_categories'] is not None, \
|
|
574
|
+
'Classification thresholds specified, but no classification results available'
|
|
575
|
+
|
|
576
|
+
classification_categories = results['classification_categories']
|
|
577
|
+
classification_category_name_to_id = {v: k for k, v in classification_categories.items()}
|
|
578
|
+
classification_category_id_to_name = {k: v for k, v in classification_categories.items()}
|
|
579
|
+
options.classification_category_id_to_name = classification_category_id_to_name
|
|
580
|
+
options.classification_categories = classification_categories
|
|
581
|
+
|
|
582
|
+
if isinstance(options.classification_thresholds,str):
|
|
583
|
+
|
|
584
|
+
# E.g. deer=0.75,cow=0.75
|
|
585
|
+
tokens = options.classification_thresholds.split(',')
|
|
586
|
+
classification_thresholds = {}
|
|
587
|
+
|
|
588
|
+
# token = tokens[0]
|
|
589
|
+
for token in tokens:
|
|
590
|
+
subtokens = token.split('=')
|
|
591
|
+
assert (len(subtokens) == 2) and (is_float(subtokens[1])), \
|
|
592
|
+
'Illegal classification threshold {}'.format(token)
|
|
593
|
+
classification_thresholds[subtokens[0]] = float(subtokens[1])
|
|
594
|
+
|
|
595
|
+
# ...for each token
|
|
596
|
+
|
|
597
|
+
options.classification_thresholds = classification_thresholds
|
|
598
|
+
|
|
599
|
+
# ...if classification thresholds are still in string format
|
|
600
|
+
|
|
601
|
+
# Validate the classes in the threshold list
|
|
602
|
+
for class_name in options.classification_thresholds.keys():
|
|
603
|
+
assert class_name in classification_category_name_to_id, \
|
|
604
|
+
'Category {} specified at the command line, but is not available in the results file'.format(
|
|
605
|
+
class_name)
|
|
606
|
+
|
|
607
|
+
# ...if we need to deal with classification categories
|
|
608
|
+
|
|
609
|
+
if options.n_threads <= 1 or options.debug_max_images is not None:
|
|
610
|
+
|
|
611
|
+
# i_image = 14; im = images[i_image]; im
|
|
612
|
+
for i_image,im in enumerate(tqdm(images)):
|
|
613
|
+
if options.debug_max_images is not None and i_image > options.debug_max_images:
|
|
614
|
+
break
|
|
615
|
+
_process_detections(im,options)
|
|
616
|
+
# ...for each image
|
|
617
|
+
|
|
618
|
+
else:
|
|
619
|
+
|
|
620
|
+
print('Starting a pool with {} threads'.format(options.n_threads))
|
|
621
|
+
pool = ThreadPool(options.n_threads)
|
|
622
|
+
try:
|
|
623
|
+
process_detections_with_options = partial(_process_detections, options=options)
|
|
624
|
+
_ = list(tqdm(pool.imap(process_detections_with_options, images), total=len(images)))
|
|
625
|
+
finally:
|
|
626
|
+
pool.close()
|
|
627
|
+
pool.join()
|
|
628
|
+
print('Pool closed and joined for folder separation')
|
|
629
|
+
|
|
630
|
+
if options.remove_empty_folders:
|
|
631
|
+
print('Removing empty folders from {}'.format(options.base_output_folder))
|
|
632
|
+
remove_empty_folders(options.base_output_folder)
|
|
633
|
+
|
|
634
|
+
# ...def separate_detections_into_folders
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
#%% Interactive driver
|
|
638
|
+
|
|
639
|
+
if False:
|
|
640
|
+
|
|
641
|
+
pass
|
|
642
|
+
|
|
643
|
+
#%%
|
|
644
|
+
|
|
645
|
+
options = SeparateDetectionsIntoFoldersOptions()
|
|
646
|
+
|
|
647
|
+
options.results_file = os.path.expanduser(
|
|
648
|
+
'~/data/snapshot-safari-2022-08-16-KRU-v5a.0.0_detections.json')
|
|
649
|
+
options.base_input_folder = os.path.expanduser('~/data/KRU/KRU_public')
|
|
650
|
+
options.base_output_folder = os.path.expanduser('~/data/KRU-separated')
|
|
651
|
+
options.n_threads = 100
|
|
652
|
+
options.render_boxes = True
|
|
653
|
+
options.allow_existing_directory = True
|
|
654
|
+
|
|
655
|
+
#%%
|
|
656
|
+
|
|
657
|
+
options = SeparateDetectionsIntoFoldersOptions()
|
|
658
|
+
|
|
659
|
+
options.results_file = os.path.expanduser('~/data/ena24-2022-06-15-v5a.0.0_megaclassifier.json')
|
|
660
|
+
options.base_input_folder = os.path.expanduser('~/data/ENA24/images')
|
|
661
|
+
options.base_output_folder = os.path.expanduser('~/data/ENA24-separated')
|
|
662
|
+
options.n_threads = 100
|
|
663
|
+
options.classification_thresholds = 'deer=0.75,cow=0.75,bird=0.75'
|
|
664
|
+
options.render_boxes = True
|
|
665
|
+
options.allow_existing_directory = True
|
|
666
|
+
|
|
667
|
+
#%%
|
|
668
|
+
|
|
669
|
+
separate_detections_into_folders(options)
|
|
670
|
+
|
|
671
|
+
#%% Testing various command-line invocations
|
|
672
|
+
|
|
673
|
+
"""
|
|
674
|
+
# With boxes, no classification
|
|
675
|
+
python separate_detections_into_folders.py \
|
|
676
|
+
~/data/ena24-2022-06-15-v5a.0.0_megaclassifier.json \
|
|
677
|
+
~/data/ENA24/images ~/data/ENA24-separated \
|
|
678
|
+
--threshold 0.17 --animal_threshold 0.2 --n_threads 10 \
|
|
679
|
+
--allow_existing_directory --render_boxes --line_thickness 10 --box_expansion 10
|
|
680
|
+
|
|
681
|
+
# No boxes, no classification (default)
|
|
682
|
+
python separate_detections_into_folders.py \
|
|
683
|
+
~/data/ena24-2022-06-15-v5a.0.0_megaclassifier.json \
|
|
684
|
+
~/data/ENA24/images ~/data/ENA24-separated \
|
|
685
|
+
--threshold 0.17 --animal_threshold 0.2 --n_threads 10 --allow_existing_directory
|
|
686
|
+
|
|
687
|
+
# With boxes, with classification
|
|
688
|
+
python separate_detections_into_folders.py \
|
|
689
|
+
~/data/ena24-2022-06-15-v5a.0.0_megaclassifier.json ~/data/ENA24/images ~/data/ENA24-separated \
|
|
690
|
+
--threshold 0.17 --animal_threshold 0.2 --n_threads 10 --allow_existing_directory \
|
|
691
|
+
--render_boxes --line_thickness 10 --box_expansion 10 \
|
|
692
|
+
--classification_thresholds "deer=0.75,cow=0.75,bird=0.75"
|
|
693
|
+
|
|
694
|
+
# No boxes, with classification
|
|
695
|
+
python separate_detections_into_folders.py \
|
|
696
|
+
~/data/ena24-2022-06-15-v5a.0.0_megaclassifier.json ~/data/ENA24/images ~/data/ENA24-separated \
|
|
697
|
+
--threshold 0.17 --animal_threshold 0.2 --n_threads 10 --allow_existing_directory \
|
|
698
|
+
--classification_thresholds "deer=0.75,cow=0.75,bird=0.75"
|
|
699
|
+
"""
|
|
700
|
+
|
|
701
|
+
#%% Command-line driver
|
|
702
|
+
|
|
703
|
+
def main(): # noqa
|
|
704
|
+
|
|
705
|
+
parser = argparse.ArgumentParser()
|
|
706
|
+
parser.add_argument('results_file', type=str, help='Input .json filename')
|
|
707
|
+
parser.add_argument('base_input_folder', type=str, help='Input image folder')
|
|
708
|
+
parser.add_argument('base_output_folder', type=str, help='Output image folder')
|
|
709
|
+
|
|
710
|
+
parser.add_argument('--threshold', type=float, default=None,
|
|
711
|
+
help='Default confidence threshold for all categories (defaults to ' + \
|
|
712
|
+
'selection based on model version, other options may override this ' + \
|
|
713
|
+
'for specific categories)')
|
|
714
|
+
parser.add_argument('--animal_threshold', type=float, default=None,
|
|
715
|
+
help='Confidence threshold for the animal category')
|
|
716
|
+
parser.add_argument('--human_threshold', type=float, default=None,
|
|
717
|
+
help='Confidence threshold for the human category')
|
|
718
|
+
parser.add_argument('--vehicle_threshold', type=float, default=None,
|
|
719
|
+
help='Confidence threshold for vehicle category')
|
|
720
|
+
parser.add_argument('--classification_thresholds', type=str, default=None,
|
|
721
|
+
help='List of classification thresholds to use for species-based folder ' + \
|
|
722
|
+
'separation, formatted as, e.g., "deer=0.75,cow=0.75"')
|
|
723
|
+
|
|
724
|
+
parser.add_argument('--n_threads', type=int, default=1,
|
|
725
|
+
help='Number of threads to use for parallel operation (default=1)')
|
|
726
|
+
|
|
727
|
+
parser.add_argument('--allow_existing_directory', action='store_true',
|
|
728
|
+
help='Proceed even if the target directory exists and is not empty')
|
|
729
|
+
parser.add_argument('--no_overwrite', action='store_true',
|
|
730
|
+
help='Skip images that already exist in the target folder, must also ' + \
|
|
731
|
+
'specify --allow_existing_directory')
|
|
732
|
+
parser.add_argument('--skip_empty_images', action='store_true',
|
|
733
|
+
help='Do not copy empty images to the output folder')
|
|
734
|
+
parser.add_argument('--move_images', action='store_true',
|
|
735
|
+
help='Move images (rather than copying) (not recommended this if you have not ' + \
|
|
736
|
+
'backed up your data!)')
|
|
737
|
+
|
|
738
|
+
parser.add_argument('--render_boxes', action='store_true',
|
|
739
|
+
help='Render bounding boxes on output images; may result in some ' + \
|
|
740
|
+
'metadata not being transferred')
|
|
741
|
+
parser.add_argument('--line_thickness', type=int, default=default_line_thickness,
|
|
742
|
+
help='Line thickness (in pixels) for rendering, only meaningful if ' + \
|
|
743
|
+
'using render_boxes (defaults to {})'.format(
|
|
744
|
+
default_line_thickness))
|
|
745
|
+
parser.add_argument('--box_expansion', type=int, default=default_box_expansion,
|
|
746
|
+
help='Box expansion (in pixels) for rendering, only meaningful if ' + \
|
|
747
|
+
'using render_boxes (defaults to {})'.format(
|
|
748
|
+
default_box_expansion))
|
|
749
|
+
parser.add_argument('--category_names_to_blur', type=str, default=None,
|
|
750
|
+
help='Comma-separated list of category names to blur ' + \
|
|
751
|
+
'(or a single category name, e.g. "person")')
|
|
752
|
+
parser.add_argument('--remove_empty_folders', action='store_true',
|
|
753
|
+
help='Remove all empty folders from the target folder at the end of the process, ' + \
|
|
754
|
+
'whether or not they were created by this script')
|
|
755
|
+
|
|
756
|
+
if len(sys.argv[1:])==0:
|
|
757
|
+
parser.print_help()
|
|
758
|
+
parser.exit()
|
|
759
|
+
|
|
760
|
+
args = parser.parse_args()
|
|
761
|
+
|
|
762
|
+
# Convert to an options object
|
|
763
|
+
options = SeparateDetectionsIntoFoldersOptions()
|
|
764
|
+
|
|
765
|
+
args_to_object(args, options)
|
|
766
|
+
|
|
767
|
+
def validate_threshold(v,name):
|
|
768
|
+
# print('{} {}'.format(v,name))
|
|
769
|
+
if v is not None:
|
|
770
|
+
assert v >= 0.0 and v <= 1.0, \
|
|
771
|
+
'Illegal {} threshold {}'.format(name,v)
|
|
772
|
+
|
|
773
|
+
validate_threshold(args.threshold,'default')
|
|
774
|
+
validate_threshold(args.animal_threshold,'animal')
|
|
775
|
+
validate_threshold(args.vehicle_threshold,'vehicle')
|
|
776
|
+
validate_threshold(args.human_threshold,'human')
|
|
777
|
+
|
|
778
|
+
if args.threshold is not None:
|
|
779
|
+
if args.animal_threshold is not None \
|
|
780
|
+
and args.human_threshold is not None \
|
|
781
|
+
and args.vehicle_threshold is not None:
|
|
782
|
+
raise ValueError('Default threshold specified, but all category thresholds ' + \
|
|
783
|
+
'also specified... not exactly wrong, but it\'s likely that you ' + \
|
|
784
|
+
'meant something else.')
|
|
785
|
+
|
|
786
|
+
options.category_name_to_threshold['animal'] = args.animal_threshold
|
|
787
|
+
options.category_name_to_threshold['person'] = args.human_threshold
|
|
788
|
+
options.category_name_to_threshold['vehicle'] = args.vehicle_threshold
|
|
789
|
+
|
|
790
|
+
options.overwrite = (not args.no_overwrite)
|
|
791
|
+
|
|
792
|
+
separate_detections_into_folders(options)
|
|
793
|
+
|
|
794
|
+
if __name__ == '__main__':
|
|
795
|
+
main()
|