-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpose.py
67 lines (52 loc) · 2.2 KB
/
pose.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
import numpy as np
class PoseSequence:
def __init__(self, sequence):
self.poses = []
for parts in sequence:
self.poses.append(Pose(parts))
# normalize poses based on the average torso pixel length
torso_lengths = np.array([Part.dist(pose.neck, pose.lhip) for pose in self.poses if pose.neck.exists and pose.lhip.exists] +
[Part.dist(pose.neck, pose.rhip) for pose in self.poses if pose.neck.exists and pose.rhip.exists])
mean_torso = np.mean(torso_lengths)
for pose in self.poses:
for attr, part in pose:
setattr(pose, attr, part / mean_torso)
class Pose:
PART_NAMES = ['nose', 'neck', 'rshoulder', 'relbow', 'rwrist', 'lshoulder', 'lelbow', 'lwrist', 'rhip', 'rknee', 'rankle', 'lhip', 'lknee', 'lankle', 'reye', 'leye', 'rear', 'lear']
def __init__(self, parts):
"""Construct a pose for one frame, given an array of parts
Arguments:
parts - 18 * 3 ndarray of x, y, confidence values
"""
for name, vals in zip(self.PART_NAMES, parts):
setattr(self, name, Part(vals))
def __iter__(self):
for attr, value in self.__dict__.items():
yield attr, value
def __str__(self):
out = ""
for name in self.PART_NAMES:
_ = "{}: {},{}".format(name, getattr(self, name).x, getattr(self, name).x)
out = out + _ +"\n"
return out
def print(self, parts):
out = ""
for name in parts:
if not name in self.PART_NAMES:
raise NameError(name)
_ = "{}: {},{}".format(name, getattr(self, name).x, getattr(self, name).x)
out = out + _ +"\n"
return out
class Part:
def __init__(self, vals):
self.x = vals[0]
self.y = vals[1]
self.c = vals[2]
self.exists = self.c != 0.0
def __floordiv__(self, scalar):
__truediv__(self, scalar)
def __truediv__(self, scalar):
return Part([self.x / scalar, self.y / scalar, self.c])
@staticmethod
def dist(part1, part2):
return np.sqrt(np.square(part1.x - part2.x) + np.square(part1.y - part2.y))