Skip to content

Commit ccf73fa

Browse files
committed
perception box user wrapper
1 parent 46f3dd4 commit ccf73fa

File tree

2 files changed

+145
-51
lines changed

2 files changed

+145
-51
lines changed

example_client.py

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,34 @@
22
import time
33
import numpy as np
44
import open3d as o3d
5-
6-
def random_color_mapping(n_labels):
7-
"""Generate random colors for labels."""
8-
rng = np.random.default_rng()
9-
return rng.uniform(0, 1, size=(n_labels, 3))
10-
11-
def visualize_point_cloud(points, labels, n_labels):
12-
"""Visualize point cloud with colored labels."""
13-
# Convert points to Open3D point cloud format
14-
pcd = o3d.geometry.PointCloud()
15-
pcd.points = o3d.utility.Vector3dVector(points)
16-
17-
# Generate random colors for labels
18-
label_colors = random_color_mapping(n_labels)
19-
20-
# Assign colors to points based on their labels
21-
colors = label_colors[labels]
22-
pcd.colors = o3d.utility.Vector3dVector(colors)
23-
24-
# Visualize
25-
o3d.visualization.draw_geometries([pcd])
26-
27-
def visualize_point_cloud_color(points, colors):
28-
pcd = o3d.geometry.PointCloud()
29-
pcd.points = o3d.utility.Vector3dVector(points)
30-
pcd.colors = o3d.utility.Vector3dVector(colors)
31-
o3d.visualization.draw_geometries([pcd])
5+
from perception_box import PerceptionBox
326

337

348
def main():
35-
# Connect to the server
36-
server = xmlrpc.client.ServerProxy("http://172.16.244.187:5003")
37-
# with open("onnx_model_transfer/ESANET/esanet_with_preproc.onnx", "rb") as f:
38-
# model_data = f.read()
39-
# # send model as a binary using load_segmentation_model()
40-
# response = server.load_segmentation_model(xmlrpc.client.Binary(model_data))
41-
# print(response)
42-
# Start the task
43-
print(server.start_mapping(False, True))
9+
box = PerceptionBox("http://172.16.244.187:5003")
10+
box.send_segmentation_model(name = "finetuned_segformer", onnx_file_path= "onnx_model_transfer/segformer/fine_tuned_segformer.onnx")
11+
print(box.start_mapping(integrate_semantics=False, color = True, onnx_model_name="finetuned_segformer"))
4412

4513

46-
# Wait for mapping to process data
47-
# time.sleep(90) #
4814
while True:
4915
key = input("q to stop mapping")
5016
if key:
5117
if key.lower() == 'q':
5218
print("User chose to quit.")
5319
break
5420

55-
# server.pause_task()
56-
# time.sleep(50)
57-
# server.resume_task()
58-
# time.sleep(50)
59-
# Get the map
6021
time_begin = time.time()
61-
map_data = server.get_metric_map()
22+
map_data = box.get_metric_map()
6223
time_end = time.time()
63-
64-
65-
# Stop the task
66-
print(server.stop_mapping())
24+
25+
print(box.stop_mapping())
6726
print(f"time to transmit the map:{time_end - time_begin}")
6827
print(type(map_data['points']))
6928

7029
points = np.array(map_data['points'])
7130
colors = np.array(map_data['colors'])
72-
print(colors.shape)
73-
print(colors[0:50])
74-
visualize_point_cloud_color(points, colors)
31+
box.visualize_point_cloud_color(map_data)
32+
7533
# Extract points and labels
7634
# points = np.array(map_data['points'])
7735
# labels = np.array(map_data['labels'])

perception_box.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)