ConvertHumanEva.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. function [] = ConvertDataset()
  8. N_JOINTS = 15; % Set to 20 if you want to export a 20-joint skeleton
  9. function [pose_out] = ExtractPose15(pose, dimensions)
  10. % We use the same 15-joint skeleton as in the evaluation
  11. % script "@body_pose/error.m". Proximal and Distal joints
  12. % are averaged.
  13. pose_out = NaN(15, dimensions);
  14. pose_out(1, :) = pose.torsoDistal; % Pelvis (root)
  15. pose_out(2, :) = (pose.torsoProximal + pose.headProximal) / 2; % Thorax
  16. pose_out(3, :) = pose.upperLArmProximal; % Left shoulder
  17. pose_out(4, :) = (pose.upperLArmDistal + pose.lowerLArmProximal) / 2; % Left elbow
  18. pose_out(5, :) = pose.lowerLArmDistal; % Left wrist
  19. pose_out(6, :) = pose.upperRArmProximal; % Right shoulder
  20. pose_out(7, :) = (pose.upperRArmDistal + pose.lowerRArmProximal) / 2; % Right elbow
  21. pose_out(8, :) = pose.lowerRArmDistal; % Right wrist
  22. pose_out(9, :) = pose.upperLLegProximal; % Left hip
  23. pose_out(10, :) = (pose.upperLLegDistal + pose.lowerLLegProximal) / 2; % Left knee
  24. pose_out(11, :) = pose.lowerLLegDistal; % Left ankle
  25. pose_out(12, :) = pose.upperRLegProximal; % Right hip
  26. pose_out(13, :) = (pose.upperRLegDistal + pose.lowerRLegProximal) / 2; % Right knee
  27. pose_out(14, :) = pose.lowerRLegDistal; % Right ankle
  28. pose_out(15, :) = pose.headDistal; % Head
  29. end
  30. function [pose_out] = ExtractPose20(pose, dimensions)
  31. pose_out = NaN(20, dimensions);
  32. pose_out(1, :) = pose.torsoDistal; % Pelvis (root)
  33. pose_out(2, :) = pose.torsoProximal;
  34. pose_out(3, :) = pose.headProximal;
  35. pose_out(4, :) = pose.upperLArmProximal; % Left shoulder
  36. pose_out(5, :) = pose.upperLArmDistal;
  37. pose_out(6, :) = pose.lowerLArmProximal;
  38. pose_out(7, :) = pose.lowerLArmDistal; % Left wrist
  39. pose_out(8, :) = pose.upperRArmProximal; % Right shoulder
  40. pose_out(9, :) = pose.upperRArmDistal;
  41. pose_out(10, :) = pose.lowerRArmProximal;
  42. pose_out(11, :) = pose.lowerRArmDistal; % Right wrist
  43. pose_out(12, :) = pose.upperLLegProximal; % Left hip
  44. pose_out(13, :) = pose.upperLLegDistal;
  45. pose_out(14, :) = pose.lowerLLegProximal;
  46. pose_out(15, :) = pose.lowerLLegDistal; % Left ankle
  47. pose_out(16, :) = pose.upperRLegProximal; % Right hip
  48. pose_out(17, :) = pose.upperRLegDistal;
  49. pose_out(18, :) = pose.lowerRLegProximal;
  50. pose_out(19, :) = pose.lowerRLegDistal; % Right ankle
  51. pose_out(20, :) = pose.headDistal; % Head
  52. end
  53. addpath('./TOOLBOX_calib/');
  54. addpath('./TOOLBOX_common/');
  55. addpath('./TOOLBOX_dxAvi/');
  56. addpath('./TOOLBOX_readc3d/');
  57. % Create the output directory for the converted dataset
  58. OUT_DIR = ['./converted_', int2str(N_JOINTS), 'j'];
  59. warning('off', 'MATLAB:MKDIR:DirectoryExists');
  60. mkdir(OUT_DIR);
  61. % We use the validation set as the test set
  62. for SPLIT = {'Train', 'Validate'}
  63. mkdir([OUT_DIR, '/', SPLIT{1}]);
  64. CurrentDataset = he_dataset('HumanEvaI', SPLIT{1});
  65. for SEQ = 1:length(CurrentDataset)
  66. Subject = char(get(CurrentDataset(SEQ), 'SubjectName'));
  67. Action = char(get(CurrentDataset(SEQ), 'ActionType'));
  68. Trial = char(get(CurrentDataset(SEQ), 'Trial'));
  69. DatasetBasePath = char(get(CurrentDataset(SEQ), 'DatasetBasePath'));
  70. if Trial ~= '1'
  71. % We are only interested in fully-annotated data
  72. continue;
  73. end
  74. if strcmp(Action, 'ThrowCatch') && strcmp(Subject, 'S3')
  75. % Damaged mocap stream
  76. continue;
  77. end
  78. fprintf('Converting...\n')
  79. fprintf('\tSplit: %s\n', SPLIT{1});
  80. fprintf('\tSubject: %s\n', Subject);
  81. fprintf('\tAction: %s\n', Action);
  82. fprintf('\tTrial: %s\n', Trial);
  83. % Create subject directory if it does not exist
  84. mkdir([OUT_DIR, '/', SPLIT{1}, '/', Subject]);
  85. % Load the sequence
  86. [~, ~, MocapStream, MocapStream_Enabled] ...
  87. = sync_stream(CurrentDataset(SEQ));
  88. % Set frame range
  89. FrameStart = get(CurrentDataset(SEQ), 'FrameStart');
  90. FrameStart = [FrameStart{:}];
  91. FrameEnd = get(CurrentDataset(SEQ), 'FrameEnd');
  92. FrameEnd = [FrameEnd{:}];
  93. fprintf('\tNum. frames: %d\n', FrameEnd - FrameStart + 1);
  94. poses_3d = NaN(FrameEnd - FrameStart + 1, N_JOINTS, 3);
  95. poses_2d = NaN(3, FrameEnd - FrameStart + 1, N_JOINTS, 2);
  96. corrupt = 0;
  97. for FRAME = FrameStart:FrameEnd
  98. if (MocapStream_Enabled)
  99. [MocapStream, pose, ValidPose] = cur_frame(MocapStream, FRAME, 'body_pose');
  100. if (ValidPose)
  101. i = FRAME - FrameStart + 1;
  102. % Extract 3D pose
  103. if N_JOINTS == 15
  104. poses_3d(i, :, :) = ExtractPose15(pose, 3);
  105. else
  106. poses_3d(i, :, :) = ExtractPose20(pose, 3);
  107. end
  108. % Extract ground-truth 2D pose via camera
  109. % projection
  110. for CAM = 1:3
  111. if (CAM == 1)
  112. CameraName = 'C1';
  113. elseif (CAM == 2)
  114. CameraName = 'C2';
  115. elseif (CAM == 3)
  116. CameraName = 'C3';
  117. end
  118. CalibrationFilename = [DatasetBasePath, Subject, '/Calibration_Data/', CameraName, '.cal'];
  119. pose_2d = project2d(pose, CalibrationFilename);
  120. if N_JOINTS == 15
  121. poses_2d(CAM, i, :, :) = ExtractPose15(pose_2d, 2);
  122. else
  123. poses_2d(CAM, i, :, :) = ExtractPose20(pose_2d, 2);
  124. end
  125. end
  126. else
  127. corrupt = corrupt + 1;
  128. end
  129. end
  130. end
  131. fprintf('\n%d out of %d frames are damaged\n', corrupt, FrameEnd - FrameStart + 1);
  132. FileName = [OUT_DIR, '/', SPLIT{1}, '/', Subject, '/', Action, '_', Trial, '.mat'];
  133. save(FileName, 'poses_3d', 'poses_2d');
  134. fprintf('... saved to %s\n\n', FileName);
  135. end
  136. end
  137. end