-
Notifications
You must be signed in to change notification settings - Fork 1
Pose utils and some tests. #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0eb0833
pose utils and some tests
cleong110 32931dd
some gitignore updates
cleong110 b71b3e3
adding test data
cleong110 ff76be3
autoformat pose_utils and fix np.ma import
cleong110 66b257f
Some more formatting and pathlib changes
cleong110 232b0b1
some pylint updates for pose_utils
cleong110 bdbc49d
fix nontest items starting with tests
cleong110 4025da5
rename test files
cleong110 457c004
switch to detect_known_pose_format
cleong110 aecafa5
remove copy_pose in favor of pose.copy() from https://github.com/sign…
cleong110 3197ff8
replace remove_components with new one from https://github.com/sign-l…
cleong110 d417a2b
Fix for pose_remove_legs: Was only removing from POSE_LANDMARKS, not …
cleong110 31d8526
Add OpenPose support for remove_legs
cleong110 c1da39d
Merge branch 'main' into pose_utils
cleong110 22abdbb
remove score and add_preprocessor
cleong110 feb50b4
remove base_pose_metric, wasn't supposed to be part of this PR
cleong110 1608078
Add back original version of base_pose_metric
cleong110 5e67049
Update some tests
cleong110 e8cfb8e
cleanup pose_utils
cleong110 02fc673
fix some issues in base_pose_metric pointed out by pylint
cleong110 384ffdd
change set_masked_to_origin_position to use pose.body.zero_filled()
cleong110 fa9ff31
change reduce_poses_to_intersection fn name
cleong110 734181f
Cleaner reduce_poses_to_intersection, written by @amitMY
cleong110 7d3283f
import numpy.ma as ma
cleong110 dbd0080
fix import in base_pose_metric
cleong110 8b8292e
Some minor pylint fixes
cleong110 8a75753
take out the set-to-origin pose util function
cleong110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
.idea/ | ||
build/ | ||
pose_evaluation.egg-info/ | ||
**/__pycache__/ | ||
**/__pycache__/ | ||
.coverage | ||
.vscode/ | ||
coverage.lcov | ||
**/test_data/ | ||
*.npz | ||
*.code-workspace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
temp/ | ||
tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
import copy | ||
from pathlib import Path | ||
from typing import List, Dict | ||
|
||
import pytest | ||
from pose_format import Pose | ||
from pose_format.utils.generic import fake_pose | ||
from pose_format.utils.openpose_135 import ( | ||
OpenPose_Components as openpose_135_components, | ||
) | ||
|
||
from pose_evaluation.utils.pose_utils import load_pose_file | ||
|
||
|
||
|
||
utils_test_data_dir = Path(__file__).parent / "test" / "test_data" | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mediapipe_poses_test_data_paths() -> List[Path]: | ||
pose_file_paths = list(utils_test_data_dir.glob("*.pose")) | ||
return pose_file_paths | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mediapipe_poses_test_data(mediapipe_poses_test_data_paths) -> List[Pose]: | ||
original_poses = [ | ||
load_pose_file(pose_path) for pose_path in mediapipe_poses_test_data_paths | ||
] | ||
# I ran into issues where if one test would modify a Pose, it would affect other tests. | ||
# specifically, pose.header.components[0].name = unsupported_component_name in test_detect_format | ||
# this ensures we get a fresh object each time. | ||
return copy.deepcopy(original_poses) | ||
|
||
|
||
@pytest.fixture | ||
def standard_mediapipe_components_dict() -> Dict[str, List[str]]: | ||
format_json = utils_test_data_dir / "mediapipe_components_and_points.json" | ||
with open(format_json, "r", encoding="utf-8") as f: | ||
return json.load(f) | ||
|
||
|
||
@pytest.fixture | ||
def fake_openpose_poses(count: int = 3) -> List[Pose]: | ||
return [fake_pose(30) for _ in range(count)] | ||
|
||
|
||
@pytest.fixture | ||
def fake_openpose_135_poses(count: int = 3) -> List[Pose]: | ||
return [fake_pose(30, components=openpose_135_components) for _ in range(count)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
from typing import List, Tuple, Dict, Iterable | ||
from collections import defaultdict | ||
import numpy as np | ||
from numpy import ma | ||
cleong110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from pose_format import Pose | ||
|
||
|
||
def pose_remove_world_landmarks(pose: Pose) -> Pose: | ||
return pose.remove_components(["POSE_WORLD_LANDMARKS"]) | ||
|
||
|
||
def get_component_names_and_points_dict( | ||
pose: Pose, | ||
) -> Tuple[List[str], Dict[str, List[str]]]: | ||
component_names = [] | ||
points_dict = defaultdict(list) | ||
for component in pose.header.components: | ||
component_names.append(component.name) | ||
|
||
for point in component.points: | ||
points_dict[component.name].append(point) | ||
|
||
return component_names, points_dict | ||
|
||
|
||
def get_face_and_hands_from_pose(pose: Pose) -> Pose: | ||
# based on MediaPipe Holistic format. | ||
components_to_keep = [ | ||
"FACE_LANDMARKS", | ||
"LEFT_HAND_LANDMARKS", | ||
"RIGHT_HAND_LANDMARKS", | ||
] | ||
return pose.get_components(components_to_keep) | ||
|
||
|
||
def load_pose_file(pose_path: Path) -> Pose: | ||
pose_path = Path(pose_path).resolve() | ||
with pose_path.open("rb") as f: | ||
pose = Pose.read(f.read()) | ||
return pose | ||
|
||
|
||
def reduce_poses_to_intersection( | ||
poses: Iterable[Pose], | ||
) -> List[Pose]: | ||
poses = list(poses) # get a list, no need to copy | ||
|
||
# look at the first pose | ||
component_names = {c.name for c in poses[0].header.components} | ||
points = {c.name: set(c.points) for c in poses[0].header.components} | ||
|
||
# remove anything that other poses don't have | ||
for pose in poses[1:]: | ||
component_names.intersection_update({c.name for c in pose.header.components}) | ||
for component in pose.header.components: | ||
points[component.name].intersection_update(set(component.points)) | ||
|
||
# change datatypes to match get_components, then update the poses | ||
points_dict = {} | ||
for c_name in points.keys(): | ||
points_dict[c_name] = list(points[c_name]) | ||
poses = [pose.get_components(list(component_names), points_dict) for pose in poses] | ||
return poses | ||
|
||
|
||
def zero_pad_shorter_poses(poses: Iterable[Pose]) -> List[Pose]: | ||
poses = [pose.copy() for pose in poses] | ||
# arrays = [pose.body.data for pose in poses] | ||
|
||
# first dimension is frames. Then People, joint-points, XYZ or XY | ||
max_frame_count = max(len(pose.body.data) for pose in poses) | ||
# Pad the shorter array with zeros | ||
for pose in poses: | ||
if len(pose.body.data) < max_frame_count: | ||
desired_shape = list(pose.body.data.shape) | ||
desired_shape[0] = max_frame_count - len(pose.body.data) | ||
padding_tensor = ma.zeros(desired_shape) | ||
padding_tensor_conf = ma.ones(desired_shape[:-1]) | ||
pose.body.data = ma.concatenate([pose.body.data, padding_tensor], axis=0) | ||
pose.body.confidence = ma.concatenate( | ||
[pose.body.confidence, padding_tensor_conf] | ||
) | ||
return poses | ||
|
||
|
||
def pose_hide_low_conf(pose: Pose, confidence_threshold: float = 0.2) -> None: | ||
mask = pose.body.confidence <= confidence_threshold | ||
pose.body.confidence[mask] = 0 | ||
stacked_confidence = np.stack([mask, mask, mask], axis=3) | ||
masked_data = ma.masked_array(pose.body.data, mask=stacked_confidence) | ||
pose.body.data = masked_data |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.