File size: 3,723 Bytes
2d76586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from segments import SegmentsClient

def get_samples(client, dataset_identifier):
    page = 1
    per_page = 1000
    samples = []
    while True:
        response = client.get_samples(dataset_identifier, per_page=per_page, page=page)
        # Handle both paginated and direct list responses
        if isinstance(response, list):
            samples.extend(response)
            break  # No pagination in this case
        else:
            samples.extend(response.results)
            if not response.has_next:
                break
        page += 1
    return samples

def export_number_of_samples(samples):
    """Return the number of samples in the dataset."""
    return len(samples)

def export_frames_and_annotations(label):
    """Export frames and their annotations for non-multisensor (no sensors) labels."""
    frames = getattr(label.attributes, "frames", None)
    if frames is None:
        return []
    result = []
    for frame in frames:
        annotations = getattr(frame, "annotations", [])
        result.append({
            "frame": frame,
            "annotations": annotations
        })
    return result

def export_sensor_frames_and_annotations(label, sensor_name):
    """Export frames and annotations for a specific sensor in a multisensor label."""
    sensors = getattr(label.attributes, "sensors", None)
    if sensors is None:
        return []
    for sensor in sensors:
        if getattr(sensor, "name", None) == sensor_name:
            sensor_attrs = getattr(sensor, "attributes", None)
            if sensor_attrs is None:
                return []
            frames = getattr(sensor_attrs, "frames", None)
            if frames is None:
                return []
            result = []
            for frame in frames:
                annotations = getattr(frame, "annotations", [])
                result.append({
                    "frame": frame,
                    "annotations": annotations
                })
            return result
    return []

def export_all_sensor_frames_and_annotations(label):
    """Export all frames and annotations for all sensors in a multisensor label."""
    sensors = getattr(label.attributes, "sensors", None)
    if sensors is None:
        return []
    result = {}
    for sensor in sensors:
        sensor_name = getattr(sensor, "name", None)
        sensor_attrs = getattr(sensor, "attributes", None)
        if sensor_attrs is None:
            result[sensor_name] = []
            continue
        frames = getattr(sensor_attrs, "frames", None)
        if frames is None:
            result[sensor_name] = []
            continue
        sensor_result = []
        for frame in frames:
            annotations = getattr(frame, "annotations", [])
            sensor_result.append({
                "frame": frame,
                "annotations": annotations
            })
        result[sensor_name] = sensor_result
    return result

def main():
    api_key = DEFAULT_API_KEY
    dataset_identifier = DEFAULT_DATASET_IDENTIFIER

    client = SegmentsClient(api_key)
    samples = get_samples(client, dataset_identifier)
    number_of_samples = export_number_of_samples(samples)
    print(number_of_samples)

    first_sample = samples[0]
    label = client.get_label(first_sample.uuid)
    frames_and_annotations = export_frames_and_annotations(label)
    print(frames_and_annotations)

    sensor_name = "sensor_name"
    sensor_frames_and_annotations = export_sensor_frames_and_annotations(label, sensor_name)
    print(sensor_frames_and_annotations)

    all_sensor_frames_and_annotations = export_all_sensor_frames_and_annotations(label)
    print(all_sensor_frames_and_annotations)

if __name__ == "__main__":
    main()