|
5 | 5 | from enum import Enum |
6 | 6 | from typing import Any, Dict, Optional, Sequence |
7 | 7 |
|
8 | | -from .annotation import Point3D, is_local_path |
| 8 | +from .annotation import is_local_path |
| 9 | +from .camera_params import CameraParams |
9 | 10 | from .constants import ( |
10 | 11 | BACKEND_REFERENCE_ID_KEY, |
11 | | - CAMERA_MODEL_KEY, |
12 | 12 | CAMERA_PARAMS_KEY, |
13 | | - CX_KEY, |
14 | | - CY_KEY, |
15 | | - FX_KEY, |
16 | | - FY_KEY, |
17 | | - HEADING_KEY, |
18 | 13 | IMAGE_URL_KEY, |
19 | | - K1_KEY, |
20 | | - K2_KEY, |
21 | | - K3_KEY, |
22 | | - K4_KEY, |
23 | 14 | METADATA_KEY, |
24 | 15 | ORIGINAL_IMAGE_URL_KEY, |
25 | | - P1_KEY, |
26 | | - P2_KEY, |
27 | 16 | POINTCLOUD_URL_KEY, |
28 | | - POSITION_KEY, |
29 | 17 | REFERENCE_ID_KEY, |
30 | 18 | TYPE_KEY, |
31 | 19 | UPLOAD_TO_SCALE_KEY, |
32 | 20 | URL_KEY, |
33 | | - W_KEY, |
34 | | - X_KEY, |
35 | | - Y_KEY, |
36 | | - Z_KEY, |
37 | 21 | ) |
38 | 22 |
|
39 | 23 |
|
40 | | -class CameraModels(str, Enum): |
41 | | - BROWN_CONRADY = "brown_conrady" |
42 | | - FISHEYE = "fisheye" |
43 | | - |
44 | | - def __contains__(self, item): |
45 | | - try: |
46 | | - self(item) |
47 | | - except ValueError: |
48 | | - return False |
49 | | - return True |
50 | | - |
51 | | - |
52 | | -@dataclass |
53 | | -class Quaternion: |
54 | | - """Quaternion objects are used to represent rotation. |
55 | | -
|
56 | | - We use the Hamilton/right-handed quaternion convention, where |
57 | | - :: |
58 | | -
|
59 | | - i^2 = j^2 = k^2 = ijk = -1 |
60 | | -
|
61 | | - The quaternion represented by the tuple ``(x, y, z, w)`` is equal to |
62 | | - ``w + x*i + y*j + z*k``. |
63 | | -
|
64 | | - Parameters: |
65 | | - x (float): The x value. |
66 | | - y (float): The y value. |
67 | | - x (float): The z value. |
68 | | - w (float): The w value. |
69 | | - """ |
70 | | - |
71 | | - x: float |
72 | | - y: float |
73 | | - z: float |
74 | | - w: float |
75 | | - |
76 | | - @classmethod |
77 | | - def from_json(cls, payload: Dict[str, float]): |
78 | | - """Instantiates quaternion object from schematized JSON dict payload.""" |
79 | | - return cls( |
80 | | - payload[X_KEY], payload[Y_KEY], payload[Z_KEY], payload[W_KEY] |
81 | | - ) |
82 | | - |
83 | | - def to_payload(self) -> dict: |
84 | | - """Serializes quaternion object to schematized JSON dict.""" |
85 | | - return { |
86 | | - X_KEY: self.x, |
87 | | - Y_KEY: self.y, |
88 | | - Z_KEY: self.z, |
89 | | - W_KEY: self.w, |
90 | | - } |
91 | | - |
92 | | - |
93 | | -@dataclass |
94 | | -class CameraParams: |
95 | | - """Camera position/heading used to record the image. |
96 | | -
|
97 | | - Args: |
98 | | - position (:class:`Point3D`): World-normalized position of the camera |
99 | | - heading (:class:`Quaternion`): Vector4 indicating the quaternion of the |
100 | | - camera direction; note that the z-axis of the camera frame |
101 | | - represents the camera's optical axis. See `Heading Examples |
102 | | - <https://docs.scale.com/reference/data-types-and-the-frame-objects#heading-examples>`_. |
103 | | - fx (float): Focal length in x direction (in pixels). |
104 | | - fy (float): Focal length in y direction (in pixels). |
105 | | - cx (float): Principal point x value. |
106 | | - cy (float): Principal point y value. |
107 | | - """ |
108 | | - |
109 | | - position: Point3D |
110 | | - heading: Quaternion |
111 | | - fx: float |
112 | | - fy: float |
113 | | - cx: float |
114 | | - cy: float |
115 | | - camera_model: str |
116 | | - k1: float |
117 | | - k2: float |
118 | | - k3: float |
119 | | - k4: float |
120 | | - p1: float |
121 | | - p2: float |
122 | | - |
123 | | - def __post_init__(self): |
124 | | - if self.camera_model is not None: |
125 | | - if self.camera_model not in (k for k in CameraModels): |
126 | | - raise ValueError( |
127 | | - f'Invalid Camera Model, the supported options are "{CameraModels.BROWN_CONRADY}" and "{CameraModels.FISHEYE}"' |
128 | | - ) |
129 | | - |
130 | | - @classmethod |
131 | | - def from_json(cls, payload: Dict[str, Any]): |
132 | | - """Instantiates camera params object from schematized JSON dict payload.""" |
133 | | - return cls( |
134 | | - Point3D.from_json(payload[POSITION_KEY]), |
135 | | - Quaternion.from_json(payload[HEADING_KEY]), |
136 | | - payload[FX_KEY], |
137 | | - payload[FY_KEY], |
138 | | - payload[CX_KEY], |
139 | | - payload[CY_KEY], |
140 | | - payload.get(CAMERA_MODEL_KEY, None), |
141 | | - payload.get(K1_KEY, None), |
142 | | - payload.get(K2_KEY, None), |
143 | | - payload.get(K3_KEY, None), |
144 | | - payload.get(K4_KEY, None), |
145 | | - payload.get(P1_KEY, None), |
146 | | - payload.get(P2_KEY, None), |
147 | | - ) |
148 | | - |
149 | | - def to_payload(self) -> dict: |
150 | | - """Serializes camera params object to schematized JSON dict.""" |
151 | | - payload = { |
152 | | - POSITION_KEY: self.position.to_payload(), |
153 | | - HEADING_KEY: self.heading.to_payload(), |
154 | | - FX_KEY: self.fx, |
155 | | - FY_KEY: self.fy, |
156 | | - CX_KEY: self.cx, |
157 | | - CY_KEY: self.cy, |
158 | | - } |
159 | | - if self.k1: |
160 | | - payload[K1_KEY] = self.k1 |
161 | | - if self.k2: |
162 | | - payload[K2_KEY] = self.k2 |
163 | | - if self.k3: |
164 | | - payload[K3_KEY] = self.k3 |
165 | | - if self.k4: |
166 | | - payload[K4_KEY] = self.k4 |
167 | | - if self.p1: |
168 | | - payload[P1_KEY] = self.p1 |
169 | | - if self.p2: |
170 | | - payload[P2_KEY] = self.p2 |
171 | | - if self.camera_model: |
172 | | - payload[CAMERA_MODEL_KEY] = self.camera_model |
173 | | - return payload |
174 | | - |
175 | | - |
176 | 24 | class DatasetItemType(Enum): |
177 | 25 | IMAGE = "image" |
178 | 26 | POINTCLOUD = "pointcloud" |
|
0 commit comments