ego_to_json.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import os
  2. import shutil
  3. import json
  4. import scipy.io
  5. import random
  6. # ego_to_json.py maakt egohands_data bruikbaar om om te laten zetten naar de verschillende
  7. # formaten voor de netwerken.
  8. #drie folders, train, test, val
  9. # |-- train
  10. # | | -- images
  11. # | | -- annotations.json
  12. # |-- val
  13. # | | -- images
  14. # | | -- annotations.json
  15. # |-- test
  16. # | | -- images
  17. # | | -- annotations.json
  18. # annotations.json:
  19. # {
  20. # "CARDS_OFFICE_H_T_frame_0001.jpg":
  21. # {
  22. # "name": "CARDS_OFFICE_H_T_frame_0001.jpg",
  23. # "objects": [[]]
  24. # },
  25. # "CARDS_OFFICE_H_T_frame_0002.jpg":
  26. # {
  27. # "name": "CARDS_OFFICE_H_T_frame_0002.jpg",
  28. # "objects": [[]]
  29. # }
  30. # }
  31. ROOT_DIR = "../egohands_data"
  32. ANNOTATION_FILE = "polygons.mat"
  33. SAVE_FILE = "annotations.json"
  34. def split_test():
  35. os.makedirs(os.path.join(ROOT_DIR, "test"))
  36. os.makedirs(os.path.join(ROOT_DIR, "val"))
  37. os.makedirs(os.path.join(ROOT_DIR, "train"))
  38. shutil.move(os.path.join(ROOT_DIR, "CARDS_COURTYARD_B_T"), os.path.join(ROOT_DIR, "test", "CARDS_COURTYARD_B_T"))
  39. shutil.move(os.path.join(ROOT_DIR, "CARDS_OFFICE_S_B"), os.path.join(ROOT_DIR, "test", "CARDS_OFFICE_S_B"))
  40. shutil.move(os.path.join(ROOT_DIR, "CHESS_COURTYARD_B_T"), os.path.join(ROOT_DIR, "test", "CHESS_COURTYARD_B_T"))
  41. shutil.move(os.path.join(ROOT_DIR, "CHESS_LIVINGROOM_T_H"), os.path.join(ROOT_DIR, "test", "CHESS_LIVINGROOM_T_H"))
  42. shutil.move(os.path.join(ROOT_DIR, "JENGA_LIVINGROOM_S_T"), os.path.join(ROOT_DIR, "test", "JENGA_LIVINGROOM_S_T"))
  43. shutil.move(os.path.join(ROOT_DIR, "JENGA_OFFICE_H_T"), os.path.join(ROOT_DIR, "test", "JENGA_OFFICE_H_T"))
  44. shutil.move(os.path.join(ROOT_DIR, "PUZZLE_COURTYARD_H_T"), os.path.join(ROOT_DIR, "test", "PUZZLE_COURTYARD_H_T"))
  45. shutil.move(os.path.join(ROOT_DIR, "PUZZLE_LIVINGROOM_T_B"), os.path.join(ROOT_DIR, "test", "PUZZLE_LIVINGROOM_T_B"))
  46. shutil.move(os.path.join(ROOT_DIR, "CARDS_LIVINGROOM_S_H"), os.path.join(ROOT_DIR, "val", "CARDS_LIVINGROOM_S_H"))
  47. shutil.move(os.path.join(ROOT_DIR, "CHESS_COURTYARD_H_S"), os.path.join(ROOT_DIR, "val", "CHESS_COURTYARD_H_S"))
  48. shutil.move(os.path.join(ROOT_DIR, "JENGA_COURTYARD_T_S"), os.path.join(ROOT_DIR, "val", "JENGA_COURTYARD_T_S"))
  49. shutil.move(os.path.join(ROOT_DIR, "PUZZLE_OFFICE_S_T"), os.path.join(ROOT_DIR, "val", "PUZZLE_OFFICE_S_T"))
  50. train = ['CARDS_COURTYARD_H_S','CARDS_COURTYARD_S_H','CARDS_COURTYARD_T_B','CARDS_LIVINGROOM_B_T','CARDS_LIVINGROOM_H_S','CARDS_LIVINGROOM_T_B','CARDS_OFFICE_B_S','CARDS_OFFICE_H_T','CARDS_OFFICE_T_H','CHESS_COURTYARD_S_H','CHESS_COURTYARD_T_B','CHESS_LIVINGROOM_B_S','CHESS_LIVINGROOM_H_T','CHESS_LIVINGROOM_S_B','CHESS_OFFICE_B_S','CHESS_OFFICE_H_T','CHESS_OFFICE_S_B','CHESS_OFFICE_T_H','JENGA_COURTYARD_B_H','JENGA_COURTYARD_H_B','JENGA_COURTYARD_S_T','JENGA_LIVINGROOM_B_H','JENGA_LIVINGROOM_H_B','JENGA_LIVINGROOM_T_S','JENGA_OFFICE_B_S','JENGA_OFFICE_S_B','JENGA_OFFICE_T_H','PUZZLE_COURTYARD_B_S','PUZZLE_COURTYARD_S_B','PUZZLE_COURTYARD_T_H','PUZZLE_LIVINGROOM_B_T','PUZZLE_LIVINGROOM_H_S','PUZZLE_LIVINGROOM_S_H','PUZZLE_OFFICE_B_H','PUZZLE_OFFICE_H_B','PUZZLE_OFFICE_T_S']
  51. for folder in train:
  52. shutil.move(os.path.join(ROOT_DIR, folder), os.path.join(ROOT_DIR, "train", folder))
  53. def json_test():
  54. # test_dir = os.path.join(ROOT_DIR, "test")
  55. # os.makedirs(os.path.join(test_dir, "images"))
  56. # img_dir = os.path.join(test_dir, "images")
  57. # create_annotations(test_dir,img_dir)
  58. # val_dir = os.path.join(ROOT_DIR, "val")
  59. # os.makedirs(os.path.join(val_dir, "images"))
  60. # img_dir = os.path.join(val_dir, "images")
  61. # create_annotations(val_dir,img_dir)
  62. train_dir = os.path.join(ROOT_DIR, "train")
  63. # os.makedirs(os.path.join(train_dir, "images"))
  64. img_dir = os.path.join(train_dir, "images")
  65. create_annotations(train_dir,img_dir)
  66. def json_train_val():
  67. os.makedirs(os.path.join(ROOT_DIR, "tmp"))
  68. tmp_dir = os.path.join(ROOT_DIR, "tmp")
  69. os.makedirs(os.path.join(tmp_dir, "images"))
  70. img_dir = os.path.join(tmp_dir, "images")
  71. for dir_name in os.listdir(ROOT_DIR):
  72. if not (dir_name == "tmp" or dir_name == "test"):
  73. shutil.move(os.path.join(ROOT_DIR, dir_name), os.path.join(ROOT_DIR, tmp_dir, dir_name))
  74. create_annotations(tmp_dir, img_dir)
  75. def create_annotations(directory, img_dir):
  76. annotations = {}
  77. for dir_name in os.listdir(directory):
  78. if not (dir_name == "images"):
  79. for _, _, files in os.walk(os.path.join(directory, dir_name)):
  80. mat = scipy.io.loadmat(os.path.join(directory, dir_name, ANNOTATION_FILE))
  81. for i, img_file in enumerate(sorted(files)):
  82. if not (img_file.endswith(".mat")):
  83. new_img_file = dir_name + "_" + img_file
  84. image = {
  85. "name": new_img_file,
  86. "objects": []
  87. }
  88. for segmentation in mat["polygons"][0][i]:
  89. if segmentation.any():
  90. image["objects"].append(segmentation.tolist())
  91. annotations[new_img_file] = image
  92. shutil.move(os.path.join(directory, dir_name, img_file), os.path.join(img_dir, new_img_file))
  93. with open(os.path.join(directory, SAVE_FILE), 'w') as output_json_file:
  94. json.dump(annotations, output_json_file)
  95. for dir_name in os.listdir(directory):
  96. if not (dir_name == "images" or dir_name == "annotations.json"):
  97. shutil.rmtree(os.path.join(directory, dir_name))
  98. def split_train_val():
  99. tmp_dir = os.path.join(ROOT_DIR, "tmp")
  100. os.makedirs(os.path.join(ROOT_DIR, "train"))
  101. train_dir = os.path.join(ROOT_DIR, "train")
  102. os.makedirs(os.path.join(train_dir, "images"))
  103. os.makedirs(os.path.join(ROOT_DIR, "val"))
  104. val_dir = os.path.join(ROOT_DIR, "val")
  105. os.makedirs(os.path.join(val_dir, "images"))
  106. # Opening JSON file
  107. with open(os.path.join(tmp_dir, 'annotations.json')) as json_file:
  108. data = json.load(json_file)
  109. # 0.1765 is 15% van 100% omdat test al 20 % is (niet helemaal)
  110. val_keys = random.sample(list(data), round(len(data) * 0.1765))
  111. validation = {k: v for k, v in data.items() if k in val_keys}
  112. train = {k: v for k, v in data.items() if k not in val_keys}
  113. with open(os.path.join(val_dir, SAVE_FILE), 'w') as output_json_file:
  114. json.dump(validation, output_json_file)
  115. with open(os.path.join(train_dir, SAVE_FILE), 'w') as output_json_file:
  116. json.dump(train, output_json_file)
  117. for key, _ in validation.items():
  118. shutil.move(os.path.join(tmp_dir, "images", key), os.path.join(val_dir, "images", key))
  119. for key, _ in train.items():
  120. shutil.move(os.path.join(tmp_dir, "images", key), os.path.join(train_dir, "images"))
  121. shutil.rmtree(tmp_dir)
  122. def move_to_folder():
  123. os.makedirs(os.path.join(ROOT_DIR, "json"))
  124. json_dir = os.path.join(ROOT_DIR, "json")
  125. shutil.move(os.path.join(ROOT_DIR, "test"), json_dir)
  126. shutil.move(os.path.join(ROOT_DIR, "val"), json_dir)
  127. shutil.move(os.path.join(ROOT_DIR, "train"), json_dir)
  128. shutil.move(ROOT_DIR, "../data")
  129. # split_test()
  130. json_test()
  131. # json_train_val()
  132. # split_train_val()
  133. move_to_folder()