Skip to content

Commit

Permalink
A bit of pylint cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cleong110 committed Jan 6, 2025
1 parent 04472e1 commit 1f5767d
Showing 1 changed file with 40 additions and 78 deletions.
118 changes: 40 additions & 78 deletions pose_evaluation/metrics/ham2pose_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
# and then code was copied to this repo by @cleong110

import os
import json
import random
import numpy as np
import importlib
from scipy.spatial.distance import euclidean
from fastdtw import fastdtw

import sys
from pose_evaluation.utils.pose_utils import get_preprocessed_pose, pose_hide_low_conf

# rootdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
# sys.path.insert(0, rootdir)

Expand All @@ -31,47 +29,9 @@
# pose_header = PoseHeader.read(BufferReader(buffer.read()))


# utils
# def get_pose(keypoints_path: str, datum_id: str, fps: int = 25):
# pose = get_pose(keypoints_path, fps)

# if datum_id in PJM_LEFT_VIDEOS_LST:
# pose = flip_pose(pose)

# normalization_info = pose_normalization_info(pose_header)
# pose = pose.normalize(normalization_info)
# pose.focus()

# pose_hide_legs(pose)
# pose_hide_low_conf(pose)

# # Prune all leading frames containing only zeros, almost no face, or no hands
# for i in range(len(pose.body.data)):
# if pose.body.confidence[i][:, 25:-42].sum() > 35 and \
# pose.body.confidence[i][:, 4] + pose.body.confidence[i][:, 7] > 0:
# if i != 0:
# pose.body.data = pose.body.data[i:]
# pose.body.confidence = pose.body.confidence[i:]
# break

# # Prune all trailing frames containing only zeros, almost no face, or no hands
# for i in range(len(pose.body.data) - 1, 0, -1):
# if pose.body.confidence[i][:, 25:-42].sum() > 35 and \
# pose.body.confidence[i][:, 4] + pose.body.confidence[i][:, 7] > 0:
# if i != len(pose.body.data) - 1:
# pose.body.data = pose.body.data[:i + 1]
# pose.body.confidence = pose.body.confidence[:i + 1]
# break

# return pose


def pose_hide_low_conf(pose):
mask = pose.body.confidence <= 0.2
pose.body.confidence[mask] = 0
stacked_confidence = np.stack([mask, mask, mask], axis=3)
masked_data = np.ma.masked_array(pose.body.data, mask=stacked_confidence)
pose.body.data = masked_data


def masked_euclidean(point1, point2):
Expand Down Expand Up @@ -138,8 +98,8 @@ def APE(trajectory1, trajectory2):


def compare_pose_videos(pose1_id, pose2_id, keypoints_path, distance_function=fastdtw):
pose1 = get_pose(os.path.join(keypoints_path, pose1_id), pose1_id)
pose2 = get_pose(os.path.join(keypoints_path, pose2_id), pose2_id)
pose1 = get_preprocessed_pose(os.path.join(keypoints_path, pose1_id), pose1_id)
pose2 = get_preprocessed_pose(os.path.join(keypoints_path, pose2_id), pose2_id)
return compare_poses(pose1, pose2, distance_function=distance_function)


Expand Down Expand Up @@ -191,52 +151,54 @@ def get_pose_data(poses):
# return relevant pose data for trajectory distance computations- only upper body and hands
poses_data = []
for pose in poses:
# Note: pose format shape is typically frames, persons (almost always 1), and then XYZ components/points.
# The following is therefore selecting specific points via index.
poses_data.append(np.ma.concatenate([pose.body.data[:, :, :95],
pose.body.data[:, :, 95:116],
pose.body.data[:, :, 116:]], axis=2))
return poses_data


def __compare_pred_to_video(pred, keypoints_path, pose_id, distance_function=fastdtw):
label_pose = get_pose(os.path.join(keypoints_path, pose_id), pose_id)
return compare_poses(pred, label_pose, distance_function=distance_function)
# def __compare_pred_to_video(pred, keypoints_path, pose_id, distance_function=fastdtw):
# label_pose = get_pose(os.path.join(keypoints_path, pose_id), pose_id)
# return compare_poses(pred, label_pose, distance_function=distance_function)


def check_ranks(distances, index):
rank_1 = (index == distances[0])
rank_5 = (index in distances[:5])
rank_10 = (index in distances)
return rank_1, rank_5, rank_10
# def check_ranks(distances, index):
# rank_1 = (index == distances[0])
# rank_5 = (index in distances[:5])
# rank_10 = (index in distances)
# return rank_1, rank_5, rank_10


def get_poses_ranks(pred, pred_id, keypoints_path, data_ids, distance_function=fastdtw, num_samples=20,
model=None, pose_header=None, ds=None):
pred2label_distance = __compare_pred_to_video(pred, keypoints_path, pred_id, distance_function=distance_function)
# def get_poses_ranks(pred, pred_id, keypoints_path, data_ids, distance_function=fastdtw, num_samples=20,
# model=None, pose_header=None, ds=None):
# pred2label_distance = __compare_pred_to_video(pred, keypoints_path, pred_id, distance_function=distance_function)

distances_to_label = [pred2label_distance]
distances_to_pred = [pred2label_distance]
pred2label_index = 0
# distances_to_label = [pred2label_distance]
# distances_to_pred = [pred2label_distance]
# pred2label_index = 0

if model is not None:
indices = random.sample(range(len(ds)), num_samples)
for idx in indices:
if ds[idx]["id"] == pred_id:
continue
cur_pred = predict_pose(model, ds[idx], pose_header)
distances_to_label.append(__compare_pred_to_video(cur_pred, keypoints_path, pred_id,
distance_function=distance_function))
distances_to_pred.append(compare_poses(pred, cur_pred, distance_function=distance_function))
# if model is not None:
# indices = random.sample(range(len(ds)), num_samples)
# for idx in indices:
# if ds[idx]["id"] == pred_id:
# continue
# cur_pred = predict_pose(model, ds[idx], pose_header)
# distances_to_label.append(__compare_pred_to_video(cur_pred, keypoints_path, pred_id,
# distance_function=distance_function))
# distances_to_pred.append(compare_poses(pred, cur_pred, distance_function=distance_function))

pose_ids = random.sample(data_ids, num_samples)
for pose_id in pose_ids:
distances_to_label.append(compare_pose_videos(pose_id, pred_id, keypoints_path,
distance_function=distance_function))
distances_to_pred.append(__compare_pred_to_video(pred, keypoints_path, pose_id,
distance_function=distance_function))
# pose_ids = random.sample(data_ids, num_samples)
# for pose_id in pose_ids:
# distances_to_label.append(compare_pose_videos(pose_id, pred_id, keypoints_path,
# distance_function=distance_function))
# distances_to_pred.append(__compare_pred_to_video(pred, keypoints_path, pose_id,
# distance_function=distance_function))

best_pred = np.argsort(distances_to_pred)[:10]
rank_1_pred, rank_5_pred, rank_10_pred = check_ranks(best_pred, pred2label_index)
best_label = np.argsort(distances_to_label)[:10]
rank_1_label, rank_5_label, rank_10_label = check_ranks(best_label, pred2label_index)
# best_pred = np.argsort(distances_to_pred)[:10]
# rank_1_pred, rank_5_pred, rank_10_pred = check_ranks(best_pred, pred2label_index)
# best_label = np.argsort(distances_to_label)[:10]
# rank_1_label, rank_5_label, rank_10_label = check_ranks(best_label, pred2label_index)

return pred2label_distance, rank_1_pred, rank_5_pred, rank_10_pred, rank_1_label, rank_5_label, rank_10_label
# return pred2label_distance, rank_1_pred, rank_5_pred, rank_10_pred, rank_1_label, rank_5_label, rank_10_label

0 comments on commit 1f5767d

Please sign in to comment.