-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_videos.py
165 lines (122 loc) · 4.76 KB
/
label_videos.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
#!/usr/bin/env python3
import os.path
import numpy as np
from glob import glob
import pandas as pd
import cv2
import skvideo.io
from tqdm import trange
from matplotlib.pyplot import get_cmap
from .common import make_process_fun, natural_keys, get_nframes
def connect(img, points, bps, bodyparts, col=(0,255,0,255)):
try:
ixs = [bodyparts.index(bp) for bp in bps]
except ValueError:
return
for a, b in zip(ixs, ixs[1:]):
if np.any(np.isnan(points[[a,b]])):
continue
pa = tuple(np.int32(points[a]))
pb = tuple(np.int32(points[b]))
cv2.line(img, tuple(pa), tuple(pb), col, 4)
def connect_all(img, points, scheme, bodyparts):
cmap = get_cmap('tab10')
for cnum, bps in enumerate(scheme):
col = cmap(cnum % 10, bytes=True)
col = [int(c) for c in col]
connect(img, points, bps, bodyparts, col)
def label_frame(img, points, scheme, bodyparts, cmap='tab10'):
n_joints, _ = points.shape
cmap_c = get_cmap(cmap)
connect_all(img, points, scheme, bodyparts)
for lnum, (x, y) in enumerate(points):
if np.isnan(x) or np.isnan(y):
continue
x = np.clip(x, 1, img.shape[1]-1)
y = np.clip(y, 1, img.shape[0]-1)
x = int(round(x))
y = int(round(y))
# col = cmap_c(lnum % 10, bytes=True)
# col = [int(c) for c in col]
col = (255, 255, 255)
cv2.circle(img,(x,y), 7, col[:3], -1)
return img
def visualize_labels(config, labels_fname, vid_fname, outname):
try:
scheme = config['labeling']['scheme']
except KeyError:
scheme = []
if isinstance(labels_fname, str):
dlabs = pd.read_hdf(labels_fname)
elif isinstance(labels_fname, pd.DataFrame):
dlabs = labels_fname
else:
raise TypeError('visualize_labels could not understand type for labels: {}', type(labels_fname))
if len(dlabs.columns.levels) > 2:
scorer = dlabs.columns.levels[0][0]
dlabs = dlabs.loc[:, scorer]
if len(scheme) == 0:
bodyparts = list(dlabs.columns.levels[0])
else:
bodyparts = sorted(set([x for dx in scheme for x in dx]))
cap = cv2.VideoCapture(vid_fname)
# cap.set(1,0)
fps = cap.get(cv2.CAP_PROP_FPS)
writer = skvideo.io.FFmpegWriter(outname, inputdict={
# '-hwaccel': 'auto',
'-framerate': str(fps),
}, outputdict={
'-vcodec': 'h264', '-qp': '28',
'-pix_fmt': 'yuv420p', # to support more players
'-vf': 'pad=ceil(iw/2)*2:ceil(ih/2)*2' # to handle width/height not divisible by 2
})
last = len(dlabs)
cmap = get_cmap('tab10')
points = [(dlabs[bp]['x'], dlabs[bp]['y']) for bp in bodyparts]
points = np.array(points)
scores = [dlabs[bp]['likelihood'] for bp in bodyparts]
scores = np.array(scores)
scores[np.isnan(scores)] = 0
scores[np.isnan(points[:, 0])] = 0
good = np.array(scores) > 0.1
points[:, 0, :][~good] = np.nan
points[:, 1, :][~good] = np.nan
all_points = points
for ix in trange(last, ncols=70):
ret, frame = cap.read()
if not ret:
break
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
points = all_points[:, :, ix]
img = label_frame(img, points, scheme, bodyparts)
writer.writeFrame(img)
cap.release()
writer.close()
def process_session(config, session_path, filtered=False):
pipeline_videos_raw = config['pipeline']['videos_raw']
if filtered:
pipeline_videos_labeled = config['pipeline']['videos_labeled_2d_filter']
pipeline_pose = config['pipeline']['pose_2d_filter']
else:
pipeline_videos_labeled = config['pipeline']['videos_labeled_2d']
pipeline_pose = config['pipeline']['pose_2d']
video_ext = config['video_extension']
print(session_path)
labels_fnames = glob(os.path.join(session_path, pipeline_pose, '*.h5'))
labels_fnames = sorted(labels_fnames, key=natural_keys)
outdir = os.path.join(session_path, pipeline_videos_labeled)
if len(labels_fnames) > 0:
os.makedirs(outdir, exist_ok=True)
for fname in labels_fnames:
basename = os.path.basename(fname)
basename = os.path.splitext(basename)[0]
out_fname = os.path.join(outdir, basename+'.mp4')
vidname = os.path.join(session_path, pipeline_videos_raw, basename+'.'+video_ext)
if os.path.exists(vidname):
if os.path.exists(out_fname) and \
abs(get_nframes(out_fname) - get_nframes(vidname)) < 100:
continue
print(out_fname)
visualize_labels(config, fname, vidname, out_fname)
label_videos_all = make_process_fun(process_session, filtered=False)
label_videos_filtered_all = make_process_fun(process_session, filtered=True)