294 lines
8.9 KiB
Python
294 lines
8.9 KiB
Python
import os
|
|
|
|
import numpy as np
|
|
try:
|
|
import matplotlib.cm as mplcm
|
|
from matplotlib.animation import FuncAnimation
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
except ImportError:
|
|
pass
|
|
|
|
import openpifpaf
|
|
|
|
CAR_KEYPOINTS_24 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',
|
|
'13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']
|
|
|
|
CAR_SKELETON_24 = [[1, 2],
|
|
[1, 9],
|
|
[1, 12],
|
|
[1, 18],
|
|
[1, 17],
|
|
[1, 8],
|
|
[12, 18],
|
|
[2, 9],
|
|
[17, 7],
|
|
[8, 7],
|
|
[2, 10],
|
|
[9, 10],
|
|
[18, 19],
|
|
[12, 19],
|
|
[19, 13],
|
|
[10, 3],
|
|
[13, 14],
|
|
[3, 4],
|
|
[4, 11],
|
|
[14, 20],
|
|
[20, 15],
|
|
[20, 16],
|
|
[11, 6],
|
|
[11, 5],
|
|
[5, 23],
|
|
[15, 22],
|
|
[22, 23],
|
|
[22, 21],
|
|
[21, 24],
|
|
[23, 24]]
|
|
|
|
CAR_CATEGORIES_24 = ['gen 2']
|
|
|
|
|
|
CAR_SCORE_WEIGHTS_24 = [5,3,1,1,2,0.5,5,1,3,3,3,3,1,1,2,0.5,1,3,3,3,0.5,0.5,0.5,0.5]
|
|
|
|
CAR_SIGMAS_24 = [0.05] * len(CAR_KEYPOINTS_24)
|
|
|
|
CAR_POSE_24 = np.array([[ 3.16610e-02, 1.26484e+02, -7.90370e+00],
|
|
[ 3.70193e+01, 1.00028e+02, -1.67113e+01],
|
|
[ 4.41759e+01, 7.71484e+00, -1.67119e+01],
|
|
[ 4.40693e+01, -6.23645e+01, -1.68886e+01],
|
|
[ 4.49225e+01, -1.14833e+02, 2.25995e+01],
|
|
[ 4.25990e+01, -4.79440e+01, 4.99305e+00],
|
|
[ 1.97800e-03, -1.82719e+01, 3.22667e+01],
|
|
[ 1.99550e+01, 2.51571e+01, 1.61091e+01],
|
|
[ 3.63039e+01, 1.03593e+02, -3.92045e+00],
|
|
[ 4.54021e+01, 7.21575e+01, -4.14269e+00],
|
|
[ 4.53684e+01, -9.26003e+01, -4.14376e+00],
|
|
[-3.69654e+01, 1.00044e+02, -1.67116e+01],
|
|
[-4.41584e+01, 7.73299e+00, -1.67122e+01],
|
|
[-4.40825e+01, -6.23463e+01, -1.68889e+01],
|
|
[-4.49579e+01, -1.14815e+02, 2.25992e+01],
|
|
[-4.26056e+01, -4.79265e+01, 4.99274e+00],
|
|
[-1.99304e+01, 2.51653e+01, 1.61090e+01],
|
|
[-3.63039e+01, 1.03593e+02, -3.92045e+00],
|
|
[-4.53610e+01, 7.21734e+01, -4.14302e+00],
|
|
[-4.53947e+01, -9.25844e+01, -4.14409e+00],
|
|
[-2.73159e+01, -1.26582e+02, -2.83129e+00],
|
|
[-2.68627e+01, -1.15836e+02, 2.03855e+01],
|
|
[ 2.68268e+01, -1.15847e+02, 2.03857e+01],
|
|
[ 2.72785e+01, -1.26593e+02, -2.83109e+00]])
|
|
|
|
CAR_POSE_24[:, [0, 1, 2]] = CAR_POSE_24[:, [0, 2, 1]]
|
|
|
|
HFLIP_24 = {
|
|
'1': '1',
|
|
'2': '12',
|
|
'3': '13',
|
|
'4': '14',
|
|
'5': '15',
|
|
'6': '16',
|
|
'7': '7',
|
|
'8': '17',
|
|
'9': '18',
|
|
'10': '19',
|
|
'11': '20',
|
|
'12': '2',
|
|
'13': '3',
|
|
'14': '4',
|
|
'15': '5',
|
|
'16': '6',
|
|
'17': '8',
|
|
'18': '9',
|
|
'19': '10',
|
|
'20': '11',
|
|
'21': '24',
|
|
'22': '23',
|
|
'23': '22',
|
|
'24': '21'
|
|
}
|
|
|
|
training_weights_local_centrality = [
|
|
0.890968488270775,
|
|
0.716506138617812,
|
|
1.05674590410869,
|
|
0.764774195768455,
|
|
0.637682585483328,
|
|
0.686680807728366,
|
|
0.955422595797394,
|
|
0.936714585642375,
|
|
1.34823795445326,
|
|
1.38308992581967,
|
|
1.32689945125819,
|
|
1.38838655605483,
|
|
1.18980184904613,
|
|
1.02584355494795,
|
|
0.90969156732068,
|
|
1.24732068576104,
|
|
1.11338768064342,
|
|
0.933815217550391,
|
|
0.852297518872114,
|
|
1.04167641424727,
|
|
1.01668968075247,
|
|
1.34625964088011,
|
|
0.911796331039028,
|
|
0.866206536337413,
|
|
1.55957820407853,
|
|
0.730844382675724,
|
|
0.651138644197359,
|
|
0.758018559633786,
|
|
1.31842501396691,
|
|
1.32186116654782,
|
|
0.744347016851606,
|
|
0.636390683664723,
|
|
0.715244950821949,
|
|
1.63122349407032,
|
|
0.849835699185461,
|
|
0.910488007220499,
|
|
1.44244151650561,
|
|
1.14150437331681,
|
|
1.19808610191343,
|
|
0.960186788642886,
|
|
1.05023623286937,
|
|
1.19761709710598,
|
|
1.3872216313401,
|
|
1.01256700741214,
|
|
1.1167909667759,
|
|
1.27893496336199,
|
|
1.54475684725655,
|
|
1.40343733870633,
|
|
1.45552060866114,
|
|
1.47264222155031,
|
|
0.970060423999993,
|
|
0.944450314768933,
|
|
0.623987071240172,
|
|
0.5745237907704,
|
|
0.66890646050993,
|
|
0.978411632994504,
|
|
0.587396395188292,
|
|
0.76307999741129,
|
|
0.609793563449648,
|
|
0.67983566494545,
|
|
0.685883538168462,
|
|
0.753587600664775,
|
|
0.770335133588157,
|
|
0.764713638033368,
|
|
0.792364155965385,
|
|
0.796435233566833
|
|
]
|
|
|
|
|
|
def get_constants(num_kps):
|
|
if num_kps == 24:
|
|
CAR_POSE_24[:, 2] = 2.0
|
|
return [CAR_KEYPOINTS_24, CAR_SKELETON_24, HFLIP_24, CAR_SIGMAS_24,
|
|
CAR_POSE_24, CAR_CATEGORIES_24, CAR_SCORE_WEIGHTS_24]
|
|
if num_kps == 66:
|
|
CAR_POSE_66[:, 2] = 2.0
|
|
return [CAR_KEYPOINTS_66, CAR_SKELETON_66, HFLIP_66, CAR_SIGMAS_66,
|
|
CAR_POSE_66, CAR_CATEGORIES_66, CAR_SCORE_WEIGHTS_66]
|
|
# using no if-elif-else construction due to pylint no-else-return error
|
|
raise Exception("Only poses with 24 or 66 keypoints are available.")
|
|
|
|
|
|
def draw_ann(ann, *, keypoint_painter, filename=None, margin=0.5, aspect=None, **kwargs):
|
|
from openpifpaf import show # pylint: disable=import-outside-toplevel
|
|
|
|
bbox = ann.bbox()
|
|
xlim = bbox[0] - margin, bbox[0] + bbox[2] + margin
|
|
ylim = bbox[1] - margin, bbox[1] + bbox[3] + margin
|
|
if aspect == 'equal':
|
|
fig_w = 5.0
|
|
else:
|
|
fig_w = 5.0 / (ylim[1] - ylim[0]) * (xlim[1] - xlim[0])
|
|
|
|
with show.canvas(filename, figsize=(fig_w, 5), nomargin=True, **kwargs) as ax:
|
|
ax.set_axis_off()
|
|
ax.set_xlim(*xlim)
|
|
ax.set_ylim(*ylim)
|
|
|
|
if aspect is not None:
|
|
ax.set_aspect(aspect)
|
|
|
|
keypoint_painter.annotation(ax, ann)
|
|
|
|
|
|
def draw_skeletons(pose, sigmas, skel, kps, scr_weights):
|
|
from openpifpaf.annotation import Annotation # pylint: disable=import-outside-toplevel
|
|
from openpifpaf import show # pylint: disable=import-outside-toplevel
|
|
|
|
scale = np.sqrt(
|
|
(np.max(pose[:, 0]) - np.min(pose[:, 0]))
|
|
* (np.max(pose[:, 1]) - np.min(pose[:, 1]))
|
|
)
|
|
|
|
show.KeypointPainter.show_joint_scales = True
|
|
keypoint_painter = show.KeypointPainter()
|
|
ann = Annotation(keypoints=kps, skeleton=skel, score_weights=scr_weights)
|
|
ann.set(pose, np.array(sigmas) * scale)
|
|
os.makedirs('docs', exist_ok=True)
|
|
draw_ann(ann, filename='docs/skeleton_car.png', keypoint_painter=keypoint_painter)
|
|
|
|
|
|
def plot3d_red(ax_2D, p3d, skeleton):
|
|
skeleton = [(bone[0] - 1, bone[1] - 1) for bone in skeleton]
|
|
|
|
rot_p90_x = np.array([[1, 0, 0], [0, 0, 1], [0, 1, 0]])
|
|
p3d = p3d @ rot_p90_x
|
|
|
|
fig = ax_2D.get_figure()
|
|
ax = Axes3D(fig, auto_add_to_figure=False)
|
|
fig.add_axes(ax)
|
|
ax.set_axis_off()
|
|
ax_2D.set_axis_off()
|
|
|
|
ax.view_init(azim=-90, elev=20)
|
|
ax.set_xlabel('X')
|
|
ax.set_ylabel('Y')
|
|
ax.set_zlabel('Z')
|
|
max_range = np.array([p3d[:, 0].max() - p3d[:, 0].min(),
|
|
p3d[:, 1].max() - p3d[:, 1].min(),
|
|
p3d[:, 2].max() - p3d[:, 2].min()]).max() / 2.0
|
|
mid_x = (p3d[:, 0].max() + p3d[:, 0].min()) * 0.5
|
|
mid_y = (p3d[:, 1].max() + p3d[:, 1].min()) * 0.5
|
|
mid_z = (p3d[:, 2].max() + p3d[:, 2].min()) * 0.5
|
|
|
|
ax.set_xlim(mid_x - max_range, mid_x + max_range)
|
|
ax.set_ylim(mid_y - max_range, mid_y + max_range)
|
|
ax.set_zlim(mid_z - max_range, mid_z + max_range) # pylint: disable=no-member
|
|
|
|
for ci, bone in enumerate(skeleton):
|
|
c = mplcm.get_cmap('tab20')((ci % 20 + 0.05) / 20) # Same coloring as Pifpaf preds
|
|
ax.plot(p3d[bone, 0], p3d[bone, 1], p3d[bone, 2], color=c)
|
|
|
|
def animate(i):
|
|
ax.view_init(elev=10., azim=i)
|
|
return fig
|
|
|
|
return FuncAnimation(fig, animate, frames=360, interval=100)
|
|
|
|
|
|
def print_associations():
|
|
print("\nAssociations of the car skeleton with 24 keypoints")
|
|
for j1, j2 in CAR_SKELETON_24:
|
|
print(CAR_KEYPOINTS_24[j1 - 1], '-', CAR_KEYPOINTS_24[j2 - 1])
|
|
print("\nAssociations of the car skeleton with 66 keypoints")
|
|
for j1, j2 in CAR_SKELETON_66:
|
|
print(CAR_KEYPOINTS_66[j1 - 1], '-', CAR_KEYPOINTS_66[j2 - 1])
|
|
|
|
|
|
def main():
|
|
# print_associations()
|
|
# =============================================================================
|
|
# draw_skeletons(CAR_POSE_24, sigmas = CAR_SIGMAS_24, skel = CAR_SKELETON_24,
|
|
# kps = CAR_KEYPOINTS_24, scr_weights = CAR_SCORE_WEIGHTS_24)
|
|
# draw_skeletons(CAR_POSE_66, sigmas = CAR_SIGMAS_66, skel = CAR_SKELETON_66,
|
|
# kps = CAR_KEYPOINTS_66, scr_weights = CAR_SCORE_WEIGHTS_66)
|
|
# =============================================================================
|
|
# with openpifpaf.show.Canvas.blank(nomargin=True) as ax_2D:
|
|
# anim_66 = plot3d_red(ax_2D, CAR_POSE_66, CAR_SKELETON_66)
|
|
# anim_66.save('./CAR_66_Pose.gif', fps=30)
|
|
with openpifpaf.show.Canvas.blank(nomargin=True) as ax_2D:
|
|
anim_24 = plot3d_red(ax_2D, CAR_POSE_24, CAR_SKELETON_24)
|
|
anim_24.save('./CAR_24_Pose.gif', fps=30)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |