I use the code below to visualize the pointcloud data and predicted labels using open3d. I would like to display the predicted label and its confidence score as a text (labels[i]+str(scores[i])) on the top of each bounding boxes. I need to control the position (x,y,z), font size, font style, font color of the printed text. How can I modify this code in this regard?
I appreciate any help in advance.
Thanks,
Abbas
import open3d
import torch
import matplotlib
import numpy as np
import time
from . import common_utils, box_utils
box_colormap = {
'Car': (0, 1, 0),
} # RGB
k=0
def draw_scenes(points, gt_boxes=None, ref_boxes=None, ref_labels=None, ref_scores=None,
point_colors=None, point_size=1.0, window_name='Open3D'):
global k
if isinstance(points, torch.Tensor):
points = points.cpu().numpy()
if isinstance(gt_boxes, torch.Tensor):
gt_boxes = gt_boxes.cpu().numpy()
if isinstance(ref_boxes, torch.Tensor):
ref_boxes = ref_boxes.cpu().numpy()
if isinstance(point_colors, torch.Tensor):
point_colors = point_colors.cpu().numpy()
if isinstance(ref_scores, torch.Tensor):
ref_scores = ref_scores.cpu().numpy()
vis = open3d.visualization.Visualizer()
# vis.create_window(window_name=window_name, width=1280, height=720)
vis.create_window(window_name=window_name, width=1280, height=720, visible = False)
vis.get_render_option().point_size = point_size
vis.get_render_option().background_color = np.asarray([0.4, 0.4, 0.4])
pts = open3d.geometry.PointCloud()
pts.points = open3d.utility.Vector3dVector(points[:, :3])
vis.add_geometry(pts)
if point_colors is not None:
pts.colors = open3d.utility.Vector3dVector(point_colors)
else:
pts.colors = open3d.utility.Vector3dVector(np.ones((points.shape[0], 3)) * 0.9)
if gt_boxes is not None:
vis = draw_box(vis, gt_boxes, color=(1, 0, 0))
if ref_boxes is not None:
if ref_labels is not None:
vis = draw_box(vis, ref_boxes, ref_labels, ref_scores)
else:
vis = draw_box(vis, ref_boxes, color=(0, 1, 0), ref_scores)
# save the images of pointcloud with predicted bounding boxes
vis.update_geometry(pts)
vis.poll_events()
vis.update_renderer()
# time.sleep(0.5)
im_path = "/home/user/Predicted_images/{:06d}.png".format(k)
vis.capture_screen_image(im_path)
vis.destroy_window()
k=k+1
def draw_box(vis, boxes, labels=None, scores=None, color=(1, 0, 0)):
"""
7 -------- 4
/| /|
6 -------- 5 .
| | | |
. 3 -------- 0
|/ |/
2 -------- 1
Args:
boxes: [x, y, z, dx, dy, dz, heading], (x, y, z) is the box center
labels: [name]
Returns:
"""
for i in range(boxes.shape[0]):
corners3d = box_utils.boxes_to_corners_3d(np.array([boxes[i]]))[0]
edges = np.array([
[0, 1], [1, 2], [2, 3], [3, 0],
[4, 5], [5, 6], [6, 7], [7, 4],
[0, 4], [1, 5], [2, 6], [3, 7],
[0, 5], [1, 4], # heading
])
line_set = open3d.geometry.LineSet()
line_set.points = open3d.utility.Vector3dVector(corners3d)
line_set.lines = open3d.utility.Vector2iVector(edges)
if labels is not None:
line_set.paint_uniform_color(box_colormap[labels[i]])
else:
line_set.paint_uniform_color(color)
vis.add_geometry(line_set)
return vis