当前位置:   article > 正文

labelme标注的多边形json文件批量化转txtx文件_labelme转换txt的脚本

labelme转换txt的脚本
  1. import json
  2. import os
  3. def json_to_txt(json_path, txt_path):
  4. with open(json_path, 'r') as json_file:
  5. data = json.load(json_file)
  6. with open(txt_path, 'w') as txt_file:
  7. for shape in data['shapes']:
  8. if shape['shape_type'] == 'polygon':
  9. points = shape['points']
  10. label = shape['label']
  11. txt_file.write(f"{label} {' '.join([f'{x},{y}' for x, y in points])}\n")
  12. def batch_convert_json_to_txt(json_folder, txt_folder):
  13. os.makedirs(txt_folder, exist_ok=True)
  14. for json_file in os.listdir(json_folder):
  15. if json_file.endswith('.json'):
  16. json_path = os.path.join(json_folder, json_file)
  17. txt_file = os.path.splitext(json_file)[0] + '.txt'
  18. txt_path = os.path.join(txt_folder, txt_file)
  19. json_to_txt(json_path, txt_path)
  20. if __name__ == "__main__":
  21. json_folder = r'/path/to/json/folder'
  22. txt_folder = r'/path/to/txt/folder'
  23. batch_convert_json_to_txt(json_folder, txt_folder)

请注意替换/path/to/json/folder/path/to/txt/folder为你实际的文件夹路径。

第一个为你json文件存放的文件夹路径,第二个为转换为txt路径后存放的文件夹路径,路径建议用绝对路径

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/665793
推荐阅读
相关标签
  

闽ICP备14008679号