|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +from pydantic import BaseModel, validator |
| 4 | +from labelbox.data.annotation_types.annotation import BaseAnnotation, ClassificationAnnotation, ObjectAnnotation |
| 5 | + |
| 6 | +from typing import List, Optional, Tuple |
| 7 | +from labelbox.data.mixins import ConfidenceNotSupportedMixin |
| 8 | + |
| 9 | +from labelbox.utils import _CamelCaseMixin, is_valid_uri |
| 10 | + |
| 11 | + |
| 12 | +class VideoClassificationAnnotation(ClassificationAnnotation): |
| 13 | + """Video classification |
| 14 | + Args: |
| 15 | + name (Optional[str]) |
| 16 | + feature_schema_id (Optional[Cuid]) |
| 17 | + value (Union[Text, Checklist, Radio, Dropdown]) |
| 18 | + frame (int): The frame index that this annotation corresponds to |
| 19 | + segment_id (Optional[Int]): Index of video segment this annotation belongs to |
| 20 | + extra (Dict[str, Any]) |
| 21 | + """ |
| 22 | + frame: int |
| 23 | + segment_index: Optional[int] = None |
| 24 | + |
| 25 | + |
| 26 | +class VideoObjectAnnotation(ObjectAnnotation, ConfidenceNotSupportedMixin): |
| 27 | + """Video object annotation |
| 28 | + >>> VideoObjectAnnotation( |
| 29 | + >>> keyframe=True, |
| 30 | + >>> frame=10, |
| 31 | + >>> value=Rectangle( |
| 32 | + >>> start=Point(x=0, y=0), |
| 33 | + >>> end=Point(x=1, y=1) |
| 34 | + >>> ), |
| 35 | + >>> feature_schema_id="my-feature-schema-id" |
| 36 | + >>> ) |
| 37 | + Args: |
| 38 | + name (Optional[str]) |
| 39 | + feature_schema_id (Optional[Cuid]) |
| 40 | + value (Geometry) |
| 41 | + frame (Int): The frame index that this annotation corresponds to |
| 42 | + keyframe (bool): Whether or not this annotation was a human generated or interpolated annotation |
| 43 | + segment_id (Optional[Int]): Index of video segment this annotation belongs to |
| 44 | + classifications (List[ClassificationAnnotation]) = [] |
| 45 | + extra (Dict[str, Any]) |
| 46 | + """ |
| 47 | + frame: int |
| 48 | + keyframe: bool |
| 49 | + segment_index: Optional[int] = None |
| 50 | + |
| 51 | + |
| 52 | +class GroupKey(Enum): |
| 53 | + """Group key for DICOM annotations |
| 54 | + """ |
| 55 | + AXIAL = "axial" |
| 56 | + SAGITTAL = "sagittal" |
| 57 | + CORONAL = "coronal" |
| 58 | + |
| 59 | + |
| 60 | +class DICOMObjectAnnotation(VideoObjectAnnotation): |
| 61 | + """DICOM object annotation |
| 62 | + >>> DICOMObjectAnnotation( |
| 63 | + >>> name="dicom_polyline", |
| 64 | + >>> frame=2, |
| 65 | + >>> value=lb_types.Line(points = [ |
| 66 | + >>> lb_types.Point(x=680, y=100), |
| 67 | + >>> lb_types.Point(x=100, y=190), |
| 68 | + >>> lb_types.Point(x=190, y=220) |
| 69 | + >>> ]), |
| 70 | + >>> segment_index=0, |
| 71 | + >>> keyframe=True, |
| 72 | + >>> Group_key=GroupKey.AXIAL |
| 73 | + >>> ) |
| 74 | + Args: |
| 75 | + name (Optional[str]) |
| 76 | + feature_schema_id (Optional[Cuid]) |
| 77 | + value (Geometry) |
| 78 | + group_key (GroupKey) |
| 79 | + frame (Int): The frame index that this annotation corresponds to |
| 80 | + keyframe (bool): Whether or not this annotation was a human generated or interpolated annotation |
| 81 | + segment_id (Optional[Int]): Index of video segment this annotation belongs to |
| 82 | + classifications (List[ClassificationAnnotation]) = [] |
| 83 | + extra (Dict[str, Any]) |
| 84 | + """ |
| 85 | + group_key: GroupKey |
| 86 | + |
| 87 | + |
| 88 | +class MaskFrame(_CamelCaseMixin, BaseModel): |
| 89 | + index: int |
| 90 | + instance_uri: str |
| 91 | + |
| 92 | + @validator("instance_uri") |
| 93 | + def validate_uri(cls, v): |
| 94 | + if not is_valid_uri(v): |
| 95 | + raise ValueError(f"{v} is not a valid uri") |
| 96 | + return v |
| 97 | + |
| 98 | + |
| 99 | +class MaskInstance(_CamelCaseMixin, BaseModel): |
| 100 | + color_rgb: Tuple[int, int, int] |
| 101 | + name: str |
| 102 | + |
| 103 | + |
| 104 | +class VideoMaskAnnotation(BaseAnnotation): |
| 105 | + """DICOM video annotation |
| 106 | + >>> DICOMVideoAnnotation( |
| 107 | + >>> name="dicom_mask", |
| 108 | + >>> frames=[ |
| 109 | + >>> MaskFrame(index=1, instance_uri='https://storage.labelbox.com/cjhfn5y6s0pk507024nz1ocys%2F1d60856c-59b7-3060-2754-83f7e93e0d01-1?Expires=1666901963361&KeyName=labelbox-assets-key-3&Signature=t-2s2DB4YjFuWEFak0wxYqfBfZA'), |
| 110 | + >>> MaskFrame(index=5, instance_uri='https://storage.labelbox.com/cjhfn5y6s0pk507024nz1ocys1%2F1d60856c-59b7-3060-2754-83f7e93e0d01-1?Expires=1666901963361&KeyName=labelbox-assets-key-3&Signature=t-2s2DB4YjFuWEFak0wxYqfBfZA'), |
| 111 | + >>> ], |
| 112 | + >>> instances=[ |
| 113 | + >>> MaskInstance(color_rgb=(0, 0, 255), name="mask1"), |
| 114 | + >>> MaskInstance(color_rgb=(0, 255, 0), name="mask2"), |
| 115 | + >>> MaskInstance(color_rgb=(255, 0, 0), name="mask3") |
| 116 | + >>> ] |
| 117 | + >>> ) |
| 118 | + """ |
| 119 | + frames: List[MaskFrame] |
| 120 | + instances: List[MaskInstance] |
| 121 | + |
| 122 | + |
| 123 | +class DICOMMaskAnnotation(VideoMaskAnnotation): |
| 124 | + """DICOM mask annotation |
| 125 | + >>> DICOMMaskAnnotation( |
| 126 | + >>> name="dicom_mask", |
| 127 | + >>> group_key=GroupKey.AXIAL, |
| 128 | + >>> frames=[ |
| 129 | + >>> MaskFrame(index=1, instance_uri='https://storage.labelbox.com/cjhfn5y6s0pk507024nz1ocys%2F1d60856c-59b7-3060-2754-83f7e93e0d01-1?Expires=1666901963361&KeyName=labelbox-assets-key-3&Signature=t-2s2DB4YjFuWEFak0wxYqfBfZA'), |
| 130 | + >>> MaskFrame(index=5, instance_uri='https://storage.labelbox.com/cjhfn5y6s0pk507024nz1ocys1%2F1d60856c-59b7-3060-2754-83f7e93e0d01-1?Expires=1666901963361&KeyName=labelbox-assets-key-3&Signature=t-2s2DB4YjFuWEFak0wxYqfBfZA'), |
| 131 | + >>> ], |
| 132 | + >>> instances=[ |
| 133 | + >>> MaskInstance(color_rgb=(0, 0, 255), name="mask1"), |
| 134 | + >>> MaskInstance(color_rgb=(0, 255, 0), name="mask2"), |
| 135 | + >>> MaskInstance(color_rgb=(255, 0, 0), name="mask3") |
| 136 | + >>> ] |
| 137 | + >>> ) |
| 138 | + """ |
| 139 | + group_key: GroupKey |
0 commit comments