megadetector 10.0.15__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.
- 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 +701 -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 +563 -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 +192 -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 +665 -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 +984 -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 +2172 -0
- megadetector/detection/run_inference_with_yolov5_val.py +1314 -0
- megadetector/detection/run_md_and_speciesnet.py +1604 -0
- megadetector/detection/run_tiled_inference.py +1044 -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 +1943 -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 +2140 -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 +211 -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 +231 -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 +2872 -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 +1766 -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 +1973 -0
- megadetector/visualization/visualize_db.py +630 -0
- megadetector/visualization/visualize_detector_output.py +498 -0
- megadetector/visualization/visualize_video_output.py +705 -0
- megadetector-10.0.15.dist-info/METADATA +115 -0
- megadetector-10.0.15.dist-info/RECORD +147 -0
- megadetector-10.0.15.dist-info/WHEEL +5 -0
- megadetector-10.0.15.dist-info/licenses/LICENSE +19 -0
- megadetector-10.0.15.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
labelme_to_yolo.py
|
|
4
|
+
|
|
5
|
+
Create YOLO .txt files in a folder containing labelme .json files.
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
#%% Imports
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import json
|
|
13
|
+
import argparse
|
|
14
|
+
|
|
15
|
+
from multiprocessing.pool import Pool, ThreadPool
|
|
16
|
+
from functools import partial
|
|
17
|
+
from tqdm import tqdm
|
|
18
|
+
|
|
19
|
+
from megadetector.utils.path_utils import recursive_file_list
|
|
20
|
+
from megadetector.utils.ct_utils import write_json
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
#%% Main function
|
|
24
|
+
|
|
25
|
+
def labelme_file_to_yolo_file(labelme_file,
|
|
26
|
+
category_name_to_category_id,
|
|
27
|
+
yolo_file=None,
|
|
28
|
+
required_token=None,
|
|
29
|
+
overwrite_behavior='overwrite'):
|
|
30
|
+
"""
|
|
31
|
+
Convert the single .json file labelme_file to yolo format, writing the results to the text
|
|
32
|
+
file yolo_file (defaults to s/json/txt).
|
|
33
|
+
|
|
34
|
+
If required_token is not None and the dict in labelme_file does not contain the key [required_token],
|
|
35
|
+
this function no-ops (i.e., does not generate a YOLO file).
|
|
36
|
+
|
|
37
|
+
overwrite_behavior should be 'skip' or 'overwrite' (default).
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
labelme_file (str): .json file to convert
|
|
41
|
+
category_name_to_category_id (dict): category name --> ID mapping
|
|
42
|
+
yolo_file (str, optional): output .txt file defaults to s/json/txt
|
|
43
|
+
required_token (str, optional): only process filenames containing this token
|
|
44
|
+
overwrite_behavior (str, optional): "skip" or "overwrite"
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
result = {}
|
|
48
|
+
result['labelme_file'] = labelme_file
|
|
49
|
+
result['status'] = 'unknown'
|
|
50
|
+
|
|
51
|
+
assert os.path.isfile(labelme_file), 'Could not find labelme .json file {}'.format(labelme_file)
|
|
52
|
+
assert labelme_file.endswith('.json'), 'Illegal labelme .json file {}'.format(labelme_file)
|
|
53
|
+
|
|
54
|
+
if yolo_file is None:
|
|
55
|
+
yolo_file = os.path.splitext(labelme_file)[0] + '.txt'
|
|
56
|
+
|
|
57
|
+
if os.path.isfile(yolo_file):
|
|
58
|
+
if overwrite_behavior == 'skip':
|
|
59
|
+
result['status'] = 'skip-exists'
|
|
60
|
+
return result
|
|
61
|
+
else:
|
|
62
|
+
assert overwrite_behavior == 'overwrite', \
|
|
63
|
+
'Unrecognized overwrite behavior {}'.format(overwrite_behavior)
|
|
64
|
+
|
|
65
|
+
with open(labelme_file,'r') as f:
|
|
66
|
+
labelme_data = json.load(f)
|
|
67
|
+
|
|
68
|
+
if required_token is not None and required_token not in labelme_data:
|
|
69
|
+
result['status'] = 'skip-no-required-token'
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
im_height = labelme_data['imageHeight']
|
|
73
|
+
im_width = labelme_data['imageWidth']
|
|
74
|
+
|
|
75
|
+
yolo_lines = []
|
|
76
|
+
|
|
77
|
+
for shape in labelme_data['shapes']:
|
|
78
|
+
|
|
79
|
+
assert shape['shape_type'] == 'rectangle', \
|
|
80
|
+
'I only know how to convert rectangles to YOLO format'
|
|
81
|
+
assert shape['label'] in category_name_to_category_id, \
|
|
82
|
+
'Category {} not in category mapping'.format(shape['label'])
|
|
83
|
+
assert len(shape['points']) == 2, 'Illegal rectangle'
|
|
84
|
+
category_id = category_name_to_category_id[shape['label']]
|
|
85
|
+
|
|
86
|
+
p0 = shape['points'][0]
|
|
87
|
+
p1 = shape['points'][1]
|
|
88
|
+
|
|
89
|
+
# Labelme: [[x0,y0],[x1,y1]] (arbitrarily sorted) (absolute coordinates)
|
|
90
|
+
#
|
|
91
|
+
# YOLO: [class, x_center, y_center, width, height] (normalized coordinates)
|
|
92
|
+
minx_abs = min(p0[0],p1[0])
|
|
93
|
+
maxx_abs = max(p0[0],p1[0])
|
|
94
|
+
miny_abs = min(p0[1],p1[1])
|
|
95
|
+
maxy_abs = max(p0[1],p1[1])
|
|
96
|
+
|
|
97
|
+
if (minx_abs >= (im_width-1)) or (maxx_abs <= 0) or \
|
|
98
|
+
(miny_abs >= (im_height-1)) or (maxy_abs <= 0):
|
|
99
|
+
print('Skipping invalid shape in {}'.format(labelme_file))
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
# Clip to [0,1]... it's not obvious that the YOLO format doesn't allow bounding
|
|
103
|
+
# boxes to extend outside the image, but YOLOv5 and YOLOv8 get sad about boxes
|
|
104
|
+
# that extend outside the image.
|
|
105
|
+
maxx_abs = min(maxx_abs,im_width-1)
|
|
106
|
+
maxy_abs = min(maxy_abs,im_height-1)
|
|
107
|
+
minx_abs = max(minx_abs,0.0)
|
|
108
|
+
miny_abs = max(miny_abs,0.0)
|
|
109
|
+
|
|
110
|
+
# Handle degenerate cases where image is one pixel wide
|
|
111
|
+
if im_width == 1:
|
|
112
|
+
minx_rel = 0.0
|
|
113
|
+
maxx_rel = 0.0
|
|
114
|
+
else:
|
|
115
|
+
minx_rel = minx_abs / (im_width-1)
|
|
116
|
+
maxx_rel = maxx_abs / (im_width-1)
|
|
117
|
+
|
|
118
|
+
# Handle degenerate cases where image is one pixel tall
|
|
119
|
+
if im_height == 1:
|
|
120
|
+
miny_rel = 0.0
|
|
121
|
+
maxy_rel = 0.0
|
|
122
|
+
else:
|
|
123
|
+
miny_rel = miny_abs / (im_height-1)
|
|
124
|
+
maxy_rel = maxy_abs / (im_height-1)
|
|
125
|
+
|
|
126
|
+
assert maxx_rel >= minx_rel
|
|
127
|
+
assert maxy_rel >= miny_rel
|
|
128
|
+
|
|
129
|
+
xcenter_rel = (maxx_rel + minx_rel) / 2.0
|
|
130
|
+
ycenter_rel = (maxy_rel + miny_rel) / 2.0
|
|
131
|
+
w_rel = maxx_rel - minx_rel
|
|
132
|
+
h_rel = maxy_rel - miny_rel
|
|
133
|
+
|
|
134
|
+
yolo_line = '{} {:.3f} {:.3f} {:.3f} {:.3f}'.format(category_id,
|
|
135
|
+
xcenter_rel, ycenter_rel, w_rel, h_rel)
|
|
136
|
+
yolo_lines.append(yolo_line)
|
|
137
|
+
|
|
138
|
+
# ...for each shape
|
|
139
|
+
|
|
140
|
+
with open(yolo_file,'w') as f:
|
|
141
|
+
for s in yolo_lines:
|
|
142
|
+
f.write(s + '\n')
|
|
143
|
+
|
|
144
|
+
result['status'] = 'converted'
|
|
145
|
+
return result
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def labelme_folder_to_yolo(labelme_folder,
|
|
149
|
+
category_name_to_category_id=None,
|
|
150
|
+
required_token=None,
|
|
151
|
+
overwrite_behavior='overwrite',
|
|
152
|
+
relative_filenames_to_convert=None,
|
|
153
|
+
n_workers=1,
|
|
154
|
+
use_threads=True):
|
|
155
|
+
"""
|
|
156
|
+
Given a folder with images and labelme .json files, convert the .json files
|
|
157
|
+
to YOLO .txt format. If category_name_to_category_id is None, first reads
|
|
158
|
+
all the labels in the folder to build a zero-indexed name --> ID mapping.
|
|
159
|
+
|
|
160
|
+
If required_token is not None and a labelme_file does not contain the key [required_token],
|
|
161
|
+
it won't be converted. Typically used to specify a field that indicates which files have
|
|
162
|
+
been reviewed.
|
|
163
|
+
|
|
164
|
+
If relative_filenames_to_convert is not None, this should be a list of .json (not image)
|
|
165
|
+
files that should get converted, relative to the base folder.
|
|
166
|
+
|
|
167
|
+
overwrite_behavior should be 'skip' or 'overwrite' (default).
|
|
168
|
+
|
|
169
|
+
returns a dict with:
|
|
170
|
+
'category_name_to_category_id', whether it was passed in or constructed
|
|
171
|
+
'image_results': a list of results for each image (converted, skipped, error)
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
labelme_folder (str): folder of .json files to convert
|
|
175
|
+
category_name_to_category_id (dict): category name --> ID mapping
|
|
176
|
+
required_token (str, optional): only process filenames containing this token
|
|
177
|
+
overwrite_behavior (str, optional): "skip" or "overwrite"
|
|
178
|
+
relative_filenames_to_convert (list of str, optional): only process filenames on this list
|
|
179
|
+
n_workers (int, optional): parallelism level
|
|
180
|
+
use_threads (bool, optional): whether to use threads (True) or processes (False) for
|
|
181
|
+
parallelism
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
if relative_filenames_to_convert is not None:
|
|
185
|
+
labelme_files_relative = relative_filenames_to_convert
|
|
186
|
+
assert all([fn.endswith('.json') for fn in labelme_files_relative]), \
|
|
187
|
+
'relative_filenames_to_convert contains non-json files'
|
|
188
|
+
else:
|
|
189
|
+
labelme_files_relative = recursive_file_list(labelme_folder,return_relative_paths=True)
|
|
190
|
+
labelme_files_relative = [fn for fn in labelme_files_relative if fn.endswith('.json')]
|
|
191
|
+
|
|
192
|
+
if required_token is None:
|
|
193
|
+
valid_labelme_files_relative = labelme_files_relative
|
|
194
|
+
else:
|
|
195
|
+
valid_labelme_files_relative = []
|
|
196
|
+
|
|
197
|
+
# fn_relative = labelme_files_relative[-1]
|
|
198
|
+
for fn_relative in labelme_files_relative:
|
|
199
|
+
|
|
200
|
+
fn_abs = os.path.join(labelme_folder,fn_relative)
|
|
201
|
+
|
|
202
|
+
with open(fn_abs,'r') as f:
|
|
203
|
+
labelme_data = json.load(f)
|
|
204
|
+
if required_token not in labelme_data:
|
|
205
|
+
continue
|
|
206
|
+
|
|
207
|
+
valid_labelme_files_relative.append(fn_relative)
|
|
208
|
+
|
|
209
|
+
print('{} of {} files are valid'.format(len(valid_labelme_files_relative),
|
|
210
|
+
len(labelme_files_relative)))
|
|
211
|
+
|
|
212
|
+
del labelme_files_relative
|
|
213
|
+
|
|
214
|
+
if category_name_to_category_id is None:
|
|
215
|
+
|
|
216
|
+
category_name_to_category_id = {}
|
|
217
|
+
|
|
218
|
+
for fn_relative in valid_labelme_files_relative:
|
|
219
|
+
|
|
220
|
+
fn_abs = os.path.join(labelme_folder,fn_relative)
|
|
221
|
+
with open(fn_abs,'r') as f:
|
|
222
|
+
labelme_data = json.load(f)
|
|
223
|
+
for shape in labelme_data['shapes']:
|
|
224
|
+
label = shape['label']
|
|
225
|
+
if label not in category_name_to_category_id:
|
|
226
|
+
category_name_to_category_id[label] = len(category_name_to_category_id)
|
|
227
|
+
# ...for each file
|
|
228
|
+
|
|
229
|
+
# ...if we need to build a category mapping
|
|
230
|
+
|
|
231
|
+
image_results = []
|
|
232
|
+
|
|
233
|
+
n_workers = min(n_workers,len(valid_labelme_files_relative))
|
|
234
|
+
|
|
235
|
+
if n_workers <= 1:
|
|
236
|
+
for fn_relative in tqdm(valid_labelme_files_relative):
|
|
237
|
+
|
|
238
|
+
fn_abs = os.path.join(labelme_folder,fn_relative)
|
|
239
|
+
image_result = labelme_file_to_yolo_file(fn_abs,
|
|
240
|
+
category_name_to_category_id,
|
|
241
|
+
yolo_file=None,
|
|
242
|
+
required_token=required_token,
|
|
243
|
+
overwrite_behavior=overwrite_behavior)
|
|
244
|
+
image_results.append(image_result)
|
|
245
|
+
# ...for each file
|
|
246
|
+
else:
|
|
247
|
+
pool = None
|
|
248
|
+
try:
|
|
249
|
+
if use_threads:
|
|
250
|
+
pool = ThreadPool(n_workers)
|
|
251
|
+
else:
|
|
252
|
+
pool = Pool(n_workers)
|
|
253
|
+
|
|
254
|
+
valid_labelme_files_abs = [os.path.join(labelme_folder,fn_relative) for \
|
|
255
|
+
fn_relative in valid_labelme_files_relative]
|
|
256
|
+
|
|
257
|
+
image_results = list(tqdm(pool.imap(
|
|
258
|
+
partial(labelme_file_to_yolo_file,
|
|
259
|
+
category_name_to_category_id=category_name_to_category_id,
|
|
260
|
+
yolo_file=None,
|
|
261
|
+
required_token=required_token,
|
|
262
|
+
overwrite_behavior=overwrite_behavior),
|
|
263
|
+
valid_labelme_files_abs),
|
|
264
|
+
total=len(valid_labelme_files_abs)))
|
|
265
|
+
finally:
|
|
266
|
+
if pool is not None:
|
|
267
|
+
pool.close()
|
|
268
|
+
pool.join()
|
|
269
|
+
print('Pool closed and joined for labelme conversion to YOLO')
|
|
270
|
+
|
|
271
|
+
assert len(valid_labelme_files_relative) == len(image_results)
|
|
272
|
+
|
|
273
|
+
print('Converted {} labelme .json files to YOLO'.format(
|
|
274
|
+
len(valid_labelme_files_relative)))
|
|
275
|
+
|
|
276
|
+
labelme_to_yolo_results = {}
|
|
277
|
+
labelme_to_yolo_results['category_name_to_category_id'] = category_name_to_category_id
|
|
278
|
+
labelme_to_yolo_results['image_results'] = image_results
|
|
279
|
+
|
|
280
|
+
return labelme_to_yolo_results
|
|
281
|
+
|
|
282
|
+
# ...def labelme_folder_to_yolo(...)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
#%% Command-line driver
|
|
286
|
+
|
|
287
|
+
def main():
|
|
288
|
+
"""
|
|
289
|
+
Command-line interface to convert Labelme JSON files to YOLO format
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
parser = argparse.ArgumentParser(
|
|
293
|
+
description='Convert a folder of Labelme .json files to YOLO .txt format'
|
|
294
|
+
)
|
|
295
|
+
parser.add_argument(
|
|
296
|
+
'labelme_folder',
|
|
297
|
+
type=str,
|
|
298
|
+
help='Folder of Labelme .json files to convert'
|
|
299
|
+
)
|
|
300
|
+
parser.add_argument(
|
|
301
|
+
'--output_category_file',
|
|
302
|
+
type=str,
|
|
303
|
+
default=None,
|
|
304
|
+
help='Path to save the generated category mapping (.json)'
|
|
305
|
+
)
|
|
306
|
+
parser.add_argument(
|
|
307
|
+
'--required_token',
|
|
308
|
+
type=str,
|
|
309
|
+
default=None,
|
|
310
|
+
help='Only process files containing this token as a key in the Labelme JSON dict'
|
|
311
|
+
)
|
|
312
|
+
parser.add_argument(
|
|
313
|
+
'--overwrite_behavior',
|
|
314
|
+
type=str,
|
|
315
|
+
default='overwrite',
|
|
316
|
+
choices=['skip', 'overwrite'],
|
|
317
|
+
help="Behavior if YOLO .txt files exist (default: 'overwrite')"
|
|
318
|
+
)
|
|
319
|
+
parser.add_argument(
|
|
320
|
+
'--n_workers',
|
|
321
|
+
type=int,
|
|
322
|
+
default=1,
|
|
323
|
+
help='Number of workers for parallel processing (default: 1)'
|
|
324
|
+
)
|
|
325
|
+
parser.add_argument(
|
|
326
|
+
'--use_processes',
|
|
327
|
+
action='store_true',
|
|
328
|
+
help='Use processes instead of threads for parallelization (defaults to threads)'
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
args = parser.parse_args()
|
|
332
|
+
|
|
333
|
+
results = labelme_folder_to_yolo(
|
|
334
|
+
labelme_folder=args.labelme_folder,
|
|
335
|
+
category_name_to_category_id=None,
|
|
336
|
+
required_token=args.required_token,
|
|
337
|
+
overwrite_behavior=args.overwrite_behavior,
|
|
338
|
+
relative_filenames_to_convert=None,
|
|
339
|
+
n_workers=args.n_workers,
|
|
340
|
+
use_threads=(not args.use_processes)
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
if args.output_category_file:
|
|
344
|
+
category_map = results['category_name_to_category_id']
|
|
345
|
+
write_json(args.output_category_file,category_map)
|
|
346
|
+
print(f'Saved category mapping to {args.output_category_file}')
|
|
347
|
+
|
|
348
|
+
if __name__ == '__main__':
|
|
349
|
+
main()
|
|
File without changes
|