Skip to content

Instantly share code, notes, and snippets.

@jeffbaumes
Created November 16, 2021 21:05
Show Gist options
  • Save jeffbaumes/1731ca73bcdebd3ed421856abb80a747 to your computer and use it in GitHub Desktop.
Save jeffbaumes/1731ca73bcdebd3ed421856abb80a747 to your computer and use it in GitHub Desktop.
superpixels-to-annotation.py
import sys
def superpixels_to_annotation(chunk_size, input_filename):
output_file_prefix = '.'.join(input_filename.split('.')[:-1])
with open(input_filename) as input_file:
elements = []
chunk = 0
for line in input_file:
output_line = ','.join(
[f'[{val},0]' for val in line.strip().split(' ')[3:]])
elements.append(
f'{{"type": "polyline", "closed": false, "points": [{output_line}]}}')
if len(elements) >= chunk_size:
output_filename = f'{output_file_prefix}_{str(chunk).zfill(2)}'
with open(f'{output_filename}.json', 'w') as output_file:
output_file.write(
f'{{"name":"{output_filename}", "description": "", "elements": [{",".join(elements)}]}}')
elements = []
chunk += 1
if chunk >= 10:
print('Too many chunks, only outputting first 10')
break
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: python superpixels-to-annotation.py <chunk-size> <annotation-text-file>')
sys.exit(0)
chunk_size = int(sys.argv[1])
input_filename = sys.argv[2]
superpixels_to_annotation(chunk_size, input_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment