MCPcopy Create free account
hub / github.com/SpatialVLA/SpatialVLA / decode_and_resize

Function decode_and_resize

data/obs_transforms.py:47–113  ·  view source on GitHub ↗

Decodes images and depth images, and then optionally resizes them.

(
    obs: Dict,
    resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]],
    depth_resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]],
)

Source from the content-addressed store, hash-verified

45
46
47def decode_and_resize(
48 obs: Dict,
49 resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]],
50 depth_resize_size: Union[Tuple[int, int], Dict[str, Tuple[int, int]]],
51) -> Dict:
52 """Decodes images and depth images, and then optionally resizes them."""
53 image_names = {key[6:] for key in obs if key.startswith("image_")}
54 depth_names = {key[6:] for key in obs if key.startswith("depth_")}
55
56 if isinstance(resize_size, tuple):
57 resize_size = {name: resize_size for name in image_names}
58 if isinstance(depth_resize_size, tuple):
59 depth_resize_size = {name: depth_resize_size for name in depth_names}
60
61 for name in image_names:
62 if name not in resize_size:
63 logging.warning(
64 f"No resize_size was provided for image_{name}. This will result in 1x1 "
65 "padding images, which may cause errors if you mix padding and non-padding images."
66 )
67 image = obs[f"image_{name}"]
68 if image.dtype == tf.string:
69 if tf.strings.length(image) == 0:
70 # this is a padding image
71 image = tf.zeros((*resize_size.get(name, (1, 1)), 3), dtype=tf.uint8)
72 else:
73 image = tf.io.decode_image(
74 image, expand_animations=False, dtype=tf.uint8
75 )
76 elif image.dtype != tf.uint8:
77 raise ValueError(
78 f"Unsupported image dtype: found image_{name} with dtype {image.dtype}"
79 )
80 if name in resize_size:
81 image = dl.transforms.resize_image(image, size=resize_size[name])
82 obs[f"image_{name}"] = image
83
84 for name in depth_names:
85 if name not in depth_resize_size:
86 logging.warning(
87 f"No depth_resize_size was provided for depth_{name}. This will result in 1x1 "
88 "padding depth images, which may cause errors if you mix padding and non-padding images."
89 )
90 depth = obs[f"depth_{name}"]
91
92 if depth.dtype == tf.string:
93 if tf.strings.length(depth) == 0:
94 depth = tf.zeros(
95 (*depth_resize_size.get(name, (1, 1)), 1), dtype=tf.float32
96 )
97 else:
98 depth = tf.io.decode_image(
99 depth, expand_animations=False, dtype=tf.float32
100 )[..., 0]
101 elif depth.dtype != tf.float32:
102 raise ValueError(
103 f"Unsupported depth dtype: found depth_{name} with dtype {depth.dtype}"
104 )

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected