-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplat.py
328 lines (259 loc) · 12.3 KB
/
splat.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
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
326
327
328
# import rospy
# import sensor_msgs.point_cloud2 as pc2
# import sensor_msgs.msg as sensor_msgs
# import std_msgs.msg as std_msgs
# import numpy as np
# import open3d as o3d
# import os
# class PCDPublisher:
# def __init__(self):
# rospy.init_node('pcd_publisher_node', anonymous=True)
# rospy.loginfo("PCD Publisher Node Initialized")
# # Specify the path to the point cloud file directly in the code.
# pcd_path = '/home/shriram/19_06_2024.ply'
# rospy.loginfo(f"Point cloud file path: {pcd_path}")
# # Check if the file exists
# assert os.path.exists(pcd_path), "File doesn't exist."
# rospy.loginfo("File exists. Reading point cloud file.")
# # Use Open3D to read point clouds and meshes.
# pcd = o3d.io.read_point_cloud(pcd_path)
# rospy.loginfo("Point cloud file read successfully.")
# # Convert the point cloud to numpy arrays for points and colors.
# self.points = np.asarray(pcd.points)
# self.colors = np.asarray(pcd.colors)
# rospy.loginfo(f"Points shape: {self.points.shape}")
# rospy.loginfo(f"Colors shape: {self.colors.shape}")
# # Create a publisher that publishes sensor_msgs.PointCloud2 to the topic 'pcd'.
# self.pcd_publisher = rospy.Publisher('pcd', sensor_msgs.PointCloud2, queue_size=10)
# self.timer = rospy.Timer(rospy.Duration(1/30.0), self.timer_callback)
# # Rotation matrix for visualization purposes. Rotates the point cloud on each timer callback.
# self.R = o3d.geometry.get_rotation_matrix_from_xyz([0, 0, np.pi/48])
# rospy.loginfo("Publisher and timer initialized.")
# def timer_callback(self, event):
# rospy.loginfo("Timer callback triggered.")
# # Rotate the point cloud for visualization purposes (if needed)
# # self.points = self.points @ self.R
# # Convert the numpy array into a sensor_msgs.PointCloud2 object
# self.pcd = point_cloud(self.points, self.colors, 'map')
# rospy.loginfo("Point cloud message created.")
# # Publish the PointCloud2 object
# self.pcd_publisher.publish(self.pcd)
# rospy.loginfo("Point cloud message published.")
# def point_cloud(points, colors, parent_frame):
# """ Creates a point cloud message.
# Args:
# points: Nx3 array of xyz positions.
# colors: Nx3 array of rgb colors.
# parent_frame: frame in which the point cloud is defined
# Returns:
# sensor_msgs/PointCloud2 message
# """
# rospy.loginfo("Creating point cloud message.")
# # In a PointCloud2 message, the point cloud is stored as an byte array.
# ros_dtype = sensor_msgs.PointField.FLOAT32
# dtype = np.float32
# itemsize = np.dtype(dtype).itemsize # A 32-bit float takes 4 bytes.
# # Combine points and colors into a single Nx6 array
# data = np.hstack([points, colors]).astype(dtype).tobytes()
# # The fields specify what the bytes represents. The first 4 bytes represents the x-coordinate, the next 4 the y-coordinate, etc.
# fields = [sensor_msgs.PointField(
# name=n, offset=i*itemsize, datatype=ros_dtype, count=1)
# for i, n in enumerate(['x', 'y', 'z', 'r', 'g', 'b'])]
# # The PointCloud2 message also has a header which specifies which coordinate frame it is represented in.
# header = std_msgs.Header(frame_id=parent_frame)
# header.stamp = rospy.Time.now()
# rospy.loginfo("Point cloud message created successfully.")
# return sensor_msgs.PointCloud2(
# header=header,
# height=1,
# width=points.shape[0],
# is_dense=False,
# is_bigendian=False,
# fields=fields,
# point_step=(itemsize * 6), # Every point consists of three float32s and three colors.
# row_step=(itemsize * 6 * points.shape[0]),
# data=data
# )
# def main():
# rospy.loginfo("Starting PCD Publisher Node.")
# # Boilerplate code.
# pcd_publisher = PCDPublisher()
# rospy.spin()
# # Destroy the node explicitly
# # (optional - otherwise it will be done automatically when the garbage collector destroys the node object)
# pcd_publisher.pcd_publisher.unregister()
# rospy.loginfo("PCD Publisher Node stopped.")
# if __name__ == '__main__':
# main()
#!/usr/bin/env python
# import rospy
# import sensor_msgs.point_cloud2 as pc2
# import sensor_msgs.msg as sensor_msgs
# import std_msgs.msg as std_msgs
# import numpy as np
# import open3d as o3d
# import os
# class PCDPublisher:
# def __init__(self):
# rospy.init_node('pcd_publisher_node', anonymous=True)
# rospy.loginfo("PCD Publisher Node Initialized")
# # Specify the path to the point cloud file directly in the code.
# pcd_path = '/home/shriram/pc3.ply'
# rospy.loginfo(f"Point cloud file path: {pcd_path}")
# # Check if the file exists
# assert os.path.exists(pcd_path), "File doesn't exist."
# rospy.loginfo("File exists. Reading point cloud file.")
# # Use Open3D to read point clouds and meshes.
# pcd = o3d.io.read_point_cloud(pcd_path)
# rospy.loginfo("Point cloud file read successfully.")
# # Convert the point cloud to numpy arrays for points and colors.
# self.points = np.asarray(pcd.points)
# self.colors = np.asarray(pcd.colors)
# rospy.loginfo(f"Points shape: {self.points.shape}")
# rospy.loginfo(f"Colors shape: {self.colors.shape}")
# # Create a publisher that publishes sensor_msgs.PointCloud2 to the topic 'pcd'.
# self.pcd_publisher = rospy.Publisher('pcd', sensor_msgs.PointCloud2, queue_size=10)
# self.timer = rospy.Timer(rospy.Duration(1/30.0), self.timer_callback)
# rospy.loginfo("Publisher and timer initialized.")
# def timer_callback(self, event):
# rospy.loginfo("Timer callback triggered.")
# # Convert the numpy array into a sensor_msgs.PointCloud2 object
# self.pcd = point_cloud(self.points, self.colors, 'world')
# rospy.loginfo("Point cloud message created.")
# # Publish the PointCloud2 object
# self.pcd_publisher.publish(self.pcd)
# rospy.loginfo("Point cloud message published.")
# def point_cloud(points, colors, parent_frame):
# """ Creates a point cloud message.
# Args:
# points: Nx3 array of xyz positions.
# colors: Nx3 array of rgb colors.
# parent_frame: frame in which the point cloud is defined
# Returns:
# sensor_msgs/PointCloud2 message
# """
# rospy.loginfo("Creating point cloud message.")
# # In a PointCloud2 message, the point cloud is stored as a byte array.
# ros_dtype = sensor_msgs.PointField.FLOAT32
# dtype = np.float32
# itemsize = np.dtype(dtype).itemsize # A 32-bit float takes 4 bytes.
# # Combine points and colors into a single Nx6 array
# data = np.hstack([points, colors]).astype(dtype).tobytes()
# # The fields specify what the bytes represent. The first 4 bytes represent the x-coordinate, the next 4 the y-coordinate, etc.
# fields = [sensor_msgs.PointField(
# name=n, offset=i*itemsize, datatype=ros_dtype, count=1)
# for i, n in enumerate(['x', 'y', 'z', 'r', 'g', 'b'])]
# # The PointCloud2 message also has a header which specifies which coordinate frame it is represented in.
# header = std_msgs.Header(frame_id=parent_frame)
# header.stamp = rospy.Time.now()
# rospy.loginfo("Point cloud message created successfully.")
# return sensor_msgs.PointCloud2(
# header=header,
# height=1,
# width=points.shape[0],
# is_dense=False,
# is_bigendian=False,
# fields=fields,
# point_step=(itemsize * 6), # Every point consists of three float32s and three colors.
# row_step=(itemsize * 6 * points.shape[0]),
# data=data
# )
# def main():
# rospy.loginfo("Starting PCD Publisher Node.")
# # Boilerplate code.
# pcd_publisher = PCDPublisher()
# rospy.spin()
# # Destroy the node explicitly
# # (optional - otherwise it will be done automatically when the garbage collector destroys the node object)
# pcd_publisher.pcd_publisher.unregister()
# rospy.loginfo("PCD Publisher Node stopped.")
# if __name__ == '__main__':
# main()
#!/usr/bin/env python
import rospy
import sensor_msgs.point_cloud2 as pc2
import sensor_msgs.msg as sensor_msgs
import std_msgs.msg as std_msgs
import numpy as np
import open3d as o3d
import os
class PCDPublisher:
def __init__(self):
rospy.init_node('pcd_publisher_node', anonymous=True)
# rospy.loginfo("PCD Publisher Node Initialized")
# Specify the path to the point cloud file directly in the code.
pcd_path = '/home/shriram/200.ply'
# rospy.loginfo(f"Point cloud file path: {pcd_path}")
# Check if the file exists
assert os.path.exists(pcd_path), "File doesn't exist."
# rospy.loginfo("File exists. Reading point cloud file.")
# Use Open3D to read point clouds and meshes.
pcd = o3d.io.read_point_cloud(pcd_path)
# rospy.loginfo("Point cloud file read successfully.")
# Convert the point cloud to numpy arrays for points and colors.
self.points = np.asarray(pcd.points)
if pcd.has_colors():
self.colors = np.asarray(pcd.colors)
else:
# Assign a default color (e.g., white) if no color information is available.
self.colors = np.ones((self.points.shape[0], 3))
# rospy.loginfo(f"Points shape: {self.points.shape}")
# rospy.loginfo(f"Colors shape: {self.colors.shape}")
# Create a publisher that publishes sensor_msgs.PointCloud2 to the topic 'pcd'.
self.pcd_publisher = rospy.Publisher('pcd', sensor_msgs.PointCloud2, queue_size=10)
self.timer = rospy.Timer(rospy.Duration(1/30.0), self.timer_callback)
# rospy.loginfo("Publisher and timer initialized.")
def timer_callback(self, event):
# rospy.loginfo("Timer callback triggered.")
# Convert the numpy array into a sensor_msgs.PointCloud2 object
self.pcd = point_cloud(self.points, self.colors, 'map')
# rospy.loginfo("Point cloud message created.")
# Publish the PointCloud2 object
self.pcd_publisher.publish(self.pcd)
# rospy.loginfo("Point cloud message published.")
def point_cloud(points, colors, parent_frame):
""" Creates a point cloud message.
Args:
points: Nx3 array of xyz positions.
colors: Nx3 array of rgb colors.
parent_frame: frame in which the point cloud is defined
Returns:
sensor_msgs/PointCloud2 message
"""
# rospy.loginfo("Creating point cloud message.")
# In a PointCloud2 message, the point cloud is stored as a byte array.
ros_dtype = sensor_msgs.PointField.FLOAT32
dtype = np.float32
itemsize = np.dtype(dtype).itemsize # A 32-bit float takes 4 bytes.
# Combine points and colors into a single Nx6 array
data = np.hstack([points, colors]).astype(dtype).tobytes()
# The fields specify what the bytes represent. The first 4 bytes represent the x-coordinate, the next 4 the y-coordinate, etc.
fields = [sensor_msgs.PointField(
name=n, offset=i*itemsize, datatype=ros_dtype, count=1)
for i, n in enumerate(['x', 'y', 'z', 'r', 'g', 'b'])]
# The PointCloud2 message also has a header which specifies which coordinate frame it is represented in.
header = std_msgs.Header(frame_id=parent_frame)
header.stamp = rospy.Time.now()
rospy.loginfo("Point cloud message created successfully.")
return sensor_msgs.PointCloud2(
header=header,
height=1,
width=points.shape[0],
is_dense=False,
is_bigendian=False,
fields=fields,
point_step=(itemsize * 6), # Every point consists of three float32s and three colors.
row_step=(itemsize * 6 * points.shape[0]),
data=data
)
def main():
# rospy.loginfo("Starting PCD Publisher Node.")
# Boilerplate code.
pcd_publisher = PCDPublisher()
rospy.spin()
# Destroy the node explicitly
# (optional - otherwise it will be done automatically when the garbage collector destroys the node object)
pcd_publisher.pcd_publisher.unregister()
rospy.loginfo("PCD Publisher Node stopped.")
if __name__ == '__main__':
main()