7
7
import math
8
8
import os
9
9
10
-
11
10
import struct
12
11
13
12
# 3rd-party
@@ -60,6 +59,7 @@ def getPointsInSensorAsNPArray_local(_collection_key, _sensor_key, _label_key, _
60
59
points [3 , :] = 1
61
60
return points
62
61
62
+
63
63
def getCvImageFromCollectionSensor (collection_key , sensor_key , dataset ):
64
64
dictionary = dataset ['collections' ][collection_key ]['data' ][sensor_key ]
65
65
return getCvImageFromDictionary (dictionary )
@@ -212,7 +212,7 @@ def setupVisualization(dataset, args, selected_collection_key):
212
212
# Create image publishers ----------------------------------------------------------
213
213
# We need to republish a new image at every visualization
214
214
for sensor_key , sensor in dataset ['sensors' ].items ():
215
- if sensor ['modality' ] == 'rgb' :
215
+ if sensor ['modality' ] == 'rgb' :
216
216
msg_type = sensor_msgs .msg .Image
217
217
topic = dataset ['calibration_config' ]['sensors' ][sensor_key ]['topic_name' ]
218
218
topic_name = topic + '/labeled'
@@ -224,30 +224,38 @@ def setupVisualization(dataset, args, selected_collection_key):
224
224
graphics ['collections' ][str (sensor_key )]['publisher_camera_info' ] = \
225
225
rospy .Publisher (topic_name , msg_type , queue_size = 0 , latch = True )
226
226
227
+ if sensor ['modality' ] == 'depth' :
228
+ msg_type = sensor_msgs .msg .Image
229
+ topic = dataset ['calibration_config' ]['sensors' ][sensor_key ]['topic_name' ]
230
+ topic_name = topic + '/labeled'
231
+ graphics ['collections' ][str (sensor_key )] = {'publisher' : rospy .Publisher (
232
+ topic_name , msg_type , queue_size = 0 , latch = True )}
233
+ print ('Created image publisher' )
234
+ msg_type = sensor_msgs .msg .CameraInfo
235
+ topic_name = str (sensor_key ) + '/camera_info'
236
+ graphics ['collections' ][str (sensor_key )]['publisher_camera_info' ] = \
237
+ rospy .Publisher (topic_name , msg_type , queue_size = 0 , latch = True )
227
238
# Create Labeled and Unlabeled Data publishers ----------------------------------------------------------
228
239
markers = MarkerArray ()
229
-
240
+
230
241
lidar_data = []
231
242
graphics ['ros' ]['PubPointCloud' ] = dict ()
232
243
for sensor_key , sensor in dataset ['sensors' ].items ():
233
244
if sensor ['modality' ] == 'lidar3d' :
234
245
graphics ['ros' ]['PubPointCloud' ][sensor_key ] = \
235
246
rospy .Publisher (str (sensor_key ) + '/points' , PointCloud2 , queue_size = 0 , latch = True )
236
-
247
+
237
248
for collection_key , collection in dataset ['collections' ].items ():
238
249
# if not collection['labels'][str(sensor_key)]['detected']: # not detected by sensor in collection
239
250
# continue
240
-
251
+
241
252
# when the sensor has no label, the 'idxs_limit_points' does not exist!!!
242
253
if 'idxs_limit_points' not in collection ['labels' ][str (sensor_key )]:
243
254
collection ['labels' ][str (sensor_key )]['idxs_limit_points' ] = []
244
-
255
+
245
256
if collection ['labels' ][str (sensor_key )]['idxs' ] != []:
246
257
collection ['labels' ][str (sensor_key )]['detected' ] = True
247
-
248
-
249
-
250
-
258
+
251
259
# if sensor['msg_type'] == 'LaserScan': # -------- Publish the laser scan data ------------------------------
252
260
if sensor ['modality' ] == 'lidar2d' :
253
261
frame_id = genCollectionPrefix (collection_key , collection ['data' ][sensor_key ]['header' ]['frame_id' ])
@@ -304,7 +312,7 @@ def setupVisualization(dataset, args, selected_collection_key):
304
312
305
313
# if sensor['msg_type'] == 'PointCloud2': # -------- Publish the velodyne data ------------------------------
306
314
if sensor ['modality' ] == 'lidar3d' :
307
-
315
+
308
316
# Add labelled points to the marker
309
317
frame_id = genCollectionPrefix (collection_key , collection ['data' ][sensor_key ]['header' ]['frame_id' ])
310
318
marker = Marker (header = Header (frame_id = frame_id , stamp = now ),
@@ -344,17 +352,18 @@ def setupVisualization(dataset, args, selected_collection_key):
344
352
markers .markers .append (copy .deepcopy (marker ))
345
353
346
354
# Add 3D lidar data
347
- original_pointcloud_msg = getPointCloudMessageFromDictionary (dataset ['collections' ][collection_key ]['data' ][sensor_key ])
355
+ original_pointcloud_msg = getPointCloudMessageFromDictionary (
356
+ dataset ['collections' ][collection_key ]['data' ][sensor_key ])
348
357
final_pointcloud_msg = PointCloud2 (header = Header (frame_id = frame_id , stamp = now ),
349
- height = original_pointcloud_msg .height , width = original_pointcloud_msg .width ,
350
- fields = original_pointcloud_msg .fields , is_bigendian = original_pointcloud_msg .is_bigendian ,
351
- point_step = original_pointcloud_msg .point_step , row_step = original_pointcloud_msg .row_step ,
352
- data = original_pointcloud_msg .data , is_dense = original_pointcloud_msg .is_dense )
358
+ height = original_pointcloud_msg .height ,
359
+ width = original_pointcloud_msg .width ,
360
+ fields = original_pointcloud_msg .fields ,
361
+ is_bigendian = original_pointcloud_msg .is_bigendian ,
362
+ point_step = original_pointcloud_msg .point_step ,
363
+ row_step = original_pointcloud_msg .row_step ,
364
+ data = original_pointcloud_msg .data ,
365
+ is_dense = original_pointcloud_msg .is_dense )
353
366
lidar_data .append (final_pointcloud_msg )
354
-
355
-
356
-
357
-
358
367
359
368
graphics ['ros' ]['MarkersLabeled' ] = markers
360
369
graphics ['ros' ]['PointClouds' ] = lidar_data
@@ -542,7 +551,7 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
542
551
graphics = models ['graphics' ]
543
552
544
553
collection = dataset ['collections' ][selected_collection_key ]
545
-
554
+
546
555
# print("args['initial_pose_ghost'])" + str(args['initial_pose_ghost']))
547
556
548
557
now = rospy .Time .now () # time used to publish all visualization messages
@@ -640,55 +649,54 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
640
649
# Update timestamp for pointcloud2 message
641
650
for pointcloud_msg in graphics ['ros' ]['PointClouds' ]:
642
651
pointcloud_msg .header .stamp = now
643
-
644
- #print(graphics['ros']['PointClouds'])
652
+
653
+ # print(graphics['ros']['PointClouds'])
645
654
# Create a new pointcloud which contains only the points related to the selected collection
646
655
for pointcloud_msg in graphics ['ros' ]['PointClouds' ]:
647
-
656
+
648
657
prefix = pointcloud_msg .header .frame_id .split ('_' )[0 ] + '_'
649
658
sensor = pointcloud_msg .header .frame_id [len (prefix ):]
650
-
659
+
651
660
# prof. Miguel these two lines that you wrote wasted 2 hours of my life!
652
- #prefix = pointcloud_msg.header.frame_id[:3]
653
- #sensor = pointcloud_msg.header.frame_id[3:]
654
-
655
- #print('-----------------------------------------------------------')
661
+ # prefix = pointcloud_msg.header.frame_id[:3]
662
+ # sensor = pointcloud_msg.header.frame_id[3:]
663
+
664
+ # print('-----------------------------------------------------------')
656
665
if prefix == 'c' + str (selected_collection_key ) + '_' :
657
666
# change intensity channel according to the idxs
658
667
points_collection = pc2 .read_points (pointcloud_msg )
659
668
gen_points = list (points_collection )
660
669
final_points = []
661
-
670
+
662
671
idxs = dataset ['collections' ][selected_collection_key ]['labels' ][sensor ]['idxs' ]
663
672
idxs_limit_points = dataset ['collections' ][selected_collection_key ]['labels' ][sensor ]['idxs_limit_points' ]
664
673
sensor_idx = list (dataset ['collections' ][selected_collection_key ]['labels' ].keys ()).index (sensor )
665
-
674
+
666
675
for idx , point in enumerate (gen_points ):
667
676
if idx in idxs_limit_points :
668
- r ,g , b = 50 ,70 ,20
677
+ r , g , b = 50 , 70 , 20
669
678
elif idx in idxs :
670
- r ,g , b = 100 ,220 ,20
679
+ r , g , b = 100 , 220 , 20
671
680
else :
672
- r ,g , b = 186 ,189 , 182
673
-
681
+ r , g , b = 186 , 189 , 182
682
+
674
683
point_color = [point [0 ], point [1 ], point [2 ], idx , 0 , sensor_idx ]
675
684
rgb = struct .unpack ('I' , struct .pack ('BBBB' , b , g , r , 255 ))[0 ]
676
685
point_color [4 ] = rgb
677
-
686
+
678
687
final_points .append (point_color )
679
-
688
+
680
689
fields = [PointField ('x' , 0 , PointField .FLOAT32 , 1 ),
681
- PointField ('y' , 4 , PointField .FLOAT32 , 1 ),
682
- PointField ('z' , 8 , PointField .FLOAT32 , 1 ),
683
- PointField ('idx' , 12 , PointField .FLOAT32 , 1 ),
684
- PointField ('rgb' , 20 , PointField .UINT32 , 1 ),
685
- PointField ('sensor_idx' , 24 , PointField .FLOAT32 , 1 )
686
- ]
690
+ PointField ('y' , 4 , PointField .FLOAT32 , 1 ),
691
+ PointField ('z' , 8 , PointField .FLOAT32 , 1 ),
692
+ PointField ('idx' , 12 , PointField .FLOAT32 , 1 ),
693
+ PointField ('rgb' , 20 , PointField .UINT32 , 1 ),
694
+ PointField ('sensor_idx' , 24 , PointField .FLOAT32 , 1 )
695
+ ]
687
696
pointcloud_msg_final = pc2 .create_cloud (pointcloud_msg .header , fields , final_points )
688
-
697
+
689
698
# create a new point cloud2, and change a value related to the idx
690
699
graphics ['ros' ]['PubPointCloud' ][sensor ].publish (pointcloud_msg_final )
691
-
692
700
693
701
# Create a new marker array which contains only the marker related to the selected collection
694
702
# Publish the pattern data
@@ -698,7 +706,8 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
698
706
if prefix == 'c' + str (selected_collection_key ) + '_' :
699
707
marker_array_1 .markers .append (marker )
700
708
marker_array_1 .markers [- 1 ].action = Marker .ADD
701
- elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (previous_selected_collection_key ) + '_' :
709
+ elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (
710
+ previous_selected_collection_key ) + '_' :
702
711
marker_array_1 .markers .append (marker )
703
712
marker_array_1 .markers [- 1 ].action = Marker .DELETE
704
713
graphics ['ros' ]['PubPattern' ].publish (marker_array_1 )
@@ -711,7 +720,8 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
711
720
if prefix == 'c' + str (selected_collection_key ) + '_' :
712
721
marker_array_2 .markers .append (marker )
713
722
marker_array_2 .markers [- 1 ].action = Marker .ADD
714
- elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (previous_selected_collection_key ) + '_' :
723
+ elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (
724
+ previous_selected_collection_key ) + '_' :
715
725
marker_array_2 .markers .append (marker )
716
726
marker_array_2 .markers [- 1 ].action = Marker .DELETE
717
727
graphics ['ros' ]['publisher_models' ].publish (graphics ['ros' ]['robot_mesh_markers' ])
@@ -724,7 +734,8 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
724
734
if prefix == 'c' + str (selected_collection_key ) + '_' :
725
735
marker_array_3 .markers .append (marker )
726
736
marker_array_3 .markers [- 1 ].action = Marker .ADD
727
- elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (previous_selected_collection_key ) + '_' :
737
+ elif not previous_selected_collection_key == selected_collection_key and prefix == 'c' + str (
738
+ previous_selected_collection_key ) + '_' :
728
739
marker_array_3 .markers .append (marker )
729
740
marker_array_3 .markers [- 1 ].action = Marker .DELETE
730
741
@@ -775,15 +786,52 @@ def visualizationFunction(models, selected_collection_key, previous_selected_col
775
786
graphics ['collections' ][sensor_key ]['publisher_camera_info' ].publish (
776
787
camera_info_msg )
777
788
789
+ if sensor ['modality' ] == 'depth' :
790
+ if args ['show_images' ]:
791
+ collection = collections [selected_collection_key ]
792
+ image = copy .deepcopy (getCvImageFromCollectionSensor (selected_collection_key , sensor_key , dataset ))
793
+ width = collection ['data' ][sensor_key ]['width' ]
794
+ height = collection ['data' ][sensor_key ]['height' ]
795
+ idxs = dataset ['collections' ][selected_collection_key ]['labels' ][sensor_key ]['idxs' ]
796
+ idxs_limit_points = dataset ['collections' ][selected_collection_key ]['labels' ][sensor_key ][
797
+ 'idxs_limit_points' ]
798
+ gui_image = np .zeros ((height , width , 3 ), dtype = np .uint8 )
799
+ max_value = 5
800
+ gui_image [:, :, 0 ] = image / max_value * 255
801
+ gui_image [:, :, 1 ] = image / max_value * 255
802
+ gui_image [:, :, 2 ] = image / max_value * 255
803
+
804
+ for idx in idxs :
805
+ # convert from linear idx to x_pix and y_pix indices.
806
+ y = int (idx / width )
807
+ x = int (idx - y * width )
808
+ cv2 .line (gui_image , (x , y ), (x , y ), (0 , 200 , 255 ), 3 )
809
+
810
+ for idx in idxs_limit_points :
811
+ # convert from linear idx to x_pix and y_pix indices.
812
+ y = int (idx / width )
813
+ x = int (idx - y * width )
814
+ cv2 .line (gui_image , (x , y ), (x , y ), (255 , 0 , 200 ), 3 )
815
+
816
+ msg = CvBridge ().cv2_to_imgmsg (gui_image , "passthrough" )
817
+
818
+ msg .header .frame_id = 'c' + selected_collection_key + '_' + sensor ['parent' ]
819
+ graphics ['collections' ][sensor_key ]['publisher' ].publish (msg )
778
820
821
+ # Publish camera info message
822
+ camera_info_msg = message_converter .convert_dictionary_to_ros_message ('sensor_msgs/CameraInfo' ,
823
+ sensor ['camera_info' ])
824
+ camera_info_msg .header .frame_id = msg .header .frame_id
825
+ graphics ['collections' ][sensor_key ]['publisher_camera_info' ].publish (
826
+ camera_info_msg )
779
827
780
828
# elif sensor['msg_type'] == 'LaserScan':
781
- elif sensor ['modality' ] == 'lidar2d' :
782
- pass
829
+ elif sensor ['modality' ] == 'lidar2d' :
830
+ pass
783
831
# elif sensor['msg_type'] == 'PointCloud2':
784
- elif sensor ['modality' ] == 'lidar3d' :
785
- pass
786
- else :
787
- pass
832
+ elif sensor ['modality' ] == 'lidar3d' :
833
+ pass
834
+ else :
835
+ pass
788
836
789
837
graphics ['ros' ]['Rate' ].sleep ()
0 commit comments