12345678910111213141516171819202122232425262728 |
- import argparse
- import json
- import os
- def work(json_fold):
- json_list = os.listdir(json_fold)
- for json_path in json_list:
- path = json_fold + '/' + json_path
- # 处理json文件
- if str(path).endswith('.json'):
- with open(path, "r", encoding="utf-8") as f:
- content = json.load(f)
- # 将 JSON 数据写入 CSV 文件
- csv_path = str(path).replace('json', 'jpg') + '.csv'
- with open(csv_path, mode='w', newline='') as csv_file:
- # 使用 DictWriter 而不是 csv.DictWriter,因为需要将 JSON 中的键作为列名
- for point in content['shapes']:
- line = str(point['points'][0][0]) + ' ' + str(point['points'][0][1]) + '\n'
- print(line)
- csv_file.write(line)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.add_argument('--json_fold', type=str, default='./my_dataset_origin')
- args = parser.parse_args()
- work(args.json_fold)
|