Skip to main content

Use open cv to detect critical elements in a golf swing.

Project description

Introduction

This package detects the various elements in a golf swing. It does this by tracking various points in the swing and writing the coordinates into a json file for later processing.

No single algorithm can generate all the trackers in a frame. Hence multiple algorithms are run, which creates multiple json files per frame. These are then merged to create a single composite json file per frame.

The various algorithm used are

  • Google Media Pipe Pose Detection This detects 33 pose landmarks. These are written into json files with filename <frame_fname>_mp.json

  • Use the script golfeditor in this package to mark the position of the ball and the club head initially. Then run the cv2 tracking algorithms to track the movements of these across multiple frames.

Detailed src documentation of this package is on ReadTheDocs

Installation

This is a standard python package and follows the typical flow for installing python packages.

python -m venv env_test_tracker

// Activate the virtual env
// On Windows run  env_test_tracker/Scripts/activate
// On Linux bash source env_test_tracker/Scripts/activate.sh

pip install golftracker

To run the example below you would also need another utility package thatI use for video file manipulations.

pip install spvidoeutils

Development

The source repository is in GitHub. The development flow is similar to other python projects.

  • Clone the project.
  • Create a virtual env and install all the dependencies
  • Install the package locally in dev mode.

Distribution

This is a pure python code package and so a wheel distribution is sufficient. My typical steps are below.

  • Bump the version in setup.py
  • Build the distribution
python setup.py bdist_wheel
  • Upload the distribution
twine upload dist/*

Example

I downloaded a video of michelle wei swing side view and used the following flow to create the tracker.

The shortened copy of the video is also on my shared drive as https://drive.google.com/file/d/16TaL8PSaK46wbXr7mVsDZLbpOieLC3Nm/view?usp=sharing

Let's save/rename it as test1.mp4 for the steps below.

Step1: Split the video into frames

From the package spvideoutils, run the script video_split to create a subdir test1 that will have all the frames.

video_split test1.mp4

Step2: Run Google media script to create json

This step allows the user to create the json file by hand.

Trackers

The following points are tracked on each frame as points with co ordindates (x,y). They are written into a json file for further analysis.

  • Ball Position This is the point at which the ball is placed. It is assumed that the target is directly towards it.

  • Club Shaft Position The start of the club is where the hands grasp the shaft and the end is where the club head is

Json Output

A sample json output is shown below for a frame with width=1024 and height=900. The coordinates assume that left top is the origin.

The ball is placed at (100, 200). The hand holding the shaft is at (10, 20) and the club head is at (30, 40)

{
    "frame" : [1024, 900]
	  "ball": [1, 2],
	  "shaft": [10, 20, 30, 40]
}

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

golftracker-0.0.3-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file golftracker-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: golftracker-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for golftracker-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1e15aaf3b9dc6f9a476159f1582445ef60c5d92039e2c8ca0b0f97197c0de62c
MD5 04337f3d82632b1fc6e5714a095feaba
BLAKE2b-256 32452b80ccb0c1bdb297e24c50086d03650e5c00de7b759133ce850b8416095f

See more details on using hashes here.

Supported by

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