|
| 1 | +""" |
| 2 | +Temporal classification annotations for audio, video, and other time-based media. |
| 3 | +
|
| 4 | +These classes provide a unified, recursive structure for temporal annotations with |
| 5 | +frame-level precision. All temporal classifications support nested hierarchies. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import List, Optional, Tuple, Union |
| 9 | +from pydantic import Field |
| 10 | + |
| 11 | +from labelbox.data.annotation_types.annotation import ClassificationAnnotation |
| 12 | +from labelbox.data.annotation_types.classification.classification import ( |
| 13 | + ClassificationAnswer, |
| 14 | + FrameLocation, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TemporalClassificationAnswer(ClassificationAnswer): |
| 19 | + """ |
| 20 | + Temporal answer for Radio/Checklist questions with frame ranges. |
| 21 | +
|
| 22 | + Represents a single answer option that can exist at multiple discontinuous |
| 23 | + time ranges and contain nested classifications. |
| 24 | +
|
| 25 | + Args: |
| 26 | + name (str): Name of the answer option |
| 27 | + frames (List[Tuple[int, int]]): List of (start_frame, end_frame) ranges in milliseconds |
| 28 | + classifications (Optional[List[Union[TemporalClassificationText, TemporalClassificationQuestion]]]): |
| 29 | + Nested classifications within this answer |
| 30 | + feature_schema_id (Optional[str]): Feature schema identifier |
| 31 | + extra (dict): Additional metadata |
| 32 | +
|
| 33 | + Example: |
| 34 | + >>> # Radio answer with nested classifications |
| 35 | + >>> answer = TemporalClassificationAnswer( |
| 36 | + >>> name="user", |
| 37 | + >>> frames=[(200, 1600)], |
| 38 | + >>> classifications=[ |
| 39 | + >>> TemporalClassificationQuestion( |
| 40 | + >>> name="tone", |
| 41 | + >>> answers=[ |
| 42 | + >>> TemporalClassificationAnswer( |
| 43 | + >>> name="professional", |
| 44 | + >>> frames=[(1000, 1600)] |
| 45 | + >>> ) |
| 46 | + >>> ] |
| 47 | + >>> ) |
| 48 | + >>> ] |
| 49 | + >>> ) |
| 50 | + """ |
| 51 | + |
| 52 | + frames: List[Tuple[int, int]] = Field( |
| 53 | + default_factory=list, |
| 54 | + description="List of (start_frame, end_frame) tuples in milliseconds", |
| 55 | + ) |
| 56 | + classifications: Optional[ |
| 57 | + List[Union["TemporalClassificationText", "TemporalClassificationQuestion"]] |
| 58 | + ] = None |
| 59 | + |
| 60 | + |
| 61 | +class TemporalClassificationText(ClassificationAnnotation): |
| 62 | + """ |
| 63 | + Temporal text classification with multiple text values at different frame ranges. |
| 64 | +
|
| 65 | + Allows multiple text annotations at different time segments, each with precise |
| 66 | + frame ranges. Supports recursive nesting of text and question classifications. |
| 67 | +
|
| 68 | + Args: |
| 69 | + name (str): Name of the text classification |
| 70 | + values (List[Tuple[int, int, str]]): List of (start_frame, end_frame, text_value) tuples |
| 71 | + classifications (Optional[List[Union[TemporalClassificationText, TemporalClassificationQuestion]]]): |
| 72 | + Nested classifications |
| 73 | + feature_schema_id (Optional[str]): Feature schema identifier |
| 74 | + extra (dict): Additional metadata |
| 75 | +
|
| 76 | + Example: |
| 77 | + >>> # Simple text with multiple temporal values |
| 78 | + >>> transcription = TemporalClassificationText( |
| 79 | + >>> name="transcription", |
| 80 | + >>> values=[ |
| 81 | + >>> (1600, 2000, "Hello, how can I help you?"), |
| 82 | + >>> (2500, 3000, "Thank you for calling!"), |
| 83 | + >>> ] |
| 84 | + >>> ) |
| 85 | + >>> |
| 86 | + >>> # Text with nested classifications |
| 87 | + >>> transcription_with_notes = TemporalClassificationText( |
| 88 | + >>> name="transcription", |
| 89 | + >>> values=[ |
| 90 | + >>> (1600, 2000, "Hello, how can I help you?"), |
| 91 | + >>> ], |
| 92 | + >>> classifications=[ |
| 93 | + >>> TemporalClassificationText( |
| 94 | + >>> name="speaker_notes", |
| 95 | + >>> values=[ |
| 96 | + >>> (1600, 2000, "Polite greeting"), |
| 97 | + >>> ] |
| 98 | + >>> ) |
| 99 | + >>> ] |
| 100 | + >>> ) |
| 101 | + """ |
| 102 | + |
| 103 | + # Override parent's value field |
| 104 | + value: List[Tuple[int, int, str]] = Field( |
| 105 | + default_factory=list, |
| 106 | + description="List of (start_frame, end_frame, text_value) tuples", |
| 107 | + ) |
| 108 | + classifications: Optional[ |
| 109 | + List[Union["TemporalClassificationText", "TemporalClassificationQuestion"]] |
| 110 | + ] = None |
| 111 | + |
| 112 | + |
| 113 | +class TemporalClassificationQuestion(ClassificationAnnotation): |
| 114 | + """ |
| 115 | + Temporal Radio/Checklist question with multiple answer options. |
| 116 | +
|
| 117 | + Represents a question with one or more answer options, each having their own |
| 118 | + frame ranges. Radio questions have a single answer, Checklist can have multiple. |
| 119 | +
|
| 120 | + Args: |
| 121 | + name (str): Name of the question/classification |
| 122 | + answers (List[TemporalClassificationAnswer]): List of answer options with frame ranges |
| 123 | + feature_schema_id (Optional[str]): Feature schema identifier |
| 124 | + extra (dict): Additional metadata |
| 125 | +
|
| 126 | + Note: |
| 127 | + - Radio: Single answer in the answers list |
| 128 | + - Checklist: Multiple answers in the answers list |
| 129 | + The serializer automatically handles the distinction based on the number of answers. |
| 130 | +
|
| 131 | + Example: |
| 132 | + >>> # Radio question (single answer) |
| 133 | + >>> speaker = TemporalClassificationQuestion( |
| 134 | + >>> name="speaker", |
| 135 | + >>> answers=[ |
| 136 | + >>> TemporalClassificationAnswer( |
| 137 | + >>> name="user", |
| 138 | + >>> frames=[(200, 1600)] |
| 139 | + >>> ) |
| 140 | + >>> ] |
| 141 | + >>> ) |
| 142 | + >>> |
| 143 | + >>> # Checklist question (multiple answers) |
| 144 | + >>> audio_quality = TemporalClassificationQuestion( |
| 145 | + >>> name="audio_quality", |
| 146 | + >>> answers=[ |
| 147 | + >>> TemporalClassificationAnswer( |
| 148 | + >>> name="background_noise", |
| 149 | + >>> frames=[(0, 1500), (2000, 3000)] |
| 150 | + >>> ), |
| 151 | + >>> TemporalClassificationAnswer( |
| 152 | + >>> name="echo", |
| 153 | + >>> frames=[(2200, 2900)] |
| 154 | + >>> ) |
| 155 | + >>> ] |
| 156 | + >>> ) |
| 157 | + >>> |
| 158 | + >>> # Nested structure: Radio > Radio > Radio |
| 159 | + >>> speaker_with_tone = TemporalClassificationQuestion( |
| 160 | + >>> name="speaker", |
| 161 | + >>> answers=[ |
| 162 | + >>> TemporalClassificationAnswer( |
| 163 | + >>> name="user", |
| 164 | + >>> frames=[(200, 1600)], |
| 165 | + >>> classifications=[ |
| 166 | + >>> TemporalClassificationQuestion( |
| 167 | + >>> name="tone", |
| 168 | + >>> answers=[ |
| 169 | + >>> TemporalClassificationAnswer( |
| 170 | + >>> name="professional", |
| 171 | + >>> frames=[(1000, 1600)] |
| 172 | + >>> ) |
| 173 | + >>> ] |
| 174 | + >>> ) |
| 175 | + >>> ] |
| 176 | + >>> ) |
| 177 | + >>> ] |
| 178 | + >>> ) |
| 179 | + """ |
| 180 | + |
| 181 | + # Override parent's value field |
| 182 | + value: List[TemporalClassificationAnswer] = Field( |
| 183 | + default_factory=list, |
| 184 | + description="List of temporal answer options", |
| 185 | + ) |
| 186 | + classifications: Optional[ |
| 187 | + List[Union["TemporalClassificationText", "TemporalClassificationQuestion"]] |
| 188 | + ] = None |
| 189 | + |
| 190 | + |
| 191 | +# Update forward references for recursive types |
| 192 | +TemporalClassificationAnswer.model_rebuild() |
| 193 | +TemporalClassificationText.model_rebuild() |
| 194 | +TemporalClassificationQuestion.model_rebuild() |
0 commit comments