-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_2d.py
163 lines (120 loc) · 5.34 KB
/
project_2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
import numpy as np
from glob import glob
import pandas as pd
import os.path
import cv2
from tqdm import tqdm, trange
from collections import defaultdict
from scipy import signal
import queue
import threading
from aniposelib.cameras import CameraGroup
from .common import make_process_fun, get_nframes, \
get_video_name, get_cam_name, \
get_video_params, get_video_params_cap, \
get_data_length, natural_keys, true_basename, find_calibration_folder
from .triangulate import load_offsets_dict
from .filter_pose import write_pose_2d
def get_projected_points(config, pose_fname, cgroup, offsets_dict):
pose_data = pd.read_csv(pose_fname)
cols = [x for x in pose_data.columns if '_error' in x]
bodyparts = [c.replace('_error', '') for c in cols]
M = np.identity(3)
center = np.zeros(3)
for i in range(3):
center[i] = np.mean(pose_data['center_{}'.format(i)])
for j in range(3):
M[i, j] = np.mean(pose_data['M_{}{}'.format(i, j)])
bp_dict = dict(zip(bodyparts, range(len(bodyparts))))
all_points = np.array([np.array(pose_data.loc[:, (bp+'_x', bp+'_y', bp+'_z')])
for bp in bodyparts])
all_errors = np.array([np.array(pose_data.loc[:, bp+'_error'])
for bp in bodyparts])
all_scores = np.array([np.array(pose_data.loc[:, bp+'_score'])
for bp in bodyparts])
if config['triangulation']['optim']:
all_errors[np.isnan(all_errors)] = 0
else:
all_errors[np.isnan(all_errors)] = 10000
good = (all_errors < 50)
all_points[~good] = np.nan
n_joints, n_frames, _ = all_points.shape
n_cams = len(cgroup.cameras)
all_points_flat = all_points.reshape(-1, 3)
all_points_flat_t = (all_points_flat + center).dot(np.linalg.inv(M.T))
points_2d_proj_flat = cgroup.project(all_points_flat_t)
points_2d_proj = points_2d_proj_flat.reshape(n_cams, n_joints, n_frames, 2)
cam_names = cgroup.get_names()
for cix, cname in enumerate(cam_names):
offset = offsets_dict[cname]
dx, dy = offset[0], offset[1]
points_2d_proj[cix, :, :, 0] -= dx
points_2d_proj[cix, :, :, 1] -= dy
return bodyparts, points_2d_proj, all_scores
def process_session(config, session_path):
pipeline_videos_raw = config['pipeline']['videos_raw']
pipeline_pose_3d = config['pipeline']['pose_3d']
pipeline_pose_2d_projected = config['pipeline']['pose_2d_projected']
video_ext = config['video_extension']
vid_fnames_2d = glob(os.path.join(
session_path, pipeline_videos_raw, "*."+video_ext))
vid_fnames_2d = sorted(vid_fnames_2d, key=natural_keys)
pose_fnames_3d = glob(os.path.join(
session_path, pipeline_pose_3d, "*.csv"))
pose_fnames_3d = sorted(pose_fnames_3d, key=natural_keys)
if len(pose_fnames_3d) == 0:
return
fnames_2d = defaultdict(list)
for vid in vid_fnames_2d:
vidname = get_video_name(config, vid)
fnames_2d[vidname].append(vid)
fnames_3d = defaultdict(list)
for fname in pose_fnames_3d:
vidname = true_basename(fname)
fnames_3d[vidname].append(fname)
cgroup = None
calib_folder = find_calibration_folder(config, session_path)
if calib_folder is not None:
calib_fname = os.path.join(calib_folder,
config['pipeline']['calibration_results'],
'calibration.toml')
if os.path.exists(calib_fname):
cgroup = CameraGroup.load(calib_fname)
if cgroup is None:
print('session {}: no calibration found, skipping'.format(session_path))
return
outdir = os.path.join(session_path, pipeline_pose_2d_projected)
os.makedirs(outdir, exist_ok=True)
for pose_fname in pose_fnames_3d:
basename = true_basename(pose_fname)
if len(fnames_2d[basename]) == 0:
print(pose_fname, 'missing raw videos')
continue
fname_3d_current = pose_fname
fnames_2d_current = fnames_2d[basename]
fnames_2d_current = sorted(fnames_2d_current, key=natural_keys)
out_fnames = [os.path.join(outdir, true_basename(fname) + '.h5')
for fname in fnames_2d_current]
if all([os.path.exists(f) for f in out_fnames]):
continue
print(pose_fname)
cam_names = [get_cam_name(config, fname)
for fname in fnames_2d_current]
video_folder = os.path.join(session_path, pipeline_videos_raw)
offsets_dict = load_offsets_dict(config, cam_names, video_folder)
cgroup_subset = cgroup.subset_cameras_names(cam_names)
bodyparts, points_2d_proj, all_scores = get_projected_points(
config, fname_3d_current, cgroup_subset, offsets_dict)
metadata = {
'scorer': 'scorer',
'bodyparts': bodyparts,
'index': np.arange(points_2d_proj.shape[2])
}
n_cams, n_joints, n_frames, _ = points_2d_proj.shape
pts = np.zeros((n_frames, n_joints, 3), dtype='float64')
for cix, (cname, outname) in enumerate(zip(cam_names, out_fnames)):
pts[:, :, :2] = points_2d_proj[cix].swapaxes(0, 1)
pts[:, :, 2] = all_scores.T
write_pose_2d(pts, metadata, outname)
project_2d_all = make_process_fun(process_session)