data_copy.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. import json
  3. import argparse
  4. import numpy as np
  5. import glob
  6. import cv2
  7. from sklearn.model_selection import train_test_split
  8. from labelme import utils
  9. from tqdm import tqdm
  10. import shutil
  11. if __name__ == '__main__':
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('--anno_dirs', type=str, nargs="+", default=[])
  14. parser.add_argument('--data_dir', type=str, default='')
  15. args = parser.parse_args()
  16. labelme_folds = args.anno_dirs
  17. json_list_path = []
  18. for labelme_path in labelme_folds:
  19. list_path = glob.glob(labelme_path + "/*.json")
  20. json_list_path.extend(list_path)
  21. print('reading...')
  22. print("images: %d" % len(json_list_path))
  23. for file in tqdm(json_list_path):
  24. json_path = file
  25. temp_json = {}
  26. img_name = ''
  27. with open(json_path, 'r', encoding='utf-8') as jp:
  28. info = json.load(jp)
  29. info['imagePath'] = os.path.basename(info['imagePath'])
  30. img_name = info['imagePath']
  31. info['imageData'] = None
  32. temp_json = info
  33. jp.close()
  34. img_path = file.replace('json', str(img_name).split('.')[1])
  35. try:
  36. shutil.copy(img_path, args.data_dir)
  37. json.dump(temp_json,
  38. open(os.path.join(args.data_dir, os.path.basename(json_path)), 'w', encoding='utf-8'),
  39. ensure_ascii=False, indent=4)
  40. except Exception as e:
  41. print(e)
  42. print('Wrong Image:', img_name)
  43. continue