Datasets:

Modalities:
Text
ArXiv:
Libraries:
Datasets
File size: 7,684 Bytes
6eb4316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import logging
from typing import Iterable, Tuple

import cv2
import numpy as np
import pyzed.sl as sl
from tqdm import tqdm

log = logging.getLogger(__name__)


def extract_camera_data(

    svo_path: str,

    frame_indices: Iterable[int],

    depth_mode: str = "NEURAL_PLUS",

    confidence_threshold: int = 100,

    texture_confidence_threshold: int = 100,

) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """

    Extract selected RGB and depth frames from a recorded SVO/SVO2.



    This function ensures 1:1 pixel alignment between RGB and depth by:

    - Using the same rectified view (sl.VIEW.LEFT) for RGB

    - Using sl.MEASURE.DEPTH which matches the rectified resolution

    - Validating dimensions on the first frame to catch FOV mismatches



    Parameters:

    - svo_path: path to the .svo/.svo2 file recorded by `src/cameraRecorder.py`.

    - frame_indices: iterable of camera frame indices to keep

      (e.g. the `camera_frame_idx` column from `sync_pairs.csv`).

    - depth_mode: ZED depth mode, e.g. "NEURAL_PLUS" for the finest model.

      Note: NEURAL modes may fill in peripheral regions beyond traditional stereo overlap.

    - confidence_threshold: Depth confidence threshold (1-100). Higher values (closer to 100)

      fill in more depth holes but may add noise. Lower values filter more strictly.

      Default: 100 (least filtering, most coverage).

    - texture_confidence_threshold: Texture confidence threshold (1-100). Controls depth

      estimation in uniform/textureless areas. Lower values allow depth in less detailed

      regions. Default: 100 (least filtering).



    Returns:

    - rgb: array of shape (N, H, W, 3), dtype uint8, LEFT camera RGB frames (rectified).

    - depth_mm: array of shape (N, H, W), dtype float32, depth in millimeters (rectified).

      RGB and depth are guaranteed to have identical (H, W) dimensions for 1:1 alignment.

    - intrinsics: array of shape (4,), dtype float32, `[fx, fy, cx, cy]` for the

      rectified LEFT camera. These intrinsics apply to all returned frames.

    """
    if not os.path.exists(svo_path):
        raise FileNotFoundError(f"SVO not found: {svo_path}")

    indices = np.asarray(list(frame_indices), dtype=np.int64)
    if indices.size == 0:
        return (
            np.empty((0, 0, 0, 3), dtype=np.uint8),
            np.empty((0, 0), dtype=np.float32),
            np.empty((4,), dtype=np.float32),
        )

    # Ensure indices are sorted and unique.
    indices = np.unique(indices)

    init = sl.InitParameters()
    init.set_from_svo_file(svo_path)
    init.svo_real_time_mode = False  # Process SVO as fast as possible
    init.depth_mode = getattr(sl.DEPTH_MODE, depth_mode.upper().replace(" ", "_"), sl.DEPTH_MODE.NEURAL)
    init.coordinate_units = sl.UNIT.MILLIMETER  # depth in millimeters

    zed = sl.Camera()
    status = zed.open(init)
    if status != sl.ERROR_CODE.SUCCESS:
        raise RuntimeError(f"Failed to open SVO: {status}")
    
    log.info(f"Opened SVO with depth mode: {depth_mode}")
    
    # Validate camera calibration and resolution to ensure 1:1 RGB-Depth alignment
    cam_info = zed.get_camera_information()
    resolution = cam_info.camera_configuration.resolution
    calib = cam_info.camera_configuration.calibration_parameters.left_cam
    
    log.info(f"Camera resolution: {resolution.width}x{resolution.height}")
    log.info(f"Left camera calibration - fx={calib.fx:.2f}, fy={calib.fy:.2f}, cx={calib.cx:.2f}, cy={calib.cy:.2f}")
    intrinsics = np.array(
        [calib.fx, calib.fy, calib.cx, calib.cy],
        dtype=np.float32,
    )

    # Configure runtime parameters for depth estimation
    runtime_params = sl.RuntimeParameters()
    runtime_params.confidence_threshold = confidence_threshold
    runtime_params.texture_confidence_threshold = texture_confidence_threshold
    log.info(f"Depth confidence thresholds: confidence={confidence_threshold}, texture={texture_confidence_threshold}")

    num_frames_svo = zed.get_svo_number_of_frames()
    log.info(f"SVO contains {num_frames_svo} frames")
    log.info(f"Extracting {len(indices)} frame indices (min={indices.min()}, max={indices.max()})")

    # Convert indices to a set for O(1) lookup
    indices_set = set(indices.tolist())
    
    rgb_frames: list[np.ndarray] = []
    depth_frames: list[np.ndarray] = []

    image_left = sl.Mat()
    depth_mat = sl.Mat()

    try:
        # Loop through all frames in SVO sequentially
        # We must process sequentially because SVO cannot seek randomly
        extracted_count = 0
        first_frame_validated = False
        
        with tqdm(total=len(indices), desc="ZED camera extraction", unit="frame") as pbar:
            for frame_idx in range(num_frames_svo):
                # Grab the next frame from SVO
                grab_status = zed.grab(runtime_params)
                if grab_status != sl.ERROR_CODE.SUCCESS:
                    # End of file or error
                    log.warning(f"Grab failed at frame {frame_idx}: {grab_status}")
                    break
                
                # Always retrieve RGB and depth after grab() to keep SDK state clean
                # RGB: LEFT view (rectified)
                zed.retrieve_image(image_left, sl.VIEW.LEFT)
                bgra = image_left.get_data()  # (H, W, 4) uint8 - ZED SDK returns BGRA
                rgb = cv2.cvtColor(bgra, cv2.COLOR_BGRA2RGB)  # (H, W, 3) uint8

                # Depth (millimeters) - should match RGB resolution
                zed.retrieve_measure(depth_mat, sl.MEASURE.DEPTH)
                depth = depth_mat.get_data()  # (H, W) or (H, W, 1) - ZED reuses this buffer
                depth = np.asarray(depth, dtype=np.float32).squeeze().copy()  # (H, W) float32, own copy to prevent buffer reuse issues
                
                # Replace NaN values with 0 (invalid depth)
                depth = np.nan_to_num(depth, nan=0.0)

                # Validate 1:1 alignment on first frame
                if not first_frame_validated:
                    if rgb.shape[:2] != depth.shape:
                        raise ValueError(
                            f"RGB-Depth dimension mismatch! RGB: {rgb.shape[:2]}, Depth: {depth.shape}. "
                            f"This indicates FOV alignment issues."
                        )
                    log.info(f"✓ Validated 1:1 RGB-Depth alignment: {rgb.shape[:2]}")
                    first_frame_validated = True

                # Only keep this frame if it's in our desired indices
                if frame_idx in indices_set:
                    rgb_frames.append(rgb)
                    depth_frames.append(depth)
                    extracted_count += 1
                    pbar.update(1)
                    if extracted_count == len(indices):
                        break
                # If frame_idx not in indices_set, we skip this frame (don't append)
        
        log.info(f"Finished: Extracted {extracted_count}/{len(indices)} frames from {num_frames_svo} total frames")
    finally:
        zed.close()

    if not rgb_frames:
        log.warning("No frames were extracted!")
        return (
            np.empty((0, 0, 0, 3), dtype=np.uint8),
            np.empty((0, 0), dtype=np.float32),
            intrinsics,
        )

    rgb_arr = np.stack(rgb_frames, axis=0)
    depth_arr = np.stack(depth_frames, axis=0)
    
    log.info(f"Final arrays - RGB: {rgb_arr.shape}, Depth: {depth_arr.shape}")

    return rgb_arr, depth_arr, intrinsics