Spaces:
Sleeping
Sleeping
Create get_labels_from_samples.py
Browse files- src/get_labels_from_samples.py +109 -0
src/get_labels_from_samples.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from segments import SegmentsClient
|
| 2 |
+
|
| 3 |
+
def get_samples(client, dataset_identifier):
|
| 4 |
+
page = 1
|
| 5 |
+
per_page = 1000
|
| 6 |
+
samples = []
|
| 7 |
+
while True:
|
| 8 |
+
response = client.get_samples(dataset_identifier, per_page=per_page, page=page)
|
| 9 |
+
# Handle both paginated and direct list responses
|
| 10 |
+
if isinstance(response, list):
|
| 11 |
+
samples.extend(response)
|
| 12 |
+
break # No pagination in this case
|
| 13 |
+
else:
|
| 14 |
+
samples.extend(response.results)
|
| 15 |
+
if not response.has_next:
|
| 16 |
+
break
|
| 17 |
+
page += 1
|
| 18 |
+
return samples
|
| 19 |
+
|
| 20 |
+
def export_number_of_samples(samples):
|
| 21 |
+
"""Return the number of samples in the dataset."""
|
| 22 |
+
return len(samples)
|
| 23 |
+
|
| 24 |
+
def export_frames_and_annotations(label):
|
| 25 |
+
"""Export frames and their annotations for non-multisensor (no sensors) labels."""
|
| 26 |
+
frames = getattr(label.attributes, "frames", None)
|
| 27 |
+
if frames is None:
|
| 28 |
+
return []
|
| 29 |
+
result = []
|
| 30 |
+
for frame in frames:
|
| 31 |
+
annotations = getattr(frame, "annotations", [])
|
| 32 |
+
result.append({
|
| 33 |
+
"frame": frame,
|
| 34 |
+
"annotations": annotations
|
| 35 |
+
})
|
| 36 |
+
return result
|
| 37 |
+
|
| 38 |
+
def export_sensor_frames_and_annotations(label, sensor_name):
|
| 39 |
+
"""Export frames and annotations for a specific sensor in a multisensor label."""
|
| 40 |
+
sensors = getattr(label.attributes, "sensors", None)
|
| 41 |
+
if sensors is None:
|
| 42 |
+
return []
|
| 43 |
+
for sensor in sensors:
|
| 44 |
+
if getattr(sensor, "name", None) == sensor_name:
|
| 45 |
+
sensor_attrs = getattr(sensor, "attributes", None)
|
| 46 |
+
if sensor_attrs is None:
|
| 47 |
+
return []
|
| 48 |
+
frames = getattr(sensor_attrs, "frames", None)
|
| 49 |
+
if frames is None:
|
| 50 |
+
return []
|
| 51 |
+
result = []
|
| 52 |
+
for frame in frames:
|
| 53 |
+
annotations = getattr(frame, "annotations", [])
|
| 54 |
+
result.append({
|
| 55 |
+
"frame": frame,
|
| 56 |
+
"annotations": annotations
|
| 57 |
+
})
|
| 58 |
+
return result
|
| 59 |
+
return []
|
| 60 |
+
|
| 61 |
+
def export_all_sensor_frames_and_annotations(label):
|
| 62 |
+
"""Export all frames and annotations for all sensors in a multisensor label."""
|
| 63 |
+
sensors = getattr(label.attributes, "sensors", None)
|
| 64 |
+
if sensors is None:
|
| 65 |
+
return []
|
| 66 |
+
result = {}
|
| 67 |
+
for sensor in sensors:
|
| 68 |
+
sensor_name = getattr(sensor, "name", None)
|
| 69 |
+
sensor_attrs = getattr(sensor, "attributes", None)
|
| 70 |
+
if sensor_attrs is None:
|
| 71 |
+
result[sensor_name] = []
|
| 72 |
+
continue
|
| 73 |
+
frames = getattr(sensor_attrs, "frames", None)
|
| 74 |
+
if frames is None:
|
| 75 |
+
result[sensor_name] = []
|
| 76 |
+
continue
|
| 77 |
+
sensor_result = []
|
| 78 |
+
for frame in frames:
|
| 79 |
+
annotations = getattr(frame, "annotations", [])
|
| 80 |
+
sensor_result.append({
|
| 81 |
+
"frame": frame,
|
| 82 |
+
"annotations": annotations
|
| 83 |
+
})
|
| 84 |
+
result[sensor_name] = sensor_result
|
| 85 |
+
return result
|
| 86 |
+
|
| 87 |
+
def main():
|
| 88 |
+
api_key = DEFAULT_API_KEY
|
| 89 |
+
dataset_identifier = DEFAULT_DATASET_IDENTIFIER
|
| 90 |
+
|
| 91 |
+
client = SegmentsClient(api_key)
|
| 92 |
+
samples = get_samples(client, dataset_identifier)
|
| 93 |
+
number_of_samples = export_number_of_samples(samples)
|
| 94 |
+
print(number_of_samples)
|
| 95 |
+
|
| 96 |
+
first_sample = samples[0]
|
| 97 |
+
label = client.get_label(first_sample.uuid)
|
| 98 |
+
frames_and_annotations = export_frames_and_annotations(label)
|
| 99 |
+
print(frames_and_annotations)
|
| 100 |
+
|
| 101 |
+
sensor_name = "sensor_name"
|
| 102 |
+
sensor_frames_and_annotations = export_sensor_frames_and_annotations(label, sensor_name)
|
| 103 |
+
print(sensor_frames_and_annotations)
|
| 104 |
+
|
| 105 |
+
all_sensor_frames_and_annotations = export_all_sensor_frames_and_annotations(label)
|
| 106 |
+
print(all_sensor_frames_and_annotations)
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|