Skip to main content

Detect pose and compute 2D joint angles from a video.

Project description

Continuous integration PyPI version
Downloads Stars GitHub issues GitHub issues-closed
status DOI License

Sports2D

Sports2D automatically computes 2D joint positions, as well as joint and segment angles from a video or a webcam.


Announcement:
Complete rewriting of the code!
Run pip install sports2d -U to get the latest version.

  • Faster, more accurate
  • Works from a webcam
  • Better visualization output
  • More flexible, easier to run
  • Batch process multiple videos at once

Note: Colab version broken for now. I'll fix it in the next few weeks.


https://github.com/user-attachments/assets/1c6e2d6b-d0cf-4165-864e-d9f01c0b8a0e

Warning: Angle estimation is only as good as the pose estimation algorithm, i.e., it is not perfect.
Warning: Results are acceptable only if the persons move in the 2D plane (sagittal or frontal plane). The persons need to be filmed as parallel as possible to the motion plane.
If you need 3D research-grade markerless joint kinematics, consider using several cameras, and constraining angles to a biomechanically accurate model. See Pose2Sim for example.

Contents

  1. Installation and Demonstration
    1. Installation
    2. Demonstration
    3. Play with the parameters
  2. Go further
    1. Too slow for you?
    2. What you need is what you get
    3. How it works
  3. How to cite and how to contribute

Installation and Demonstration

Installation

  • OPTION 1: Quick install
    Open a terminal. Type python -V to make sure python >=3.8 <=3.11 is installed, and then:

    pip install sports2d
    
  • OPTION 2: Safer install with Anaconda
    Install Miniconda:
    Open an Anaconda prompt and create a virtual environment by typing:

    conda create -n Sports2D python=3.9 -y
    conda activate Sports2D
    pip install sports2d
    
  • OPTION 3: Build from source and test the last changes
    Open a terminal in the directory of your choice and clone the Sports2D repository.

    git clone https://github.com/davidpagnon/sports2d.git
    cd sports2d
    pip install .
    

Demonstration

Just open a command line and run:

sports2d

You should see the joint positions and angles being displayed in real time.

Check the folder where you run that command line to find the resulting video, images, TRC pose and MOT angle files (which can be opened with any spreadsheet software), and logs.

Important: If you ran the conda install, you first need to activate the environment: run conda activate sports2d in the Anaconda prompt.

Note:
The Demo video is voluntarily challenging to demonstrate the robustness of the process after sorting, interpolation and filtering. It contains:

  • One person walking in the sagittal plane
  • One person doing jumping jacks in the frontal plane. This person then performs a flip while being backlit, both of which are challenging for the pose detection algorithm
  • One tiny person flickering in the background who needs to be ignored

Play with the parameters

For a full list of the available parameters, check the Config_Demo.toml file or type:

sports2d --help

  • Run on custom video with default parameters:
    sports2d --video_input path_to_video.mp4
    
  • Run on multiple videos with default parameters:
    sports2d --video_input path_to_video1.mp4 path_to_video2.mp4
    
  • Run on webcam with default parameters:
    sports2d --video_input webcam
    
  • Run with custom parameters (all non specified are set to default):
    sports2d --show_graphs False --time_range 0 2.1 --result_dir path_to_result_dir
    
    sports2d --multiperson false --mode lightweight --det_frequency 50
    
  • Run with a toml configuration file:
    sports2d --config path_to_config.toml
    
  • Run within a Python script:
    from Sports2D import Sports2D; Sports2D.process('Config_demo.toml')
    

Go further

Too slow for you?

Quick fixes:

  • Use --multiperson false: Can be used if one single person is present in the video. Otherwise, persons' IDs may be mixed up.
  • Use --mode lightweight: Will use a lighter version of RTMPose, which is faster but less accurate.
  • Use --det_frequency 50: Will detect poses only every 50 frames, and track keypoints inbetween, which is faster.

Use your GPU:
Will be much faster, with no impact on accuracy. However, the installation takes about 6 GB of additional storage space.

  1. Run nvidia-smi in a terminal. If this results in an error, your GPU is probably not compatible with CUDA. If not, note the "CUDA version": it is the latest version your driver is compatible with (more information on this post).

    Then go to the ONNXruntime requirement page, note the latest compatible CUDA and cuDNN requirements. Next, go to the pyTorch website and install the latest version that satisfies these requirements (beware that torch 2.4 ships with cuDNN 9, while torch 2.3 installs cuDNN 8). For example:

    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
    
  1. Finally, install ONNX Runtime with GPU support:

    pip install onnxruntime-gpu
    
  2. Check that everything went well within Python with these commands:

    python -c 'import torch; print(torch.cuda.is_available())'
    python -c 'import onnxruntime as ort; print(ort.get_available_providers())'
    # Should print "True ['CUDAExecutionProvider', ...]"
    

What you need is what you get

  • Choose whether you want video, images, trc pose file, and angle mot file. For example:
    sports2d --save_vid false --save_img true --save_trc false --save_mot true
    
  • Choose which angles you needfile. For example:
    sports2d --joint_angles 'right knee' 'left knee' --segment_angles None
    
  • Choose where to display the angles: either as a list on the upper-left of the image, or near the joint/segment, or bothfile. For example:
    sports2d --display_angle_values_on body
    
  • Choose where to save the resultsfile. For example:
    sports2d --result_dir path_to_result_dir
    
  • Choose a fraction of the video to analyze (in seconds)file. For example:
    sports2d --time_range 0 2.1
    

How it works

Sports2D:

  • Detects 2D joint centers from a video or a webcam with RTMLib.
  • Computes selected joint and segment angles.
  • Optionally saves processed image files and video file. Optionally saves processed poses as a TRC file, and angles as a MOT file (OpenSim compatible).

Okay but how does it work, really?
Sports2D:

  1. Reads stream from a webcam, from one video, or from a list of videos. Selects the specified time range to process.
  2. Sets up the RTMLib pose tracker from RTMlib with specified parameters. It can be run in lightweight, balanced, or performance mode, and for faster inference, keypoints can be tracked instead of detected for a certain number of frames. Any RTMPose model can be used.
  3. Tracks people so that their IDs are consistent across frames. A person is associated to another in the next frame when they are at a small distance. IDs remain consistent even if the person disappears from a few frames. This carefully crafted sports2d tracker runs at a comparable speed as the RTMlib one but is much more robust. The user can still choose the RTMLib method if they need it by specifying it in the Config.toml file.
  4. Retrieves the keypoints with high enough confidence, and only keeps the persons with high enough average confidence.
  5. Computes the selected joint and segment angles, and flips them on the left/right side if the respective foot is pointing to the left/right.
  6. Draws bounding boxes around each person and writes their IDs
    Draws the skeleton and the keypoints, with a green to red color scale to account for their confidence
    Draws joint and segment angles on the body, and writes the values either near the joint/segment, or on the upper-left of the image with a progress bar
  7. Interpolates missing pose and angle sequences if gaps are not too large. Filters them with the selected filter (among Butterworth, Gaussian, LOESS, or Median) and their parameters
  8. Optionally show processed images, saves them, or saves them as a video
    Optionally plots pose and angle data before and after processing for comparison
    Optionally saves poses for each person as a TRC file, and angles as a MOT file

Joint angle conventions:

  • Ankle dorsiflexion: Between heel and big toe, and ankle and knee.
    -90° when the foot is aligned with the shank.
  • Knee flexion: Between hip, knee, and ankle.
    0° when the shank is aligned with the thigh.
  • Hip flexion: Between knee, hip, and shoulder.
    0° when the trunk is aligned with the thigh.
  • Shoulder flexion: Between hip, shoulder, and elbow.
    180° when the arm is aligned with the trunk.
  • Elbow flexion: Between wrist, elbow, and shoulder.
    0° when the forearm is aligned with the arm.

Segment angle conventions:
Angles are measured anticlockwise between the horizontal and the segment.

  • Foot: Between heel and big toe
  • Shank: Between ankle and knee
  • Thigh: Between hip and knee
  • Pelvis: Between left and right hip
  • Trunk: Between hip midpoint and shoulder midpoint
  • Shoulders: Between left and right shoulder
  • Head: Between neck and top of the head
  • Arm: Between shoulder and elbow
  • Forearm: Between elbow and wrist

How to cite and how to contribute

How to cite

If you use Sports2D, please cite Pagnon, 2024.

 @article{Pagnon_Sports2D_Compute_2D_2024,
   author = {Pagnon, David and Kim, HunMin},
   doi = {10.21105/joss.06849},
   journal = {Journal of Open Source Software},
   month = sep,
   number = {101},
   pages = {6849},
   title = {{Sports2D: Compute 2D human pose and angles from a video or a webcam}},
   url = {https://joss.theoj.org/papers/10.21105/joss.06849},
   volume = {9},
   year = {2024}
 }

How to contribute

I would happily welcome any proposal for new features, code improvement, and more!
If you want to contribute to Sports2D, please follow this guide on how to fork, modify and push code, and submit a pull request. I would appreciate it if you provided as much useful information as possible about how you modified the code, and a rationale for why you're making this pull request. Please also specify on which operating system and on which python version you have tested the code.

Here is a to-do list: feel free to complete it:

  • Compute segment angles.
  • Multi-person detection, consistent over time.
  • Only interpolate small gaps.
  • Filtering and plotting tools.
  • Handle sudden changes of direction.
  • Batch processing for the analysis of multiple videos at once.
  • Colab version: more user-friendly, usable on a smartphone.
  • GUI applications for Windows, Mac, and Linux, as well as for Android and iOS.
  • Convert positions to meters by providing the distance between two clicked points
  • Perform Inverse kinematics and dynamics with OpenSim (cf. Pose2Sim, but in 2D). Update this model (add arms, markers, remove muscles and contact spheres). Add pipeline example.

  • Track other points and angles with classic tracking methods (cf. Kinovea), or by training a model (cf. DeepLabCut).
  • Pose refinement. Click and move badly estimated 2D points. See DeepLabCut for inspiration.
  • Add tools for annotating images, undistort them, take perspective into account, etc. (cf. Kinovea).

BSD 3-Clause License

Copyright (c) 2022, perfanalytics All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sports2d-0.4.8.tar.gz (3.9 MB view details)

Uploaded Source

Built Distribution

sports2d-0.4.8-py3-none-any.whl (3.9 MB view details)

Uploaded Python 3

File details

Details for the file sports2d-0.4.8.tar.gz.

File metadata

  • Download URL: sports2d-0.4.8.tar.gz
  • Upload date:
  • Size: 3.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sports2d-0.4.8.tar.gz
Algorithm Hash digest
SHA256 627a9590c96f65f84078c254229e474884563b50aeb45fe4bb2fc79abbc51785
MD5 5d40f6f9ad6f7233e6a82fb04846f68f
BLAKE2b-256 26ab8b5f1ecdffcd12b8708ba39ea212040c5aa2430c189ce36f760bc7fb5151

See more details on using hashes here.

File details

Details for the file sports2d-0.4.8-py3-none-any.whl.

File metadata

  • Download URL: sports2d-0.4.8-py3-none-any.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sports2d-0.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5c54d9c25ffa80626e3200b28d03e8020e38b50544d2c326d1f35c5379d1c534
MD5 d83c066b9c508395addffd95f00bd815
BLAKE2b-256 b598f863c3cf8e0557fadcf4651afd9c04c71843f01eba6a1ed8bd3b8af21441

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page