megadetector 5.0.9__py3-none-any.whl → 5.0.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

Files changed (226) hide show
  1. {megadetector-5.0.9.dist-info → megadetector-5.0.11.dist-info}/LICENSE +0 -0
  2. {megadetector-5.0.9.dist-info → megadetector-5.0.11.dist-info}/METADATA +12 -11
  3. megadetector-5.0.11.dist-info/RECORD +5 -0
  4. megadetector-5.0.11.dist-info/top_level.txt +1 -0
  5. api/__init__.py +0 -0
  6. api/batch_processing/__init__.py +0 -0
  7. api/batch_processing/api_core/__init__.py +0 -0
  8. api/batch_processing/api_core/batch_service/__init__.py +0 -0
  9. api/batch_processing/api_core/batch_service/score.py +0 -439
  10. api/batch_processing/api_core/server.py +0 -294
  11. api/batch_processing/api_core/server_api_config.py +0 -98
  12. api/batch_processing/api_core/server_app_config.py +0 -55
  13. api/batch_processing/api_core/server_batch_job_manager.py +0 -220
  14. api/batch_processing/api_core/server_job_status_table.py +0 -152
  15. api/batch_processing/api_core/server_orchestration.py +0 -360
  16. api/batch_processing/api_core/server_utils.py +0 -92
  17. api/batch_processing/api_core_support/__init__.py +0 -0
  18. api/batch_processing/api_core_support/aggregate_results_manually.py +0 -46
  19. api/batch_processing/api_support/__init__.py +0 -0
  20. api/batch_processing/api_support/summarize_daily_activity.py +0 -152
  21. api/batch_processing/data_preparation/__init__.py +0 -0
  22. api/batch_processing/data_preparation/manage_local_batch.py +0 -2391
  23. api/batch_processing/data_preparation/manage_video_batch.py +0 -327
  24. api/batch_processing/integration/digiKam/setup.py +0 -6
  25. api/batch_processing/integration/digiKam/xmp_integration.py +0 -465
  26. api/batch_processing/integration/eMammal/test_scripts/config_template.py +0 -5
  27. api/batch_processing/integration/eMammal/test_scripts/push_annotations_to_emammal.py +0 -126
  28. api/batch_processing/integration/eMammal/test_scripts/select_images_for_testing.py +0 -55
  29. api/batch_processing/postprocessing/__init__.py +0 -0
  30. api/batch_processing/postprocessing/add_max_conf.py +0 -64
  31. api/batch_processing/postprocessing/categorize_detections_by_size.py +0 -163
  32. api/batch_processing/postprocessing/combine_api_outputs.py +0 -249
  33. api/batch_processing/postprocessing/compare_batch_results.py +0 -958
  34. api/batch_processing/postprocessing/convert_output_format.py +0 -397
  35. api/batch_processing/postprocessing/load_api_results.py +0 -195
  36. api/batch_processing/postprocessing/md_to_coco.py +0 -310
  37. api/batch_processing/postprocessing/md_to_labelme.py +0 -330
  38. api/batch_processing/postprocessing/merge_detections.py +0 -401
  39. api/batch_processing/postprocessing/postprocess_batch_results.py +0 -1904
  40. api/batch_processing/postprocessing/remap_detection_categories.py +0 -170
  41. api/batch_processing/postprocessing/render_detection_confusion_matrix.py +0 -661
  42. api/batch_processing/postprocessing/repeat_detection_elimination/find_repeat_detections.py +0 -211
  43. api/batch_processing/postprocessing/repeat_detection_elimination/remove_repeat_detections.py +0 -82
  44. api/batch_processing/postprocessing/repeat_detection_elimination/repeat_detections_core.py +0 -1631
  45. api/batch_processing/postprocessing/separate_detections_into_folders.py +0 -731
  46. api/batch_processing/postprocessing/subset_json_detector_output.py +0 -696
  47. api/batch_processing/postprocessing/top_folders_to_bottom.py +0 -223
  48. api/synchronous/__init__.py +0 -0
  49. api/synchronous/api_core/animal_detection_api/__init__.py +0 -0
  50. api/synchronous/api_core/animal_detection_api/api_backend.py +0 -152
  51. api/synchronous/api_core/animal_detection_api/api_frontend.py +0 -266
  52. api/synchronous/api_core/animal_detection_api/config.py +0 -35
  53. api/synchronous/api_core/animal_detection_api/data_management/annotations/annotation_constants.py +0 -47
  54. api/synchronous/api_core/animal_detection_api/detection/detector_training/copy_checkpoints.py +0 -43
  55. api/synchronous/api_core/animal_detection_api/detection/detector_training/model_main_tf2.py +0 -114
  56. api/synchronous/api_core/animal_detection_api/detection/process_video.py +0 -543
  57. api/synchronous/api_core/animal_detection_api/detection/pytorch_detector.py +0 -304
  58. api/synchronous/api_core/animal_detection_api/detection/run_detector.py +0 -627
  59. api/synchronous/api_core/animal_detection_api/detection/run_detector_batch.py +0 -1029
  60. api/synchronous/api_core/animal_detection_api/detection/run_inference_with_yolov5_val.py +0 -581
  61. api/synchronous/api_core/animal_detection_api/detection/run_tiled_inference.py +0 -754
  62. api/synchronous/api_core/animal_detection_api/detection/tf_detector.py +0 -165
  63. api/synchronous/api_core/animal_detection_api/detection/video_utils.py +0 -495
  64. api/synchronous/api_core/animal_detection_api/md_utils/azure_utils.py +0 -174
  65. api/synchronous/api_core/animal_detection_api/md_utils/ct_utils.py +0 -262
  66. api/synchronous/api_core/animal_detection_api/md_utils/directory_listing.py +0 -251
  67. api/synchronous/api_core/animal_detection_api/md_utils/matlab_porting_tools.py +0 -97
  68. api/synchronous/api_core/animal_detection_api/md_utils/path_utils.py +0 -416
  69. api/synchronous/api_core/animal_detection_api/md_utils/process_utils.py +0 -110
  70. api/synchronous/api_core/animal_detection_api/md_utils/sas_blob_utils.py +0 -509
  71. api/synchronous/api_core/animal_detection_api/md_utils/string_utils.py +0 -59
  72. api/synchronous/api_core/animal_detection_api/md_utils/url_utils.py +0 -144
  73. api/synchronous/api_core/animal_detection_api/md_utils/write_html_image_list.py +0 -226
  74. api/synchronous/api_core/animal_detection_api/md_visualization/visualization_utils.py +0 -841
  75. api/synchronous/api_core/tests/__init__.py +0 -0
  76. api/synchronous/api_core/tests/load_test.py +0 -110
  77. classification/__init__.py +0 -0
  78. classification/aggregate_classifier_probs.py +0 -108
  79. classification/analyze_failed_images.py +0 -227
  80. classification/cache_batchapi_outputs.py +0 -198
  81. classification/create_classification_dataset.py +0 -627
  82. classification/crop_detections.py +0 -516
  83. classification/csv_to_json.py +0 -226
  84. classification/detect_and_crop.py +0 -855
  85. classification/efficientnet/__init__.py +0 -9
  86. classification/efficientnet/model.py +0 -415
  87. classification/efficientnet/utils.py +0 -610
  88. classification/evaluate_model.py +0 -520
  89. classification/identify_mislabeled_candidates.py +0 -152
  90. classification/json_to_azcopy_list.py +0 -63
  91. classification/json_validator.py +0 -695
  92. classification/map_classification_categories.py +0 -276
  93. classification/merge_classification_detection_output.py +0 -506
  94. classification/prepare_classification_script.py +0 -194
  95. classification/prepare_classification_script_mc.py +0 -228
  96. classification/run_classifier.py +0 -286
  97. classification/save_mislabeled.py +0 -110
  98. classification/train_classifier.py +0 -825
  99. classification/train_classifier_tf.py +0 -724
  100. classification/train_utils.py +0 -322
  101. data_management/__init__.py +0 -0
  102. data_management/annotations/__init__.py +0 -0
  103. data_management/annotations/annotation_constants.py +0 -34
  104. data_management/camtrap_dp_to_coco.py +0 -238
  105. data_management/cct_json_utils.py +0 -395
  106. data_management/cct_to_md.py +0 -176
  107. data_management/cct_to_wi.py +0 -289
  108. data_management/coco_to_labelme.py +0 -272
  109. data_management/coco_to_yolo.py +0 -662
  110. data_management/databases/__init__.py +0 -0
  111. data_management/databases/add_width_and_height_to_db.py +0 -33
  112. data_management/databases/combine_coco_camera_traps_files.py +0 -206
  113. data_management/databases/integrity_check_json_db.py +0 -477
  114. data_management/databases/subset_json_db.py +0 -115
  115. data_management/generate_crops_from_cct.py +0 -149
  116. data_management/get_image_sizes.py +0 -188
  117. data_management/importers/add_nacti_sizes.py +0 -52
  118. data_management/importers/add_timestamps_to_icct.py +0 -79
  119. data_management/importers/animl_results_to_md_results.py +0 -158
  120. data_management/importers/auckland_doc_test_to_json.py +0 -372
  121. data_management/importers/auckland_doc_to_json.py +0 -200
  122. data_management/importers/awc_to_json.py +0 -189
  123. data_management/importers/bellevue_to_json.py +0 -273
  124. data_management/importers/cacophony-thermal-importer.py +0 -796
  125. data_management/importers/carrizo_shrubfree_2018.py +0 -268
  126. data_management/importers/carrizo_trail_cam_2017.py +0 -287
  127. data_management/importers/cct_field_adjustments.py +0 -57
  128. data_management/importers/channel_islands_to_cct.py +0 -913
  129. data_management/importers/eMammal/copy_and_unzip_emammal.py +0 -180
  130. data_management/importers/eMammal/eMammal_helpers.py +0 -249
  131. data_management/importers/eMammal/make_eMammal_json.py +0 -223
  132. data_management/importers/ena24_to_json.py +0 -275
  133. data_management/importers/filenames_to_json.py +0 -385
  134. data_management/importers/helena_to_cct.py +0 -282
  135. data_management/importers/idaho-camera-traps.py +0 -1407
  136. data_management/importers/idfg_iwildcam_lila_prep.py +0 -294
  137. data_management/importers/jb_csv_to_json.py +0 -150
  138. data_management/importers/mcgill_to_json.py +0 -250
  139. data_management/importers/missouri_to_json.py +0 -489
  140. data_management/importers/nacti_fieldname_adjustments.py +0 -79
  141. data_management/importers/noaa_seals_2019.py +0 -181
  142. data_management/importers/pc_to_json.py +0 -365
  143. data_management/importers/plot_wni_giraffes.py +0 -123
  144. data_management/importers/prepare-noaa-fish-data-for-lila.py +0 -359
  145. data_management/importers/prepare_zsl_imerit.py +0 -131
  146. data_management/importers/rspb_to_json.py +0 -356
  147. data_management/importers/save_the_elephants_survey_A.py +0 -320
  148. data_management/importers/save_the_elephants_survey_B.py +0 -332
  149. data_management/importers/snapshot_safari_importer.py +0 -758
  150. data_management/importers/snapshot_safari_importer_reprise.py +0 -665
  151. data_management/importers/snapshot_serengeti_lila.py +0 -1067
  152. data_management/importers/snapshotserengeti/make_full_SS_json.py +0 -150
  153. data_management/importers/snapshotserengeti/make_per_season_SS_json.py +0 -153
  154. data_management/importers/sulross_get_exif.py +0 -65
  155. data_management/importers/timelapse_csv_set_to_json.py +0 -490
  156. data_management/importers/ubc_to_json.py +0 -399
  157. data_management/importers/umn_to_json.py +0 -507
  158. data_management/importers/wellington_to_json.py +0 -263
  159. data_management/importers/wi_to_json.py +0 -441
  160. data_management/importers/zamba_results_to_md_results.py +0 -181
  161. data_management/labelme_to_coco.py +0 -548
  162. data_management/labelme_to_yolo.py +0 -272
  163. data_management/lila/__init__.py +0 -0
  164. data_management/lila/add_locations_to_island_camera_traps.py +0 -97
  165. data_management/lila/add_locations_to_nacti.py +0 -147
  166. data_management/lila/create_lila_blank_set.py +0 -557
  167. data_management/lila/create_lila_test_set.py +0 -151
  168. data_management/lila/create_links_to_md_results_files.py +0 -106
  169. data_management/lila/download_lila_subset.py +0 -177
  170. data_management/lila/generate_lila_per_image_labels.py +0 -515
  171. data_management/lila/get_lila_annotation_counts.py +0 -170
  172. data_management/lila/get_lila_image_counts.py +0 -111
  173. data_management/lila/lila_common.py +0 -300
  174. data_management/lila/test_lila_metadata_urls.py +0 -132
  175. data_management/ocr_tools.py +0 -874
  176. data_management/read_exif.py +0 -681
  177. data_management/remap_coco_categories.py +0 -84
  178. data_management/remove_exif.py +0 -66
  179. data_management/resize_coco_dataset.py +0 -189
  180. data_management/wi_download_csv_to_coco.py +0 -246
  181. data_management/yolo_output_to_md_output.py +0 -441
  182. data_management/yolo_to_coco.py +0 -676
  183. detection/__init__.py +0 -0
  184. detection/detector_training/__init__.py +0 -0
  185. detection/detector_training/model_main_tf2.py +0 -114
  186. detection/process_video.py +0 -703
  187. detection/pytorch_detector.py +0 -337
  188. detection/run_detector.py +0 -779
  189. detection/run_detector_batch.py +0 -1219
  190. detection/run_inference_with_yolov5_val.py +0 -917
  191. detection/run_tiled_inference.py +0 -935
  192. detection/tf_detector.py +0 -188
  193. detection/video_utils.py +0 -606
  194. docs/source/conf.py +0 -43
  195. md_utils/__init__.py +0 -0
  196. md_utils/azure_utils.py +0 -174
  197. md_utils/ct_utils.py +0 -612
  198. md_utils/directory_listing.py +0 -246
  199. md_utils/md_tests.py +0 -968
  200. md_utils/path_utils.py +0 -1044
  201. md_utils/process_utils.py +0 -157
  202. md_utils/sas_blob_utils.py +0 -509
  203. md_utils/split_locations_into_train_val.py +0 -228
  204. md_utils/string_utils.py +0 -92
  205. md_utils/url_utils.py +0 -323
  206. md_utils/write_html_image_list.py +0 -225
  207. md_visualization/__init__.py +0 -0
  208. md_visualization/plot_utils.py +0 -293
  209. md_visualization/render_images_with_thumbnails.py +0 -275
  210. md_visualization/visualization_utils.py +0 -1537
  211. md_visualization/visualize_db.py +0 -551
  212. md_visualization/visualize_detector_output.py +0 -406
  213. megadetector-5.0.9.dist-info/RECORD +0 -224
  214. megadetector-5.0.9.dist-info/top_level.txt +0 -8
  215. taxonomy_mapping/__init__.py +0 -0
  216. taxonomy_mapping/map_lila_taxonomy_to_wi_taxonomy.py +0 -491
  217. taxonomy_mapping/map_new_lila_datasets.py +0 -154
  218. taxonomy_mapping/prepare_lila_taxonomy_release.py +0 -142
  219. taxonomy_mapping/preview_lila_taxonomy.py +0 -591
  220. taxonomy_mapping/retrieve_sample_image.py +0 -71
  221. taxonomy_mapping/simple_image_download.py +0 -218
  222. taxonomy_mapping/species_lookup.py +0 -834
  223. taxonomy_mapping/taxonomy_csv_checker.py +0 -159
  224. taxonomy_mapping/taxonomy_graph.py +0 -346
  225. taxonomy_mapping/validate_lila_category_mappings.py +0 -83
  226. {megadetector-5.0.9.dist-info → megadetector-5.0.11.dist-info}/WHEEL +0 -0
@@ -1,275 +0,0 @@
1
- """
2
-
3
- ena24_to_json_2017.py
4
-
5
- Convert the ENA24 data set to a COCO-camera-traps .json file
6
-
7
- """
8
-
9
- #%% Constants and environment
10
-
11
- import os
12
- import json
13
- import uuid
14
- import time
15
- import humanfriendly
16
- import numpy as np
17
- from PIL import Image
18
- from tqdm import tqdm
19
- import shutil
20
- import zipfile
21
-
22
- base_directory = r'e:\wildlife_data\ena24'
23
- output_file = os.path.join(base_directory,'ena24.json')
24
- image_directory = os.path.join(base_directory,'images')
25
- label_directory = os.path.join(base_directory,'labels')
26
-
27
- assert(os.path.isdir(label_directory))
28
- assert(os.path.isdir(image_directory))
29
-
30
- # Temporary folders for human and non-human images
31
- human_dir = os.path.join(base_directory, 'human')
32
- non_human_dir = os.path.join(base_directory, 'non-human')
33
-
34
- human_zipfile = os.path.join(base_directory, 'ena24_humans.zip')
35
- non_human_zipfile = os.path.join(base_directory, 'ena24.zip')
36
-
37
- # Clean existing output folders/zipfiles
38
- if os.path.isdir(human_dir):
39
- shutil.rmtree(human_dir)
40
- if os.path.isdir(non_human_dir):
41
- shutil.rmtree(non_human_dir)
42
-
43
- if os.path.isfile(human_zipfile):
44
- os.remove(human_zipfile)
45
- if os.path.isfile(human_zipfile):
46
- os.remove(non_human_zipfile)
47
-
48
- os.makedirs(human_dir,exist_ok=True)
49
- os.makedirs(non_human_dir,exist_ok=True)
50
-
51
- labels = ['White_Tailed_Deer', 'Dog', 'Bobcat', 'Red Fox', 'Horse',
52
- 'Domestic Cat', 'American Black Bear', 'Eastern Cottontail', 'Grey Fox', 'Coyote',
53
- 'Eastern Fox Squirrel', 'Eastern Gray Squirrel', 'Vehicle', 'Eastern Chipmunk', 'Wild Turkey',
54
- 'Northern Raccoon', 'Striped Skunk', 'Woodchuck', 'Virginia Opossum', 'Human',
55
- 'Bird', 'American Crow', 'Chicken']
56
-
57
-
58
- #%% Support functions
59
-
60
- def zipdir(path, zipfilename, basepath=None):
61
- """
62
- Zip everything in [path] into [zipfilename], with paths in the zipfile relative to [basepath]
63
- """
64
- ziph = zipfile.ZipFile(zipfilename, 'w', zipfile.ZIP_STORED)
65
-
66
- for root, dirs, files in os.walk(path):
67
- for file in files:
68
- src = os.path.join(root, file)
69
- if basepath is None:
70
- dst = file
71
- else:
72
- dst = os.path.relpath(src,basepath)
73
- ziph.write(src, dst, zipfile.ZIP_STORED)
74
-
75
- ziph.close()
76
-
77
-
78
- #%% Read source data
79
-
80
- image_list = os.listdir(label_directory)
81
- print('Enumerated {} label files'.format(len(image_list)))
82
-
83
-
84
- #%% Map filenames to rows, verify image existence
85
-
86
- startTime = time.time()
87
-
88
- # Build up a map from filenames to a list of rows, checking image existence as we go
89
- for filename in image_list:
90
- imagePath = os.path.join(image_directory, "{}.jpg".format(filename.split(".")[0]))
91
- assert(os.path.isfile(imagePath))
92
-
93
- elapsed = time.time() - startTime
94
- print('Finished verifying image existence for {} files in {}'.format(
95
- len(image_list),humanfriendly.format_timespan(elapsed)))
96
-
97
-
98
- #%% Create CCT dictionaries
99
-
100
- # Also gets image sizes, so this takes ~6 minutes
101
- #
102
- # Implicitly checks images for overt corruptness, i.e. by not crashing.
103
-
104
- images = []
105
- annotations = []
106
-
107
- # Map categories to integer IDs (that's what COCO likes)
108
- nextCategoryID = 0
109
- categoriesToCategoryId = {}
110
- categoriesToCounts = {}
111
-
112
- # For each image
113
- #
114
- # Because in practice images are 1:1 with annotations in this data set,
115
- # this is also a loop over annotations.
116
-
117
- startTime = time.time()
118
- for filename in tqdm(image_list):
119
-
120
- contains_human = False
121
- im = {}
122
- im['id'] = filename.split('.')[0]
123
- fn = "{}.jpg".format(filename.split('.')[0])
124
- im['file_name'] = fn
125
-
126
- # Check image height and width
127
- imagePath = os.path.join(image_directory, fn)
128
- assert(os.path.isfile(imagePath))
129
- pilImage = Image.open(imagePath)
130
- width, height = pilImage.size
131
- im['width'] = width
132
- im['height'] = height
133
-
134
- images.append(im)
135
-
136
- label_path = os.path.join(label_directory, filename)
137
- file_data = open(label_path, 'r').read()
138
- row = file_data.split()
139
- category = labels[int(row[0])-1]
140
-
141
- rows = np.loadtxt(label_path)
142
-
143
- # Each row is category, [box coordinates]
144
-
145
- # If there's just one row, loadtxt reads it as a 1d array; make it a 2d array
146
- # with one row
147
- if len(rows.shape)==1:
148
- rows = rows.reshape(1,-5)
149
-
150
- assert (len(rows.shape)==2 and rows.shape[1] == 5)
151
-
152
- categories_this_image = set()
153
-
154
- # Each row is a bounding box
155
- for row in rows:
156
-
157
- i_category = int(row[0])-1
158
- category = labels[i_category]
159
- if category == 'Human':
160
- contains_human = True
161
- categories_this_image.add(category)
162
-
163
- # Have we seen this category before?
164
- if category in categoriesToCategoryId:
165
- categoryID = categoriesToCategoryId[category]
166
- categoriesToCounts[category] += 1
167
- else:
168
- categoryID = nextCategoryID
169
- categoriesToCategoryId[category] = categoryID
170
- categoriesToCounts[category] = 0
171
- nextCategoryID += 1
172
-
173
- # Create an annotation
174
- ann = {}
175
-
176
- ann['id'] = str(uuid.uuid1())
177
- ann['image_id'] = im['id']
178
- ann['category_id'] = categoryID
179
- ann['bbox'] = [row[1]*width, row[2]*height, row[3]*width, row[4]*height]
180
- annotations.append(ann)
181
-
182
- # ...for each bounding box
183
-
184
- # This was here for debugging; nearly every instance is Human+Horse, Human+Vehicle,
185
- # or Human+Dog, but there is one Rabbit+Opossium, and a few Deer+Chicken!
186
- if False:
187
- if len(categories_this_image) > 1:
188
- print('Image {} has multiple categories: '.format(filename),end='')
189
- for c in categories_this_image:
190
- print(c, end=',')
191
- print('')
192
-
193
- if contains_human:
194
- shutil.copy(imagePath, os.path.join(base_directory, human_dir))
195
- else:
196
- shutil.copy(imagePath, os.path.join(base_directory, non_human_dir))
197
-
198
- # ...for each image
199
-
200
- # Convert categories to a CCT-style dictionary
201
-
202
- categories = []
203
-
204
- for category in categoriesToCounts:
205
- print('Category {}, count {}'.format(category, categoriesToCounts[category]))
206
- categoryID = categoriesToCategoryId[category]
207
- cat = {}
208
- cat['name'] = category
209
- cat['id'] = categoryID
210
- categories.append(cat)
211
-
212
- elapsed = time.time() - startTime
213
- print('Finished creating CCT dictionaries in {}'.format(
214
- humanfriendly.format_timespan(elapsed)))
215
-
216
-
217
- #%% Create info struct
218
-
219
- info = {}
220
- info['year'] = 2016
221
- info['version'] = 1
222
- info['description'] = ''
223
- info['secondary_contributor'] = 'Converted to COCO .json by Vardhan Duvvuri'
224
- info['contributor'] = 'University of Missouri'
225
-
226
-
227
- #%% Write output
228
-
229
- json_data = {}
230
- json_data['images'] = images
231
- json_data['annotations'] = annotations
232
- json_data['categories'] = categories
233
- json_data['info'] = info
234
- json.dump(json_data, open(output_file, 'w'), indent=2)
235
-
236
- print('Finished writing .json file with {} images, {} annotations, and {} categories'.format(
237
- len(images),len(annotations),len(categories)))
238
-
239
-
240
- #%% Create ZIP files for human and non human
241
-
242
- zipdir(human_dir,human_zipfile)
243
- zipdir(non_human_dir,non_human_zipfile)
244
-
245
-
246
- #%% Validate output
247
-
248
- from data_management.databases import integrity_check_json_db
249
-
250
- fn = output_file
251
- options = integrity_check_json_db.IntegrityCheckOptions()
252
- options.baseDir = image_directory
253
- options.bCheckImageSizes = False
254
- options.bCheckImageExistence = True
255
- options.bFindUnusedImages = True
256
-
257
- sortedCategories, data = integrity_check_json_db.integrity_check_json_db(fn,options)
258
-
259
-
260
- #%% Preview labels
261
-
262
- from md_visualization import visualize_db
263
- from data_management.databases import integrity_check_json_db
264
-
265
- viz_options = visualize_db.DbVizOptions()
266
- viz_options.num_to_visualize = None
267
- viz_options.trim_to_images_with_bboxes = False
268
- viz_options.add_search_links = True
269
- viz_options.sort_by_filename = False
270
- viz_options.parallelize_rendering = True
271
- html_output_file,image_db = visualize_db.visualize_db(db_path=output_file,
272
- output_dir=os.path.join(base_directory,'preview'),
273
- image_base_dir=image_directory,
274
- options=viz_options)
275
- os.startfile(html_output_file)
@@ -1,385 +0,0 @@
1
- """
2
-
3
- filenames_to_json.py
4
-
5
- Take a directory of images in which species labels are encoded by folder
6
- names, and produces a COCO-style .json file
7
-
8
- """
9
-
10
- #%% Constants and imports
11
-
12
- import json
13
- import io
14
- import os
15
- import uuid
16
- import csv
17
- import warnings
18
- import datetime
19
- from PIL import Image
20
-
21
- from md_utils.path_utils import find_images
22
-
23
- # ignoring all "PIL cannot read EXIF metainfo for the images" warnings
24
- warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)
25
- # Metadata Warning, tag 256 had too many entries: 42, expected 1
26
- warnings.filterwarnings("ignore", "Metadata warning", UserWarning)
27
-
28
- # Filenames will be stored in the output .json relative to this base dir
29
- baseDir = r'D:\wildlife_data\bellevue_camera_traps\bellevue_camera_traps.19.06.02.1320'
30
- outputJsonFilename = os.path.join(baseDir,'bellevue_camera_traps.19.06.02.1320.json')
31
- outputCsvFilename = os.path.join(baseDir,'bellevue_camera_traps.19.06.02.1320.csv')
32
-
33
- # rawClassListFilename = os.path.join(baseDir,'bellevue_camera_traps.19.06.02.1320_classes.csv')
34
- # classMappingsFilename = os.path.join(baseDir,'bellevue_camera_traps.19.06.02.1320_class_mapping.csv')
35
- outputEncoding = 'utf-8'
36
-
37
- classMappings = {'transitional':'unlabeled','moving':'unlabeled','setup':'unlabeled','blurry':'unlabeled','transitional':'unlabeled','junk':'unlabeled','unknown':'unlabeled'}
38
-
39
- bLoadFileListIfAvailable = True
40
-
41
- info = {}
42
- info['year'] = 2019
43
- info['version'] = '1.0'
44
- info['description'] = 'Bellevue Camera Traps'
45
- info['contributor'] = 'Dan Morris'
46
- info['date_created'] = str(datetime.date.today())
47
-
48
- maxFiles = -1
49
- bReadImageSizes = False
50
- bUseExternalRemappingTable = False
51
-
52
-
53
- #%% Enumerate files, read image sizes
54
-
55
- # Each element will be a list of relative path/full path/width/height
56
- fileInfo = []
57
- nonImages = []
58
- nFiles = 0
59
-
60
- if bLoadFileListIfAvailable and os.path.isfile(outputCsvFilename):
61
-
62
- print('Loading file list from {}'.format(outputCsvFilename))
63
-
64
- with open(outputCsvFilename,'r') as f:
65
- reader = csv.reader(f)
66
- csvInfo = list(list(item) for item in csv.reader(f, delimiter=','))
67
-
68
- for iRow in range(len(csvInfo)):
69
- csvInfo[iRow][2] = int(csvInfo[iRow][2])
70
- csvInfo[iRow][3] = int(csvInfo[iRow][3])
71
-
72
- fileInfo = csvInfo
73
-
74
- print('Finished reading list of {} files'.format(len(fileInfo)))
75
-
76
- else:
77
-
78
- print('Enumerating files from {} to {}'.format(baseDir,outputCsvFilename))
79
-
80
- image_files = find_images(baseDir,bRecursive=True)
81
- print('Enumerated {} images'.format(len(image_files)))
82
-
83
- with io.open(outputCsvFilename, "w", encoding=outputEncoding) as outputFileHandle:
84
-
85
- for fname in image_files:
86
-
87
- nFiles = nFiles + 1
88
- if maxFiles >= 0 and nFiles > maxFiles:
89
- print('Warning: early break at {} files'.format(maxFiles))
90
- break
91
-
92
- fullPath = fname
93
- relativePath = os.path.relpath(fullPath,baseDir)
94
-
95
- if maxFiles >= 0:
96
- print(relativePath)
97
-
98
- h = -1
99
- w = -1
100
-
101
- if bReadImageSizes:
102
-
103
- # Read the image
104
- try:
105
-
106
- im = Image.open(fullPath)
107
- h = im.height
108
- w = im.width
109
-
110
- except:
111
- # Corrupt or not an image
112
- nonImages.append(fullPath)
113
- continue
114
-
115
- # Store file info
116
- imageInfo = [relativePath, fullPath, w, h]
117
- fileInfo.append(imageInfo)
118
-
119
- # Write to output file
120
- outputFileHandle.write('"' + relativePath + '"' + ',' +
121
- '"' + fullPath + '"' + ',' +
122
- str(w) + ',' + str(h) + '\n')
123
-
124
- # ...for each image file
125
-
126
- # ...csv file output
127
-
128
- print("Finished writing {} file names to {}".format(nFiles,outputCsvFilename))
129
-
130
- # ...if the file list is/isn't available
131
-
132
-
133
- #%% Enumerate classes
134
-
135
- # Maps classes to counts
136
- classList = {}
137
-
138
- for iRow,row in enumerate(fileInfo):
139
-
140
- fullPath = row[0]
141
- className = os.path.split(os.path.dirname(fullPath))[1]
142
- className = className.lower().strip()
143
- if className in classList:
144
- classList[className] += 1
145
- else:
146
- classList[className] = 1
147
- row.append(className)
148
-
149
- classNames = list(classList.keys())
150
-
151
- # We like 'empty' to be class 0
152
- if 'empty' in classNames:
153
- classNames.remove('empty')
154
- classNames.insert(0,'empty')
155
-
156
- print('Finished enumerating {} classes'.format(len(classList)))
157
-
158
-
159
- #%% Assemble dictionaries
160
-
161
- images = []
162
- annotations = []
163
- categories = []
164
-
165
- categoryNameToId = {}
166
- idToCategory = {}
167
- imageIdToImage = {}
168
-
169
- nextId = 0
170
-
171
- for categoryName in classNames:
172
-
173
- catId = nextId
174
- nextId += 1
175
- categoryNameToId[categoryName] = catId
176
- newCat = {}
177
- newCat['id'] = categoryNameToId[categoryName]
178
- newCat['name'] = categoryName
179
- newCat['count'] = 0
180
- categories.append(newCat)
181
- idToCategory[catId] = newCat
182
-
183
- # ...for each category
184
-
185
-
186
- # Each element is a list of relative path/full path/width/height/className
187
-
188
- for iRow,row in enumerate(fileInfo):
189
-
190
- relativePath = row[0]
191
- w = row[2]
192
- h = row[3]
193
- className = row[4]
194
-
195
- assert className in categoryNameToId
196
- categoryId = categoryNameToId[className]
197
-
198
- im = {}
199
- im['id'] = str(uuid.uuid1())
200
- im['file_name'] = relativePath
201
- im['height'] = h
202
- im['width'] = w
203
- images.append(im)
204
- imageIdToImage[im['id']] = im
205
-
206
- ann = {}
207
- ann['id'] = str(uuid.uuid1())
208
- ann['image_id'] = im['id']
209
- ann['category_id'] = categoryId
210
- annotations.append(ann)
211
-
212
- cat = idToCategory[categoryId]
213
- cat['count'] += 1
214
-
215
- # ...for each image
216
-
217
- oldNameToOldId = categoryNameToId
218
- originalCategories = categories
219
-
220
- print('Finished assembling dictionaries')
221
-
222
-
223
- #%% External class mapping
224
-
225
- if bUseExternalRemappingTable:
226
-
227
- assert classMappings is None
228
-
229
-
230
- #%% Write raw class table
231
-
232
- # cat = categories[0]
233
- if os.path.isfile(rawClassListFilename):
234
-
235
- print('Not over-writing raw class table')
236
-
237
- else:
238
-
239
- with io.open(rawClassListFilename, "w", encoding=outputEncoding) as classListFileHandle:
240
- for cat in categories:
241
- catId = cat['id']
242
- categoryName = cat['name']
243
- categoryCount = cat['count']
244
- classListFileHandle.write(str(catId) + ',"' + categoryName + '",' + str(categoryCount) + '\n')
245
-
246
- print('Finished writing raw class table')
247
-
248
-
249
- #%% Read the mapped class table
250
-
251
- classMappings = {}
252
-
253
- if os.path.isfile(classMappingsFilename):
254
-
255
- print('Loading file list from {}'.format(classMappingsFilename))
256
-
257
- with open(classMappingsFilename,'r') as f:
258
- reader = csv.reader(f)
259
- mappingInfo = list(list(item) for item in csv.reader(f, delimiter=','))
260
-
261
- for mapping in mappingInfo:
262
- assert len(mapping) == 4
263
-
264
- # id, source, count, target
265
- sourceClass = mapping[1]
266
- targetClass = mapping[3]
267
- assert sourceClass not in classMappings
268
- classMappings[sourceClass] = targetClass
269
-
270
- print('Finished reading list of {} class mappings'.format(len(mappingInfo)))
271
-
272
- else:
273
-
274
- #%% Make classMappings contain *all* classes, not just remapped classes
275
-
276
- # cat = categories[0]
277
- for cat in categories:
278
- if cat['name'] not in classMappings:
279
- classMappings[cat['name']] = cat['name']
280
-
281
-
282
- #%% Create new class list
283
-
284
- categories = []
285
- categoryNameToId = {}
286
- oldIdToNewId = {}
287
-
288
- # Start at 1, explicitly assign 0 to "empty"
289
- nextCategoryId = 1
290
- for sourceClass in classMappings:
291
- targetClass = classMappings[sourceClass]
292
-
293
- if targetClass not in categoryNameToId:
294
-
295
- if targetClass == 'empty':
296
- categoryId = 0
297
- else:
298
- categoryId = nextCategoryId
299
- nextCategoryId = nextCategoryId + 1
300
-
301
- categoryNameToId[targetClass] = categoryId
302
- newCat = {}
303
- newCat['id'] = categoryId
304
- newCat['name'] = targetClass
305
- newCat['count'] = 0
306
-
307
- if targetClass == 'empty':
308
- categories.insert(0,newCat)
309
- else:
310
- categories.append(newCat)
311
-
312
- else:
313
-
314
- categoryId = categoryNameToId[targetClass]
315
-
316
- # One-off issue with character encoding
317
- if sourceClass == 'human':
318
- sourceClass = 'human'
319
-
320
- assert sourceClass in oldNameToOldId
321
- oldId = oldNameToOldId[sourceClass]
322
- oldIdToNewId[oldId] = categoryId
323
-
324
- categoryIdToCat = {}
325
- for cat in categories:
326
- categoryIdToCat[cat['id']] = cat
327
-
328
- print('Mapped {} original classes to {} new classes'.format(len(originalCategories),len(categories)))
329
-
330
-
331
- #%% Re-map annotations
332
-
333
- # ann = annotations[0]
334
- for ann in annotations:
335
-
336
- ann['category_id'] = oldIdToNewId[ann['category_id']]
337
-
338
-
339
- #%% Write output .json
340
-
341
- data = {}
342
- data['info'] = info
343
- data['images'] = images
344
- data['annotations'] = annotations
345
- data['categories'] = categories
346
-
347
- json.dump(data, open(outputJsonFilename,'w'), indent=4)
348
-
349
- print('Finished writing json to {}'.format(outputJsonFilename))
350
-
351
-
352
- #%% Utilities
353
-
354
- if False:
355
-
356
- #%%
357
- # Find images with a particular tag
358
- className = 'hum'
359
- matches = []
360
- assert className in categoryNameToId
361
- catId = categoryNameToId[className]
362
- for ann in annotations:
363
- if ann['category_id'] == catId:
364
- imageId = ann['image_id']
365
- im = imageIdToImage[imageId]
366
- matches.append(im['file_name'])
367
- print('Found {} matches'.format(len(matches)))
368
-
369
- os.startfile(os.path.join(baseDir,matches[0]))
370
-
371
-
372
- #%% Randomly sample annotations
373
-
374
- import random
375
- nAnnotations = len(annotations)
376
- iAnn = random.randint(0,nAnnotations)
377
- ann = annotations[iAnn]
378
- catId = ann['category_id']
379
- imageId = ann['image_id']
380
- im = imageIdToImage[imageId]
381
- fn = os.path.join(baseDir,im['file_name'])
382
- cat = categoryIdToCat[catId]
383
- className = cat['name']
384
- print('This should be a {}'.format(className))
385
- os.startfile(fn)