kinemotion 0.10.3__tar.gz → 0.10.4__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.
Potentially problematic release.
This version of kinemotion might be problematic. Click here for more details.
- {kinemotion-0.10.3 → kinemotion-0.10.4}/CHANGELOG.md +8 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/PKG-INFO +1 -1
- {kinemotion-0.10.3 → kinemotion-0.10.4}/pyproject.toml +1 -1
- kinemotion-0.10.4/src/kinemotion/api.py +604 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/uv.lock +1 -1
- kinemotion-0.10.3/src/kinemotion/api.py +0 -428
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.dockerignore +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.github/ISSUE_TEMPLATE/bug_report.yml +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.github/ISSUE_TEMPLATE/feature_request.yml +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.github/pull_request_template.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.github/workflows/release.yml +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.gitignore +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.pre-commit-config.yaml +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/.tool-versions +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/CLAUDE.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/CODE_OF_CONDUCT.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/CONTRIBUTING.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/Dockerfile +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/GEMINI.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/LICENSE +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/README.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/SECURITY.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/BULK_PROCESSING.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/ERRORS_FINDINGS.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/FRAMERATE.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/IMU_METADATA_PRESERVATION.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/PARAMETERS.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/docs/VALIDATION_PLAN.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/examples/bulk/README.md +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/examples/bulk/bulk_processing.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/examples/bulk/simple_example.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/examples/programmatic_usage.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/__init__.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/cli.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/__init__.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/auto_tuning.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/filtering.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/pose.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/smoothing.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/core/video_io.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/dropjump/__init__.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/dropjump/analysis.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/dropjump/cli.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/dropjump/debug_overlay.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/dropjump/kinematics.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/src/kinemotion/py.typed +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/__init__.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_adaptive_threshold.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_api.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_aspect_ratio.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_com_estimation.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_contact_detection.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_filtering.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_kinematics.py +0 -0
- {kinemotion-0.10.3 → kinemotion-0.10.4}/tests/test_polyorder.py +0 -0
|
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
<!-- version list -->
|
|
9
9
|
|
|
10
|
+
## v0.10.4 (2025-11-03)
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
- **api**: Reduce cognitive complexity in process_video function
|
|
15
|
+
([`d2e05cb`](https://github.com/feniix/kinemotion/commit/d2e05cb415067a1a1b081216a9474ccda1ae2567))
|
|
16
|
+
|
|
17
|
+
|
|
10
18
|
## v0.10.3 (2025-11-03)
|
|
11
19
|
|
|
12
20
|
### Bug Fixes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kinemotion
|
|
3
|
-
Version: 0.10.
|
|
3
|
+
Version: 0.10.4
|
|
4
4
|
Summary: Video-based kinematic analysis for athletic performance
|
|
5
5
|
Project-URL: Homepage, https://github.com/feniix/kinemotion
|
|
6
6
|
Project-URL: Repository, https://github.com/feniix/kinemotion
|
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
"""Public API for programmatic use of kinemotion analysis."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from concurrent.futures import ProcessPoolExecutor, as_completed
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from .core.auto_tuning import (
|
|
12
|
+
AnalysisParameters,
|
|
13
|
+
QualityPreset,
|
|
14
|
+
VideoCharacteristics,
|
|
15
|
+
analyze_video_sample,
|
|
16
|
+
auto_tune_parameters,
|
|
17
|
+
)
|
|
18
|
+
from .core.pose import PoseTracker
|
|
19
|
+
from .core.smoothing import smooth_landmarks, smooth_landmarks_advanced
|
|
20
|
+
from .core.video_io import VideoProcessor
|
|
21
|
+
from .dropjump.analysis import (
|
|
22
|
+
ContactState,
|
|
23
|
+
compute_average_foot_position,
|
|
24
|
+
detect_ground_contact,
|
|
25
|
+
)
|
|
26
|
+
from .dropjump.debug_overlay import DebugOverlayRenderer
|
|
27
|
+
from .dropjump.kinematics import DropJumpMetrics, calculate_drop_jump_metrics
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _parse_quality_preset(quality: str) -> QualityPreset:
|
|
31
|
+
"""Parse and validate quality preset string.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
quality: Quality preset string ('fast', 'balanced', or 'accurate')
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
QualityPreset enum value
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
ValueError: If quality preset is invalid
|
|
41
|
+
"""
|
|
42
|
+
try:
|
|
43
|
+
return QualityPreset(quality.lower())
|
|
44
|
+
except ValueError as e:
|
|
45
|
+
raise ValueError(
|
|
46
|
+
f"Invalid quality preset: {quality}. Must be 'fast', 'balanced', or 'accurate'"
|
|
47
|
+
) from e
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _determine_confidence_levels(
|
|
51
|
+
quality_preset: QualityPreset,
|
|
52
|
+
detection_confidence: float | None,
|
|
53
|
+
tracking_confidence: float | None,
|
|
54
|
+
) -> tuple[float, float]:
|
|
55
|
+
"""Determine detection and tracking confidence levels.
|
|
56
|
+
|
|
57
|
+
Confidence levels are set based on quality preset and can be overridden
|
|
58
|
+
by expert parameters.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
quality_preset: Quality preset enum
|
|
62
|
+
detection_confidence: Optional expert override for detection confidence
|
|
63
|
+
tracking_confidence: Optional expert override for tracking confidence
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Tuple of (detection_confidence, tracking_confidence)
|
|
67
|
+
"""
|
|
68
|
+
# Set initial confidence from quality preset
|
|
69
|
+
initial_detection_conf = 0.5
|
|
70
|
+
initial_tracking_conf = 0.5
|
|
71
|
+
|
|
72
|
+
if quality_preset == QualityPreset.FAST:
|
|
73
|
+
initial_detection_conf = 0.3
|
|
74
|
+
initial_tracking_conf = 0.3
|
|
75
|
+
elif quality_preset == QualityPreset.ACCURATE:
|
|
76
|
+
initial_detection_conf = 0.6
|
|
77
|
+
initial_tracking_conf = 0.6
|
|
78
|
+
|
|
79
|
+
# Override with expert values if provided
|
|
80
|
+
if detection_confidence is not None:
|
|
81
|
+
initial_detection_conf = detection_confidence
|
|
82
|
+
if tracking_confidence is not None:
|
|
83
|
+
initial_tracking_conf = tracking_confidence
|
|
84
|
+
|
|
85
|
+
return initial_detection_conf, initial_tracking_conf
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _apply_expert_overrides(
|
|
89
|
+
params: AnalysisParameters,
|
|
90
|
+
smoothing_window: int | None,
|
|
91
|
+
velocity_threshold: float | None,
|
|
92
|
+
min_contact_frames: int | None,
|
|
93
|
+
visibility_threshold: float | None,
|
|
94
|
+
) -> AnalysisParameters:
|
|
95
|
+
"""Apply expert parameter overrides to auto-tuned parameters.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
params: Auto-tuned parameters object
|
|
99
|
+
smoothing_window: Optional override for smoothing window
|
|
100
|
+
velocity_threshold: Optional override for velocity threshold
|
|
101
|
+
min_contact_frames: Optional override for minimum contact frames
|
|
102
|
+
visibility_threshold: Optional override for visibility threshold
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
Modified params object (mutated in place)
|
|
106
|
+
"""
|
|
107
|
+
if smoothing_window is not None:
|
|
108
|
+
params.smoothing_window = smoothing_window
|
|
109
|
+
if velocity_threshold is not None:
|
|
110
|
+
params.velocity_threshold = velocity_threshold
|
|
111
|
+
if min_contact_frames is not None:
|
|
112
|
+
params.min_contact_frames = min_contact_frames
|
|
113
|
+
if visibility_threshold is not None:
|
|
114
|
+
params.visibility_threshold = visibility_threshold
|
|
115
|
+
return params
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _print_verbose_parameters(
|
|
119
|
+
video: VideoProcessor,
|
|
120
|
+
characteristics: VideoCharacteristics,
|
|
121
|
+
quality_preset: QualityPreset,
|
|
122
|
+
params: AnalysisParameters,
|
|
123
|
+
) -> None:
|
|
124
|
+
"""Print auto-tuned parameters in verbose mode.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
video: Video processor with fps and dimensions
|
|
128
|
+
characteristics: Video analysis characteristics
|
|
129
|
+
quality_preset: Selected quality preset
|
|
130
|
+
params: Auto-tuned parameters
|
|
131
|
+
"""
|
|
132
|
+
print("\n" + "=" * 60)
|
|
133
|
+
print("AUTO-TUNED PARAMETERS")
|
|
134
|
+
print("=" * 60)
|
|
135
|
+
print(f"Video FPS: {video.fps:.2f}")
|
|
136
|
+
print(
|
|
137
|
+
f"Tracking quality: {characteristics.tracking_quality} "
|
|
138
|
+
f"(avg visibility: {characteristics.avg_visibility:.2f})"
|
|
139
|
+
)
|
|
140
|
+
print(f"Quality preset: {quality_preset.value}")
|
|
141
|
+
print("\nSelected parameters:")
|
|
142
|
+
print(f" smoothing_window: {params.smoothing_window}")
|
|
143
|
+
print(f" polyorder: {params.polyorder}")
|
|
144
|
+
print(f" velocity_threshold: {params.velocity_threshold:.4f}")
|
|
145
|
+
print(f" min_contact_frames: {params.min_contact_frames}")
|
|
146
|
+
print(f" visibility_threshold: {params.visibility_threshold}")
|
|
147
|
+
print(f" detection_confidence: {params.detection_confidence}")
|
|
148
|
+
print(f" tracking_confidence: {params.tracking_confidence}")
|
|
149
|
+
print(f" outlier_rejection: {params.outlier_rejection}")
|
|
150
|
+
print(f" bilateral_filter: {params.bilateral_filter}")
|
|
151
|
+
print(f" use_curvature: {params.use_curvature}")
|
|
152
|
+
print("=" * 60 + "\n")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _process_all_frames(
|
|
156
|
+
video: VideoProcessor, tracker: PoseTracker, verbose: bool
|
|
157
|
+
) -> tuple[list, list]:
|
|
158
|
+
"""Process all frames from video and extract pose landmarks.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
video: Video processor to read frames from
|
|
162
|
+
tracker: Pose tracker for landmark detection
|
|
163
|
+
verbose: Print progress messages
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
Tuple of (frames, landmarks_sequence)
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
ValueError: If no frames could be processed
|
|
170
|
+
"""
|
|
171
|
+
if verbose:
|
|
172
|
+
print("Tracking pose landmarks...")
|
|
173
|
+
|
|
174
|
+
landmarks_sequence = []
|
|
175
|
+
frames = []
|
|
176
|
+
|
|
177
|
+
while True:
|
|
178
|
+
frame = video.read_frame()
|
|
179
|
+
if frame is None:
|
|
180
|
+
break
|
|
181
|
+
|
|
182
|
+
frames.append(frame)
|
|
183
|
+
landmarks = tracker.process_frame(frame)
|
|
184
|
+
landmarks_sequence.append(landmarks)
|
|
185
|
+
|
|
186
|
+
tracker.close()
|
|
187
|
+
|
|
188
|
+
if not landmarks_sequence:
|
|
189
|
+
raise ValueError("No frames could be processed from video")
|
|
190
|
+
|
|
191
|
+
return frames, landmarks_sequence
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _apply_smoothing(
|
|
195
|
+
landmarks_sequence: list, params: AnalysisParameters, verbose: bool
|
|
196
|
+
) -> list:
|
|
197
|
+
"""Apply smoothing to landmark sequence with auto-tuned parameters.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
landmarks_sequence: Sequence of landmarks from all frames
|
|
201
|
+
params: Auto-tuned parameters containing smoothing settings
|
|
202
|
+
verbose: Print progress messages
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
Smoothed landmarks sequence
|
|
206
|
+
"""
|
|
207
|
+
if params.outlier_rejection or params.bilateral_filter:
|
|
208
|
+
if verbose:
|
|
209
|
+
if params.outlier_rejection:
|
|
210
|
+
print("Smoothing landmarks with outlier rejection...")
|
|
211
|
+
if params.bilateral_filter:
|
|
212
|
+
print("Using bilateral temporal filter...")
|
|
213
|
+
return smooth_landmarks_advanced(
|
|
214
|
+
landmarks_sequence,
|
|
215
|
+
window_length=params.smoothing_window,
|
|
216
|
+
polyorder=params.polyorder,
|
|
217
|
+
use_outlier_rejection=params.outlier_rejection,
|
|
218
|
+
use_bilateral=params.bilateral_filter,
|
|
219
|
+
)
|
|
220
|
+
else:
|
|
221
|
+
if verbose:
|
|
222
|
+
print("Smoothing landmarks...")
|
|
223
|
+
return smooth_landmarks(
|
|
224
|
+
landmarks_sequence,
|
|
225
|
+
window_length=params.smoothing_window,
|
|
226
|
+
polyorder=params.polyorder,
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _extract_vertical_positions(
|
|
231
|
+
smoothed_landmarks: list,
|
|
232
|
+
) -> tuple[np.ndarray, np.ndarray]:
|
|
233
|
+
"""Extract vertical foot positions and visibilities from smoothed landmarks.
|
|
234
|
+
|
|
235
|
+
Args:
|
|
236
|
+
smoothed_landmarks: Smoothed landmark sequence
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
Tuple of (vertical_positions, visibilities) as numpy arrays
|
|
240
|
+
"""
|
|
241
|
+
position_list: list[float] = []
|
|
242
|
+
visibilities_list: list[float] = []
|
|
243
|
+
|
|
244
|
+
for frame_landmarks in smoothed_landmarks:
|
|
245
|
+
if frame_landmarks:
|
|
246
|
+
_, foot_y = compute_average_foot_position(frame_landmarks)
|
|
247
|
+
position_list.append(foot_y)
|
|
248
|
+
|
|
249
|
+
# Average visibility of foot landmarks
|
|
250
|
+
foot_vis = []
|
|
251
|
+
for key in ["left_ankle", "right_ankle", "left_heel", "right_heel"]:
|
|
252
|
+
if key in frame_landmarks:
|
|
253
|
+
foot_vis.append(frame_landmarks[key][2])
|
|
254
|
+
visibilities_list.append(float(np.mean(foot_vis)) if foot_vis else 0.0)
|
|
255
|
+
else:
|
|
256
|
+
position_list.append(position_list[-1] if position_list else 0.5)
|
|
257
|
+
visibilities_list.append(0.0)
|
|
258
|
+
|
|
259
|
+
return np.array(position_list), np.array(visibilities_list)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _generate_outputs(
|
|
263
|
+
metrics: DropJumpMetrics,
|
|
264
|
+
json_output: str | None,
|
|
265
|
+
output_video: str | None,
|
|
266
|
+
frames: list,
|
|
267
|
+
smoothed_landmarks: list,
|
|
268
|
+
contact_states: list[ContactState],
|
|
269
|
+
video: VideoProcessor,
|
|
270
|
+
verbose: bool,
|
|
271
|
+
) -> None:
|
|
272
|
+
"""Generate JSON and debug video outputs if requested.
|
|
273
|
+
|
|
274
|
+
Args:
|
|
275
|
+
metrics: Calculated drop jump metrics
|
|
276
|
+
json_output: Optional path for JSON output
|
|
277
|
+
output_video: Optional path for debug video
|
|
278
|
+
frames: List of video frames
|
|
279
|
+
smoothed_landmarks: Smoothed landmark sequence
|
|
280
|
+
contact_states: Ground contact state for each frame
|
|
281
|
+
video: Video processor with dimensions and fps
|
|
282
|
+
verbose: Print progress messages
|
|
283
|
+
"""
|
|
284
|
+
# Save JSON if requested
|
|
285
|
+
if json_output:
|
|
286
|
+
import json
|
|
287
|
+
|
|
288
|
+
output_path = Path(json_output)
|
|
289
|
+
output_path.write_text(json.dumps(metrics.to_dict(), indent=2))
|
|
290
|
+
if verbose:
|
|
291
|
+
print(f"Metrics written to: {json_output}")
|
|
292
|
+
|
|
293
|
+
# Generate debug video if requested
|
|
294
|
+
if output_video:
|
|
295
|
+
if verbose:
|
|
296
|
+
print(f"Generating debug video: {output_video}")
|
|
297
|
+
|
|
298
|
+
with DebugOverlayRenderer(
|
|
299
|
+
output_video,
|
|
300
|
+
video.width,
|
|
301
|
+
video.height,
|
|
302
|
+
video.display_width,
|
|
303
|
+
video.display_height,
|
|
304
|
+
video.fps,
|
|
305
|
+
) as renderer:
|
|
306
|
+
for i, frame in enumerate(frames):
|
|
307
|
+
annotated = renderer.render_frame(
|
|
308
|
+
frame,
|
|
309
|
+
smoothed_landmarks[i],
|
|
310
|
+
contact_states[i],
|
|
311
|
+
i,
|
|
312
|
+
metrics,
|
|
313
|
+
use_com=False,
|
|
314
|
+
)
|
|
315
|
+
renderer.write_frame(annotated)
|
|
316
|
+
|
|
317
|
+
if verbose:
|
|
318
|
+
print(f"Debug video saved: {output_video}")
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
@dataclass
|
|
322
|
+
class VideoResult:
|
|
323
|
+
"""Result of processing a single video."""
|
|
324
|
+
|
|
325
|
+
video_path: str
|
|
326
|
+
success: bool
|
|
327
|
+
metrics: DropJumpMetrics | None = None
|
|
328
|
+
error: str | None = None
|
|
329
|
+
processing_time: float = 0.0
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
@dataclass
|
|
333
|
+
class VideoConfig:
|
|
334
|
+
"""Configuration for processing a single video."""
|
|
335
|
+
|
|
336
|
+
video_path: str
|
|
337
|
+
drop_height: float
|
|
338
|
+
quality: str = "balanced"
|
|
339
|
+
output_video: str | None = None
|
|
340
|
+
json_output: str | None = None
|
|
341
|
+
drop_start_frame: int | None = None
|
|
342
|
+
smoothing_window: int | None = None
|
|
343
|
+
velocity_threshold: float | None = None
|
|
344
|
+
min_contact_frames: int | None = None
|
|
345
|
+
visibility_threshold: float | None = None
|
|
346
|
+
detection_confidence: float | None = None
|
|
347
|
+
tracking_confidence: float | None = None
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def process_video(
|
|
351
|
+
video_path: str,
|
|
352
|
+
drop_height: float,
|
|
353
|
+
quality: str = "balanced",
|
|
354
|
+
output_video: str | None = None,
|
|
355
|
+
json_output: str | None = None,
|
|
356
|
+
drop_start_frame: int | None = None,
|
|
357
|
+
smoothing_window: int | None = None,
|
|
358
|
+
velocity_threshold: float | None = None,
|
|
359
|
+
min_contact_frames: int | None = None,
|
|
360
|
+
visibility_threshold: float | None = None,
|
|
361
|
+
detection_confidence: float | None = None,
|
|
362
|
+
tracking_confidence: float | None = None,
|
|
363
|
+
verbose: bool = False,
|
|
364
|
+
) -> DropJumpMetrics:
|
|
365
|
+
"""
|
|
366
|
+
Process a single drop jump video and return metrics.
|
|
367
|
+
|
|
368
|
+
Args:
|
|
369
|
+
video_path: Path to the input video file
|
|
370
|
+
drop_height: Height of drop box/platform in meters (e.g., 0.40 for 40cm)
|
|
371
|
+
quality: Analysis quality preset ("fast", "balanced", or "accurate")
|
|
372
|
+
output_video: Optional path for debug video output
|
|
373
|
+
json_output: Optional path for JSON metrics output
|
|
374
|
+
drop_start_frame: Optional manual drop start frame
|
|
375
|
+
smoothing_window: Optional override for smoothing window
|
|
376
|
+
velocity_threshold: Optional override for velocity threshold
|
|
377
|
+
min_contact_frames: Optional override for minimum contact frames
|
|
378
|
+
visibility_threshold: Optional override for visibility threshold
|
|
379
|
+
detection_confidence: Optional override for pose detection confidence
|
|
380
|
+
tracking_confidence: Optional override for pose tracking confidence
|
|
381
|
+
verbose: Print processing details
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
DropJumpMetrics object containing analysis results
|
|
385
|
+
|
|
386
|
+
Raises:
|
|
387
|
+
ValueError: If video cannot be processed or parameters are invalid
|
|
388
|
+
FileNotFoundError: If video file does not exist
|
|
389
|
+
"""
|
|
390
|
+
if not Path(video_path).exists():
|
|
391
|
+
raise FileNotFoundError(f"Video file not found: {video_path}")
|
|
392
|
+
|
|
393
|
+
# Convert quality string to enum
|
|
394
|
+
quality_preset = _parse_quality_preset(quality)
|
|
395
|
+
|
|
396
|
+
# Initialize video processor
|
|
397
|
+
with VideoProcessor(video_path) as video:
|
|
398
|
+
if verbose:
|
|
399
|
+
print(
|
|
400
|
+
f"Video: {video.width}x{video.height} @ {video.fps:.2f} fps, "
|
|
401
|
+
f"{video.frame_count} frames"
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
# Determine detection/tracking confidence levels
|
|
405
|
+
detection_conf, tracking_conf = _determine_confidence_levels(
|
|
406
|
+
quality_preset, detection_confidence, tracking_confidence
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
# Process all frames with pose tracking
|
|
410
|
+
tracker = PoseTracker(
|
|
411
|
+
min_detection_confidence=detection_conf,
|
|
412
|
+
min_tracking_confidence=tracking_conf,
|
|
413
|
+
)
|
|
414
|
+
frames, landmarks_sequence = _process_all_frames(video, tracker, verbose)
|
|
415
|
+
|
|
416
|
+
# Analyze video characteristics and auto-tune parameters
|
|
417
|
+
characteristics = analyze_video_sample(
|
|
418
|
+
landmarks_sequence, video.fps, video.frame_count
|
|
419
|
+
)
|
|
420
|
+
params = auto_tune_parameters(characteristics, quality_preset)
|
|
421
|
+
|
|
422
|
+
# Apply expert overrides if provided
|
|
423
|
+
params = _apply_expert_overrides(
|
|
424
|
+
params,
|
|
425
|
+
smoothing_window,
|
|
426
|
+
velocity_threshold,
|
|
427
|
+
min_contact_frames,
|
|
428
|
+
visibility_threshold,
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
# Show selected parameters if verbose
|
|
432
|
+
if verbose:
|
|
433
|
+
_print_verbose_parameters(video, characteristics, quality_preset, params)
|
|
434
|
+
|
|
435
|
+
# Apply smoothing with auto-tuned parameters
|
|
436
|
+
smoothed_landmarks = _apply_smoothing(landmarks_sequence, params, verbose)
|
|
437
|
+
|
|
438
|
+
# Extract vertical positions from feet
|
|
439
|
+
if verbose:
|
|
440
|
+
print("Extracting foot positions...")
|
|
441
|
+
vertical_positions, visibilities = _extract_vertical_positions(
|
|
442
|
+
smoothed_landmarks
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
# Detect ground contact
|
|
446
|
+
contact_states = detect_ground_contact(
|
|
447
|
+
vertical_positions,
|
|
448
|
+
velocity_threshold=params.velocity_threshold,
|
|
449
|
+
min_contact_frames=params.min_contact_frames,
|
|
450
|
+
visibility_threshold=params.visibility_threshold,
|
|
451
|
+
visibilities=visibilities,
|
|
452
|
+
window_length=params.smoothing_window,
|
|
453
|
+
polyorder=params.polyorder,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
# Calculate metrics
|
|
457
|
+
if verbose:
|
|
458
|
+
print("Calculating metrics...")
|
|
459
|
+
print(
|
|
460
|
+
f"Using drop height calibration: {drop_height}m ({drop_height*100:.0f}cm)"
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
metrics = calculate_drop_jump_metrics(
|
|
464
|
+
contact_states,
|
|
465
|
+
vertical_positions,
|
|
466
|
+
video.fps,
|
|
467
|
+
drop_height_m=drop_height,
|
|
468
|
+
drop_start_frame=drop_start_frame,
|
|
469
|
+
velocity_threshold=params.velocity_threshold,
|
|
470
|
+
smoothing_window=params.smoothing_window,
|
|
471
|
+
polyorder=params.polyorder,
|
|
472
|
+
use_curvature=params.use_curvature,
|
|
473
|
+
kinematic_correction_factor=1.0,
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
# Generate outputs (JSON and debug video)
|
|
477
|
+
_generate_outputs(
|
|
478
|
+
metrics,
|
|
479
|
+
json_output,
|
|
480
|
+
output_video,
|
|
481
|
+
frames,
|
|
482
|
+
smoothed_landmarks,
|
|
483
|
+
contact_states,
|
|
484
|
+
video,
|
|
485
|
+
verbose,
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
if verbose:
|
|
489
|
+
print("Analysis complete!")
|
|
490
|
+
|
|
491
|
+
return metrics
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def process_videos_bulk(
|
|
495
|
+
configs: list[VideoConfig],
|
|
496
|
+
max_workers: int = 4,
|
|
497
|
+
progress_callback: Callable[[VideoResult], None] | None = None,
|
|
498
|
+
) -> list[VideoResult]:
|
|
499
|
+
"""
|
|
500
|
+
Process multiple videos in parallel using ProcessPoolExecutor.
|
|
501
|
+
|
|
502
|
+
Args:
|
|
503
|
+
configs: List of VideoConfig objects specifying video paths and parameters
|
|
504
|
+
max_workers: Maximum number of parallel workers (default: 4)
|
|
505
|
+
progress_callback: Optional callback function called after each video completes.
|
|
506
|
+
Receives VideoResult object.
|
|
507
|
+
|
|
508
|
+
Returns:
|
|
509
|
+
List of VideoResult objects, one per input video, in completion order
|
|
510
|
+
|
|
511
|
+
Example:
|
|
512
|
+
>>> configs = [
|
|
513
|
+
... VideoConfig("video1.mp4", drop_height=0.40),
|
|
514
|
+
... VideoConfig("video2.mp4", drop_height=0.30, quality="accurate"),
|
|
515
|
+
... VideoConfig("video3.mp4", drop_height=0.50, output_video="debug3.mp4"),
|
|
516
|
+
... ]
|
|
517
|
+
>>> results = process_videos_bulk(configs, max_workers=4)
|
|
518
|
+
>>> for result in results:
|
|
519
|
+
... if result.success:
|
|
520
|
+
... print(f"{result.video_path}: {result.metrics.jump_height_m:.3f}m")
|
|
521
|
+
... else:
|
|
522
|
+
... print(f"{result.video_path}: FAILED - {result.error}")
|
|
523
|
+
"""
|
|
524
|
+
results: list[VideoResult] = []
|
|
525
|
+
|
|
526
|
+
# Use ProcessPoolExecutor for CPU-bound video processing
|
|
527
|
+
with ProcessPoolExecutor(max_workers=max_workers) as executor:
|
|
528
|
+
# Submit all jobs
|
|
529
|
+
future_to_config = {
|
|
530
|
+
executor.submit(_process_video_wrapper, config): config
|
|
531
|
+
for config in configs
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
# Process results as they complete
|
|
535
|
+
for future in as_completed(future_to_config):
|
|
536
|
+
config = future_to_config[future]
|
|
537
|
+
result: VideoResult
|
|
538
|
+
|
|
539
|
+
try:
|
|
540
|
+
result = future.result()
|
|
541
|
+
except Exception as exc:
|
|
542
|
+
# Handle unexpected errors
|
|
543
|
+
result = VideoResult(
|
|
544
|
+
video_path=config.video_path,
|
|
545
|
+
success=False,
|
|
546
|
+
error=f"Unexpected error: {str(exc)}",
|
|
547
|
+
)
|
|
548
|
+
|
|
549
|
+
results.append(result)
|
|
550
|
+
|
|
551
|
+
# Call progress callback if provided
|
|
552
|
+
if progress_callback:
|
|
553
|
+
progress_callback(result)
|
|
554
|
+
|
|
555
|
+
return results
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def _process_video_wrapper(config: VideoConfig) -> VideoResult:
|
|
559
|
+
"""
|
|
560
|
+
Wrapper function for parallel processing. Must be picklable (top-level function).
|
|
561
|
+
|
|
562
|
+
Args:
|
|
563
|
+
config: VideoConfig object with processing parameters
|
|
564
|
+
|
|
565
|
+
Returns:
|
|
566
|
+
VideoResult object with metrics or error information
|
|
567
|
+
"""
|
|
568
|
+
start_time = time.time()
|
|
569
|
+
|
|
570
|
+
try:
|
|
571
|
+
metrics = process_video(
|
|
572
|
+
video_path=config.video_path,
|
|
573
|
+
drop_height=config.drop_height,
|
|
574
|
+
quality=config.quality,
|
|
575
|
+
output_video=config.output_video,
|
|
576
|
+
json_output=config.json_output,
|
|
577
|
+
drop_start_frame=config.drop_start_frame,
|
|
578
|
+
smoothing_window=config.smoothing_window,
|
|
579
|
+
velocity_threshold=config.velocity_threshold,
|
|
580
|
+
min_contact_frames=config.min_contact_frames,
|
|
581
|
+
visibility_threshold=config.visibility_threshold,
|
|
582
|
+
detection_confidence=config.detection_confidence,
|
|
583
|
+
tracking_confidence=config.tracking_confidence,
|
|
584
|
+
verbose=False, # Disable verbose in parallel mode
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
processing_time = time.time() - start_time
|
|
588
|
+
|
|
589
|
+
return VideoResult(
|
|
590
|
+
video_path=config.video_path,
|
|
591
|
+
success=True,
|
|
592
|
+
metrics=metrics,
|
|
593
|
+
processing_time=processing_time,
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
except Exception as e:
|
|
597
|
+
processing_time = time.time() - start_time
|
|
598
|
+
|
|
599
|
+
return VideoResult(
|
|
600
|
+
video_path=config.video_path,
|
|
601
|
+
success=False,
|
|
602
|
+
error=str(e),
|
|
603
|
+
processing_time=processing_time,
|
|
604
|
+
)
|
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
"""Public API for programmatic use of kinemotion analysis."""
|
|
2
|
-
|
|
3
|
-
import time
|
|
4
|
-
from collections.abc import Callable
|
|
5
|
-
from concurrent.futures import ProcessPoolExecutor, as_completed
|
|
6
|
-
from dataclasses import dataclass
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
|
|
9
|
-
import numpy as np
|
|
10
|
-
|
|
11
|
-
from .core.auto_tuning import (
|
|
12
|
-
QualityPreset,
|
|
13
|
-
analyze_video_sample,
|
|
14
|
-
auto_tune_parameters,
|
|
15
|
-
)
|
|
16
|
-
from .core.pose import PoseTracker
|
|
17
|
-
from .core.smoothing import smooth_landmarks, smooth_landmarks_advanced
|
|
18
|
-
from .core.video_io import VideoProcessor
|
|
19
|
-
from .dropjump.analysis import compute_average_foot_position, detect_ground_contact
|
|
20
|
-
from .dropjump.debug_overlay import DebugOverlayRenderer
|
|
21
|
-
from .dropjump.kinematics import DropJumpMetrics, calculate_drop_jump_metrics
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@dataclass
|
|
25
|
-
class VideoResult:
|
|
26
|
-
"""Result of processing a single video."""
|
|
27
|
-
|
|
28
|
-
video_path: str
|
|
29
|
-
success: bool
|
|
30
|
-
metrics: DropJumpMetrics | None = None
|
|
31
|
-
error: str | None = None
|
|
32
|
-
processing_time: float = 0.0
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
@dataclass
|
|
36
|
-
class VideoConfig:
|
|
37
|
-
"""Configuration for processing a single video."""
|
|
38
|
-
|
|
39
|
-
video_path: str
|
|
40
|
-
drop_height: float
|
|
41
|
-
quality: str = "balanced"
|
|
42
|
-
output_video: str | None = None
|
|
43
|
-
json_output: str | None = None
|
|
44
|
-
drop_start_frame: int | None = None
|
|
45
|
-
smoothing_window: int | None = None
|
|
46
|
-
velocity_threshold: float | None = None
|
|
47
|
-
min_contact_frames: int | None = None
|
|
48
|
-
visibility_threshold: float | None = None
|
|
49
|
-
detection_confidence: float | None = None
|
|
50
|
-
tracking_confidence: float | None = None
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def process_video(
|
|
54
|
-
video_path: str,
|
|
55
|
-
drop_height: float,
|
|
56
|
-
quality: str = "balanced",
|
|
57
|
-
output_video: str | None = None,
|
|
58
|
-
json_output: str | None = None,
|
|
59
|
-
drop_start_frame: int | None = None,
|
|
60
|
-
smoothing_window: int | None = None,
|
|
61
|
-
velocity_threshold: float | None = None,
|
|
62
|
-
min_contact_frames: int | None = None,
|
|
63
|
-
visibility_threshold: float | None = None,
|
|
64
|
-
detection_confidence: float | None = None,
|
|
65
|
-
tracking_confidence: float | None = None,
|
|
66
|
-
verbose: bool = False,
|
|
67
|
-
) -> DropJumpMetrics:
|
|
68
|
-
"""
|
|
69
|
-
Process a single drop jump video and return metrics.
|
|
70
|
-
|
|
71
|
-
Args:
|
|
72
|
-
video_path: Path to the input video file
|
|
73
|
-
drop_height: Height of drop box/platform in meters (e.g., 0.40 for 40cm)
|
|
74
|
-
quality: Analysis quality preset ("fast", "balanced", or "accurate")
|
|
75
|
-
output_video: Optional path for debug video output
|
|
76
|
-
json_output: Optional path for JSON metrics output
|
|
77
|
-
drop_start_frame: Optional manual drop start frame
|
|
78
|
-
smoothing_window: Optional override for smoothing window
|
|
79
|
-
velocity_threshold: Optional override for velocity threshold
|
|
80
|
-
min_contact_frames: Optional override for minimum contact frames
|
|
81
|
-
visibility_threshold: Optional override for visibility threshold
|
|
82
|
-
detection_confidence: Optional override for pose detection confidence
|
|
83
|
-
tracking_confidence: Optional override for pose tracking confidence
|
|
84
|
-
verbose: Print processing details
|
|
85
|
-
|
|
86
|
-
Returns:
|
|
87
|
-
DropJumpMetrics object containing analysis results
|
|
88
|
-
|
|
89
|
-
Raises:
|
|
90
|
-
ValueError: If video cannot be processed or parameters are invalid
|
|
91
|
-
FileNotFoundError: If video file does not exist
|
|
92
|
-
"""
|
|
93
|
-
if not Path(video_path).exists():
|
|
94
|
-
raise FileNotFoundError(f"Video file not found: {video_path}")
|
|
95
|
-
|
|
96
|
-
# Convert quality string to enum
|
|
97
|
-
try:
|
|
98
|
-
quality_preset = QualityPreset(quality.lower())
|
|
99
|
-
except ValueError as e:
|
|
100
|
-
raise ValueError(
|
|
101
|
-
f"Invalid quality preset: {quality}. Must be 'fast', 'balanced', or 'accurate'"
|
|
102
|
-
) from e
|
|
103
|
-
|
|
104
|
-
# Initialize video processor
|
|
105
|
-
with VideoProcessor(video_path) as video:
|
|
106
|
-
if verbose:
|
|
107
|
-
print(
|
|
108
|
-
f"Video: {video.width}x{video.height} @ {video.fps:.2f} fps, "
|
|
109
|
-
f"{video.frame_count} frames"
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
# Determine initial detection/tracking confidence from quality preset
|
|
113
|
-
initial_detection_conf = 0.5
|
|
114
|
-
initial_tracking_conf = 0.5
|
|
115
|
-
|
|
116
|
-
if quality_preset == QualityPreset.FAST:
|
|
117
|
-
initial_detection_conf = 0.3
|
|
118
|
-
initial_tracking_conf = 0.3
|
|
119
|
-
elif quality_preset == QualityPreset.ACCURATE:
|
|
120
|
-
initial_detection_conf = 0.6
|
|
121
|
-
initial_tracking_conf = 0.6
|
|
122
|
-
|
|
123
|
-
# Override with expert values if provided
|
|
124
|
-
if detection_confidence is not None:
|
|
125
|
-
initial_detection_conf = detection_confidence
|
|
126
|
-
if tracking_confidence is not None:
|
|
127
|
-
initial_tracking_conf = tracking_confidence
|
|
128
|
-
|
|
129
|
-
# Initialize pose tracker
|
|
130
|
-
tracker = PoseTracker(
|
|
131
|
-
min_detection_confidence=initial_detection_conf,
|
|
132
|
-
min_tracking_confidence=initial_tracking_conf,
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
# Process all frames
|
|
136
|
-
if verbose:
|
|
137
|
-
print("Tracking pose landmarks...")
|
|
138
|
-
|
|
139
|
-
landmarks_sequence = []
|
|
140
|
-
frames = []
|
|
141
|
-
|
|
142
|
-
while True:
|
|
143
|
-
frame = video.read_frame()
|
|
144
|
-
if frame is None:
|
|
145
|
-
break
|
|
146
|
-
|
|
147
|
-
frames.append(frame)
|
|
148
|
-
landmarks = tracker.process_frame(frame)
|
|
149
|
-
landmarks_sequence.append(landmarks)
|
|
150
|
-
|
|
151
|
-
tracker.close()
|
|
152
|
-
|
|
153
|
-
if not landmarks_sequence:
|
|
154
|
-
raise ValueError("No frames could be processed from video")
|
|
155
|
-
|
|
156
|
-
# Analyze video characteristics and auto-tune parameters
|
|
157
|
-
characteristics = analyze_video_sample(
|
|
158
|
-
landmarks_sequence, video.fps, video.frame_count
|
|
159
|
-
)
|
|
160
|
-
|
|
161
|
-
params = auto_tune_parameters(characteristics, quality_preset)
|
|
162
|
-
|
|
163
|
-
# Apply expert overrides if provided
|
|
164
|
-
if smoothing_window is not None:
|
|
165
|
-
params.smoothing_window = smoothing_window
|
|
166
|
-
if velocity_threshold is not None:
|
|
167
|
-
params.velocity_threshold = velocity_threshold
|
|
168
|
-
if min_contact_frames is not None:
|
|
169
|
-
params.min_contact_frames = min_contact_frames
|
|
170
|
-
if visibility_threshold is not None:
|
|
171
|
-
params.visibility_threshold = visibility_threshold
|
|
172
|
-
|
|
173
|
-
# Show selected parameters if verbose
|
|
174
|
-
if verbose:
|
|
175
|
-
print("\n" + "=" * 60)
|
|
176
|
-
print("AUTO-TUNED PARAMETERS")
|
|
177
|
-
print("=" * 60)
|
|
178
|
-
print(f"Video FPS: {video.fps:.2f}")
|
|
179
|
-
print(
|
|
180
|
-
f"Tracking quality: {characteristics.tracking_quality} "
|
|
181
|
-
f"(avg visibility: {characteristics.avg_visibility:.2f})"
|
|
182
|
-
)
|
|
183
|
-
print(f"Quality preset: {quality_preset.value}")
|
|
184
|
-
print("\nSelected parameters:")
|
|
185
|
-
print(f" smoothing_window: {params.smoothing_window}")
|
|
186
|
-
print(f" polyorder: {params.polyorder}")
|
|
187
|
-
print(f" velocity_threshold: {params.velocity_threshold:.4f}")
|
|
188
|
-
print(f" min_contact_frames: {params.min_contact_frames}")
|
|
189
|
-
print(f" visibility_threshold: {params.visibility_threshold}")
|
|
190
|
-
print(f" detection_confidence: {params.detection_confidence}")
|
|
191
|
-
print(f" tracking_confidence: {params.tracking_confidence}")
|
|
192
|
-
print(f" outlier_rejection: {params.outlier_rejection}")
|
|
193
|
-
print(f" bilateral_filter: {params.bilateral_filter}")
|
|
194
|
-
print(f" use_curvature: {params.use_curvature}")
|
|
195
|
-
print("=" * 60 + "\n")
|
|
196
|
-
|
|
197
|
-
# Apply smoothing with auto-tuned parameters
|
|
198
|
-
if params.outlier_rejection or params.bilateral_filter:
|
|
199
|
-
if verbose:
|
|
200
|
-
if params.outlier_rejection:
|
|
201
|
-
print("Smoothing landmarks with outlier rejection...")
|
|
202
|
-
if params.bilateral_filter:
|
|
203
|
-
print("Using bilateral temporal filter...")
|
|
204
|
-
smoothed_landmarks = smooth_landmarks_advanced(
|
|
205
|
-
landmarks_sequence,
|
|
206
|
-
window_length=params.smoothing_window,
|
|
207
|
-
polyorder=params.polyorder,
|
|
208
|
-
use_outlier_rejection=params.outlier_rejection,
|
|
209
|
-
use_bilateral=params.bilateral_filter,
|
|
210
|
-
)
|
|
211
|
-
else:
|
|
212
|
-
if verbose:
|
|
213
|
-
print("Smoothing landmarks...")
|
|
214
|
-
smoothed_landmarks = smooth_landmarks(
|
|
215
|
-
landmarks_sequence,
|
|
216
|
-
window_length=params.smoothing_window,
|
|
217
|
-
polyorder=params.polyorder,
|
|
218
|
-
)
|
|
219
|
-
|
|
220
|
-
# Extract vertical positions from feet
|
|
221
|
-
if verbose:
|
|
222
|
-
print("Extracting foot positions...")
|
|
223
|
-
|
|
224
|
-
position_list: list[float] = []
|
|
225
|
-
visibilities_list: list[float] = []
|
|
226
|
-
|
|
227
|
-
for frame_landmarks in smoothed_landmarks:
|
|
228
|
-
if frame_landmarks:
|
|
229
|
-
_, foot_y = compute_average_foot_position(frame_landmarks)
|
|
230
|
-
position_list.append(foot_y)
|
|
231
|
-
|
|
232
|
-
# Average visibility of foot landmarks
|
|
233
|
-
foot_vis = []
|
|
234
|
-
for key in ["left_ankle", "right_ankle", "left_heel", "right_heel"]:
|
|
235
|
-
if key in frame_landmarks:
|
|
236
|
-
foot_vis.append(frame_landmarks[key][2])
|
|
237
|
-
visibilities_list.append(float(np.mean(foot_vis)) if foot_vis else 0.0)
|
|
238
|
-
else:
|
|
239
|
-
position_list.append(position_list[-1] if position_list else 0.5)
|
|
240
|
-
visibilities_list.append(0.0)
|
|
241
|
-
|
|
242
|
-
vertical_positions: np.ndarray = np.array(position_list)
|
|
243
|
-
visibilities: np.ndarray = np.array(visibilities_list)
|
|
244
|
-
|
|
245
|
-
# Detect ground contact
|
|
246
|
-
contact_states = detect_ground_contact(
|
|
247
|
-
vertical_positions,
|
|
248
|
-
velocity_threshold=params.velocity_threshold,
|
|
249
|
-
min_contact_frames=params.min_contact_frames,
|
|
250
|
-
visibility_threshold=params.visibility_threshold,
|
|
251
|
-
visibilities=visibilities,
|
|
252
|
-
window_length=params.smoothing_window,
|
|
253
|
-
polyorder=params.polyorder,
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
# Calculate metrics
|
|
257
|
-
if verbose:
|
|
258
|
-
print("Calculating metrics...")
|
|
259
|
-
print(
|
|
260
|
-
f"Using drop height calibration: {drop_height}m ({drop_height*100:.0f}cm)"
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
metrics = calculate_drop_jump_metrics(
|
|
264
|
-
contact_states,
|
|
265
|
-
vertical_positions,
|
|
266
|
-
video.fps,
|
|
267
|
-
drop_height_m=drop_height,
|
|
268
|
-
drop_start_frame=drop_start_frame,
|
|
269
|
-
velocity_threshold=params.velocity_threshold,
|
|
270
|
-
smoothing_window=params.smoothing_window,
|
|
271
|
-
polyorder=params.polyorder,
|
|
272
|
-
use_curvature=params.use_curvature,
|
|
273
|
-
kinematic_correction_factor=1.0,
|
|
274
|
-
)
|
|
275
|
-
|
|
276
|
-
# Save JSON if requested
|
|
277
|
-
if json_output:
|
|
278
|
-
import json
|
|
279
|
-
|
|
280
|
-
output_path = Path(json_output)
|
|
281
|
-
output_path.write_text(json.dumps(metrics.to_dict(), indent=2))
|
|
282
|
-
if verbose:
|
|
283
|
-
print(f"Metrics written to: {json_output}")
|
|
284
|
-
|
|
285
|
-
# Generate debug video if requested
|
|
286
|
-
if output_video:
|
|
287
|
-
if verbose:
|
|
288
|
-
print(f"Generating debug video: {output_video}")
|
|
289
|
-
|
|
290
|
-
with DebugOverlayRenderer(
|
|
291
|
-
output_video,
|
|
292
|
-
video.width,
|
|
293
|
-
video.height,
|
|
294
|
-
video.display_width,
|
|
295
|
-
video.display_height,
|
|
296
|
-
video.fps,
|
|
297
|
-
) as renderer:
|
|
298
|
-
for i, frame in enumerate(frames):
|
|
299
|
-
annotated = renderer.render_frame(
|
|
300
|
-
frame,
|
|
301
|
-
smoothed_landmarks[i],
|
|
302
|
-
contact_states[i],
|
|
303
|
-
i,
|
|
304
|
-
metrics,
|
|
305
|
-
use_com=False,
|
|
306
|
-
)
|
|
307
|
-
renderer.write_frame(annotated)
|
|
308
|
-
|
|
309
|
-
if verbose:
|
|
310
|
-
print(f"Debug video saved: {output_video}")
|
|
311
|
-
|
|
312
|
-
if verbose:
|
|
313
|
-
print("Analysis complete!")
|
|
314
|
-
|
|
315
|
-
return metrics
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
def process_videos_bulk(
|
|
319
|
-
configs: list[VideoConfig],
|
|
320
|
-
max_workers: int = 4,
|
|
321
|
-
progress_callback: Callable[[VideoResult], None] | None = None,
|
|
322
|
-
) -> list[VideoResult]:
|
|
323
|
-
"""
|
|
324
|
-
Process multiple videos in parallel using ProcessPoolExecutor.
|
|
325
|
-
|
|
326
|
-
Args:
|
|
327
|
-
configs: List of VideoConfig objects specifying video paths and parameters
|
|
328
|
-
max_workers: Maximum number of parallel workers (default: 4)
|
|
329
|
-
progress_callback: Optional callback function called after each video completes.
|
|
330
|
-
Receives VideoResult object.
|
|
331
|
-
|
|
332
|
-
Returns:
|
|
333
|
-
List of VideoResult objects, one per input video, in completion order
|
|
334
|
-
|
|
335
|
-
Example:
|
|
336
|
-
>>> configs = [
|
|
337
|
-
... VideoConfig("video1.mp4", drop_height=0.40),
|
|
338
|
-
... VideoConfig("video2.mp4", drop_height=0.30, quality="accurate"),
|
|
339
|
-
... VideoConfig("video3.mp4", drop_height=0.50, output_video="debug3.mp4"),
|
|
340
|
-
... ]
|
|
341
|
-
>>> results = process_videos_bulk(configs, max_workers=4)
|
|
342
|
-
>>> for result in results:
|
|
343
|
-
... if result.success:
|
|
344
|
-
... print(f"{result.video_path}: {result.metrics.jump_height_m:.3f}m")
|
|
345
|
-
... else:
|
|
346
|
-
... print(f"{result.video_path}: FAILED - {result.error}")
|
|
347
|
-
"""
|
|
348
|
-
results: list[VideoResult] = []
|
|
349
|
-
|
|
350
|
-
# Use ProcessPoolExecutor for CPU-bound video processing
|
|
351
|
-
with ProcessPoolExecutor(max_workers=max_workers) as executor:
|
|
352
|
-
# Submit all jobs
|
|
353
|
-
future_to_config = {
|
|
354
|
-
executor.submit(_process_video_wrapper, config): config
|
|
355
|
-
for config in configs
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
# Process results as they complete
|
|
359
|
-
for future in as_completed(future_to_config):
|
|
360
|
-
config = future_to_config[future]
|
|
361
|
-
result: VideoResult
|
|
362
|
-
|
|
363
|
-
try:
|
|
364
|
-
result = future.result()
|
|
365
|
-
except Exception as exc:
|
|
366
|
-
# Handle unexpected errors
|
|
367
|
-
result = VideoResult(
|
|
368
|
-
video_path=config.video_path,
|
|
369
|
-
success=False,
|
|
370
|
-
error=f"Unexpected error: {str(exc)}",
|
|
371
|
-
)
|
|
372
|
-
|
|
373
|
-
results.append(result)
|
|
374
|
-
|
|
375
|
-
# Call progress callback if provided
|
|
376
|
-
if progress_callback:
|
|
377
|
-
progress_callback(result)
|
|
378
|
-
|
|
379
|
-
return results
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
def _process_video_wrapper(config: VideoConfig) -> VideoResult:
|
|
383
|
-
"""
|
|
384
|
-
Wrapper function for parallel processing. Must be picklable (top-level function).
|
|
385
|
-
|
|
386
|
-
Args:
|
|
387
|
-
config: VideoConfig object with processing parameters
|
|
388
|
-
|
|
389
|
-
Returns:
|
|
390
|
-
VideoResult object with metrics or error information
|
|
391
|
-
"""
|
|
392
|
-
start_time = time.time()
|
|
393
|
-
|
|
394
|
-
try:
|
|
395
|
-
metrics = process_video(
|
|
396
|
-
video_path=config.video_path,
|
|
397
|
-
drop_height=config.drop_height,
|
|
398
|
-
quality=config.quality,
|
|
399
|
-
output_video=config.output_video,
|
|
400
|
-
json_output=config.json_output,
|
|
401
|
-
drop_start_frame=config.drop_start_frame,
|
|
402
|
-
smoothing_window=config.smoothing_window,
|
|
403
|
-
velocity_threshold=config.velocity_threshold,
|
|
404
|
-
min_contact_frames=config.min_contact_frames,
|
|
405
|
-
visibility_threshold=config.visibility_threshold,
|
|
406
|
-
detection_confidence=config.detection_confidence,
|
|
407
|
-
tracking_confidence=config.tracking_confidence,
|
|
408
|
-
verbose=False, # Disable verbose in parallel mode
|
|
409
|
-
)
|
|
410
|
-
|
|
411
|
-
processing_time = time.time() - start_time
|
|
412
|
-
|
|
413
|
-
return VideoResult(
|
|
414
|
-
video_path=config.video_path,
|
|
415
|
-
success=True,
|
|
416
|
-
metrics=metrics,
|
|
417
|
-
processing_time=processing_time,
|
|
418
|
-
)
|
|
419
|
-
|
|
420
|
-
except Exception as e:
|
|
421
|
-
processing_time = time.time() - start_time
|
|
422
|
-
|
|
423
|
-
return VideoResult(
|
|
424
|
-
video_path=config.video_path,
|
|
425
|
-
success=False,
|
|
426
|
-
error=str(e),
|
|
427
|
-
processing_time=processing_time,
|
|
428
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|