-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
194 lines (161 loc) · 6.25 KB
/
player.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# import sys
# sys.path.append("..")
import argparse
import cv2
import imageio
import numpy as np
import matplotlib.pyplot as plt
from common import utils
from common.camera import Camera
from common.motion_capture import MotionCapture
from matplotlib.animation import FuncAnimation
from data_player.visualizer.motion_capture_visualizer import MotionCaptureVisualizer
from data_player.visualizer.pose_3d_visualizer import Pose3DVisualizer
def get_flags():
"""Get command flags.
:return: flags
:rtype: argparse.ArgumentParser
"""
parser = argparse.ArgumentParser()
parser.add_argument('--extrinsic_data',
help='Path to the camera\'s extrinsic parameters.',
default='../data/Calib/Extrinsics_PG1.npz',
type=str)
parser.add_argument('--camera_data',
help='Path to the camera\'s parameters file.',
default='../data/Calib/cameraParams_PG1.npz',
type=str)
parser.add_argument('--motion_capture_data',
help='Path to the motion capture file (.npz).',
default='../data/output/F_amass_Subject_1_1.npz',
type=str)
parser.add_argument('--video_file',
help='Path to the video file.',
default='../data/output/F_PG1_Subject_1_L_1.avi',
type=str)
parser.add_argument('--output_video_file',
help='Path to the output video file (e.g., ../output/output.avi).',
type=str)
return parser
def display_window(video_file_path, image_points):
"""Display the player and run a video.
:param video_file_path: path to the video file
:type video_file_path: str
:param image_points: points for painting
:type image_points: np.ndarray
"""
cap = cv2.VideoCapture(video_file_path)
current_frame_num = 0
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame_points = image_points[current_frame_num]
for idx, val in enumerate(frame_points):
frame = cv2.circle(
frame,
(val[0], val[1]),
radius=2,
color=(124, 252, 0),
lineType=cv2.LINE_AA,
thickness=-1
)
# Display the resulting frame
cv2.imshow('Motion Capture', frame)
current_frame_num = current_frame_num + 1
# Close program by using key
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
def save_video(output_video_file_path, video_file_path, image_points):
"""Save results into a video file.
:param output_video_file_path: path to the output video file
:type output_video_file_path: str
:param video_file_path: path to the video file
:type video_file_path: str
:param image_points: points for painting
:type image_points: np.ndarray
"""
cap = cv2.VideoCapture(video_file_path)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter(output_video_file_path, fourcc, fps, (width, height))
current_frame_num = 0
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame_points = image_points[current_frame_num]
for idx, val in enumerate(frame_points):
frame = cv2.circle(
frame,
(val[0], val[1]),
radius=2,
color=(124, 252, 0),
lineType=cv2.LINE_AA,
thickness=-1
)
video.write(frame)
current_frame_num = current_frame_num + 1
else:
break
cap.release()
video.release()
def run_opencv_player(camera, motion_capture, video_file_path, **kwargs):
"""Run motion capture player. Based on OpenCV.
:param camera: camara params
:type camera: Camera
:param motion_capture: motion capture data
:type motion_capture: MotionCapture
:param video_file_path: path to the video file
:type video_file_path: str
:key output_video_file_path: path to the video file, optional
"""
image_points = utils.adapt_motion_data_for_video(motion_capture, camera)
display_window(video_file_path, image_points)
output_video_file_path = kwargs.get('output_video_file_path', None)
if output_video_file_path:
save_video(output_video_file_path, video_file_path, image_points)
def run_3d_player(motion_capture, video_file_path, camera):
"""Show motion capture date in a 3d plot.
:param motion_capture: motion capture data
:type motion_capture: MotionCapture
:param video_file_path: path to the video file
:type video_file_path: str
:param camera: camara params
:type camera: Camera
"""
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
video = imageio.get_reader(video_file_path, 'ffmpeg')
motion_capture_visualizer = MotionCaptureVisualizer(fig, ax1, motion_capture, video, camera)
ax2 = fig.add_subplot(2, 1, 2, projection='3d')
pose_visualizer = Pose3DVisualizer(fig, ax2, motion_capture)
fps = 30
def update(frame):
motion_capture_visualizer.update(frame)
pose_visualizer.update(frame)
frames = np.arange(0, motion_capture.get_joints_reduced_by_fps(fps).shape[0])
interval = motion_capture.joints.shape[0] / fps
anim = FuncAnimation(
fig,
update,
frames=frames,
interval=interval,
repeat=True,
)
plt.show(block=True)
if __name__ == '__main__':
args = get_flags().parse_args()
camera_params = utils.read_camera_params(args.extrinsic_data, args.camera_data)
motion_capture_data = utils.read_motion_capture_data(args.motion_capture_data)
run_3d_player(motion_capture_data, args.video_file, camera_params)
run_opencv_player(
camera_params,
motion_capture_data,
args.video_file,
output_video_file_path=args.output_video_file
)