| 50 | |
| 51 | |
| 52 | class CameraVisualizer: |
| 53 | |
| 54 | def __init__(self, poses, legends, colors, images=None, mesh_path=None, pc_path=None, camera_x=1.0): |
| 55 | self._fig = None |
| 56 | |
| 57 | self._camera_x = camera_x |
| 58 | |
| 59 | self._poses = poses |
| 60 | self._legends = legends |
| 61 | self._colors = colors |
| 62 | |
| 63 | self._raw_images = None |
| 64 | self._bit_images = None |
| 65 | self._image_colorscale = None |
| 66 | |
| 67 | if images is not None: |
| 68 | self._raw_images = images |
| 69 | self._bit_images = [] |
| 70 | self._image_colorscale = [] |
| 71 | for img in images: |
| 72 | if img is None: |
| 73 | self._bit_images.append(None) |
| 74 | self._image_colorscale.append(None) |
| 75 | continue |
| 76 | |
| 77 | bit_img, colorscale = self.encode_image(img) |
| 78 | self._bit_images.append(bit_img) |
| 79 | self._image_colorscale.append(colorscale) |
| 80 | |
| 81 | self._mesh = None |
| 82 | if mesh_path is not None and os.path.exists(mesh_path): |
| 83 | import trimesh |
| 84 | self._mesh = trimesh.load(mesh_path, force='mesh') |
| 85 | self._pc = None |
| 86 | if pc_path is not None and os.path.exists(pc_path): |
| 87 | self._pc = np.load(pc_path) |
| 88 | |
| 89 | |
| 90 | def encode_image(self, raw_image): |
| 91 | ''' |
| 92 | :param raw_image (H, W, 3) array of uint8 in [0, 255]. |
| 93 | ''' |
| 94 | # https://stackoverflow.com/questions/60685749/python-plotly-how-to-add-an-image-to-a-3d-scatter-plot |
| 95 | |
| 96 | dum_img = Image.fromarray(np.ones((3, 3, 3), dtype='uint8')).convert('P', palette='WEB') |
| 97 | idx_to_color = np.array(dum_img.getpalette()).reshape((-1, 3)) |
| 98 | |
| 99 | bit_image = Image.fromarray(raw_image).convert('P', palette='WEB', dither=None) |
| 100 | # bit_image = Image.fromarray(raw_image.clip(0, 254)).convert( |
| 101 | # 'P', palette='WEB', dither=None) |
| 102 | colorscale = [ |
| 103 | [i / 255.0, 'rgb({}, {}, {})'.format(*rgb)] for i, rgb in enumerate(idx_to_color)] |
| 104 | |
| 105 | return bit_image, colorscale |
| 106 | |
| 107 | |
| 108 | def update_figure( |
| 109 | self, |
no outgoing calls
no test coverage detected