Skip to content

Commit 3b41b82

Browse files
committed
骨架对比整合到类中
1 parent 54bce48 commit 3b41b82

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

angle.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,70 @@
11
import numpy as np
22
from mxnet import ndarray
3+
from sklearn.metrics import r2_score
4+
35

46
# 需要测量角度的部位,每个部位需要用它本身和与之连接的两个节点来计算角度
57
# 第一点是关节点
68
KeyPoints = [
79
(5, 6, 7), # 左肩
810
(6, 7, 8), # 右肩
9-
(7, 5, 9), # 左臂
11+
(7, 5, 9), # 左臂
1012
(8, 6, 10), # 右臂
1113
(11, 5, 13), # 左胯
1214
(12, 6, 14), # 右胯
1315
(13, 11, 15), # 左膝
1416
(14, 12, 16), # 右膝
1517
]
1618

17-
# 计算所有人关键部位的夹角的余弦值
18-
def CalAngle(coords, confidence, keypoint_thresh=0.2):
19-
joint_visible = confidence[:, :, 0] > keypoint_thresh
20-
angles = np.empty((coords.shape[0], len(KeyPoints)))
19+
class AngeleCal():
20+
21+
def __init__(self, filename):
22+
self.stdAngles = np.loadtxt(filename, delimiter='\t')
23+
self.pos = 0
24+
25+
# 计算角度
26+
@staticmethod
27+
def cal(coords, confidence, keypoint_thresh=0.2):
28+
joint_visible = confidence[:, :, 0] > keypoint_thresh
29+
angles = np.empty((coords.shape[0], len(KeyPoints)))
2130

22-
for i, pts in enumerate(coords):
23-
# 某个人
24-
for j, keyPoint in enumerate(KeyPoints):
25-
# 是否识别到这个关节
26-
if joint_visible[i, keyPoint[0]] and joint_visible[i, keyPoint[1]] and joint_visible[i, keyPoint[2]]:
27-
# 计算
28-
# print(pts)
31+
for i, pts in enumerate(coords):
32+
# 某个人
33+
for j, keyPoint in enumerate(KeyPoints):
34+
# 是否识别到这个关节
35+
if joint_visible[i, keyPoint[0]] and joint_visible[i, keyPoint[1]] and joint_visible[i, keyPoint[2]]:
36+
# 计算
37+
# print(pts)
2938

30-
p0x = pts[keyPoint[0], 0].asscalar()
31-
p0y = pts[keyPoint[0], 1].asscalar()
32-
p1x = pts[keyPoint[1], 0].asscalar()
33-
p1y = pts[keyPoint[1], 1].asscalar()
34-
p2x = pts[keyPoint[2], 0].asscalar()
35-
p2y = pts[keyPoint[2], 1].asscalar()
39+
p0x = pts[keyPoint[0], 0].asscalar()
40+
p0y = pts[keyPoint[0], 1].asscalar()
41+
p1x = pts[keyPoint[1], 0].asscalar()
42+
p1y = pts[keyPoint[1], 1].asscalar()
43+
p2x = pts[keyPoint[2], 0].asscalar()
44+
p2y = pts[keyPoint[2], 1].asscalar()
3645

37-
v1 = np.array([ p1x - p0x, p1y - p0y ])
38-
v2 = np.array([ p2x - p0x, p2y - p0y ])
46+
v1 = np.array([ p1x - p0x, p1y - p0y ])
47+
v2 = np.array([ p2x - p0x, p2y - p0y ])
3948

40-
angles[i][j] = np.dot(v1, v2) / np.linalg.norm(v1) / np.linalg.norm(v2)
49+
angles[i][j] = np.dot(v1, v2) / np.linalg.norm(v1) / np.linalg.norm(v2)
4150

51+
else:
52+
angles[i][j] = np.nan
53+
54+
return angles
55+
56+
# 角度对比
57+
def compare(self, angles):
58+
# 每次读取一行标准角度
59+
stdAngle = self.stdAngles[self.pos]
60+
scores = []
61+
visibles = ~np.isnan(stdAngle) # 样本中没有缺失值的
62+
for angle in angles:
63+
angle_v = angle[visibles] # 过滤样本中也有缺失值的点
64+
if np.isnan(angle_v).any(): # 还有缺失值
65+
scores.append('NaN')
4266
else:
43-
angles[i][j] = np.nan
44-
45-
return angles
67+
scores.append('{:.4f}'.format(r2_score(angle_v, stdAngle[visibles])))
68+
self.pos += 1
69+
70+
return scores

data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from gluoncv.data.transforms.pose import detector_to_simple_pose, heatmap_to_coord
99
from gluoncv.utils.viz import cv_plot_image, cv_plot_keypoints
1010
from mxnet.gluon.data.vision import transforms
11-
from angle import CalAngle
11+
from angle import AngeleCal
1212

1313
# 读取参数
1414
parser = argparse.ArgumentParser()
@@ -53,7 +53,7 @@
5353
pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox)
5454
img = cv_plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores)
5555

56-
X = CalAngle(pred_coords, confidence)[0]
56+
X = AngeleCal.cal(pred_coords, confidence)[0]
5757
print(X)
5858
features.append(X)
5959
else:

run.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
from gluoncv.data.transforms.pose import detector_to_simple_pose, heatmap_to_coord
99
from gluoncv.utils.viz import cv_plot_image, cv_plot_keypoints
1010
from mxnet.gluon.data.vision import transforms
11-
from angle import CalAngle
12-
from sklearn.metrics import r2_score
11+
from angle import AngeleCal
1312

1413
# 读取参数
1514
parser = argparse.ArgumentParser()
@@ -39,7 +38,7 @@
3938
cap2 = cv.VideoCapture(args.demo)
4039

4140
# 标准特征
42-
stdAngle = np.loadtxt(args.data, delimiter='\t')
41+
angeleCal = AngeleCal(args.data)
4342
pos = 0
4443

4544
ret1, frame1 = cap1.read()
@@ -62,24 +61,17 @@
6261
img = cv_plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores)
6362

6463
# 动作对比
65-
scores = []
66-
# print(stdAngle[pos])
67-
visibles = ~np.isnan(stdAngle[pos]) # 样本中没有缺失值的点
68-
angles = CalAngle(pred_coords, confidence)
69-
for angle in angles:
70-
angle_v = angle[visibles] # 过滤样本中也有缺失值的点
71-
print(angle_v)
72-
if np.isnan(angle_v).any(): # 还有缺失值
73-
scores.append('NaN')
74-
else:
75-
scores.append('{:.4f}'.format(r2_score(angle_v, stdAngle[pos][visibles])))
76-
pos += 1
64+
angles = AngeleCal.cal(pred_coords, confidence)
65+
results = angeleCal.compare(angles)
66+
else:
67+
results = ['NaN']
7768

69+
print('result', results)
7870
cv_plot_image(img,
7971
upperleft_txt=f"FPS:{(1.0 / (time.time() - fps_time)):.2f}", upperleft_txt_corner=(10,25),
80-
left_txt_list=scores, canvas_name='pose')
72+
left_txt_list=results, canvas_name='pose')
8173
fps_time = time.time()
82-
# cv.imshow('demo', frame2)
74+
cv.imshow('demo', frame2)
8375

8476
# ESC键退出
8577
if cv.waitKey(1) == 27:

0 commit comments

Comments
 (0)