-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetashape_loader.py
More file actions
276 lines (232 loc) · 10.6 KB
/
metashape_loader.py
File metadata and controls
276 lines (232 loc) · 10.6 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import xml.etree.ElementTree as ET
import numpy as np
import zipfile
class Sensor:
def __init__(self):
self.id = None
self.label = None
self.type = None
self.resolution = {"width": None, "height": None}
self.properties = {}
self.bands = []
self.data_type = None
self.calibration = {
"type": None,
"class": None,
"resolution": {"width": None, "height": None},
"f": None,
"cx": None,
"cy": None,
"k1": None,
"k2": None,
"k3": None,
"p1": None,
"p2": None
}
self.covariance = {"params": None, "coeffs": None}
self.meta = {}
def to_array(self):
"""
Convert the sensor data to a flat array suitable for uniform blocks in shaders.
This assumes all values are present.
"""
# Directly extract and flatten the sensor data
ret_array = [
self.resolution["width"], # Resolution width
self.resolution["height"], # Resolution height
self.properties["pixel_width"], # Pixel width
self.properties["pixel_height"], # Pixel height
self.properties["focal_length"], # Focal length
self.properties["layer_index"], # Layer index
self.calibration["f"], # Focal length calibration
self.calibration["cx"], # Camera center x
self.calibration["cy"], # Camera center y
self.calibration["k1"], # Radial distortion k1
self.calibration["k2"], # Radial distortion k2
self.calibration["k3"], # Radial distortion k3
self.calibration["p1"], # Tangential distortion p1
self.calibration["p2"], # Tangential distortion p2
]
# Flatten the covariance coefficients (assuming there are 24 values)
ret_array += self.covariance["coeffs"][:24] # Limit to 24 values if necessary
return np.array(ret_array, dtype=np.float32)
def __str__(self):
return f"Sensor(id={self.id}, label={self.label}, type={self.type}, resolution={self.resolution}, " \
f"properties={self.properties}, bands={self.bands}, data_type={self.data_type}, " \
f"calibration={self.calibration}, covariance={self.covariance}, meta={self.meta})"
def load_sensors_from_xml(file_path):
with zipfile.ZipFile(file_path, 'r') as zip_ref:
with zip_ref.open("doc.xml") as file:
# Parse the XML file
tree = ET.parse(file)
root = tree.getroot()
# Navigate to the sensor element
sensor_elems = root.findall("./chunks/chunk/sensors/sensor")
if not sensor_elems:
raise ValueError("No <sensor> element found in the XML file.")
sensors = []
for sensor_elem in sensor_elems:
# Create a Sensor object
sensor = Sensor()
# Fill the sensor fields
sensor.id = sensor_elem.get("id")
sensor.label = sensor_elem.get("label")
sensor.type = sensor_elem.get("type")
# Resolution
resolution_elem = sensor_elem.find("resolution")
if resolution_elem is not None:
sensor.resolution["width"] = int(resolution_elem.get("width"))
sensor.resolution["height"] = int(resolution_elem.get("height"))
# Properties
for prop_elem in sensor_elem.findall("property"):
name = prop_elem.get("name")
value = prop_elem.get("value")
if name and value:
sensor.properties[name] = float(value) if "." in value else int(value)
# Bands
bands_elem = sensor_elem.find("bands")
if bands_elem is not None:
sensor.bands = [band_elem.get("label") for band_elem in bands_elem.findall("band") if band_elem.get("label")]
# Data Type
data_type_elem = sensor_elem.find("data_type")
if data_type_elem is not None:
sensor.data_type = data_type_elem.text
# Calibration
calibration_elem = sensor_elem.find("calibration")
if calibration_elem is not None:
sensor.calibration["type"] = calibration_elem.get("type")
sensor.calibration["class"] = calibration_elem.get("class")
resolution_elem = calibration_elem.find("resolution")
if resolution_elem is not None:
sensor.calibration["resolution"]["width"] = int(resolution_elem.get("width"))
sensor.calibration["resolution"]["height"] = int(resolution_elem.get("height"))
for field in ["f", "cx", "cy", "k1", "k2", "k3", "p1", "p2"]:
field_elem = calibration_elem.find(field)
if field_elem is not None:
sensor.calibration[field] = float(field_elem.text)
# Covariance
covariance_elem = sensor_elem.find("covariance")
if covariance_elem is not None:
params_elem = covariance_elem.find("params")
coeffs_elem = covariance_elem.find("coeffs")
if params_elem is not None:
sensor.covariance["params"] = params_elem.text.split()
if coeffs_elem is not None:
sensor.covariance["coeffs"] = list(map(float, coeffs_elem.text.split()))
# Meta
meta_elem = sensor_elem.find("meta")
if meta_elem is not None:
for prop_elem in meta_elem.findall("property"):
name = prop_elem.get("name")
value = prop_elem.get("value")
if name and value:
sensor.meta[name] = value
sensors.append(sensor)
return sensors
class Camera:
def __init__(self, id, sensor_id, component_id, label, enabled, transform, rotation_covariance, location_covariance, orientation):
self.id = id
self.sensor_id = sensor_id
self.component_id = component_id
self.label = label
self.enabled = enabled
self.transform = transform
self.rotation_covariance = rotation_covariance
self.location_covariance = location_covariance
self.orientation = orientation
def __repr__(self):
return (
f"Camera(id={self.id}, sensor_id={self.sensor_id}, component_id={self.component_id}, "
f"label='{self.label}', enabled={self.enabled}, transform={self.transform}, "
f"rotation_covariance={self.rotation_covariance}, location_covariance={self.location_covariance}, "
f"orientation={self.orientation})"
)
import xml.etree.ElementTree as ET
class Camera:
def __init__(self, id, sensor_id, component_id, label, enabled, transform,
rotation_covariance, location_covariance, orientation):
self.id = id
self.sensor_id = sensor_id
self.component_id = component_id
self.label = label
self.enabled = enabled
self.transform = transform
self.rotation_covariance = rotation_covariance
self.location_covariance = location_covariance
self.orientation = orientation
def load_cameras_from_xml(file_path):
"""
Load cameras from a Metashape XML file and return chunk component transform details.
:param filepath: Path to the XML file.
:return: Tuple containing a list of Camera objects, the rotation matrix (3x3), and the scale value.
"""
with zipfile.ZipFile(file_path, 'r') as zip_ref:
with zip_ref.open("doc.xml") as file:
# Parse the XML file
tree = ET.parse(file)
root = tree.getroot()
# Find the first <component> in <chunk/components>
component_elem = root.find("./chunks/chunk/components/component")
if component_elem is None:
return [], None, None # No component found
# Extract transform data
transform_elem = component_elem.find("transform")
if transform_elem is not None:
rotation_elem = transform_elem.find("rotation")
if rotation_elem is not None:
rotation_matrix = [float(value) for value in rotation_elem.text.split()]
else:
rotation_matrix = None
translation_elem = transform_elem.find("translation")
if translation_elem is not None:
translation = [float(value) for value in translation_elem.text.split()]
else:
translation = None
scale_elem = transform_elem.find("scale")
scale_value = float(scale_elem.text.split()[0]) if scale_elem is not None else None
else:
rotation_matrix = None
scale_value = None
# Find the <cameras> section
cameras_section = root.find("./chunks/chunk/cameras")
if cameras_section is None:
return [], rotation_matrix, scale_value # No cameras found
cameras = []
# Iterate through each <camera> element
for camera_elem in cameras_section.findall("camera"):
camera_id = int(camera_elem.get("id")) if camera_elem.get("id") else None
sensor_id = int(camera_elem.get("sensor_id")) if camera_elem.get("sensor_id") else None
component_id = int(camera_elem.get("component_id")) if camera_elem.get("component_id") else None
label = camera_elem.get("label") if camera_elem.get("label") else None
enabled = camera_elem.get("enabled") == "true" if camera_elem.get("enabled") else None
if(component_id != None):
# Parse transform (space-separated string to list of floats)
transform = [
float(value) for value in camera_elem.findtext("transform", "").split()
]
# Parse rotation_covariance (if exists)
rotation_covariance = [
float(value)
for value in camera_elem.findtext("rotation_covariance", "").split()
]
# Parse location_covariance (if exists)
location_covariance = [
float(value)
for value in camera_elem.findtext("location_covariance", "").split()
]
# Parse orientation
orientation = int(camera_elem.findtext("orientation", "0"))
# Create Camera object
camera = Camera(
id=camera_id,
sensor_id=sensor_id,
component_id=component_id,
label=label,
enabled=enabled,
transform=transform,
rotation_covariance=rotation_covariance,
location_covariance=location_covariance,
orientation=orientation,
)
cameras.append(camera)
return cameras, rotation_matrix, translation, scale_value