-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_csv_animation.py
152 lines (133 loc) · 4.56 KB
/
visualize_csv_animation.py
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
"""
Animate Point Cloud by CSVs in a specific directory
usage:
./visualize_csv_animation.py <directory>
"""
import open3d as o3d
import numpy as np
import os
import sys
import glob
from lidar_util.process_base import ProcessBase
TERMINATE_SIGNAL = "TERMINATED"
POINTS_PER_TURN = 54000
class ReadCsvProcess(ProcessBase):
def __init__(self, dir) -> None:
super().__init__()
self.dir = dir
def run(self, put_queue, _):
# meter threshold
x_thresh = [-6.0/2, 6.0/2]
y_thresh = [-6.0/2, 6.0/2]
z_thresh = [-2.0, 2.0]
files = glob.glob(f"{self.dir}/*.csv")
print(f"{len(files)} files")
for x in files:
point_list = []
with open(x) as f:
f.readline()
while True:
data = f.readline()
if not data:
break
if not "," in data:
continue
point_coords = np.float64(data.strip().split(",")[:3])
if (point_coords[0] > x_thresh[0]) and (point_coords[0] < x_thresh[1]) and \
(point_coords[1] > y_thresh[0]) and (point_coords[1] < y_thresh[1]) and \
(point_coords[2] > z_thresh[0]) and (point_coords[2] < z_thresh[1]):
point_list.append(point_coords)
print("read csv")
put_queue.put(point_list)
put_queue.put(TERMINATE_SIGNAL)
def o3d_animation(recv_queue):
vis = o3d.visualization.Visualizer()
vis.create_window()
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(
np.random.rand(1000,3) * 3
)
vis.add_geometry(pcd)
points = []
offset = 0
while True:
if len(points) < offset:
if recv_queue.empty():
# print("luck of data")
continue
received = recv_queue.get()
if received == TERMINATE_SIGNAL:
print("TERMINATE")
break
print(f"get points: {len(received)}")
points.extend(received)
remove_index = max(0,offset - POINTS_PER_TURN)
points = points[remove_index:]
offset -= remove_index
pcd.points = o3d.utility.Vector3dVector(
points[max(0, offset - POINTS_PER_TURN):offset]
)
vis.update_geometry(pcd)
vis.poll_events()
vis.update_renderer()
offset += 1000
def o3d_visualize(point_list: np.ndarray, view_thresh):
x_thresh = view_thresh[0]
y_thresh = view_thresh[1]
z_thresh = view_thresh[2]
vis = o3d.visualization.Visualizer()
vis.create_window()
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(point_list)
vis.add_geometry(pcd)
for i in range(0,len(point_list), 100):
pcd.points = o3d.utility.Vector3dVector(
point_list[:i,:]
)
vis.update_geometry(pcd)
vis.poll_events()
vis.update_renderer()
# o3d.visualization.draw_geometries(
# [pcd]
# )
def load_data(point_cloud_path):
# meter threshold
x_thresh = [-6.0/2, 6.0/2]
y_thresh = [-6.0/2, 6.0/2]
z_thresh = [-2.0, 2.0]
file_path = os.path.join(point_cloud_path)
point_list = []
all_point_list = []
with open(file_path) as f:
f.readline()
while True:
data = f.readline()
if not data:
break
if not "," in data:
continue
point_coords = np.float64(data.strip().split(",")[:4])
all_point_list.append(point_coords)
if (point_coords[0] > x_thresh[0]) and (point_coords[0] < x_thresh[1]) and \
(point_coords[1] > y_thresh[0]) and (point_coords[1] < y_thresh[1]) and \
(point_coords[2] > z_thresh[0]) and (point_coords[2] < z_thresh[1]):
point_list.append(point_coords)
point_list = np.array(point_list)
all_point_list = np.array(all_point_list)
thresh = [x_thresh, y_thresh, z_thresh]
return point_list, all_point_list, thresh
if __name__ == '__main__':
if len(sys.argv) < 2:
print(__doc__)
sys.exit(2)
# data, all_data, view_thresh = load_data(sys.argv[1])
# print(data.shape, all_data.shape)
# o3d_visualize(data, view_thresh)
p_read = ReadCsvProcess(sys.argv[1])
queue_read = p_read.put_queue
p_read.start()
try:
o3d_animation(queue_read)
except KeyboardInterrupt as e:
print(e)
p_read.finish()