-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplot.py
More file actions
73 lines (62 loc) · 2.29 KB
/
Copy pathplot.py
File metadata and controls
73 lines (62 loc) · 2.29 KB
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
import matplotlib.pyplot as plt
import matplotlib
import cv2 as cv
import numpy as np
import math
plt.switch_backend('agg')
def map_coco_to_personlab(keypoints):
permute = [0, 6, 8, 10, 5, 7, 9, 12, 14, 16, 11, 13, 15, 2, 1, 4, 3]
return keypoints[:, permute, :]
def plot_poses(img, skeletons, save_name='pose.jpg'):
EDGES = [
(0, 14),
(0, 13),
(0, 4),
(0, 1),
(14, 16),
(13, 15),
(4, 10),
(1, 7),
(10, 11),
(7, 8),
(11, 12),
(8, 9),
(4, 5),
(1, 2),
(5, 6),
(2, 3)
]
NUM_EDGES = len(EDGES)
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)
fig = matplotlib.pyplot.gcf()
stickwidth = 2
skeletons = map_coco_to_personlab(skeletons)
for i in range(NUM_EDGES):
for j in range(len(skeletons)):
edge = 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_name,canvas[:,:,:])
plt.close()