roverp.graphics
¶
GPU-accelerated 2D graphics using JAX.
Warning
This module is not automatically imported; you will need to explicitly import it:
You will also need to have thegraphics
extra installed.
roverp.graphics.Dilate
¶
Dilate an N-dimensional image.
Takes the maximum of a ND image with offset version of itself.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
radius
|
float
|
dilation radius; a neighborhood with size
|
3.1
|
dims
|
int
|
number of dimensions. |
2
|
Source code in processing/src/roverp/graphics/pointcloud.py
__call__
¶
Apply dilation to an image.
roverp.graphics.JaxFont
¶
GPU-accelerated vectorizable monospace text rendering.
Warning
@cached_property
doesn't seem to play well with jax, so JaxFont
pre-computes the font LUT. Don't intialize this class until needed!
Usage
- Initialize:
font = JaxFont(font_name, size)
. - Convert text to array(s):
arr = font.encode("Hello World!")
. - Render onto canvas:
canvas = font.render(arr, canvas, color, x, y)
. - Wrap any
render
calls into a JIT-compiled function to guarantee in-place editing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
font
|
str | None
|
Font file; must be monospace (or will be treated like one!). If
|
None
|
size
|
int
|
Font size; is static to allow pre-computing the font. |
18
|
Source code in processing/src/roverp/graphics/font.py
__call__
¶
__call__(
text: UInt8[Array, len],
canvas: UInt8[Array, "width height channels"],
color: UInt8[Array, channels],
x: int = 0,
y: int = 0,
) -> UInt8[Array, "width height channels"]
Render text on canvas.
Warning
x
and y
must be constant!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
UInt8[Array, len]
|
character bytes (ASCII-encoded) |
required |
canvas
|
UInt8[Array, 'width height channels']
|
array to write to. Must be a jax array. |
required |
color
|
UInt8[Array, channels]
|
color to apply, with the same number of channels as |
required |
x
|
int
|
vertical position to write text at; |
0
|
y
|
int
|
horizontal position to write text at, |
0
|
Returns:
Type | Description |
---|---|
UInt8[Array, 'width height channels']
|
Rendered canvas. If the original is no longer used (e.g. all
subsequent computation uses only the return here), |
Source code in processing/src/roverp/graphics/font.py
encode
¶
Convert a string or list of strings to an array of ASCII indices.
Warning
The inputs must all have the same length. This function is not jit-compilable.
Source code in processing/src/roverp/graphics/font.py
roverp.graphics.Render
¶
2D renderer to combine data channels in a fixed format.
Warning
If the strings being rendered every change length, this will trigger recompilation!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
tuple[int, int]
|
total frame size as |
required |
channels
|
dict[tuple[int, int, int, int], str]
|
which data channels to render. Specify as
|
required |
transforms
|
dict[str, Callable[[Shaped[Array, ...]], UInt8[Array, '?h ?w 3']]]
|
dict of jax jit-compatible transforms to apply to each data channel (organized by channel name); must output RGB images. |
required |
text
|
dict[tuple[int, int], str]
|
text to render; each key is a |
required |
font
|
JaxFont
|
rendering font. |
required |
textcolor
|
tuple[int, int, int]
|
text rendering configuration. |
(255, 255, 255)
|
Source code in processing/src/roverp/graphics/render.py
__call__
¶
__call__(
data: dict[str, Shaped[Array, ...]],
meta: dict[str, Any] | list[dict[str, Any]],
) -> UInt8[Array, "*batch h w 3"]
Render (possibly batched) frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Shaped[Array, ...]]
|
input data, organized into channels by name. Must have fixed dimensions. |
required |
meta
|
dict[str, Any] | list[dict[str, Any]]
|
metadata for text captions/labels. |
required |
Returns:
Type | Description |
---|---|
UInt8[Array, '*batch h w 3']
|
Rendered RGB frame (or batch of frames). |
Source code in processing/src/roverp/graphics/render.py
roverp.graphics.Scatter
¶
Render a scatter plot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
radius
|
float
|
point radius. |
3.1
|
resolution
|
tuple[int, int]
|
image resolution. |
(320, 640)
|
Source code in processing/src/roverp/graphics/pointcloud.py
__call__
¶
Render scatter plot image, with each point as a circle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Num[Array, N]
|
x-coordinate, with +x facing right. |
required |
y
|
Num[Array, N]
|
y-coordinate, with +y facing up. |
required |
c
|
Num[Array, N]
|
point intensity with arbitrary type. The intensity is sorted in increasing order to maximize the chances that the higher value is taken in case multiple points map to the same bin. |
required |
Returns:
Type | Description |
---|---|
Num[Array, 'height width']
|
Rendered scatter plot. Note that there is some indeterminism in case multiple points with the same intensity fall in the same initial pixel. |
Source code in processing/src/roverp/graphics/pointcloud.py
roverp.graphics.hsv_to_rgb
¶
Convert hsv values to rgb.
Copied from matplotlib, modified for vectorization, and converted to jax.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hsv
|
Float[Array, '... 3']
|
HSV colors. |
required |
Returns:
Type | Description |
---|---|
Float[Array, '... 3']
|
RGB colors |
Source code in processing/src/roverp/graphics/colors.py
roverp.graphics.lut
¶
Apply a discrete lookup table (e.g. colormap).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colors
|
Num[Array, 'n d']
|
list of discrete colors to apply (e.g. 0-255 RGB values). Can be an arbitrary number of channels, not just RGB. |
required |
data
|
Float[Array, ...]
|
input data to index ( |
required |
Returns:
Type | Description |
---|---|
Num[Array, '... d']
|
An array with the same shape as |
Source code in processing/src/roverp/graphics/colors.py
roverp.graphics.mpl_colormap
¶
mpl_colormap(cmap: str = 'viridis') -> UInt8[Array, 'n 3']
Get color LUT from matplotlib colormap.
Use with lut
.
Source code in processing/src/roverp/graphics/colors.py
roverp.graphics.render_image
¶
render_image(
data: Num[Array, "h w"],
colors: Num[Array, "n d"] | None = None,
resize: tuple[int, int] | None = None,
scale: float | int | None = None,
pmin: float | None = None,
pmax: float | None = None,
) -> UInt8[Array, "h2 w2 d"]
Apply colormap with specified scaling, clipping, and sizing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colors
|
Num[Array, 'n d'] | None
|
colormap (e.g. output of |
None
|
data
|
Num[Array, 'h w']
|
input data to map. |
required |
resize
|
tuple[int, int] | None
|
resize inputs to specified size. |
None
|
scale
|
float | int | None
|
if specified, use this exact scale to normalize the data to
|
None
|
pmin
|
float | None
|
if specified, use this percentile as the minimum for normalization instead of the actual min. |
None
|
pmax
|
float | None
|
if specified, use this percentile as the maximum for normalization instead of the actual max. |
None
|
Returns:
Type | Description |
---|---|
UInt8[Array, 'h2 w2 d']
|
Rendered RGB image. |
Source code in processing/src/roverp/graphics/colors.py
roverp.graphics.synchronize
¶
synchronize(
streams: dict[str, Iterator[Any]],
timestamps: dict[str, Float[ndarray, "?T"]],
period: float = 1 / 30.0,
round: float | None = None,
duplicate: dict[str, str] = {},
batch: int = 0,
stop_at: float = 0.0,
) -> Iterator[
tuple[float, dict[str, int], Any] | list[tuple[float, dict[str, int], Any]]
]
Sychronize asynchronous video/data streams.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
streams
|
dict[str, Iterator[Any]]
|
input iterator streams to synchronize. |
required |
timestamps
|
dict[str, Float[ndarray, '?T']]
|
timestamp arrays for each stream. |
required |
period
|
float
|
query period, in seconds. |
1 / 30.0
|
round
|
float | None
|
if specified, round the start time up to the nearest |
None
|
duplicate
|
dict[str, str]
|
duplicate selected streams (values) into the specified keys. |
{}
|
batch
|
int
|
batch size for parallelized pipelines. If |
0
|
stop_at
|
float
|
terminate early after this many seconds. If |
0.0
|
Yields:
Type | Description |
---|---|
tuple[float, dict[str, int], Any] | list[tuple[float, dict[str, int], Any]]
|
A tuple with the current timestamp relative to the start time, the index of each synchronized frame, and references to the active values at that timestamp. The active values are given by reference only, and should not be modified. |
Source code in processing/src/roverp/graphics/sync.py
roverp.graphics.write_buffered
¶
write_buffered(
queue: Queue[UInt8[ndarray | Array, "*batch H W 3"] | None],
out: str,
fps: float = 30.0,
codec: str = "h264",
) -> None
Write video from a queue of optionally batched images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
queue
|
Queue[UInt8[ndarray | Array, '*batch H W 3'] | None]
|
input queue; |
required |
out
|
str
|
output file path. |
required |
fps
|
float
|
frames per second. |
30.0
|
codec
|
str
|
video codec to use; see supported codecs. |
'h264'
|
Source code in processing/src/roverp/graphics/writer.py
roverp.graphics.write_consume
¶
write_consume(
iter: Iterator[UInt8[ndarray | Array, "*batch H W 3"]],
out: str,
fps: float = 30.0,
codec: str = "h264",
queue_size: int = 32,
) -> None
Write video from an iterator of optionally batched images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iter
|
Iterator[UInt8[ndarray | Array, '*batch H W 3']]
|
input iterator. |
required |
out
|
str
|
output file path. |
required |
fps
|
float
|
frames per second. |
30.0
|
codec
|
str
|
video codec to use; see supported codecs. |
'h264'
|
queue_size
|
int
|
maximum size of the internal queue to use for buffering. |
32
|