Improved mask conversion script
Browse filesSigned-off-by: Jiri Podivin <[email protected]>
- utils/convert_masks.py +31 -5
utils/convert_masks.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
#!/bin/env python
|
2 |
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
import json
|
5 |
import os
|
@@ -13,6 +16,8 @@ import os
|
|
13 |
import tempfile
|
14 |
import argparse
|
15 |
import glob
|
|
|
|
|
16 |
|
17 |
class InputStream:
|
18 |
def __init__(self, data):
|
@@ -174,25 +179,46 @@ def merge_masks(mask_metadata, target_mask_dir, label2id):
|
|
174 |
|
175 |
def main():
|
176 |
parser = argparse.ArgumentParser('maskconvert')
|
177 |
-
parser.add_argument('dataset_root')
|
178 |
|
179 |
arguments = parser.parse_args()
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
files_to_process = glob.glob(f"{annotations_folder_path}/*")
|
184 |
|
185 |
-
#
|
186 |
-
|
|
|
|
|
|
|
187 |
|
188 |
metadata = pd.DataFrame(process_files_in_parallel(files_to_process, tmp_mask_path, source_files))
|
189 |
|
|
|
190 |
id2label = {int(k): v for k, v in enumerate(['void', 'Fruit', 'Leaf', 'Flower', 'Stem'])}
|
191 |
|
192 |
label2id = {v: k for k, v in id2label.items()}
|
193 |
|
194 |
result = merge_masks(metadata, os.path.join(arguments.dataset_root, 'semantic_masks'), label2id)
|
|
|
195 |
result = pd.DataFrame(result).drop_duplicates()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
result.to_csv(
|
197 |
os.path.join(arguments.dataset_root, 'semantic_metadata.csv'),
|
198 |
index=False)
|
|
|
1 |
#!/bin/env python
|
2 |
|
3 |
+
# Mask conversion for plantorgans
|
4 |
+
# 2024 by Jiri Podivin
|
5 |
+
|
6 |
import numpy as np
|
7 |
import json
|
8 |
import os
|
|
|
16 |
import tempfile
|
17 |
import argparse
|
18 |
import glob
|
19 |
+
import shutil
|
20 |
+
import tarfile
|
21 |
|
22 |
class InputStream:
|
23 |
def __init__(self, data):
|
|
|
179 |
|
180 |
def main():
|
181 |
parser = argparse.ArgumentParser('maskconvert')
|
182 |
+
parser.add_argument('dataset_root', help="Root directory of the dataset repo.")
|
183 |
|
184 |
arguments = parser.parse_args()
|
185 |
+
|
186 |
+
# Unzip raw labels to temporary location
|
187 |
+
tmp_raw_label_pth = tempfile.mkdtemp('_labels_raw')
|
188 |
+
with tarfile.open(os.path.join(arguments.dataset_root, 'labels_raw.tar.gz'), mode='r:gz') as archive:
|
189 |
+
archive.extractall(tmp_raw_label_pth)
|
190 |
+
|
191 |
+
annotations_folder_path = os.path.join(tmp_raw_label_pth, 'labels_raw')
|
192 |
+
|
193 |
+
tmp_mask_path = tempfile.mkdtemp('_masks')
|
194 |
|
195 |
files_to_process = glob.glob(f"{annotations_folder_path}/*")
|
196 |
|
197 |
+
# Image name -> path dict
|
198 |
+
images = {os.path.basename(x): x for x in glob.glob('sourcedata/**/*.jpg')}
|
199 |
+
|
200 |
+
# Image names
|
201 |
+
source_files = [k for k in images.keys()]
|
202 |
|
203 |
metadata = pd.DataFrame(process_files_in_parallel(files_to_process, tmp_mask_path, source_files))
|
204 |
|
205 |
+
# Label map order IS important
|
206 |
id2label = {int(k): v for k, v in enumerate(['void', 'Fruit', 'Leaf', 'Flower', 'Stem'])}
|
207 |
|
208 |
label2id = {v: k for k, v in id2label.items()}
|
209 |
|
210 |
result = merge_masks(metadata, os.path.join(arguments.dataset_root, 'semantic_masks'), label2id)
|
211 |
+
|
212 |
result = pd.DataFrame(result).drop_duplicates()
|
213 |
+
|
214 |
+
# Moving newly labeled images to right dir
|
215 |
+
images = {os.path.basename(x): x for x in glob.glob('sourcedata/**/*.jpg')}
|
216 |
+
|
217 |
+
for img in metadata['image']:
|
218 |
+
if 'unlabeled' in images[img]:
|
219 |
+
print("Moving {img} to labeled!")
|
220 |
+
shutil.move(images[img], os.path.join('sourcedata/labeled/', img))
|
221 |
+
|
222 |
result.to_csv(
|
223 |
os.path.join(arguments.dataset_root, 'semantic_metadata.csv'),
|
224 |
index=False)
|