swingpositions 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. swingpositions-0.1.0/.gitignore +33 -0
  2. swingpositions-0.1.0/CHANGELOG.md +20 -0
  3. swingpositions-0.1.0/LICENSE +202 -0
  4. swingpositions-0.1.0/NOTICE +22 -0
  5. swingpositions-0.1.0/PKG-INFO +317 -0
  6. swingpositions-0.1.0/README.md +279 -0
  7. swingpositions-0.1.0/docs/RELEASING.md +52 -0
  8. swingpositions-0.1.0/docs/images/gallery-phone-390.png +0 -0
  9. swingpositions-0.1.0/docs/images/hero.jpg +0 -0
  10. swingpositions-0.1.0/docs/images/p-positions.jpg +0 -0
  11. swingpositions-0.1.0/docs/images/swing-desktop-1440.png +0 -0
  12. swingpositions-0.1.0/docs/labels.md +58 -0
  13. swingpositions-0.1.0/docs/output-schema.md +108 -0
  14. swingpositions-0.1.0/docs/viewer-data.md +109 -0
  15. swingpositions-0.1.0/docs/viewer.md +123 -0
  16. swingpositions-0.1.0/examples/README.md +21 -0
  17. swingpositions-0.1.0/pyproject.toml +67 -0
  18. swingpositions-0.1.0/scripts/screenshots.mjs +130 -0
  19. swingpositions-0.1.0/src/swingpositions/__init__.py +16 -0
  20. swingpositions-0.1.0/src/swingpositions/__main__.py +3 -0
  21. swingpositions-0.1.0/src/swingpositions/analyzer.py +452 -0
  22. swingpositions-0.1.0/src/swingpositions/cli.py +302 -0
  23. swingpositions-0.1.0/src/swingpositions/data/default_accuracy.json +302 -0
  24. swingpositions-0.1.0/src/swingpositions/data/default_params.json +32 -0
  25. swingpositions-0.1.0/src/swingpositions/demo.py +289 -0
  26. swingpositions-0.1.0/src/swingpositions/evaluate.py +93 -0
  27. swingpositions-0.1.0/src/swingpositions/features.py +97 -0
  28. swingpositions-0.1.0/src/swingpositions/fit.py +152 -0
  29. swingpositions-0.1.0/src/swingpositions/models.py +91 -0
  30. swingpositions-0.1.0/src/swingpositions/report.py +86 -0
  31. swingpositions-0.1.0/src/swingpositions/synthetic.py +229 -0
  32. swingpositions-0.1.0/src/swingpositions/tracking.py +202 -0
  33. swingpositions-0.1.0/src/swingpositions/video.py +100 -0
  34. swingpositions-0.1.0/src/swingpositions/viewer.py +587 -0
  35. swingpositions-0.1.0/src/swingpositions/viewer_static/app.css +375 -0
  36. swingpositions-0.1.0/src/swingpositions/viewer_static/fonts/Barlow-Medium.woff2 +0 -0
  37. swingpositions-0.1.0/src/swingpositions/viewer_static/fonts/Barlow-Regular.woff2 +0 -0
  38. swingpositions-0.1.0/src/swingpositions/viewer_static/fonts/Barlow-SemiBold.woff2 +0 -0
  39. swingpositions-0.1.0/src/swingpositions/viewer_static/fonts/OFL.txt +93 -0
  40. swingpositions-0.1.0/src/swingpositions/viewer_static/icon.svg +1 -0
  41. swingpositions-0.1.0/src/swingpositions/viewer_static/index.html +40 -0
  42. swingpositions-0.1.0/src/swingpositions/viewer_static/js/app.mjs +114 -0
  43. swingpositions-0.1.0/src/swingpositions/viewer_static/js/core.mjs +350 -0
  44. swingpositions-0.1.0/src/swingpositions/viewer_static/js/dom.mjs +62 -0
  45. swingpositions-0.1.0/src/swingpositions/viewer_static/js/player.mjs +184 -0
  46. swingpositions-0.1.0/src/swingpositions/viewer_static/js/reading.mjs +80 -0
  47. swingpositions-0.1.0/src/swingpositions/viewer_static/js/store.mjs +100 -0
  48. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/about.mjs +80 -0
  49. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/compare.mjs +149 -0
  50. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/corrections.mjs +76 -0
  51. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/gallery.mjs +53 -0
  52. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/list.mjs +88 -0
  53. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/swing.mjs +222 -0
  54. swingpositions-0.1.0/src/swingpositions/viewer_static/js/views/tempo.mjs +153 -0
  55. swingpositions-0.1.0/tests/test_cli.py +99 -0
  56. swingpositions-0.1.0/tests/test_math.py +97 -0
  57. swingpositions-0.1.0/tests/test_models.py +76 -0
  58. swingpositions-0.1.0/tests/test_synthetic_recovery.py +158 -0
  59. swingpositions-0.1.0/tests/test_tracking_club.py +61 -0
  60. swingpositions-0.1.0/tests/test_video.py +65 -0
  61. swingpositions-0.1.0/tests/test_viewer.py +280 -0
  62. swingpositions-0.1.0/tests/viewer/fixtures/data.json +1423 -0
  63. swingpositions-0.1.0/tests/viewer/frame-math.test.mjs +89 -0
  64. swingpositions-0.1.0/tests/viewer/helpers.mjs +31 -0
  65. swingpositions-0.1.0/tests/viewer/import.test.mjs +103 -0
  66. swingpositions-0.1.0/tests/viewer/make_fixture.py +31 -0
  67. swingpositions-0.1.0/tests/viewer/routing.test.mjs +75 -0
  68. swingpositions-0.1.0/tests/viewer/static.test.mjs +53 -0
  69. swingpositions-0.1.0/tests/viewer/tempo.test.mjs +156 -0
@@ -0,0 +1,33 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ .venv/
5
+ venv/
6
+ build/
7
+ dist/
8
+ .pytest_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ .DS_Store
12
+
13
+ # never commit videos, tracks, model files or analysis output
14
+ *.mp4
15
+ *.mov
16
+ *.MOV
17
+ *.m4v
18
+ *.avi
19
+ *.npz
20
+ *.task
21
+ *.jpg
22
+ *.jpeg
23
+ *.png
24
+ !docs/images/*.png
25
+ !docs/images/*.jpg
26
+ !examples/*.mp4
27
+ /out/
28
+ /results/
29
+
30
+ # local tooling
31
+ uv.lock
32
+ /site/
33
+ /demo-site/
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (unreleased)
4
+
5
+ First release.
6
+
7
+ - `swingpositions analyze`: P1-P10, tempo and flags for every swing in a down-the-line video (right-handed),
8
+ optional stills, JSON output (schema_version 1).
9
+ - Slow-motion handling: capture rate inferred from the downswing length (30 / 120 / 240 fps) or set with
10
+ `--capture-fps`; all times in real seconds.
11
+ - `track`, `fit`, `evaluate`: save tracks, fit parameters to your own labelled swings, leave-one-swing-out
12
+ accuracy.
13
+ - `model download|path`: MediaPipe Pose Landmarker (heavy) fetched from Google on first use, SHA-256 pinned.
14
+ - `swingpositions viewer`: static review site built from results and videos (frame-accurate player,
15
+ position gallery, tempo charts, compare, About with the accuracy table), corrections saved in the browser
16
+ with JSON export/import, optional reference picks, `--serve` preview with HTTP Range support, and
17
+ `--demo` built from synthetic swings. `data.json` schema in docs/viewer-data.md.
18
+ - Synthetic swing generator (now with per-phase timing, `scaled_times`) and a test suite that needs no
19
+ video and no network; node tests for the viewer logic.
20
+ - Release workflow: tag `vX.Y.Z` publishes to PyPI with trusted publishing (docs/RELEASING.md).
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 John Montgomery
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,22 @@
1
+ swingpositions
2
+ Copyright 2026 John Montgomery
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (see LICENSE).
5
+
6
+ Example media:
7
+ - examples/*.mp4: the author's own swing videos, licensed CC BY 4.0 (see examples/README.md). Not included in
8
+ the built package.
9
+
10
+ Bundled third-party files:
11
+ - Barlow font (src/swingpositions/viewer_static/fonts/Barlow-*.woff2), Copyright 2017 The Barlow Project
12
+ Authors (https://github.com/jpt/barlow), SIL Open Font License 1.1; the licence text ships next to the
13
+ font files as OFL.txt.
14
+
15
+ Third-party software used at run time (installed as dependencies, not bundled):
16
+ - MediaPipe (https://github.com/google-ai-edge/mediapipe), Apache License 2.0.
17
+ - OpenCV (https://opencv.org), Apache License 2.0.
18
+ - NumPy and SciPy, BSD licenses.
19
+
20
+ The MediaPipe Pose Landmarker model file is NOT included in this distribution. `swingpositions model download`
21
+ fetches it from Google's official model storage on first use. Its use is subject to Google's terms and model
22
+ card: https://ai.google.dev/edge/mediapipe/solutions/vision/pose_landmarker
@@ -0,0 +1,317 @@
1
+ Metadata-Version: 2.5
2
+ Name: swingpositions
3
+ Version: 0.1.0
4
+ Summary: Find the ten golf swing P-positions (P1-P10) and tempo in a down-the-line swing video.
5
+ Project-URL: Homepage, https://github.com/m0ntydad0n/swingpositions
6
+ Project-URL: Repository, https://github.com/m0ntydad0n/swingpositions
7
+ Project-URL: Issues, https://github.com/m0ntydad0n/swingpositions/issues
8
+ Project-URL: Changelog, https://github.com/m0ntydad0n/swingpositions/blob/main/CHANGELOG.md
9
+ Author-email: John Montgomery <19865096+m0ntydad0n@users.noreply.github.com>
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ License-File: NOTICE
13
+ Keywords: golf,mediapipe,p-positions,pose-estimation,swing,tempo,video-analysis,viewer
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Intended Audience :: Science/Research
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3 :: Only
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Multimedia :: Video
24
+ Classifier: Topic :: Scientific/Engineering :: Image Recognition
25
+ Requires-Python: >=3.10
26
+ Requires-Dist: mediapipe>=0.10.14
27
+ Requires-Dist: numpy>=1.23
28
+ Requires-Dist: opencv-contrib-python>=4.8
29
+ Requires-Dist: scipy<1.15,>=1.9; python_version < '3.11' and sys_platform == 'darwin'
30
+ Requires-Dist: scipy>=1.9; python_version >= '3.11' or sys_platform != 'darwin'
31
+ Provides-Extra: dev
32
+ Requires-Dist: build>=1; extra == 'dev'
33
+ Requires-Dist: pytest>=7; extra == 'dev'
34
+ Requires-Dist: ruff>=0.5; extra == 'dev'
35
+ Provides-Extra: test
36
+ Requires-Dist: pytest>=7; extra == 'test'
37
+ Description-Content-Type: text/markdown
38
+
39
+ # swingpositions
40
+
41
+ ![A golfer at the top of the backswing, seen down the line at golden hour, with a faint trail of the downswing](https://raw.githubusercontent.com/m0ntydad0n/swingpositions/main/docs/images/hero.jpg)
42
+
43
+ [![ci](https://github.com/m0ntydad0n/swingpositions/actions/workflows/ci.yml/badge.svg)](https://github.com/m0ntydad0n/swingpositions/actions/workflows/ci.yml)
44
+ [![PyPI](https://img.shields.io/pypi/v/swingpositions)](https://pypi.org/project/swingpositions/)
45
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
46
+
47
+ Find the ten golf swing **P-positions** (P1 address to P10 finish) and your **tempo** in a down-the-line
48
+ swing video. Runs locally on your own computer: pose estimation with Google's MediaPipe, a club tracker
49
+ built from moving pixels, and readable cues for each position that you can refit to your own swings.
50
+
51
+ ```sh
52
+ pip install swingpositions
53
+ swingpositions analyze my_swing.mov --out result.json --stills stills/
54
+ ```
55
+
56
+ ```
57
+ swing 1 (120 fps, downswing-length): P1=97 P2=165 P3=191 P4=224 P5=237 P6=243 P7=252 P8=257 P9=271 P10=332 | backswing 1.06s downswing 0.23s ratio 4.5:1
58
+ ```
59
+
60
+ (Output for the bundled synthetic example swing; see [docs/output-schema.md](docs/output-schema.md).)
61
+
62
+ Then review every swing frame by frame in a static web viewer and fix any position by hand:
63
+
64
+ ```sh
65
+ swingpositions viewer result.json --video my_swing.mov --out site/ --serve 8000
66
+ ```
67
+
68
+ > Status: 0.1.0, first release. Right-handed golfers, down-the-line camera only.
69
+
70
+ ## What P1-P10 are
71
+
72
+ The P-system splits the swing into ten checkpoints that coaches use to compare positions frame by frame:
73
+
74
+ ![The ten P-positions drawn on one golfer, down-the-line view: P1 address through P10 finish](https://raw.githubusercontent.com/m0ntydad0n/swingpositions/main/docs/images/p-positions.jpg)
75
+
76
+ <sub>Illustrations were made with an image model, using real swing frames as pose guides, and checked position by position. They are not analyzer output; the viewer screenshots further down are.</sub>
77
+
78
+ | | position | what it looks like |
79
+ |---|---|---|
80
+ | P1 | Address | set up and still, just before the club starts back |
81
+ | P2 | Shaft parallel, backswing | club shaft parallel to the ground, pointing along the target line |
82
+ | P3 | Lead arm parallel, backswing | lead (left) arm parallel to the ground |
83
+ | P4 | Top | the club changes direction |
84
+ | P5 | Lead arm parallel, downswing | lead arm parallel to the ground again on the way down |
85
+ | P6 | Shaft parallel, downswing | shaft parallel to the ground before impact |
86
+ | P7 | Impact | club meets ball |
87
+ | P8 | Shaft parallel, follow-through | shaft parallel to the ground after impact |
88
+ | P9 | Trail arm parallel, follow-through | trail (right) arm parallel to the ground |
89
+ | P10 | Finish | body and club at rest |
90
+
91
+ ## How it works
92
+
93
+ 1. **Tracking** (`tracking.py`). A golfer crop box is found from a few sample frames, then every frame of
94
+ the crop goes through the MediaPipe Pose Landmarker (heavy model) in video mode, giving 33 body landmarks
95
+ and a person mask. The club shaft is too thin and fast for a pose model, so it is read from pixels that
96
+ changed against both neighbouring frames, outside the person mask, in a ring around the hands: the
97
+ strongest ray from the grip is the shaft, and its far end is the clubhead.
98
+ 2. **Features** (`features.py`). Hand height and position are measured in *torso lengths* (shoulder centre
99
+ to hip centre at address), so the numbers do not depend on how far away the camera is.
100
+ 3. **Cues** (`analyzer.py`), one per position:
101
+
102
+ | | cue | fallback |
103
+ |---|---|---|
104
+ | P1 | walking back from P2, the first frame where the hands were at rest | first frame (flagged `address_before_clip`) |
105
+ | P2 | the clubhead passes the height of the hands on the way up (a short gap while the shaft points straight at the camera is bridged) | hands rise through the fitted P2 hand height |
106
+ | P3 | the lead wrist rises to lead-shoulder height | 30 frames (at 120 fps) before the top |
107
+ | P4 | the middle of the pause of the hands at the top | - |
108
+ | P5 | the hands drop through the fitted lead-arm-parallel hand height | halfway from top to impact |
109
+ | P6 | the clubhead passes hand height on the way down | hands drop through the fitted P6 hand height |
110
+ | P7 | the lowest point of the hands after the top (sub-frame) | - |
111
+ | P8 | the clubhead passes hand height after impact | hands rise through the fitted P8 hand height |
112
+ | P9 | the shaft points well up in the follow-through (clubhead about 1.15 torso lengths above the hands) | trail wrist rises to trail-shoulder height |
113
+ | P10 | the hands come to rest after the follow-through | last frame (flagged `finish_after_clip`) |
114
+
115
+ Each cue has a fitted frame offset (bias) and the hand-height thresholds are fitted too
116
+ (`src/swingpositions/data/default_params.json`); see [Fit your own parameters](#fit-your-own-parameters).
117
+ Every event in the output says which cue placed it, with a confidence.
118
+ 4. **Slow motion** (`video.py`). Phone slow motion is usually exported "flattened": every frame is kept but
119
+ the file plays at 30 fps. The analyzer needs the real capture rate, so it measures the downswing (top of
120
+ the hands to their lowest point) in stored frames: under 15 frames at 30 fps is real time, under 45 is
121
+ 120 fps slow motion, longer is 240 fps. Files whose container says 100 fps or more are taken at their
122
+ word. **Pass `--capture-fps 120` (or 240, 60, 30) whenever you know the answer**, especially for 240 fps,
123
+ where unusually fast or slow swings overlap with 120 fps ones. All times in the output are real seconds.
124
+ 5. **Several swings in one video.** Every high-hands-then-fast-drop pattern is a candidate; the hands coming
125
+ down slowly after a finish do not count, and two swings must be at least 3 real seconds apart. Each
126
+ swing is analysed on its own window (impact plus or minus 3 seconds).
127
+
128
+ ## Filming guide
129
+
130
+ - **Down the line**: camera behind the golfer, looking along the target line (through the hands toward
131
+ the target).
132
+ - **Camera at about hand height**, on a tripod or a stand, not hand-held.
133
+ - **Whole body and the whole club in frame for the entire swing**, including the club at the top and at the
134
+ finish. Leave room above the head.
135
+ - **Slow motion recommended** (120 or 240 fps). Real-time 30 fps works, but every frame is 33 ms, so
136
+ positions are only as precise as that.
137
+ - Start recording before you address the ball and stop after you have held your finish, so P1 and P10 are
138
+ inside the clip.
139
+ - Good light and a background that does not move (other golfers swinging behind you confuse the club
140
+ tracker).
141
+
142
+ ## Install
143
+
144
+ Python 3.10 or newer. MediaPipe and OpenCV are installed as dependencies.
145
+
146
+ ```sh
147
+ pip install swingpositions
148
+ swingpositions model download # optional: otherwise the model downloads on first use
149
+ ```
150
+
151
+ Or the latest code from GitHub: `pip install git+https://github.com/m0ntydad0n/swingpositions`.
152
+
153
+ The MediaPipe pose model (about 30 MB) is not part of this package. It is fetched from Google's official
154
+ model storage on first use, checked against a pinned SHA-256 and kept in `~/.cache/swingpositions`
155
+ (`SWINGPOSITIONS_CACHE` to change the directory, `SWINGPOSITIONS_MODEL` to use a file you already have).
156
+
157
+ ## Usage
158
+
159
+ ```sh
160
+ swingpositions analyze VIDEO [--capture-fps N] [--out result.json] [--stills DIR]
161
+ [--params my_params.json] [--save-track swing.npz]
162
+ swingpositions track VIDEO --out swing.npz # pose + club only, for refitting
163
+ swingpositions fit LABELS.json TRACKS_DIR [--out my_params.json]
164
+ swingpositions evaluate LABELS.json TRACKS_DIR [--params my_params.json] [--out rows.json]
165
+ swingpositions model download|path
166
+ swingpositions viewer RESULT.json [RESULT2.json ...] --video VIDEO [--video VIDEO2 ...] --out DIR [--serve PORT]
167
+ swingpositions viewer --demo --out DIR [--serve PORT]
168
+ ```
169
+
170
+ - `analyze` also accepts a saved `.npz` track instead of a video, which skips pose estimation.
171
+ - `--stills DIR` writes one JPEG per position per swing (`swing1_P1.jpg` ...), cropped around the golfer.
172
+ - The result JSON is documented in [docs/output-schema.md](docs/output-schema.md): for each swing, ten events
173
+ with `frame`, `time_s` (real seconds), `video_time_s` (playback position), `cue` and `confidence`;
174
+ `tempo` (backswing, downswing, ratio, takeaway, transition, delivery, release in real seconds); and
175
+ `flags` for an address or finish that falls outside the clip.
176
+
177
+ From Python:
178
+
179
+ ```python
180
+ from swingpositions.tracking import track_video
181
+ from swingpositions.analyzer import analyze_video_track
182
+
183
+ track = track_video("my_swing.mov")
184
+ capture_fps, source, swings = analyze_video_track(track)
185
+ for swing in swings:
186
+ print([(e["p"], e["frame"], e["time_s"]) for e in swing["events"]], swing["tempo"]["ratio"])
187
+ ```
188
+
189
+ ## Accuracy
190
+
191
+ **Evaluated on 48 swings from one golfer, one camera setup** (a phone filming down the line at a driving
192
+ range; 33 swings in 120 fps slow motion and 15 in real-time 30 fps). Each swing was predicted with
193
+ parameters fitted on the other 47 only (leave-one-swing-out), so these are out-of-sample numbers. The
194
+ bundled default parameters are the fit on all 48.
195
+
196
+ Reference labels are positions the golfer marked or corrected by hand. Numbers are the error in frames;
197
+ at 120 fps a frame is 8.3 ms.
198
+
199
+ **Slow motion, 120 fps, golfer-marked labels**
200
+
201
+ | | n | mean abs error (frames) | within 1 | within 2 | within 3 |
202
+ |---|---:|---:|---:|---:|---:|
203
+ | P1 | 2 | 2.50 | 0% | 50% | 100% |
204
+ | P2 | 19 | 0.84 | 89% | 100% | 100% |
205
+ | P3 | 16 | 1.81 | 56% | 69% | 88% |
206
+ | P4 | 14 | 1.43 | 50% | 93% | 100% |
207
+ | P5 | 12 | 0.50 | 100% | 100% | 100% |
208
+ | P6 | 10 | 0.20 | 100% | 100% | 100% |
209
+ | P7 | 9 | 0.33 | 100% | 100% | 100% |
210
+ | P8 | 6 | 1.17 | 83% | 83% | 83% |
211
+ | P9 | 3 | 2.67 | 67% | 67% | 67% |
212
+ | P10 | 3 | 16.67 | 33% | 33% | 33% |
213
+
214
+ **Real time, 30 fps, golfer-marked labels** (a frame is 33 ms; very few labels): P1-P3, P6, P8 and P9 were
215
+ exact on every labelled swing (n = 2 to 4 each); mean error was 0.50 frames for P4, 1.00 for P5 and P7, and
216
+ 6.5 frames for P10 (n = 2).
217
+
218
+ A second, weaker reference set (positions from an earlier assisted labelling pass that the golfer did not
219
+ correct) gives, at 120 fps: P2 3.71 frames mean error (n = 14), P3 2.41 (17), P4 2.68 (19), P5 1.14 (21),
220
+ P6 0.75 (4), P7 0.75 (24), P10 11.1 (18).
221
+
222
+ Read these honestly:
223
+
224
+ - One golfer, one camera, one session. Your swing, camera height and lighting will differ. Refit on a few
225
+ of your own swings if the results look consistently early or late.
226
+ - Several positions have very few labels (P1, P9, P10 in particular), so their numbers are rough.
227
+ - **P10 is the weakest position**: "the finish" is a judgement call (when exactly is the golfer at rest?),
228
+ and the "hands at rest" rule disagrees with a human by 11-17 frames at 120 fps on average. P1 is the
229
+ same kind of judgement but was closer here.
230
+
231
+ ## Limits
232
+
233
+ - **Right-handed golfers only.** Lead/trail sides are hard-coded (left = lead).
234
+ - **Down-the-line view only.** Face-on video is not supported.
235
+ - **2D.** Everything is measured in the image plane; "parallel to the ground" is approximated by heights
236
+ in the picture, which depends on the camera being roughly at hand height and behind the hands.
237
+ - **P1 and P10 are judgement positions** and the least precise (see above).
238
+ - The club tracker needs a fairly still background and the club visible against it.
239
+ - Slow-motion detection can mistake 240 fps for 120 fps on unusual swings; pass `--capture-fps`.
240
+
241
+ ## Fit your own parameters
242
+
243
+ Label a handful of your own swings (frame numbers of the positions you are sure about), then:
244
+
245
+ ```sh
246
+ swingpositions track swing_01.mp4 --out tracks/swing_01.npz # once per swing
247
+ swingpositions fit labels.json tracks/ --out my_params.json
248
+ swingpositions evaluate labels.json tracks/ # leave-one-swing-out accuracy
249
+ swingpositions analyze new_swing.mp4 --params my_params.json
250
+ ```
251
+
252
+ The labels format and what fitting does are described in [docs/labels.md](docs/labels.md).
253
+
254
+ ## Viewer
255
+
256
+ `swingpositions viewer` builds a self-contained static site from your results and videos: a frame-accurate
257
+ player with the ten positions marked, a gallery of one position across all swings, tempo charts, a
258
+ side-by-side compare view, and an About page with the accuracy table. Move any position the analyzer got
259
+ wrong; corrections stay in your browser and can be exported and imported as JSON.
260
+
261
+ ```sh
262
+ swingpositions viewer swing_01.json swing_02.json --video swing_01.mp4 --video swing_02.mov --out site/
263
+ swingpositions viewer --out site/ --serve 8000 # preview on http://127.0.0.1:8000/
264
+ swingpositions viewer --demo --out demo-site/ --serve 8000 # synthetic demo, no video needed
265
+ ```
266
+
267
+ ### Try it on real swings
268
+
269
+ [`examples/`](examples/) has three real down-the-line swings (two irons and a driver, iPhone slow motion,
270
+ 120 fps) with the analyzer's results next to them. Build the viewer from them in one go:
271
+
272
+ ```sh
273
+ git clone https://github.com/m0ntydad0n/swingpositions && cd swingpositions
274
+ swingpositions viewer examples/swing-1.json examples/swing-2.json examples/swing-3.json \
275
+ --video examples/swing-1.mp4 --video examples/swing-2.mp4 --video examples/swing-3.mp4 \
276
+ --out example-site/ --serve 8000
277
+ ```
278
+
279
+ ![Swing page at 1440 px: the player with position ticks, P1-P10 chips, the selected position's cue, confidence and reference pick, and the swing's tempo](https://raw.githubusercontent.com/m0ntydad0n/swingpositions/main/docs/images/swing-desktop-1440.png)
280
+
281
+ <img alt="Gallery of P7 (impact) across the three example swings on a 390 px phone screen" src="https://raw.githubusercontent.com/m0ntydad0n/swingpositions/main/docs/images/gallery-phone-390.png" width="300">
282
+
283
+ (Screenshots of the viewer built from the three example swings in [`examples/`](examples/), exactly as
284
+ `swingpositions analyze` and `swingpositions viewer` produce them.)
285
+
286
+ With ffmpeg on PATH each swing is cut to its own short H.264 clip (keyframe every 6 frames, every stored
287
+ frame kept on a 30 fps timeline) so stepping frame by frame is exact; without ffmpeg the videos are copied
288
+ as they are, with a warning. The site is plain HTML, CSS and ES modules with a strict Content-Security-Policy
289
+ and no network calls, so any static host works. Options, routes, keys and the corrections file:
290
+ [docs/viewer.md](docs/viewer.md); the `data.json` it reads: [docs/viewer-data.md](docs/viewer-data.md).
291
+
292
+ ## Development
293
+
294
+ ```sh
295
+ pip install -e ".[dev]"
296
+ pytest # synthetic data only, no video, no network
297
+ ruff check .
298
+ node --test tests/viewer/*.test.mjs # viewer logic (Node 22+, no dependencies)
299
+ python tests/viewer/make_fixture.py # refresh the node tests' data.json after analyzer changes
300
+ node scripts/screenshots.mjs --base http://127.0.0.1:8000/ --out docs/images # needs Chrome and a served demo
301
+ ```
302
+
303
+ Releases: [docs/RELEASING.md](docs/RELEASING.md).
304
+
305
+ Tests never touch a real video or the network: `swingpositions.synthetic` builds parametric swings
306
+ (hands rise, pause at the top, drop fast, rise to a held finish, with landmark noise, pose glitches and
307
+ short club dropouts) with planted P-positions, and the tests check that the analyzer finds them.
308
+
309
+ ## Credits and licence
310
+
311
+ - Licensed under the [Apache License 2.0](LICENSE).
312
+ - Pose estimation by [MediaPipe](https://github.com/google-ai-edge/mediapipe) (Apache-2.0), using the
313
+ Pose Landmarker heavy model. The model file is downloaded from Google and is subject to Google's terms
314
+ and model card: <https://ai.google.dev/edge/mediapipe/solutions/vision/pose_landmarker>.
315
+ - Video decoding and image processing by [OpenCV](https://opencv.org) (Apache-2.0).
316
+ - The viewer uses the [Barlow](https://github.com/jpt/barlow) typeface (SIL Open Font License 1.1, licence
317
+ shipped next to the font files).