|
| 1 | +import xmlrpc.client |
| 2 | +import numpy as np |
| 3 | +import open3d as o3d |
| 4 | +import cv2 |
| 5 | +import threading |
| 6 | +import time |
| 7 | + |
| 8 | +class PerceptionBox: |
| 9 | + def __init__(self, address): |
| 10 | + self.server = xmlrpc.client.ServerProxy(address) |
| 11 | + self._live_streaming_thread = None |
| 12 | + self._stop_live_streaming = threading.Event() |
| 13 | + |
| 14 | + def start_mapping(self, integrate_semantics=False, color=True, voxel_size=0.05, res=8, initial_num_blocks=17500, onnx_model_name=None): |
| 15 | + self.semantics = integrate_semantics |
| 16 | + self.color = color |
| 17 | + return self.server.start_mapping(integrate_semantics, color, voxel_size, res, initial_num_blocks, onnx_model_name) |
| 18 | + |
| 19 | + def stop_mapping(self): |
| 20 | + return self.server.stop_mapping() |
| 21 | + |
| 22 | + def pause_mapping(self): |
| 23 | + return self.server.pause_mapping() |
| 24 | + |
| 25 | + def resume_mapping(self): |
| 26 | + return self.server.resume_mapping() |
| 27 | + |
| 28 | + def get_metric_map(self): |
| 29 | + return self.server.get_metric_map() |
| 30 | + |
| 31 | + def get_semantic_map(self, map_type="pcd", top_label=True): |
| 32 | + return self.server.get_semantic_map(map_type, top_label) |
| 33 | + |
| 34 | + def get_map_stop_mapping(self): |
| 35 | + return self.server.get_map_stop_mapping() |
| 36 | + |
| 37 | + def load_segmentation_model(self, name, onnx_file_path): |
| 38 | + with open(onnx_file_path, "rb") as f: |
| 39 | + model_binary = xmlrpc.client.Binary(f.read()) |
| 40 | + return self.server.load_segmentation_model(name, model_binary) |
| 41 | + |
| 42 | + def list_onnx_models(self): |
| 43 | + return self.server.list_onnx_models() |
| 44 | + |
| 45 | + def delete_onnx_model(self, name): |
| 46 | + return self.server.delete_onnx_model(name) |
| 47 | + |
| 48 | +def start_live_streaming(self, refresh_rate=1.0, semantics=False, color=True): |
| 49 | + if not self.semantics and semantics: |
| 50 | + print("Warning: Semantics not being integrated! Streaming metric map instead.") |
| 51 | + semantics = False |
| 52 | + if not self.color and color: |
| 53 | + print("Warning: Color not being integrated! Streaming without color.") |
| 54 | + color = False |
| 55 | + |
| 56 | + if self._live_streaming_thread is not None and self._live_streaming_thread.is_alive(): |
| 57 | + print("Live streaming is already running.") |
| 58 | + return |
| 59 | + |
| 60 | + self._stop_live_streaming.clear() |
| 61 | + |
| 62 | + def stream_loop(): |
| 63 | + vis = o3d.visualization.Visualizer() |
| 64 | + vis.create_window(window_name='PerceptionBox Live Map', width=960, height=720) |
| 65 | + added = False |
| 66 | + pcd = o3d.geometry.PointCloud() |
| 67 | + |
| 68 | + while not self._stop_live_streaming.is_set(): |
| 69 | + try: |
| 70 | + if semantics: |
| 71 | + map_data = self.get_semantic_map() |
| 72 | + points = np.array(map_data['points']) |
| 73 | + labels = np.array(map_data['labels']) |
| 74 | + |
| 75 | + rng = np.random.default_rng() |
| 76 | + label_colors = rng.uniform(0, 1, size=(np.max(labels) + 1, 3)) |
| 77 | + colors = label_colors[labels] |
| 78 | + |
| 79 | + else: |
| 80 | + map_data = self.get_metric_map() |
| 81 | + points = np.array(map_data['points']) |
| 82 | + colors = np.array(map_data['colors']) |
| 83 | + |
| 84 | + pcd.points = o3d.utility.Vector3dVector(points) |
| 85 | + |
| 86 | + if colors is not None and (colors.size) > 0: |
| 87 | + pcd.colors = o3d.utility.Vector3dVector(colors) |
| 88 | + |
| 89 | + |
| 90 | + if not added: |
| 91 | + vis.add_geometry(pcd) |
| 92 | + added = True |
| 93 | + else: |
| 94 | + vis.update_geometry(pcd) |
| 95 | + |
| 96 | + vis.poll_events() |
| 97 | + vis.update_renderer() |
| 98 | + |
| 99 | + except Exception as e: |
| 100 | + print(f"Live streaming error: {e}") |
| 101 | + |
| 102 | + time.sleep(refresh_rate) |
| 103 | + |
| 104 | + vis.destroy_window() |
| 105 | + |
| 106 | + self._live_streaming_thread = threading.Thread(target=stream_loop, daemon=True) |
| 107 | + self._live_streaming_thread.start() |
| 108 | + |
| 109 | + def stop_live_streaming(self): |
| 110 | + if self._live_streaming_thread is not None: |
| 111 | + self._stop_live_streaming.set() |
| 112 | + self._live_streaming_thread.join() |
| 113 | + self._live_streaming_thread = None |
| 114 | + print("Live streaming stopped.") |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def visualize_point_cloud_color(map_data): |
| 118 | + points = np.array(map_data['points']) |
| 119 | + colors = np.array(map_data['colors']) |
| 120 | + pcd = o3d.geometry.PointCloud() |
| 121 | + pcd.points = o3d.utility.Vector3dVector(points) |
| 122 | + pcd.colors = o3d.utility.Vector3dVector(colors) |
| 123 | + o3d.visualization.draw_geometries([pcd]) |
| 124 | + |
| 125 | + @staticmethod |
| 126 | + def visualize_point_cloud_labels(map_data, n_labels=21): |
| 127 | + points = np.array(map_data['points']) |
| 128 | + labels = np.array(map_data['labels']) |
| 129 | + pcd = o3d.geometry.PointCloud() |
| 130 | + pcd.points = o3d.utility.Vector3dVector(points) |
| 131 | + |
| 132 | + rng = np.random.default_rng() |
| 133 | + label_colors = rng.uniform(0, 1, size=(n_labels, 3)) |
| 134 | + colors = label_colors[labels] |
| 135 | + pcd.colors = o3d.utility.Vector3dVector(colors) |
| 136 | + o3d.visualization.draw_geometries([pcd]) |
0 commit comments