-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypoints_viz.py
155 lines (137 loc) · 5.39 KB
/
keypoints_viz.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
import os
import cv2
import numpy as np
import pyrender
import reconstruction
import torch
from constants import JOINT_NAMES
from PIL import Image
from video_scene import VideoScene
if __name__ == "__main__":
model_folder = "./models"
model_type = "smplx"
exercise = "bells"
method = "align_3d"
index_frame = 1
index_video = 90
animation = True
show_mesh = False
project_all_vertices = True
if not animation:
video_scene = VideoScene(exercise=exercise, index_video=index_video)
img, img_data, infos = video_scene.load_frame(index_frame)
gender = infos["avatar_presenting_gender"]
betas = torch.tensor(infos["avatar_betas"], dtype=torch.float32).unsqueeze(0)
poses = reconstruction.get_poses(img_data)
smplx_model = reconstruction.get_smplx_model(model_folder, gender, betas, poses)
vertices, joints = reconstruction.get_vertices_and_joints(smplx_model, betas)
augmented_vertices = reconstruction.get_augmented_vertices(vertices)
if show_mesh:
scene = pyrender.Scene()
viewer = pyrender.Viewer(
scene, run_in_thread=True, use_raymond_lighting=True
)
reconstruction.show_mesh(
scene, viewer, vertices, augmented_vertices, smplx_model, joints
)
if project_all_vertices:
projected_vertices = video_scene.compute_2d_projection(
joints, vertices, method=method
)
else:
projected_vertices = video_scene.compute_2d_projection(
joints, augmented_vertices, method=method
)
image_points = np.array(
[
[
video_scene.current_ann["armature_keypoints"][joint_name]["x"],
video_scene.current_ann["armature_keypoints"][joint_name]["y"],
]
for joint_name in JOINT_NAMES[:55]
],
dtype=np.float32,
)
for point in projected_vertices:
img = cv2.circle(
np.array(img), (int(point[0]), int(point[1])), 0, (0, 0, 255), -1
)
im = Image.fromarray(cv2.cvtColor(np.uint8(img), cv2.COLOR_BGR2RGB))
im.show()
if animation:
video_scene = VideoScene(exercise=exercise, index_video=index_video)
img, img_data, infos = video_scene.load_frame(index_frame)
image_dims = (512, 512)
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
if project_all_vertices:
output_path = os.path.join(f"output/{exercise}", f"example_{method}.mp4")
else:
output_path = os.path.join(
f"output/{exercise}", f"example_{method}_augmented.mp4"
)
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
out = cv2.VideoWriter(output_path, fourcc, video_scene.fps, image_dims)
if show_mesh:
nodes = []
while True:
print(index_frame)
gender = infos["avatar_presenting_gender"]
betas = torch.tensor(infos["avatar_betas"], dtype=torch.float32).unsqueeze(
0
)
poses = reconstruction.get_poses(img_data)
smplx_model = reconstruction.get_smplx_model(
model_folder, gender, betas, poses
)
vertices, joints = reconstruction.get_vertices_and_joints(
smplx_model, betas
)
augmented_vertices = reconstruction.get_augmented_vertices(vertices)
if show_mesh:
scene = pyrender.Scene()
viewer = pyrender.Viewer(
scene, run_in_thread=True, use_raymond_lighting=True
)
nodes = reconstruction.show_mesh(
scene,
viewer,
vertices,
augmented_vertices,
smplx_model,
joints,
nodes=nodes,
)
if project_all_vertices:
projected_vertices = video_scene.compute_2d_projection(
joints, vertices, method=method
)
else:
projected_vertices = video_scene.compute_2d_projection(
joints, augmented_vertices, method=method
)
print("reprojection accuracy", video_scene.compute_reprojection_accuracy())
image_points = np.array(
[
[
video_scene.current_ann["armature_keypoints"][joint_name]["x"],
video_scene.current_ann["armature_keypoints"][joint_name]["y"],
]
for joint_name in JOINT_NAMES[:55]
],
dtype=np.float32,
)
point_size = -1 if project_all_vertices else 3
for point in projected_vertices:
img = cv2.circle(
np.array(img),
(int(point[0]), int(point[1])),
0,
(0, 0, 255),
point_size,
)
index_frame += 1
out.write(img)
print("Mean accuracy", video_scene.get_mean_accuracy())
img, img_data, infos = video_scene.load_frame(index_frame)
out.release()