davidpagnon / Sports2D

Compute 2D human pose and angles from a video or a webcam.
BSD 3-Clause "New" or "Revised" License
47 stars 6 forks source link
biomechanics blazepose joint-angles kinematics kinovea markerless openpose pose-estimation rtmpose sports-analytics

Continuous integration PyPI version\ Downloads Stars GitHub issues GitHub issues-closed\ 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


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:


Play with the parameters

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

sports2d --help



Go further

Too slow for you?

Quick fixes:


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


How it works

Sports2D:


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 for a certain number of frames instead of detected. 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 enough average high-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:

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


How to cite and how to contribute

How to cite

If you use Sports2D, please cite [Pagnon, 2023].

 @misc{Pagnon2023,
   author = {Pagnon, David},
   title = {Sports2D - Angles from video},
   year = {2023},
   doi= {10.5281/zenodo.7903963},
   publisher = {GitHub},
   journal = {GitHub repository},
   howpublished = {\url{https://github.com/davidpagnon/Sports2D}},
 }

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: