今日做人体姿态估计时,对代码进行整合查看效果
但是发现不同的数据集具有不同的关键点信息
左图中的18个关键点分别为:
neck 颈部、脖子 1
l-shoulder 左肩膀 5
r-shoulder 右肩膀 2
l-elbow 左胳膊肘 6
r-elbow 右胳膊肘 3
l-wrist 左手腕 7
r-wrist 右手腕 4
r-hip 右臀部、髋部 8
l-hip 左臀部、髋部 11
r-knee 右膝盖 9
l-knee 左膝盖 12
r-ankle 右脚踝 10
l-ankle 左脚踝 13
nose 鼻子 0
l-ear 左耳 17
r-ear 右耳 16
l-eye 左眼 15
r-eye 右眼 14
右图中的17关键点分别为:
0 hip
1 r-hip
2 r-knee
3 r-ankle
4 l-hip
5 l-knee
6 l-ankle
7 chest
8 neck
9 chin
10 head
11 l-shoulder
12 l-elbow
13 l-wrist
14 r-shoulder
15 r-elbow
16 r-wrist
我们在进行转换时可以使用下面的简约转换:
chest 胸部、胸膛 = (neck + hip)/2
chin 下巴 = (nose + neck)/2
head 头 = (l-eye + r-eye)/2
hip 臀中央 = (r-hip +l-hip )/2
下面给出18关键点转换为17关键点的程序:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
plt.grid(ls='--')
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
ax2.invert_yaxis()
ax1.invert_yaxis()
keypoint = np.array([[496, 48],
[500, 90],
[466, 93],
[421, 127],
[369, 135],
[534, 86],
[579, 123],
[639, 135],
[481, 195],
[470, 315],
[451, 405],
[526, 198],
[519, 315],
[511, 412],
[485, 37],
[500, 37],
[474, 48],
[511, 41]])
print(keypoint.shape)
for i in range(18):
points = ax2.scatter(*keypoint[i], 10, color='red', edgecolors='white', zorder=10)
# 关键点转换
new_keypoints = np.array([(keypoint[8] + keypoint[11]) // 2, keypoint[8], keypoint[9], keypoint[10],
keypoint[11], keypoint[12], keypoint[13],
(keypoint[1] + (keypoint[8] + keypoint[11]) / 2) // 2,
keypoint[1], (keypoint[0] + keypoint[1]) // 2, (keypoint[14] + keypoint[15]) // 2,
keypoint[5], keypoint[6], keypoint[7], keypoint[2], keypoint[3], keypoint[4]])
for i in range(17):
points = ax1.scatter(*new_keypoints[i], 10, color='red', edgecolors='white', zorder=10)
plt.show()
代码中的new_keypoints是对应的由18关键点转换成的17关键点,当然这种转换也并不是非常准确的方式,只能说是一种可行的方式。
另外说一下在编程绘制关键点连线时我们经常会遇到类似如下代码:
skeleton = Skeleton(parents=[-1, 0, 1, 2, 3, 4, 0, 6, 7, 8, 9, 0,
11, 12, 13, 14, 12, 16, 17, 18, 19, 20, 19, 22, 12, 24,
25, 26, 27, 28, 27, 30],
joints_left=[6, 7, 8, 9, 10, 16, 17, 18, 19, 20, 21, 22, 23],
joints_right=[1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29, 30, 31])
上面有一个parents的东西,其实这个是一个父节点的意思,方便我们绘图用的。
在绘制关键点之间的连线时,我们要只要哪两个点之间要连接,此时如果有一个关键点数组为keypoint1[],有一个parents数组,那么我们可以使用scatter(keypoint1[i], keypoint1[parents[i]])进行关键点的绘制。
如:
for j, j_parent in enumerate(parents): # 画2D连接线
# print(j)
if j_parent == -1:
continue
lines[j-1][0].set_data([input_2d_image[0, j, 0], input_2d_image[0, j_parent, 0]],
[input_2d_image[0, j, 1], input_2d_image[0, j_parent, 1]])
请注意如果parents为-1,说明这个节点就是根节点,只有子节点才不为-1。