-
Notifications
You must be signed in to change notification settings - Fork 19
/
plot.py
184 lines (150 loc) · 7.35 KB
/
plot.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
from matplotlib import pyplot as plt
import matplotlib
import cv2 as cv
import numpy as np
import math
from config import config
from post_proc import get_keypoints
def visualize_short_offsets(offsets, keypoint_id, centers=None, heatmaps=None, radius=config.KP_RADIUS, img=None, every=1,save_path='./'):
if centers is None and heatmaps is None:
raise ValueError('either keypoint locations or heatmaps must be provided')
if isinstance(keypoint_id, str):
if not keypoint_id in config.KEYPOINTS:
raise ValueError('{} not a valid keypoint name'.format(keypoint_id))
else:
keypoint_id = config.KEYPOINTS.index(keypoint_id)
if centers is None:
kp = get_keypoints(heatmaps)
kp = [k for k in kp if k['id']==keypoint_id]
centers = [k['xy'].tolist() for k in kp]
kp_offsets = offsets[:,:,2*keypoint_id:2*keypoint_id+2]
masks = np.zeros(offsets.shape[:2]+(len(centers),), dtype='bool')
idx = np.rollaxis(np.indices(offsets.shape[1::-1]), 0, 3).transpose((1,0,2))
for j, c in enumerate(centers):
dists = np.sqrt(np.square(idx-c).sum(axis=-1))
dists_x = np.abs(idx[:,:,0] - c[0])
dists_y = np.abs(idx[:,:,1] - c[1])
masks[:,:,j] = (dists<=radius)
if every > 1:
d_mask = np.logical_and(np.mod(dists_x.astype('int32'), every)==0, np.mod(dists_y.astype('int32'), every)==0)
masks[:,:,j] = np.logical_and(masks[:,:,j], d_mask)
mask = masks.sum(axis=-1) > 0
# for j, c in enumerate(centers):
# dists[:,:,j] = np.sqrt(np.square(idx-c).sum(axis=-1))
# dists = dists.min(axis=-1)
# mask = dists <= radius
I, J = np.nonzero(mask)
plt.figure()
if img is not None:
plt.imshow(img)
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['figure.dpi'] = 200
plt.quiver(J, I, kp_offsets[I,J,0], kp_offsets[I,J,1], color='r', angles='xy', scale_units='xy', scale=1)
plt.savefig('./demo_result/short_offsets.jpg',bbox_inches = 'tight')
def visualize_mid_offsets(offsets, from_kp, to_kp, centers=None, heatmaps=None, radius=config.KP_RADIUS, img=None, every=1,save_path='./'):
if centers is None and heatmaps is None:
raise ValueError('either keypoint locations or heatmaps must be provided')
if isinstance(from_kp, str):
if not from_kp in config.KEYPOINTS:
raise ValueError('{} not a valid keypoint name'.format(from_kp))
else:
from_kp = config.KEYPOINTS.index(from_kp)
if isinstance(to_kp, str):
if not to_kp in config.KEYPOINTS:
raise ValueError('{} not a valid keypoint name'.format(to_kp))
else:
to_kp = config.KEYPOINTS.index(to_kp)
edge_list = config.EDGES + [edge[::-1] for edge in config.EDGES]
edge_id = edge_list.index((from_kp, to_kp))
if centers is None:
kp = get_keypoints(heatmaps)
kp = [k for k in kp if k['id']==from_kp]
centers = [k['xy'].tolist() for k in kp]
kp_offsets = offsets[:,:,2*edge_id:2*edge_id+2]
# dists = np.zeros(offsets.shape[:2]+(len(centers),))
masks = np.zeros(offsets.shape[:2]+(len(centers),), dtype='bool')
idx = np.rollaxis(np.indices(offsets.shape[1::-1]), 0, 3).transpose((1,0,2))
for j, c in enumerate(centers):
dists = np.sqrt(np.square(idx-c).sum(axis=-1))
dists_x = np.abs(idx[:,:,0] - c[0])
dists_y = np.abs(idx[:,:,1] - c[1])
masks[:,:,j] = (dists<=radius)
if every > 1:
d_mask = np.logical_and(np.mod(dists_x.astype('int32'), every)==0, np.mod(dists_y.astype('int32'), every)==0)
masks[:,:,j] = np.logical_and(masks[:,:,j], d_mask)
mask = masks.sum(axis=-1) > 0
# dists = dists.min(axis=-1)
# mask = dists <= radius
I, J = np.nonzero(mask)
if img is not None:
plt.imshow(img)
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['figure.dpi'] = 200
plt.quiver(J, I, kp_offsets[I,J,0], kp_offsets[I,J,1], color='r', angles='xy', scale_units='xy', scale=1)
plt.savefig(save_path+'middle_offsets.jpg',bbox_inches = 'tight')
def visualize_long_offsets(offsets, keypoint_id, seg_mask, img=None, every=1,save_path='./'):
if isinstance(keypoint_id, str):
if not keypoint_id in config.KEYPOINTS:
raise ValueError('{} not a valid keypoint name'.format(keypoint_id))
else:
keypoint_id = config.KEYPOINTS.index(keypoint_id)
idx = np.rollaxis(np.indices(offsets.shape[1::-1]), 0, 3).transpose((1,0,2))
kp_offsets = offsets[:,:,2*keypoint_id:2*keypoint_id+2]
mask = seg_mask[:,:,0]>0.5
mask = np.logical_and(mask, np.mod(idx[:,:,0], every)==0)
mask = np.logical_and(mask, np.mod(idx[:,:,1], every)==0)
I, J = np.nonzero(mask)
if img is not None:
plt.imshow(img)
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['figure.dpi'] = 200
plt.quiver(J, I, kp_offsets[I,J,0], kp_offsets[I,J,1], color='r', angles='xy', scale_units='xy', scale=1)
plt.savefig(save_path+'long_offsets.jpg',bbox_inches = 'tight')
def apply_mask(img, mask, color, alpha=0.5):
image = img.copy()
for c in range(3):
image[:, :, c] = np.where(mask == 1,
image[:, :, c] *
(1 - alpha) + alpha * color[c] * 255,
image[:, :, c])
return image
def plot_instance_masks(masks, img,save_path='./'):
canvas = img.copy()
for mask in masks:
color = [np.random.uniform() for _ in range(3)]
canvas = apply_mask(canvas, mask, color, alpha=0.75)
plt.imsave(save_path+'instances_masks.jpg',canvas)
def plot_poses(img, skeletons, save_path=None):
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0], [0, 255, 0], \
[0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], \
[170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]
cmap = matplotlib.cm.get_cmap('hsv')
plt.figure()
#img = img.astype('uint8')
canvas = img.copy()
for i in range(17):
rgba = np.array(cmap(1 - i/17. - 1./34))
rgba[0:3] *= 255
for j in range(len(skeletons)):
cv.circle(canvas, tuple(skeletons[j][i, 0:2].astype('int32')), 2, colors[i], thickness=-1)
to_plot = cv.addWeighted(img, 0.3, canvas, 0.7, 0)
plt.imshow(to_plot[:,:,[2,1,0]])
fig = matplotlib.pyplot.gcf()
stickwidth = 2
for i in range(config.NUM_EDGES):
for j in range(len(skeletons)):
edge = config.EDGES[i]
if skeletons[j][edge[0],2] == 0 or skeletons[j][edge[1],2] == 0:
continue
cur_canvas = canvas.copy()
X = [skeletons[j][edge[0], 1], skeletons[j][edge[1], 1]]
Y = [skeletons[j][edge[0], 0], skeletons[j][edge[1], 0]]
mX = np.mean(X)
mY = np.mean(Y)
length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5
angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
polygon = cv.ellipse2Poly((int(mY),int(mX)), (int(length/2), stickwidth), int(angle), 0, 360, 1)
cv.fillConvexPoly(cur_canvas, polygon, colors[i])
canvas = cv.addWeighted(canvas, 0.4, cur_canvas, 0.6, 0)
plt.imsave(save_path+'pose.jpg',canvas[:,:,:])
fig = matplotlib.pyplot.gcf()