def load_trainset(data_root, out_dir):
    """Load training data, create annotation file and camera file.
    Args:
        data_root: Directory of dataset, which is organized in the following
            hierarchy:
                data_root
                |-- train
                    |-- S1
                        |-- Seq1
                        |-- Seq2
                    |-- S2
                    |-- ...
                |-- test
                    |-- TS1
                    |-- TS2
                    |-- ...
        out_dir: Directory to save annotation file.
    """
    _imgnames = []
    _centers = []
    _scales = []
    _joints_2d = []
    _joints_3d = []
    cameras = {}

    img_dir = join(out_dir, 'images')
    os.makedirs(img_dir, exist_ok=True)
    annot_dir = join(out_dir, 'annotations')
    os.makedirs(annot_dir, exist_ok=True)

    for subj in train_subjects:
        for seq in train_seqs:
            seq_path = join(data_root, 'train', f'S{subj}', f'Seq{seq}')
            num_frames = train_frame_nums[(subj, seq)]

            # load camera parametres
            camera_file = join(seq_path, 'camera.calibration')
            with open(camera_file, 'r') as fin:
                lines = fin.readlines()
                for cam in train_cams:
                    K = [float(s) for s in lines[cam * 7 + 5][11:-2].split()]
                    f = np.array([[K[0]], [K[5]]])
                    c = np.array([[K[2]], [K[6]]])
                    RT = np.array(
                        [float(s) for s in lines[cam * 7 + 6][11:-2].split()])
                    RT = np.reshape(RT, (4, 4))
                    R = RT[:3, :3]
                    # convert unit from millimeter to meter
                    T = RT[:3, 3:] * 0.001
                    size = [int(s) for s in lines[cam * 7 + 3][14:].split()]
                    w, h = size
                    cam_param = dict(
                        R=R, T=T, c=c, f=f, w=w, h=h, name=f'train_cam_{cam}')
                    cameras[f'S{subj}_Seq{seq}_Cam{cam}'] = cam_param

            # load annotations
            annot_file = os.path.join(seq_path, 'annot.mat')
            annot2 = loadmat(annot_file)['annot2']
            annot3 = loadmat(annot_file)['annot3']
            for cam in train_cams:
                # load 2D and 3D annotations
                joints_2d = np.reshape(annot2[cam][0][:num_frames],
                                       (num_frames, 28, 2))[:, joints_17]
                joints_3d = np.reshape(annot3[cam][0][:num_frames],
                                       (num_frames, 28, 3))[:, joints_17]
                joints_3d = joints_3d * 0.001
                centers, scales, joints_2d, joints_3d = get_annotations(
                    joints_2d, joints_3d)
                _centers.append(centers)
                _scales.append(scales)
                _joints_2d.append(joints_2d)
                _joints_3d.append(joints_3d)

                # extract frames from video
                video_path = join(seq_path, 'imageSequence',
                                  f'video_{cam}.avi')
                video = mmcv.VideoReader(video_path)
                for i in mmcv.track_iter_progress(range(num_frames)):
                    img = video.read()
                    if img is None:
                        break
                    imgname = f'S{subj}_Seq{seq}_Cam{cam}_{i+1:06d}.jpg'
                    _imgnames.append(imgname)
                    cv2.imwrite(join(img_dir, imgname), img)

    _imgnames = np.array(_imgnames)
    _centers = np.concatenate(_centers)
    _scales = np.concatenate(_scales)
    _joints_2d = np.concatenate(_joints_2d)
    _joints_3d = np.concatenate(_joints_3d)

    out_file = join(annot_dir, 'mpi_inf_3dhp_train.npz')
    np.savez(
        out_file,
        imgname=_imgnames,
        center=_centers,
        scale=_scales,
        part=_joints_2d,
        S=_joints_3d)
    print(f'Create annotation file for trainset: {out_file}. '
          f'{len(_imgnames)} samples in total.')

    out_file = join(annot_dir, 'cameras_train.pkl')
    with open(out_file, 'wb') as fout:
        pickle.dump(cameras, fout)
    print(f'Create camera file for trainset: {out_file}.')

    # get `mean` and `std` of pose data
    _joints_3d = _joints_3d[..., :3]  # remove visibility
    mean_3d, std_3d = get_pose_stats(_joints_3d)

    _joints_2d = _joints_2d[..., :2]  # remove visibility
    mean_2d, std_2d = get_pose_stats(_joints_2d)

    # centered around root
    _joints_3d_rel = _joints_3d - _joints_3d[..., root_index:root_index + 1, :]
    mean_3d_rel, std_3d_rel = get_pose_stats(_joints_3d_rel)
    mean_3d_rel[root_index] = mean_3d[root_index]
    std_3d_rel[root_index] = std_3d[root_index]

    _joints_2d_rel = _joints_2d - _joints_2d[..., root_index:root_index + 1, :]
    mean_2d_rel, std_2d_rel = get_pose_stats(_joints_2d_rel)
    mean_2d_rel[root_index] = mean_2d[root_index]
    std_2d_rel[root_index] = std_2d[root_index]

    stats = {
        'joint3d_stats': {
            'mean': mean_3d,
            'std': std_3d
        },
        'joint2d_stats': {
            'mean': mean_2d,
            'std': std_2d
        },
        'joint3d_rel_stats': {
            'mean': mean_3d_rel,
            'std': std_3d_rel
        },
        'joint2d_rel_stats': {
            'mean': mean_2d_rel,
            'std': std_2d_rel
        }
    }
    for name, stat_dict in stats.items():
        out_file = join(annot_dir, f'{name}.pkl')
        with open(out_file, 'wb') as f:
            pickle.dump(stat_dict, f)
        print(f'Create statistic data file: {out_file}')
Load training data, create annotation file and camera file.
    Args:
        data_root: Directory of dataset, which is organized in the following
            hierarchy:
                data_root
                |-- train
                    |-- S1
                        |-- Seq1
                        |-- Seq2
                    |-- S2
                    |-- ...
                |-- test
                    |-- TS1
                    |-- TS2
                    |-- ...
        out_dir: Directory to save annotation file.
    
mmpose/tools/dataset_converters/preprocess_mpi_inf_3dhp.py