|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import matplotlib |
|
import matplotlib.cm |
|
import numpy as np |
|
import torch |
|
|
|
def colorize(value, vmin=None, vmax=None, cmap='magma_r', invalid_val=-99, invalid_mask=None, background_color=(128, 128, 128, 255), gamma_corrected=False, value_transform=None): |
|
"""Converts a depth map to a color image. |
|
|
|
Args: |
|
value (torch.Tensor, numpy.ndarry): Input depth map. Shape: (H, W) or (1, H, W) or (1, 1, H, W). All singular dimensions are squeezed |
|
vmin (float, optional): vmin-valued entries are mapped to start color of cmap. If None, value.min() is used. Defaults to None. |
|
vmax (float, optional): vmax-valued entries are mapped to end color of cmap. If None, value.max() is used. Defaults to None. |
|
cmap (str, optional): matplotlib colormap to use. Defaults to 'magma_r'. |
|
invalid_val (int, optional): Specifies value of invalid pixels that should be colored as 'background_color'. Defaults to -99. |
|
invalid_mask (numpy.ndarray, optional): Boolean mask for invalid regions. Defaults to None. |
|
background_color (tuple[int], optional): 4-tuple RGB color to give to invalid pixels. Defaults to (128, 128, 128, 255). |
|
gamma_corrected (bool, optional): Apply gamma correction to colored image. Defaults to False. |
|
value_transform (Callable, optional): Apply transform function to valid pixels before coloring. Defaults to None. |
|
|
|
Returns: |
|
numpy.ndarray, dtype - uint8: Colored depth map. Shape: (H, W, 4) |
|
""" |
|
if isinstance(value, torch.Tensor): |
|
value = value.detach().cpu().numpy() |
|
|
|
value = value.squeeze() |
|
if invalid_mask is None: |
|
invalid_mask = value == invalid_val |
|
mask = np.logical_not(invalid_mask) |
|
|
|
|
|
vmin = np.percentile(value[mask],2) if vmin is None else vmin |
|
vmax = np.percentile(value[mask],85) if vmax is None else vmax |
|
if vmin != vmax: |
|
value = (value - vmin) / (vmax - vmin) |
|
else: |
|
|
|
value = value * 0. |
|
|
|
|
|
|
|
|
|
value[invalid_mask] = np.nan |
|
cmapper = matplotlib.cm.get_cmap(cmap) |
|
if value_transform: |
|
value = value_transform(value) |
|
|
|
value = cmapper(value, bytes=True) |
|
|
|
|
|
img = value[...] |
|
img[invalid_mask] = background_color |
|
|
|
|
|
if gamma_corrected: |
|
|
|
img = img / 255 |
|
img = np.power(img, 2.2) |
|
img = img * 255 |
|
img = img.astype(np.uint8) |
|
return img |
|
|
|
|
|
import os |
|
|
|
|
|
def find_most_recently_created_directory(temp_dir): |
|
"""Finds the most recently created directory in a directory. |
|
|
|
Args: |
|
temp_dir: The directory to search. |
|
|
|
Returns: |
|
The path to the most recently created directory. |
|
""" |
|
|
|
directories = os.listdir(temp_dir) |
|
most_recently_created_directory = None |
|
for directory in directories: |
|
path = os.path.join(temp_dir, directory) |
|
st = os.stat(path) |
|
if most_recently_created_directory is None or st.mtime > most_recently_created_directory.mtime: |
|
most_recently_created_directory = path |
|
|
|
if most_recently_created_directory is None: |
|
most_recently_created_directory = temp_dir |
|
|
|
return most_recently_created_directory |
|
|
|
|
|
|
|
def get_most_recent_subdirectory(path): |
|
if not os.path.isdir(path): |
|
return path |
|
|
|
subdirectories = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))] |
|
if not subdirectories: |
|
return path |
|
|
|
most_recent_subdirectory = max(subdirectories, key=lambda d: os.path.getctime(os.path.join(path, d))) |
|
return os.path.join(path, most_recent_subdirectory) |
|
|
|
|