ivbelkin

Untitled

Apr 17th, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import rospy
  4. import json
  5.  
  6. from geometry_msgs.msg import Point
  7. from visualization_msgs.msg import Marker, MarkerArray
  8.  
  9.  
  10. class MapPublisher:
  11.  
  12.     def __init__(self):
  13.         self.file = rospy.get_param('~file')
  14.  
  15.         rospy.loginfo('Load map from %s', self.file)
  16.         with open(self.file, 'r') as f:
  17.             data = json.load(f)
  18.        
  19.         if 'ground_truth' in data:
  20.             color = 0, 1, 0
  21.             objects = data['ground_truth']['objects']
  22.         elif 'results' in data:
  23.             color = 1, 0, 0
  24.             objects = data['results']['objects']
  25.        
  26.         print('Found', len(objects), 'objects')
  27.        
  28.         marker_o = Marker()
  29.         marker_o.header.frame_id = 'map'
  30.         marker_o.header.stamp = rospy.Time.now()
  31.         marker_o.type = Marker.LINE_LIST
  32.         marker_o.action = Marker.ADD
  33.         marker_o.scale.x = 0.01
  34.         marker_o.scale.y = 0.01
  35.         marker_o.scale.z = 0.01
  36.         marker_o.color.a = 1
  37.         marker_o.color.r = color[0]
  38.         marker_o.color.g = color[1]
  39.         marker_o.color.b = color[2]
  40.         marker_o.lifetime = rospy.Duration(0)
  41.         marker_o.pose.orientation.w = 1.0
  42.  
  43.         for obj in objects:
  44.             cx, cy, cz = obj['centroid']
  45.             sx, sy, sz = obj['extent']
  46.            
  47.             # bottom points
  48.             bA = Point(cx - sx / 2, cy - sy / 2, cz - sz / 2)
  49.             bB = Point(cx - sx / 2, cy + sy / 2, cz - sz / 2)
  50.             bC = Point(cx + sx / 2, cy + sy / 2, cz - sz / 2)
  51.             bD = Point(cx + sx / 2, cy - sy / 2, cz - sz / 2)
  52.  
  53.             # top points
  54.             tA = Point(cx - sx / 2, cy - sy / 2, cz + sz / 2)
  55.             tB = Point(cx - sx / 2, cy + sy / 2, cz + sz / 2)
  56.             tC = Point(cx + sx / 2, cy + sy / 2, cz + sz / 2)
  57.             tD = Point(cx + sx / 2, cy - sy / 2, cz + sz / 2)
  58.  
  59.             # bottom rectangle
  60.  
  61.             marker_o.points.append(bA)
  62.             marker_o.points.append(bB)
  63.  
  64.             marker_o.points.append(bB)
  65.             marker_o.points.append(bC)
  66.  
  67.             marker_o.points.append(bC)
  68.             marker_o.points.append(bD)
  69.  
  70.             marker_o.points.append(bD)
  71.             marker_o.points.append(bA)
  72.  
  73.             # top rectangle
  74.  
  75.             marker_o.points.append(tA)
  76.             marker_o.points.append(tB)
  77.  
  78.             marker_o.points.append(tB)
  79.             marker_o.points.append(tC)
  80.  
  81.             marker_o.points.append(tC)
  82.             marker_o.points.append(tD)
  83.  
  84.             marker_o.points.append(tD)
  85.             marker_o.points.append(tA)
  86.  
  87.             # vertical edges
  88.  
  89.             marker_o.points.append(bA)
  90.             marker_o.points.append(tA)
  91.  
  92.             marker_o.points.append(bB)
  93.             marker_o.points.append(tB)
  94.  
  95.             marker_o.points.append(bC)
  96.             marker_o.points.append(tC)
  97.  
  98.             marker_o.points.append(bD)
  99.             marker_o.points.append(tD)
  100.  
  101.         self.pub = rospy.Publisher('/object_map', Marker, queue_size=10)
  102.  
  103.         r = rospy.Rate(10)
  104.         while not rospy.is_shutdown():
  105.             self.pub.publish(marker_o)
  106.             r.sleep()
  107.  
  108.     def run(self):
  109.         rospy.spin()
  110.  
  111.  
  112. def main():
  113.     rospy.init_node('map_publisher')
  114.     node = MapPublisher()
  115.     node.run()
  116.  
  117.  
  118. if __name__ == '__main__':
  119.     main()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment