prepare_data_2d_h36m_generic.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) 2018-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. #
  7. import argparse
  8. import os
  9. import zipfile
  10. import numpy as np
  11. import h5py
  12. import re
  13. from glob import glob
  14. from shutil import rmtree
  15. from data_utils import suggest_metadata, suggest_pose_importer
  16. import sys
  17. sys.path.append('../')
  18. from common.utils import wrap
  19. from itertools import groupby
  20. output_prefix_2d = 'data_2d_h36m_'
  21. cam_map = {
  22. '54138969': 0,
  23. '55011271': 1,
  24. '58860488': 2,
  25. '60457274': 3,
  26. }
  27. if __name__ == '__main__':
  28. if os.path.basename(os.getcwd()) != 'data':
  29. print('This script must be launched from the "data" directory')
  30. exit(0)
  31. parser = argparse.ArgumentParser(description='Human3.6M dataset converter')
  32. parser.add_argument('-i', '--input', default='', type=str, metavar='PATH', help='input path to 2D detections')
  33. parser.add_argument('-o', '--output', default='', type=str, metavar='PATH', help='output suffix for 2D detections (e.g. detectron_pt_coco)')
  34. args = parser.parse_args()
  35. if not args.input:
  36. print('Please specify the input directory')
  37. exit(0)
  38. if not args.output:
  39. print('Please specify an output suffix (e.g. detectron_pt_coco)')
  40. exit(0)
  41. import_func = suggest_pose_importer(args.output)
  42. metadata = suggest_metadata(args.output)
  43. print('Parsing 2D detections from', args.input)
  44. output = {}
  45. file_list = glob(args.input + '/S*/*.mp4.npz')
  46. for f in file_list:
  47. path, fname = os.path.split(f)
  48. subject = os.path.basename(path)
  49. assert subject.startswith('S'), subject + ' does not look like a subject directory'
  50. if '_ALL' in fname:
  51. continue
  52. m = re.search('(.*)\\.([0-9]+)\\.mp4\\.npz', fname)
  53. action = m.group(1)
  54. camera = m.group(2)
  55. camera_idx = cam_map[camera]
  56. if subject == 'S11' and action == 'Directions':
  57. continue # Discard corrupted video
  58. # Use consistent naming convention
  59. canonical_name = action.replace('TakingPhoto', 'Photo') \
  60. .replace('WalkingDog', 'WalkDog')
  61. keypoints = import_func(f)
  62. assert keypoints.shape[1] == metadata['num_joints']
  63. if subject not in output:
  64. output[subject] = {}
  65. if canonical_name not in output[subject]:
  66. output[subject][canonical_name] = [None, None, None, None]
  67. output[subject][canonical_name][camera_idx] = keypoints.astype('float32')
  68. print('Saving...')
  69. np.savez_compressed(output_prefix_2d + args.output, positions_2d=output, metadata=metadata)
  70. print('Done.')