|
| 1 | +import numpy as np |
| 2 | +from mpl_toolkits.mplot3d.art3d import Poly3DCollection |
| 3 | + |
| 4 | +from .tools import _set_axes_equal |
| 5 | + |
| 6 | + |
| 7 | +CLASS_TO_COLOR_MAP = { |
| 8 | + 'bottle': 'aqua', |
| 9 | + 'cup': 'sandybrown', |
| 10 | + 'knife': 'silver', |
| 11 | + 'bowl': 'rosybrown', |
| 12 | + 'wine glass': 'red', |
| 13 | + 'fork': 'moccasin', |
| 14 | + 'spoon': 'darkkhaki', |
| 15 | + 'banana': 'yellow', |
| 16 | + 'apple': 'green', |
| 17 | + 'orange': 'orange', |
| 18 | + 'cake': 'mistyrose', |
| 19 | + 'potted plant': 'lawngreen', |
| 20 | + 'mouse': 'gray', |
| 21 | + 'keyboard': 'black', |
| 22 | + 'laptop': 'firebrick', |
| 23 | + 'cell phone': 'darkgreen', |
| 24 | + 'book': 'saddlebrown', |
| 25 | + 'clock': 'seagreen', |
| 26 | + 'chair': 'lightseagreen', |
| 27 | + 'table': 'darkcyan', |
| 28 | + 'couch': 'deepskyblue', |
| 29 | + 'bed': 'royalblue', |
| 30 | + 'toilet': 'navy', |
| 31 | + 'tv': 'blue', |
| 32 | + 'microwave': 'mediumpurple', |
| 33 | + 'toaster': 'darkorchid', |
| 34 | + 'refrigerator': 'fuchsia', |
| 35 | + 'oven': 'deeppink', |
| 36 | + 'sink': 'crimson', |
| 37 | + 'person': 'lightpink' |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def get_bbox3d(extent, centroid): |
| 42 | + """ Calculate 3D bounding box corners from its parameterization. |
| 43 | + Input: |
| 44 | + extent: (length, width, height) |
| 45 | + centroid: (x, y, z) |
| 46 | + Output: |
| 47 | + corners3d: 3D box corners |
| 48 | + """ |
| 49 | + |
| 50 | + l, w, h = extent |
| 51 | + |
| 52 | + corners3d = 0.5 * np.array([ |
| 53 | + [-l, -l, l, l, -l, -l, l, l], |
| 54 | + [ w, -w, -w, w, w, -w, -w, w], |
| 55 | + [-h, -h, -h, -h, h, h, h, h], |
| 56 | + ]) |
| 57 | + |
| 58 | + corners3d[0, :] = corners3d[0, :] + centroid[0] |
| 59 | + corners3d[1, :] = corners3d[1, :] + centroid[1] |
| 60 | + corners3d[2, :] = corners3d[2, :] + centroid[2] |
| 61 | + |
| 62 | + return corners3d.T |
| 63 | + |
| 64 | + |
| 65 | +def vis_bbox3d(ax, bbox3d, facecolor, edgecolor, label): |
| 66 | + """ Visualise 3D bounding box. |
| 67 | + Input: |
| 68 | + ax: matplotlib axes 3D |
| 69 | + facecolor: bounding box facecolor |
| 70 | + edgecolor: bounding box edgecolor |
| 71 | + label: bounding box object class |
| 72 | + """ |
| 73 | + # Cuboid sides |
| 74 | + surf_verts = [ |
| 75 | + [bbox3d[0], bbox3d[1], bbox3d[2], bbox3d[3]], |
| 76 | + [bbox3d[4], bbox3d[5], bbox3d[6], bbox3d[7]], |
| 77 | + [bbox3d[0], bbox3d[1], bbox3d[5], bbox3d[4]], |
| 78 | + [bbox3d[2], bbox3d[3], bbox3d[7], bbox3d[6]], |
| 79 | + [bbox3d[1], bbox3d[2], bbox3d[6], bbox3d[5]], |
| 80 | + [bbox3d[4], bbox3d[7], bbox3d[3], bbox3d[0]] |
| 81 | + ] |
| 82 | + |
| 83 | + cuboid = Poly3DCollection( |
| 84 | + surf_verts, |
| 85 | + facecolors=facecolor, |
| 86 | + linewidths=1, |
| 87 | + edgecolors=edgecolor, |
| 88 | + alpha=.25, |
| 89 | + label=label |
| 90 | + ) |
| 91 | + # Fix: 'Poly3DCollection' object has no attribute '_edgecolors2d' |
| 92 | + cuboid._facecolors2d = cuboid._facecolor3d |
| 93 | + cuboid._edgecolors2d = cuboid._edgecolor3d |
| 94 | + |
| 95 | + ax.add_collection3d(cuboid) |
| 96 | + ax.scatter3D(bbox3d[:, 0], bbox3d[:, 1], bbox3d[:, 2], s=1, color=edgecolor) |
| 97 | + |
| 98 | + |
| 99 | +def vis_semantic_map3d(ax, scene_objects, class_to_color_map, edgecolor): |
| 100 | + for obj in scene_objects: |
| 101 | + label = obj['class'] |
| 102 | + facecolor = class_to_color_map[label] |
| 103 | + bbox3d = get_bbox3d(obj['extent'], obj['centroid']) |
| 104 | + |
| 105 | + vis_bbox3d(ax, bbox3d, facecolor, edgecolor, label) |
| 106 | + |
| 107 | + _set_axes_equal(ax) |
0 commit comments