File size: 7,614 Bytes
25a49bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors: spopov@google.com (Stefan Popov), kmaninis@google.com (Kevis-Kokitsi Maninis)
"""Display utils."""

import base64
import io
import itertools
import logging
import typing

import matplotlib
from matplotlib import cm
import numpy as np
import PIL.Image
import trimesh
import torch as t
from IPython.core import display
from gl import scene_renderer

import data_util


log = logging.getLogger(__name__)


def to_hwc_rgb8(imgarr: typing.Any) -> np.ndarray:
  if t.is_tensor(imgarr):  # Torch -> Numpy
    imgarr = imgarr.detach().cpu().numpy()
  if hasattr(imgarr, "numpy"):  # TF -> Numpy
    imgarr = imgarr.numpy()
  if len(imgarr.shape) == 2:  # Monochrome -> RGB
    imgarr = np.stack([imgarr] * 3, -1)
  if (len(imgarr.shape) == 3 and imgarr.shape[0] <= 4
      and (imgarr.shape[1] > 4 or imgarr.shape[2] > 4)):  # CHW -> HWC
    imgarr = np.transpose(imgarr, [1, 2, 0])
  if len(imgarr.shape) == 3 and imgarr.shape[-1] == 4:  # RGBA -> RGB
    imgarr = imgarr[:, :, :3]
  if len(imgarr.shape) == 3 and imgarr.shape[-1] == 1:  # Monochrome -> RGB
    imgarr = np.concatenate([imgarr] * 3, -1)
  if imgarr.dtype == np.float32 or imgarr.dtype == np.float64:
    imgarr = np.minimum(np.maximum(imgarr * 255, 0), 255).astype(np.uint8)
  if imgarr.dtype == np.int32 or imgarr.dtype == np.int64:
    imgarr = np.minimum(np.maximum(imgarr, 0), 255).astype(np.uint8)
  if imgarr.dtype == bool:
    imgarr = imgarr.astype(np.uint8) * 255

  if (len(imgarr.shape) != 3 or imgarr.shape[-1] != 3
      or imgarr.dtype != np.uint8):
    raise ValueError(
        "Cannot display image from array with type={} and shape={}".format(
            imgarr.dtype, imgarr.shape))

  return imgarr[..., :3]


def image_as_url(imgarr: np.ndarray, fmt: str = "png") -> str:
  img = PIL.Image.fromarray(imgarr, "RGB")
  buf = io.BytesIO()
  img.save(buf, fmt)
  b64 = base64.encodebytes(buf.getvalue()).decode("utf8")
  b64 = "data:image/png;base64,{}".format(b64)
  return b64


class Image(typing.NamedTuple):
  image: typing.Any
  label: str
  dim_name: str
  dim_num: int


def get_html_for_images(*orig_images, fmt="png", dim_name="width"):
  table_template = """
    <div style="display: inline-flex; flex-direction: row; flex-wrap:wrap">
      {}
    </div>
  """
  item_template = """
    <div style="display: inline-flex; flex-direction: column; flex-wrap:
         nowrap; align-items: center">
      <img style="margin-right: 0.5em" src="{image}" {dim_name}="{dim_num}"/>
      <div style="margin-bottom: 0.5em; margin-right: 0.5em">{label}</div>
    </div>
  """
  images = []

  def append_image(image):
    image = to_hwc_rgb8(image)
    dim_number = image.shape[0] if dim_name == "height" else image.shape[1]
    images.append(
        Image(label="Image {}".format(idx), image=image,
              dim_name=dim_name, dim_num=dim_number))

  for idx, item in enumerate(orig_images):
    if isinstance(item, str) and images:
      images[-1] = images[-1]._replace(label=item)
    elif isinstance(item, bytes):
      image = np.array(PIL.Image.open(io.BytesIO(item)))
      append_image(image)
    elif isinstance(item, PIL.Image.Image):
      append_image(np.array(item))
    elif isinstance(item, int) and images:
      if dim_name == "width":
        images[-1] = images[-1]._replace(dim_name="width", dim_num=item)
      elif dim_name == "height":
        images[-1] = images[-1]._replace(dim_name="height", dim_num=item)
      else:
        raise ValueError("Dimensions (dim_name) not in {width, height}.")
    else:
      append_image(item)

  images = [v._replace(image=image_as_url(v.image, fmt)) for v in images]
  table = [item_template.format(**v._asdict()) for v in images]
  table = table_template.format("".join(table))
  return table


def display_images(*orig_images, dim_name="width", **kwargs):
  """Display images in a IPython environment"""
  display.display(
      display.HTML(
          get_html_for_images(
              *orig_images, dim_name=dim_name, **kwargs)))


def display_multiple_images(
    images, dim_num: int, title=None, dim_name="height"):
  """Display multiple images using the same display width or height."""
  to_display = [[images[ii], dim_num] for ii in range(len(images))]
  if title is not None:
    [x.append(title) for x in to_display]
  to_display = list(itertools.chain.from_iterable(to_display))
  display_images(*to_display, dim_name=dim_name)


def prepare_mesh_rendering_info(
    scene: trimesh.Scene, with_texture: bool = True):
  """Prepares trimesh for rendering (vertices, colors, material ids)."""
  if isinstance(scene, trimesh.Trimesh):
    mesh = scene
  elif isinstance(scene, trimesh.Scene):
    mesh = list(scene.geometry.values())[0]
  else:
    raise TypeError(f'Type {type(scene)} not supported.')

  triangles = data_util.convert_to_triangles(
      np.array(mesh.vertices), np.array(mesh.faces))
  triangle_colors = t.tensor([[0.8] * 3])
  material_ids = t.tensor([0] * len(triangles), dtype=t.int32)
  if with_texture and hasattr(mesh.visual, 'to_color'):
    visuals = mesh.visual.to_color()
    vertex_colors = t.tensor(
        visuals.vertex_colors[:, :3], dtype=t.float32) / 255.
    triangle_colors = data_util.convert_to_triangles(
        np.array(vertex_colors), np.array(mesh.faces))
    triangle_colors = t.tensor(triangle_colors).mean(axis=1)
    material_ids = t.arange(triangle_colors.shape[0], dtype=t.int32)
  return t.tensor(triangles), triangle_colors, material_ids


def render_navi_scan(scene: trimesh.Scene, extrinsics: np.ndarray,
    intrinsics: np.ndarray, image_size: typing.Tuple[int, int],
    with_texture: bool = True) -> np.ndarray:
  """Renders a NAVI scan."""
  triangles, triangle_colors, material_ids = prepare_mesh_rendering_info(
      scene, with_texture=with_texture)
  return scene_renderer.render_scene(
      triangles,
      view_projection_matrix=intrinsics @ extrinsics,
      image_size=image_size,
      cull_back_facing=False,
      diffuse_coefficients=triangle_colors,
      material_ids=material_ids).numpy()


def overlay_images(image_1: np.ndarray, image_2: np.ndarray,
    opacity: float = 0.8, white_bg: bool = False) -> np.ndarray:
  """Overlay two images."""
  image_1 = np.array(image_1)
  image_2 = np.array(image_2)
  result = image_1.copy()
  if white_bg:
    mask = np.min(image_2, axis=2) < 1
  else:
    mask = np.max(image_2, axis=2) > 0

  result[mask, :] = (
      opacity * image_2[mask, :] + (1 - opacity) * image_1[mask, :])
  return result

def apply_colors_to_depth_map(
    depth: np.ndarray, minn: typing.Optional[int] = None,
    maxx: typing.Optional[int] = None) -> np.ndarray:
  """Converts a depth map to an RGB image."""
  mask = (depth != 0.)
  if minn is None:
    minn = depth[mask].min()
  if maxx is None:
    maxx = depth[mask].max()
  norm = matplotlib.colors.Normalize(vmin=minn, vmax=maxx)
  mapper = cm.ScalarMappable(norm=norm, cmap='plasma')
  depth_colored = (mapper.to_rgba(depth)[:, :, :3] * 255).astype(np.uint8)
  depth_colored[~mask, :] = 0.
  return depth_colored