forked from wuxiaohua1011/ROAR_iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepth_cam_streamer.py
More file actions
63 lines (55 loc) · 2.17 KB
/
depth_cam_streamer.py
File metadata and controls
63 lines (55 loc) · 2.17 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
import logging
from websocket import create_connection
from typing import List, Optional, Tuple, List
import cv2
import numpy as np
from pathlib import Path
from ROAR.utilities_module.module import Module
import datetime
import websocket
class DepthCamStreamer(Module):
def save(self, **kwargs):
# no need to save. use Agent's saving mechanism
pass
def __init__(self, host, port, show=False, resize: Optional[Tuple] = None,
name: str = "depth_cam", threaded: bool = True,
update_interval: float = 0.5):
super().__init__(threaded=threaded, name=name, update_interval=update_interval)
self.logger = logging.getLogger(f"{self.name} server on [{host}:{port}]")
self.host = host
self.port = port
self.ws = websocket.WebSocket()
self.resize = resize
self.show = show
self.curr_image: Optional[np.ndarray] = None
self.intrinsics: Optional[np.ndarray] = None
self.logger.info(f"{name} initialized")
def connect(self):
for i in range(10):
try:
self.ws.connect(f"ws://{self.host}:{self.port}/{self.name}", timeout=0.1)
except Exception as e:
self.logger.error(e)
def receive(self):
try:
im = self.ws.recv()
intrinsics_str: str = self.ws.recv()
try:
intrinsics_arr = [float(i) for i in intrinsics_str.split(",")]
self.intrinsics = np.array([
[intrinsics_arr[0], 0, intrinsics_arr[2]],
[0, intrinsics_arr[1], intrinsics_arr[3]],
[0, 0, 1]
])
"""
width=256 height=192 bytesPerRow=1024 pixelFormat=fdep
"""
img: np.ndarray = np.frombuffer(im, dtype=np.float32)
self.curr_image = np.rot90(img.reshape((192, 256)), k=-1)
except Exception as e:
self.logger.error(f"Failed to decode image: {e}")
except Exception as e:
# self.logger.error(f"Failed to get image: {e}")
pass
def run_in_series(self, **kwargs):
self.receive()