Pose2Sim Documentation

repository·main·Indexed 20 days ago

https://github.com/perfanalytics/pose2sim

An open-source workflow for 3D markerless kinematics that converts 2D pose estimation from standard cameras (phones, webcams, GoPros) into 3D OpenSim motion analysis. It supports multi-person tracking, batch processing, and integration with RTMPose, providing a research-grade alternative to traditional marker-based Motion Capture (MoCap) for sports analysis, clinical assessment, and biomechanics.

Tokens
36K
Snippets
106
Records
146
Agent score
71%

What's inside pose2sim

  1. What is Pose2Sim?

    main

    Pose2Sim provides a workflow for 3D markerless kinematics (human or animal), serving as an alternative to traditional marker-based Motion Capture (MoCap) methods. It allows for research-grade accuracy and production-grade robustness using low-cost hardware like phones, webcams, or GoPros.

    Key Features:

    • Any Cameras: Compatible with phones, webcams, GoPros, etc.
    • Research-Grade Accuracy: Validated by peer-reviewed studies.
    • Multi-Person Support: Track multiple people simultaneously.
    • Full 3D Kinematics: Complete OpenSim skeletal analysis with joint angles.

    Use Cases:

    • Sports Analysis: Field-based 3D motion capture.
    • Clinical Assessment: Gait analysis in clinical settings.
    • Animation: Outdoor 3D capture with fully clothed subjects.
    • Research: Biomechanics studies with multiple participants.

    Note: For real-time analysis with a single camera (where motion is in the sagittal or frontal plane), consider using Sports2D instead.

  2. What is the Pose2Sim workflow?

    main

    Pose2Sim is a pipeline designed to bridge computer vision and biomechanics by converting 2D pose estimation data into 3D joint angles. The workflow follows these stages:

    1. 2D Keypoint Detection: Using a model like OpenPose to extract 2D coordinates from multiple video sources.
    2. Pose2Sim Core Processing:
      • Camera Calibration: Calibrating cameras via checkerboard or converting existing calibration files.
      • 2D Tracking: Identifying and isolating the specific person of interest from background subjects.
      • 3D Triangulation: Converting 2D coordinates into 3D positions (stored as a .trc file) using a weighted Direct Linear Transform (DLT) method.
      • 3D Coordinate Filtering: Cleaning the triangulated data.
    3. OpenSim Integration: Using the resulting 3D coordinates to scale a musculoskeletal model and perform inverse kinematics to obtain full-body 3D joint angles.
  3. Understand Pose2Sim project structures

    main

    Pose2Sim supports two main organizational structures depending on your experimental setup:

    SingleTrial

    Used for a single experimental run. The structure is:

    • calibration/ folder
    • videos/ folder
    • Config.toml (Project configuration)

    BatchSession

    Used for multiple trials within a single session. The structure is:

    • calibration/ folder
    • Trial_N/ (Multiple trial folders, e.g., Trial_1, Trial_2)
      • videos/ folder
      • Config.toml (Trial-specific parameters)
    • Config.toml (Global parameters applied to the whole session)
  4. Filter keypoint data in Pose2Sim

    main

    Pose2Sim provides several filtering options to process keypoint positions or speeds. Users can choose from the following filters and adjust their parameters:

    • Zero-phase low-pass Butterworth filter: Can be applied to either keypoint positions or speeds.
    • LOESS filter
    • Gaussian filter
    • Median filter

    The software allows for the visualization and comparison of waveforms before and after the filtering process to ensure data quality.

  5. Understand the OpenSim scaling and inverse kinematics bridge

    main

    A core feature of Pose2Sim is bridging OpenPose data to OpenSim. This process constrains kinematics to a physically accurate skeletal model, providing more robust results than raw keypoint data by:

    • Constraining Bone Lengths: Ensuring bones remain at a constant length.
    • Joint Angle Limits: Restricting joints to coherent, anatomically correct angle limits.

    Skeletal Model Details

    The provided model combines elements from the Rajagopal (2016) human gait model and the Beaucage (2019) lifting model:

    • Knee Joint: Uses the Rajagopal definition where abduction/adduction and internal/external rotation angles are constrained to the flexion/extension angle.
    • Spine: Uses the Beaucage definition where each lumbar vertebra is constrained to the next, allowing for coherent spine bending even with limited tracked keypoints.
    • Limitations: Hand movements are locked because standard OpenPose models do not provide hand detection.

    Systematic Error Correction

    The model automatically accounts for systematic labeling errors in OpenPose by offsetting model markers relative to true joint centers.

  6. Configure Pose2Sim via Config.toml or Dictionary

    main
    Pose2Sim parameters are primarily managed through a Config.toml file. However, you can also override any configuration parameter by passing a dictionary directly to any Pose2Sim function. This allows for programmatic control over the analysis pipeline without modifying the physical config file.
  7. Track the Person of Interest

    main

    To prevent background subjects from being included in the 3D reconstruction, Pose2Sim performs a tracking step.

    How it works:

    1. The system examines all possible triangulations of a chosen keypoint (e.g., the neck or a hip point) across all detected persons.
    2. It reprojects these triangulations onto the image planes.
    3. The triangulation with the smallest reprojection error is identified as the person of interest.
    4. If the reprojection error exceeds a predefined threshold, the system attempts to re-run the process by excluding one or more cameras (useful if the subject has left a specific camera's field of view).
  8. Estimate 2D pose using RTMPose (Default)

    main

    RTMPose is the natively integrated, state-of-the-art pose estimation solution in Pose2Sim. It is accurate, fast, and flexible.

    To run pose estimation:

    1. Open a terminal in your project folder.
    2. Activate your environment.
    3. Run ipython and execute the following code:
    from Pose2Sim import Pose2Sim
    Pose2Sim.poseEstimation()

    Results will be saved in the pose folder in OpenPose format (one .json file per frame per camera) containing keypoint coordinates and confidence scores.

    from Pose2Sim import Pose2Sim
    Pose2Sim.poseEstimation()
  9. Use AlphaPose (Legacy)

    main

    AlphaPose is a legacy option. To use it:

    1. Run AlphaPose on your videos.
    2. Translate the AlphaPose single JSON file into OpenPose frame-by-frame files using the AlphaPose_to_OpenPose.py utility:
      AlphaPose_to_OpenPose -i input_alphapose_json_file
    3. Update Config.toml with the correct pose_model and tracked_keypoint.

    Note: For COCO and MPII formats, AlphaPose must be run with the --format cmu flag.

    AlphaPose_to_OpenPose -i input_alphapose_json_file
  10. Use custom detection and pose models via RTMLib

    main

    You can use any RTMlib model (hand, face, animal, or custom trained models) by providing a dictionary to the mode configuration. The dictionary must be enclosed in triple quotes and can contain local paths or URLs to .onnx or .zip files.

    Key dictionary keys:

    • det_class: Detection class (e.g., 'YOLOX').
    • det_model: Path/URL to the detection model.
    • det_input_size: List of [width, height] for detection.
    • pose_class: Pose class (e.g., 'RTMPose', 'RTMO').
    • pose_model: Path/URL to the pose model.
    • pose_input_size: List of [width, height] for pose estimation.

    Example: Custom Balanced Mode

    mode = """{'det_class':'YOLOX',
           'det_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmposev1/onnx_sdk/yolox_m_8xb8-300e_humanart-c2c7a14a.onnx',
           'det_input_size':[640, 640],
           'pose_class':'RTMPose',
           'pose_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmposev1/onnx_sdk/rtmpose-m_simcc-body7_pt-body7-halpe26_256x192-4d3e73dd_20230605.onnx',
           'pose_input_size':[192,256]}"""

    Example: One-stage RTMO model

    mode = """{'pose_class':'RTMO', 
           'pose_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmo/onnx_sdk/rtmo-m_16xb8-600e_body7-640x640-39e78cc4_20231211.onnx', 
           'pose_input_size':[640, 640]}"""
    # Equivalent to mode='balanced', with body_with_feet pose model
    mode = """{'det_class':'YOLOX',
           'det_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmposev1/onnx_sdk/yolox_m_8xb8-300e_humanart-c2c7a14a.onnx',
           'det_input_size':[640, 640],
           'pose_class':'RTMPose',
           'pose_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmposev1/onnx_sdk/rtmpose-m_simcc-body7_pt-body7-halpe26_700e-256x192-4d3e73dd_20230605.onnx',
           'pose_input_size':[192,256]}"""
    
    # With one-stage RTMO model 
    mode = """{'pose_class':'RTMO', 
           'pose_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmo/onnx_sdk/rtmo-m_16xb16-600e_body7-640x640-39e78cc4_20231211.onnx', 
           'pose_input_size':[640, 640]}"""
    
    # With animal pose estimation:
    mode = """{'pose_class':'RTMPose',
           'pose_model':'https://huggingface.co/datasets/DavidPagnon/rtmlib_models/resolve/main/mmpose/rtmposev1/onnx_sdk/rtmpose-m_simcc-ap10k_pt-aic-coco_210e-256x256-7a041aa1_20230206.onnx',
           'pose_input_size':[256,256]}"""
  11. Synchronize your cameras

    main

    If your cameras are not natively synchronized, use this module to find the time offset for which the correlation of mean vertical speed for chosen keypoints is highest.

    How to use:

    1. Open a terminal in your project folder.
    2. Activate your environment.
    3. Run ipython and execute the synchronization command.

    Configuration: You can tune parameters (keypoints to synchronize on, reference person, and time window) via a GUI by setting synchronization_gui = true in your Config.toml, or directly in the Config.toml file.

    Best practices for success:

    • Ensure the participant performs clear vertical movement.
    • The capture should last at least 5 seconds but no more than a few minutes to avoid camera drift.
    • Avoid participants moving towards or away from the cameras.
    • Alternatively, use a physical event (flashlight, clap) or GoPro-specific methods (timecode/GPS).
    from Pose2Sim import Pose2Sim
    Pose2Sim.synchronization()
  12. Optimize video storage for Pose2Sim

    main

    To reduce storage requirements without significantly impacting accuracy, follow these guidelines:

    • Compression: Increase the CRF (Constant Rate Factor) from 20 to 30. This can decrease storage use by up to 10x.
    • Resolution: Decrease resolution from HD (1080 x 1920) to SD (720 x 1280). This can decrease storage use by about 8x.
    • Audio: Audio tracks are unnecessary and can be removed to save 1-10% of file size.
    • Framerate: Maintain a high framerate (at least 60 Hz). Framerates below 60 Hz lead to accuracy drops, especially for fast motions.
    • Privacy: Use the provided face_blurring.py utility script to preserve privacy with minimal impact on accuracy.

    You can use ffmpeg to compress and downscale your videos simultaneously.

    ffmpeg -i original_vid.MP4 \
      -vcodec libx264 -crf 30 -preset fast \
      -vf "scale='min(1280,iw)':'min(720,ih)':force_original_aspect_ratio=decrease" \
      -movflags +faststart \
      lighter_vid.mp4