Pose

Description

Default class for poses, i.e. group of joints at a specific timestamp. A pose, in the motion sequence, is the equivalent of a frame in a video. The methods in this class are mainly handled by the methods in the class Sequence, but some of them can be directly accessed.

Initialisation

class krajjat.classes.pose.Pose(timestamp=None)

Creates an instance from the class Pose and returns a Pose object, which stores Joint objects and a timestamp.

New in version 2.0.

Parameters:

timestamp (float, optional) – The timestamp of the pose (in seconds).

joints

A dictionary of joints, where the keys are the joint labels and the values are Joint instances.

Type:

OrderedDict(str: Joint)

timestamp

The timestamp of the pose (in seconds).

Type:

float

relative_timestamp

The timestamp of the pose, relative to the first pose of the sequence (in seconds).

Type:

float

Example

>>> p = Pose(1)

Magic methods

Pose.__repr__()

Returns a string containing the timestamp, the relative timestamp and all the joints labels and coordinates from the Pose instance.

Returns:

A formatted string of the information contained in timestamp, relative_timestamp and joints.

Return type:

str

Example

>>> pose = Pose(0.5)
>>> pose.add_joint(Joint("Head", 0.2580389, 0.4354536, 2.449435))
>>> pose.add_joint(Joint("HandRight", 0.2747259, -0.3047626, 2.200738))
>>> print(pose)
Timestamp: 0.5
Relative timestamp: None
Joints (2):
    Head: (0.2580389, 0.4354536, 2.449435)
    HandRight: (0.2747259, -0.3047626, 2.200738)
Pose.__eq__(other)

Returns True if all the joints in the attribute joints are identical between the two Pose objects. If the joints are equal, the function will return True regardless of the timestamps. Each joint is compared using the function Joint.__eq__().

New in version 2.0.

Parameters:

other (Pose) – Another Pose object.

Examples

>>> pose_1 = Pose(0.5)
>>> pose_1.add_joint(Joint("Head", 1, 2, 3))
>>> pose_2 = Pose(0.5)
>>> pose_2.add_joint(Joint("Head", 1, 2, 3))
>>> pose_1 == pose_2
True
>>> pose_3 = Pose(0)
>>> pose_3.add_joint(Joint("Head", 1, 2, 3))
>>> pose_1 == pose_3
True
>>> pose_4 = Pose(0.5)
>>> pose_4.add_joint(Joint("Head", 1, 2, 4))
>>> pose_1 == pose_4
False
>>> pose_5 = Pose(0.5)
>>> pose_5.add_joint(Joint("HandRight", 1, 2, 3))
>>> pose_1 == pose_5
False
>>> pose_6 = pose_1.copy()
>>> pose_1 == pose_6
True
>>> pose_7 = Pose(0.5)
>>> pose_7.add_joint(Joint("Head", 1.00001, 2, 3))
>>> pose_1 == pose_7
True
Pose.__getitem__(key)

Allows to get a joint directly from its label.

New in version 2.0.

Parameters:

key (str) – A joint label.

Returns:

A Joint, if the joint label was present in the joints attribute.

Return type:

Joint

Example

>>> pose = Pose(0)
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose["HeadFront"]
HeadFront: (0, 0, 0)

Public methods

Setter functions

Pose.set_timestamp(timestamp)

Sets the attribute timestamp of the pose (in seconds).

New in version 2.0.

Parameters:

timestamp – The timestamp of the pose (in seconds).

Example

>>> p = Pose(1)
>>> p.set_timestamp(2)
>>> p.get_timestamp()
2

Getter functions

Pose.get_joint(joint_label)

Returns a joint object from the pose.

New in version 2.0.

Parameters:

joint_label (str) – The label of the joint (e.g. "Head").

Returns:

An instance of the class Joint.

Return type:

Joint

Raises:

InvalidJointLabelException – If the joint label is not in the keys of the joints attribute.

Example

>>> p = Pose(0)
>>> j = Joint("Head", 1, 2, 3)
>>> p.add_joint(j)
>>> p.get_joint("Head")
Head: (1, 2, 3)
Pose.get_joint_labels()

Returns the labels of the joints in the joints attribute.

New in version 2.0.

Returns:

The list of joint labels.

Return type:

list(str)

Example

>>> pose = Pose(42)
>>> joint_1 = Joint("Head", 1, 2, 3)
>>> pose.add_joint(joint_1)
>>> joint_2 = Joint("HandRight", 4, 5, 6)
>>> pose.add_joint(joint_2)
>>> joint_3 = Joint("HandLeft", 7, 8, 9)
>>> pose.add_joint(joint_3)
>>> pose.get_joint_labels()
["Head", "HandRight", "HandLeft"]
Pose.get_timestamp()

Returns the attribute timestamp of the pose (in seconds).

New in version 2.0.

Returns:

The timestamp of the pose (in seconds).

Return type:

float

Example

>>> p = Pose(108)
>>> p.get_timestamp()
108
Pose.get_relative_timestamp()

Returns the attribute relative_timestamp of the pose, which is the timestamp relative to the first pose of the sequence (in seconds).

New in version 2.0.

Returns:

The timestamp of the pose relative to the first timestamp of the sequence, in seconds.

Return type:

float

Joints functions

Pose.add_joint(joint, replace_if_exists=False)

Adds a Joint object to the pose.

New in version 2.0.

Parameters:
  • joint (Joint) – A joint object.

  • replace_if_exists (bool) – If False (default), the function will return an error if there is already a key with the name joint_label in the joints parameter. If True, the function will replace the existing value if it exists, without any warning message.

Raises:

JointLabelAlreadyExistsException – If the parameter replace_if_exists is set on False and there is already a joint with the same label in the pose.

Example

>>> p = Pose(23.4268)
>>> p.add_joint(Joint("Head", 1, 2, 3))
Pose.add_joints(*joints, replace_if_exists=False)

Adds Joint objects to the pose.

New in version 2.0.

Parameters:
  • joints (Joint) – One or multiple Joint objects.

  • replace_if_exists (bool) – If False (default), the function will return an error if there is already a key with the name joint_label in the joints parameter. If True, the function will replace the existing value if it exists, without any warning message.

Raises:

JointLabelAlreadyExistsException – If the parameter replace_if_exists is set on False and there is already a joint with the same label in the pose.

Example

>>> p = Pose(23.4268)
>>> j1 = Joint("Head", 1, 2, 3)
>>> j2 = Joint("HandRight", 4, 5, 6)
>>> j3 = Joint("HandLeft", 7, 8, 9)
>>> p.add_joints(j1, j2, j3)
Pose.generate_average_joint(list_joints_to_average, new_joint_label, add_joint=True)

Generates and returns a joint that is located at the average position of the other joints.

New in version 2.0.

Parameters:
  • list_joints_to_average (list(Joint)) – A list containing the strings of the joints to average.

  • new_joint_label (str) – The label of the joint (e.g. "Head").

  • add_joint (bool, optional) – If set on True, the joint created is also added to the parameter joints of the current Pose instance. If set on False, the joint is only generated and returned.

Returns:

The average joint.

Return type:

Joint

Raises:

JointLabelAlreadyExistsException – If the label new_joint_label is already in the joints attribute and add_joint is set on True.

Example

>>> pose = Pose(7)
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose.add_joint(Joint("HeadBack", 2, 4, 6))
>>> joint = pose.generate_average_joint(["HeadFront", "HeadBack"], "Head", True)
Head: (1, 2, 3)
Pose.remove_joint(joint_label)

Removes the specified joint from the pose.

New in version 2.0.

Parameters:

joint_label (str) – The label of the joint (e.g. "Head").

Raises:

InvalidJointLabelException – If the joint label is not present in the keys of the joints attribute.

Example

>>> pose = Pose()
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose.remove_joint("HeadFront")
Pose.remove_joints(list_of_joint_labels)

Removes the specified joints from the pose.

New in version 2.0.

Parameters:

list_of_joint_labels (list(str)) – A list of labels of joints to remove.

Raises:

InvalidJointLabelException – If a joint label is not present in the keys of the joints attribute.

Example

>>> pose = Pose()
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose.add_joint(Joint("HeadBack", 2, 4, 6))
>>> pose.remove_joints(["HeadFront", "HeadBack"])

Conversion functions

Pose.to_table(use_relative_timestamp=False)

Returns a table (a list of lists) where the first row is the header, and the second row contains the values. The first column of the table contains the timestamps, while the subsequent columns, by sets of three, contain the coordinates of a joint on the x, y and z axes respectively. The output then resembles the table found in Tabled formats.

New in version 2.0.

Parameters:

use_relative_timestamp (bool, optional) – Defines if the timestamps used in the table are absolute (False) or relative to the first pose (True).

Returns:

A list of lists that can be interpreted as a table, containing headers and the values of the timestamps and the coordinates of the joints from the pose.

Return type:

list(list)

Example

>>> pose = Pose(0)
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose.add_joint(Joint("HeadBack", 2, 4, 6))
>>> pose.to_table()
[['Timestamp', 'HeadFront_X', 'HeadFront_Y', 'HeadFront_Z', 'HeadBack_X', 'HeadBack_Y', 'HeadBack_Z'],
[0, 0, 0, 0, 2, 4, 6]]
Pose.to_json(use_relative_timestamp=False)

Returns a list ready to be exported in JSON. The structure followed by the dictionary is the same as the output dictionary from Kinect, for compatibility purposes. The output then resembles the table found in JSON formats.

New in version 2.0.

Parameters:

use_relative_timestamp (bool, optional) – Defines if the timestamps used in the table are absolute (False) or relative to the first pose (True).

Returns:

A list containing the data of the sequence, ready to be exported in JSON.

Return type:

list

Example

>>> pose = Pose(0)
>>> pose.add_joint(Joint("HeadFront", 0, 0, 0))
>>> pose.add_joint(Joint("HeadBack", 2, 4, 6))
>>> pose.to_json()
{'Bodies': [{'Joints': [{'JointType': 'HeadFront', 'Position': {'X': 0.0, 'Y': 0.0, 'Z': 0.0}},
                        {'JointType': 'HeadBack', 'Position': {'X': 2.0, 'Y': 4.0, 'Z': 6.0}}]}],
 'Timestamp': 0}

Copy function

Pose.copy()

Returns a deep copy of itself, containing deep copies of all the joints.

New in version 2.0.

Returns:

A deep copy of the Pose instance.

Return type:

Pose

Private methods

Pose._calculate_relative_timestamp(timestamp_first_pose)

Calculates the timestamp relative to the first pose of the sequence. This function is typically called at the end of the initialisation of a new sequence (either by opening a file or performing a processing on an existing sequence), and sets a value to the attribute relative_timestamp.

New in version 2.0.

Parameters:

timestamp_first_pose (float) – The timestamp of the first pose of the sequence, in its original time unit.

Pose._get_copy_with_empty_joints(use_relative_timestamp=False)

Creates a deep copy of the pose with a timestamp, and a joints with the same label joints as the original, but with the values set on None. The function then returns the copy.

New in version 2.0.

Parameters:

use_relative_timestamp (bool, optional) – Defines if the timestamps used in the table are absolute (False) or relative to the first pose (True).

Returns:

A Pose instance that is the deep copy of the original, but with all the values of the joints dictionary set on None.

Return type:

Pose