delete_label.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import argparse
  2. import json
  3. import os
  4. from tqdm import tqdm
  5. def work(fold_path, label):
  6. fold_list = os.listdir(fold_path)
  7. for fold in fold_list:
  8. json_list = os.listdir(fold_path + '/' + fold + '/images')
  9. print(fold)
  10. for json_path in tqdm(json_list):
  11. if json_path.endswith('.json'):
  12. file_name = fold_path + '/' + fold + '/images/' + json_path
  13. with open(str(file_name), 'r', encoding='utf-8') as fp:
  14. json_data = json.load(fp)
  15. shape_list = []
  16. for shape in json_data['shapes']:
  17. if shape['label'] != label:
  18. shape_list.append(shape)
  19. fp.close()
  20. if len(shape_list) == 0:
  21. # print('remove', file_name)
  22. os.remove(file_name)
  23. os.remove(str(file_name).replace('json', 'jpg'))
  24. else:
  25. os.remove(file_name)
  26. json_data['shapes'] = shape_list
  27. # print(file_name)
  28. with open(str(file_name), 'w', encoding='utf-8') as fp1:
  29. json.dump(json_data, fp1, sort_keys=False, indent=2)
  30. fp1.close()
  31. if __name__ == '__main__':
  32. parser = argparse.ArgumentParser()
  33. parser.add_argument('--fold', type=str, default='./fold')
  34. parser.add_argument('--label', type=str, default='Table')
  35. args = parser.parse_args()
  36. work(args.fold, args.label)