File size: 3,401 Bytes
6724737
 
 
 
95fec78
6724737
6661f3a
 
 
95fec78
6724737
 
 
 
 
 
6661f3a
 
 
95fec78
 
6724737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations

from typing import Any

from pozify.contracts import RepAnalysis
from pozify.exercises.base import ExerciseStrategy
from pozify.exercises.push_up.analyzer import PushUpAnalyzer
from pozify.exercises.shared.issue_marker import IssueRule
from pozify.exercises.push_up.issue_markers import RULES as ISSUE_RULES
from pozify.exercises.shared.rep_counter import combine, mean_optional, normalized_samples
from pozify.steps.rep_signals import SignalSample, angle_deg, average_axis, body_line_score


class PushUpExercise(PushUpAnalyzer, ExerciseStrategy):
    exercise = "push_up"

    def issue_rules(self) -> tuple[IssueRule, ...]:
        return ISSUE_RULES

    def build_signal(self) -> tuple[list[SignalSample], dict[str, Any]]:
        sequence = self.pose_sequence
        hip_y = [average_axis(frame, ("left_hip", "right_hip"), "y") for frame in sequence.frames]
        shoulder_y = [average_axis(frame, ("left_shoulder", "right_shoulder"), "y") for frame in sequence.frames]
        elbow_bend = [
            mean_optional(
                [
                    None if angle is None else max(0.0, 180.0 - angle)
                    for angle in (
                        angle_deg(frame, "left_shoulder", "left_elbow", "left_wrist"),
                        angle_deg(frame, "right_shoulder", "right_elbow", "right_wrist"),
                    )
                ]
            )
            for frame in sequence.frames
        ]
        body_line = [body_line_score(frame) for frame in sequence.frames]
        chest_proxy = [
            mean_optional([shoulder_value, hip_value])
            for shoulder_value, hip_value in zip(shoulder_y, hip_y, strict=False)
        ]
        samples, signal_range = normalized_samples(sequence, combine(chest_proxy, elbow_bend, weight=0.25))
        return samples, {
            "selected_signal": "chest_y_plus_elbow_bend",
            "raw_signal_range": signal_range,
            "usable_signal_samples": len(samples),
            "body_line_mean": round(mean_optional(body_line) or 0.0, 4),
        }

    def detect_variation(self, analysis: RepAnalysis) -> tuple[str, float, list[str]]:
        hand_width = self.metric(analysis, "avg_hand_width_ratio")
        knee_support = self.metric(analysis, "avg_knee_support_score")
        not_issues: list[str] = []

        if knee_support is not None and knee_support >= 0.8:
            return "knee_push_up", self.confidence(0.74, analysis, knee_support), ["knee_contact"]
        if hand_width is not None and hand_width >= 1.45:
            return "wide_grip_push_up", self.confidence(0.72, analysis, hand_width), ["wide_hand_placement"]
        if hand_width is not None and hand_width <= 0.95:
            return (
                "close_grip_push_up",
                self.confidence(0.72, analysis, 1.0 - hand_width),
                ["close_hand_placement"],
            )

        if hand_width is None:
            not_issues.append("hand_width_unverified")
        return "standard_push_up", self.confidence(0.62, analysis, hand_width), not_issues

    def profile_not_issues(self, variation: str) -> list[str]:
        mapping = {
            "wide_grip_push_up": ["wide_hand_placement"],
            "close_grip_push_up": ["close_hand_placement"],
            "knee_push_up": ["knee_contact"],
        }
        return list(mapping.get(variation, []))