File size: 3,956 Bytes
2756f0f
 
 
 
6963661
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ec8d34
2756f0f
 
6963661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2756f0f
 
 
 
 
6963661
2756f0f
 
6963661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be69d1e
2756f0f
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from datetime import datetime
from typing import Annotated, Literal
from uuid import UUID

from pydantic import ConfigDict, Field, field_validator, model_validator

from app.common.schemas import RequestModel, ResponseModel
from app.modules.reviews.schemas import ReviewOut

Level = Literal["youth", "high_school", "college", "adult"]
Fraction = Annotated[float, Field(ge=0, le=1)]
Point = tuple[Fraction, Fraction]


class AthleteIn(RequestModel):
    name: str = Field(min_length=2, max_length=80)
    age: int = Field(ge=5, le=70)
    height_cm: float = Field(ge=90, le=230)
    weight_kg: float = Field(ge=20, le=200)
    bats: Literal["L", "R", "S"]
    throws: Literal["L", "R"]
    level: Level


class PitcherDirection(RequestModel):
    model_config = ConfigDict(populate_by_name=True)

    from_: Point = Field(alias="from")
    to: Point


class MarksIn(RequestModel):
    contact_frame: int = Field(ge=0)
    contact_offset: int
    contact_source: Literal["detected", "suggested", "manual"]
    stride_start_frame: int = Field(ge=0)
    stride_end_frame: int = Field(ge=0)
    pitcher_direction: PitcherDirection | None = None


class PitchMarksIn(RequestModel):
    leg_lift_frame: int | None = Field(default=None, ge=0, strict=True)
    foot_strike_frame: int | None = Field(default=None, ge=0, strict=True)
    release_frame: int | None = Field(default=None, ge=0, strict=True)


class PitchAthleteIn(RequestModel):
    name: str = Field(default="Pitching athlete", min_length=2, max_length=80)
    age: int | None = Field(default=None, ge=5, le=70)
    height_cm: float | None = Field(default=None, ge=90, le=230)
    weight_kg: float | None = Field(default=None, ge=20, le=200)
    bats: Literal["L", "R", "S"] | None = None
    throws: Literal["L", "R"] | None = None
    level: Level | None = None


class SessionIn(RequestModel):
    video_upload_id: UUID
    thumbnail_upload_id: UUID | None = None
    mode: Literal["h", "p"]
    is_self: bool
    athlete: AthleteIn | PitchAthleteIn | None = None
    duration_sec: float = Field(gt=0)
    fps: int = Field(ge=1, le=240)
    frame_count: int | None = Field(default=None, gt=1, strict=True)
    marks: MarksIn | PitchMarksIn

    @field_validator("athlete", mode="before")
    @classmethod
    def parse_athlete(cls, value, info):
        if value is None:
            return None
        return (AthleteIn if info.data.get("mode") == "h" else PitchAthleteIn).model_validate(value)

    @field_validator("marks", mode="before")
    @classmethod
    def parse_marks(cls, value, info):
        if info.data.get("mode") == "h":
            return MarksIn.model_validate(value)
        if isinstance(value, dict) and "contact_frame" in value:
            return MarksIn.model_validate(value)  # Existing pre-analysis pitching sessions remain readable.
        return PitchMarksIn.model_validate(value)

    @model_validator(mode="after")
    def discipline_fields(self):
        if self.mode == "h":
            if not isinstance(self.marks, MarksIn):
                raise ValueError("Hitting requires contact and stride marks")
            if self.athlete is not None and not isinstance(self.athlete, AthleteIn):
                raise ValueError("Hitting athlete details are incomplete")
        return self


class MediaOut(ResponseModel):
    video_url: str
    thumbnail_url: str | None
    expires_at: datetime


class PreviousOut(ResponseModel):
    id: UUID
    overall_score: int | None
    created_at: datetime


class SessionOut(ResponseModel):
    id: UUID
    owner_id: UUID
    is_self: bool
    mode: str
    status: str
    created_at: datetime
    analyzed_at: datetime | None
    athlete: dict
    duration_sec: float
    fps: int
    marks: dict
    report: dict | None
    report_error: str | None = None
    review: ReviewOut | None
    media: MediaOut
    previous: PreviousOut | None
    ordinal: int


class SessionEnvelope(ResponseModel):
    session: SessionOut