File size: 12,544 Bytes
700dd75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import time
from typing import Any, Dict, Optional, Tuple

import cv2
import depthai as dai
import gymnasium as gym
import numpy as np

from decoupled_wbc.control.base.sensor import Sensor
from decoupled_wbc.control.sensor.sensor_server import (
    CameraMountPosition,
    ImageMessageSchema,
    SensorServer,
)


class OAKConfig:
    """Configuration for the OAK camera."""

    color_image_dim: Tuple[int, int] = (640, 480)  # RGB camera resolution
    monochrome_image_dim: Tuple[int, int] = (640, 480)  # Monochrome camera resolution
    fps: int = 30
    enable_color: bool = True  # Enable CAM_A (RGB)
    enable_mono_cameras: bool = False  # Enable CAM_B & CAM_C (Monochrome stereo pair)
    mount_position: str = CameraMountPosition.EGO_VIEW.value


class OAKSensor(Sensor, SensorServer):
    """Sensor for the OAK camera family."""

    def __init__(
        self,
        run_as_server: bool = False,
        port: int = 5555,
        config: OAKConfig = OAKConfig(),
        device_id: Optional[str] = None,
        mount_position: str = CameraMountPosition.EGO_VIEW.value,
    ):
        """Initialize the OAK camera."""
        self.config = config
        self.mount_position = mount_position
        self._run_as_server = run_as_server

        device_infos = dai.Device.getAllAvailableDevices()
        assert len(device_infos) > 0, f"No OAK devices found for {mount_position}"
        print(f"Device infos: {device_infos}")
        if device_id is not None:
            device_found = False
            for device_info in device_infos:
                if device_info.getDeviceId() == device_id:
                    self.device = dai.Device(device_info)
                    device_found = True
                    break
            if not device_found:
                raise ValueError(f"Device with ID {device_id} not found")
        else:
            self.device = dai.Device()

        print(f"Connected to OAK device: {self.device.getDeviceName(), self.device.getDeviceId()}")
        print(f"Device ID: {self.device.getDeviceId()}")

        sockets: list[dai.CameraBoardSocket] = self.device.getConnectedCameras()
        print(f"Available cameras: {[str(s) for s in sockets]}")

        # Create pipeline (without context manager to persist across method calls)
        self.pipeline = dai.Pipeline(self.device)
        self.output_queues = {}

        # Configure RGB camera (CAM_A)
        if config.enable_color and dai.CameraBoardSocket.CAM_A in sockets:
            self.cam_rgb = self.pipeline.create(dai.node.Camera)
            cam_socket = dai.CameraBoardSocket.CAM_A
            self.cam_rgb = self.cam_rgb.build(cam_socket)
            # Create RGB output queue
            self.output_queues["color"] = self.cam_rgb.requestOutput(
                config.color_image_dim,
                fps=config.fps,
            ).createOutputQueue()
            print("Enabled CAM_A (RGB)")

        # Configure Monochrome cameras (CAM_B & CAM_C)
        if config.enable_mono_cameras:
            if dai.CameraBoardSocket.CAM_B in sockets:
                self.cam_mono_left = self.pipeline.create(dai.node.Camera)
                cam_socket = dai.CameraBoardSocket.CAM_B
                self.cam_mono_left = self.cam_mono_left.build(cam_socket)
                # Create mono left output queue
                self.output_queues["mono_left"] = self.cam_mono_left.requestOutput(
                    config.monochrome_image_dim,
                    fps=config.fps,
                ).createOutputQueue()
                print("Enabled CAM_B (Monochrome Left)")

            if dai.CameraBoardSocket.CAM_C in sockets:
                self.cam_mono_right = self.pipeline.create(dai.node.Camera)
                cam_socket = dai.CameraBoardSocket.CAM_C
                self.cam_mono_right = self.cam_mono_right.build(cam_socket)
                # Create mono right output queue
                self.output_queues["mono_right"] = self.cam_mono_right.requestOutput(
                    config.monochrome_image_dim,
                    fps=config.fps,
                ).createOutputQueue()
                print("Enabled CAM_C (Monochrome Right)")

        assert len(self.output_queues) > 0, "No output queues enabled"
        # auto exposure compensation, for CoRL demo
        # cam_q_in = self.cam_rgb.inputControl.createInputQueue()
        # ctrl = dai.CameraControl()
        # ctrl.setAutoExposureEnable()
        # ctrl.setAutoExposureCompensation(-2)
        # cam_q_in.send(ctrl)

        # Start pipeline on device
        self.pipeline.start()

        if run_as_server:
            self.start_server(port)

    def read(self) -> Optional[Dict[str, Any]]:
        """Read images from the camera."""
        if not self.pipeline.isRunning():
            print(f"[ERROR] OAK pipeline stopped for {self.mount_position}")
            return None

        # Check if device is still connected
        if not self.device.isPipelineRunning():
            print(f"[ERROR] OAK device disconnected for {self.mount_position}")
            return None

        timestamps = {}
        images = {}
        rgb_frame_time = None

        # Get color frame if enabled
        if "color" in self.output_queues:
            try:
                rgb_frame = self.output_queues["color"].get()
                rgb_frame_time = rgb_frame.getTimestamp()
                if rgb_frame is not None:
                    images[self.mount_position] = rgb_frame.getCvFrame()[..., ::-1]  # BGR to RGB
                    timestamps[self.mount_position] = (
                        rgb_frame_time - dai.Clock.now()
                    ).total_seconds() + time.time()
            except Exception as e:
                print(f"[ERROR] Failed to read color frame from {self.mount_position}: {e}")
                return None

        # Get mono frames if enabled
        if "mono_left" in self.output_queues:
            try:
                mono_left_frame = self.output_queues["mono_left"].get()
                mono_left_frame_time = mono_left_frame.getTimestamp()
                if mono_left_frame is not None:
                    key = f"{self.mount_position}_left_mono"
                    images[key] = mono_left_frame.getCvFrame()
                    timestamps[key] = (
                        mono_left_frame_time - dai.Clock.now()
                    ).total_seconds() + time.time()
            except Exception as e:
                print(f"[ERROR] Failed to read mono_left frame from {self.mount_position}: {e}")
                return None

        if "mono_right" in self.output_queues:
            try:
                mono_right_frame = self.output_queues["mono_right"].get()
                mono_right_frame_time = mono_right_frame.getTimestamp()
                if mono_right_frame is not None:
                    key = f"{self.mount_position}_right_mono"
                    images[key] = mono_right_frame.getCvFrame()
                    timestamps[key] = (
                        mono_right_frame_time - dai.Clock.now()
                    ).total_seconds() + time.time()
            except Exception as e:
                print(f"[ERROR] Failed to read mono_right frame from {self.mount_position}: {e}")
                return None

        if (
            rgb_frame_time is not None
            and (rgb_frame_time - dai.Clock.now()).total_seconds() <= -0.2
        ):
            print(
                f"[{self.mount_position}] OAK latency too large: "
                f"{(dai.Clock.now() - rgb_frame_time).total_seconds() * 1000}ms"
            )

        return {
            "timestamps": timestamps,
            "images": images,
        }

    def serialize(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Serialize data using ImageMessageSchema."""
        serialized_msg = ImageMessageSchema(timestamps=data["timestamps"], images=data["images"])
        return serialized_msg.serialize()

    def observation_space(self) -> gym.Space:
        spaces = {}

        if self.config.enable_color:
            spaces["color_image"] = gym.spaces.Box(
                low=0,
                high=255,
                shape=(self.config.color_image_dim[1], self.config.color_image_dim[0], 3),
                dtype=np.uint8,
            )

        if self.config.enable_mono_cameras:
            spaces["mono_left_image"] = gym.spaces.Box(
                low=0,
                high=255,
                shape=(self.config.monochrome_image_dim[1], self.config.monochrome_image_dim[0]),
                dtype=np.uint8,
            )
            spaces["mono_right_image"] = gym.spaces.Box(
                low=0,
                high=255,
                shape=(self.config.monochrome_image_dim[1], self.config.monochrome_image_dim[0]),
                dtype=np.uint8,
            )

        return gym.spaces.Dict(spaces)

    def close(self):
        """Close the camera connection."""
        if self._run_as_server:
            self.stop_server()
        if hasattr(self, "pipeline") and self.pipeline.isRunning():
            self.pipeline.stop()
        self.device.close()

    def run_server(self):
        """Run the server."""
        if not self._run_as_server:
            raise ValueError("This function is only available when run_as_server is True")

        while True:
            frame = self.read()
            if frame is None:
                continue

            msg = self.serialize(frame)
            self.send_message({self.mount_position: msg})

    def __del__(self):
        self.close()


if __name__ == "__main__":
    """Test function for OAK camera."""

    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--server", action="store_true", help="Run as server")
    parser.add_argument("--client", action="store_true", help="Run as client")
    parser.add_argument("--host", type=str, default="localhost", help="Server IP address")
    parser.add_argument("--port", type=int, default=5555, help="Port number")
    parser.add_argument("--device-id", type=str, default=None, help="Specific device ID")
    parser.add_argument(
        "--enable-mono", action="store_true", help="Enable monochrome cameras (CAM_B & CAM_C)"
    )
    parser.add_argument("--mount-position", type=str, default="ego_view", help="Mount position")
    parser.add_argument("--show-image", action="store_true", help="Display images")
    args = parser.parse_args()

    oak_config = OAKConfig()
    if args.enable_mono:
        oak_config.enable_mono_cameras = True

    if args.server:
        # Run as server
        oak = OAKSensor(
            run_as_server=True,
            port=args.port,
            config=oak_config,
            device_id=args.device_id,
            mount_position=args.mount_position,
        )
        print(f"Starting OAK server on port {args.port}")
        oak.run_server()

    else:
        # Run standalone
        oak = OAKSensor(run_as_server=False, config=oak_config, device_id=args.device_id)
        print("Running OAK camera in standalone mode")

        while True:
            frame = oak.read()
            if frame is None:
                print("Waiting for frame...")
                time.sleep(0.5)
                continue

            if "color_image" in frame:
                print(f"Color image shape: {frame['color_image'].shape}")
            if "mono_left_image" in frame:
                print(f"Mono left image shape: {frame['mono_left_image'].shape}")
            if "mono_right_image" in frame:
                print(f"Mono right image shape: {frame['mono_right_image'].shape}")
            if "depth_image" in frame:
                print(f"Depth image shape: {frame['depth_image'].shape}")

            if args.show_image:
                if "color_image" in frame:
                    cv2.imshow("Color Image", frame["color_image"])

                if "mono_left_image" in frame:
                    cv2.imshow("Mono Left", frame["mono_left_image"])
                if "mono_right_image" in frame:
                    cv2.imshow("Mono Right", frame["mono_right_image"])

                if "depth_image" in frame:
                    depth_colormap = cv2.applyColorMap(
                        cv2.convertScaleAbs(frame["depth_image"], alpha=0.03), cv2.COLORMAP_JET
                    )
                    cv2.imshow("Depth Image", depth_colormap)

                if cv2.waitKey(1) == ord("q"):
                    break

            time.sleep(0.01)

        cv2.destroyAllWindows()
        oak.close()