# visualizations

Python API: `luxonis_ml.data.utils.visualizations`

## 类

### ColorMap

将不同的 RGB 颜色分配给可哈希标签的映射。

[ColorMap](/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/utils/visualizations.md)
为任意可哈希标签生成并存储不同的颜色。颜色在请求时使用
[distinct_color_generator](/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/data/utils/visualizations.md)
延迟分配。

#### 方法

##### init

```python
def __init__(self):
```

## 函数

### add_augmentation_footer

```python
def add_augmentation_footer(image: np.ndarray, augmentations: list[str]) -> np.ndarray:
```

将应用的增强作为页脚附加到图像下方。

### append_text_block

```python
def append_text_block(image: np.ndarray, lines: list[str], font_scale: float, bg_color: Color = (245, 245, 245), text_color: Color = (32, 32, 32)) -> np.ndarray:
```

在图像下方附加一个多行文本块。

### concat_images

```python
def concat_images(image_dict: dict[str, np.ndarray], padding: int = 10, label_height: int = 30) -> np.ndarray:
```

将图像连接成一个带标签的图像。

它将尝试创建一个正方形图像网格。

参数

 * `image_dict` (`dict[str, np.ndarray]`): 图像名称到图像的映射。
 * `padding` (`int`): 图像之间的填充。
 * `label_height` (`int`): 每个标签的高度。

返回

 * `np.ndarray`: 连接后的图像。

### create_text_image

```python
def create_text_image(text: str, width: int, height: int, font_size: float = 0.7, bg_color: Color = 255, text_color: Color = 0) -> np.ndarray:
```

创建带有居中文本的图像。

参数

 * `text` (`str`): 要显示的文本。
 * `width` (`int`): 图像宽度。
 * `height` (`int`): 图像高度。
 * `font_size` (`float`): 文本的字体大小。
 * `bg_color` (`Color`): 背景颜色。
 * `text_color` (`Color`): 文本颜色。

返回

 * `np.ndarray`: 生成的图像。

### distinct_color_generator

```python
def distinct_color_generator(stop: int = -1) -> Generator[RGB, None, None]:
```

使用黄金比例生成不同的 RGB 颜色。

此生成器以 RGB 格式生成一系列不同的颜色。通过以黄金比例递增色相并保持饱和度和值不变来生成颜色。这确保了视觉上不同的颜色广泛分布。

> **示例**
> ```pycon
>>> colors = list(distinct_color_generator(stop=2))
>>> len(colors)
2
>>> all(len(color) == 3 for color in colors)
True
```

参数

 * `stop` (`int`): 可选的最大生成颜色数。如果设置为 -1，则生成器无限期地继续。

返回

 * `Generator[RGB, None, None]`

生成

 * 下一个 RGB 颜色，每个分量在 [0, 255] 范围内。

### draw_bbox_label

```python
def draw_bbox_label(image: np.ndarray, class_name: str, box: np.ndarray, color: tuple[int, int, int], font_scale: float):
```

在边界框的左上角绘制类名标签。

参数

 * `image` (`np.ndarray`): 要绘制的图像。
 * `class_name` (`str`): 类的名称。
 * `box` (`np.ndarray`): 边界框坐标，格式为 `[class_id, x1, y1, x2, y2]`，其中 `(x1, y1)` 是左上角，`(x2, y2)` 是右下角。
 * `color` (`tuple[int, int, int]`): 标签颜色。
 * `font_scale` (`float`): 字体比例。

### draw_cross

```python
def draw_cross(img: np.ndarray, center: tuple[int, int], size: int = 5, color: Color = 0, thickness: int = 1):
```

在图像上绘制一个十字。

参数

 * `img` (`np.ndarray`): 要绘制的图像。
 * `center` (`tuple[int, int]`): 十字的中心。
 * `size` (`int`): 十字的大小。
 * `color` (`Color`): 十字颜色。
 * `thickness` (`int`): 线条粗细。

### draw_dashed_rectangle

```python
def draw_dashed_rectangle(image: np.ndarray, pt1: tuple[int, int], pt2: tuple[int, int], color: Color, thickness: int = 1,
dash_length: int = 10):
```

在图像上绘制虚线矩形。

参数

 * `image` (`np.ndarray`): 要绘制的图像。
 * `pt1` (`tuple[int, int]`): 矩形的左上角。
 * `pt2` (`tuple[int, int]`): 矩形的右下角。
 * `color` (`Color`): 矩形颜色。
 * `thickness` (`int`): 线条粗细。
 * `dash_length` (`int`): 每段虚线的长度。

### draw_keypoint_label

```python
def draw_keypoint_label(image: np.ndarray, text: str, point: tuple[int, int], size: int, color: tuple[int, int, int], font_scale:
float):
```

在图像上关键点旁边绘制文本标签。

参数

 * `image` (`np.ndarray`): 要绘制的图像。
 * `text` (`str`): 要绘制的文本。
 * `point` (`tuple[int, int]`): 关键点坐标。
 * `size` (`int`): 关键点大小。
 * `color` (`tuple[int, int, int]`): 文本颜色。
 * `font_scale` (`float`): 字体比例。

### get_contrast_color

```python
def get_contrast_color(color: Color) -> RGB:
```

返回给定 RGB 颜色的对比色。

> **示例**
> ```pycon
>>> get_contrast_color((255, 0, 0))
(0, 255, 255)
>>> get_contrast_color(0)
(0, 0, 0)
```

参数

 * `color` (`Color`): 要对比的颜色。

返回

 * `RGB`: 对比色。

### hsv_to_rgb

```python
def hsv_to_rgb(color: HSV) -> RGB:
```

将 HSV 颜色转换为 RGB。

> **示例**
> ```pycon
>>> hsv_to_rgb((0, 1, 1))
(255, 0, 0)
>>> hsv_to_rgb((120, 1, 1))
(0, 255, 0)
```

参数

 * `color` (`HSV`): 要转换的 HSV 颜色。

返回

 * `RGB`: 转换后的 RGB 颜色。

### resolve_color

```python
def resolve_color(color: Color) -> RGB:
```

将颜色解析为 RGB。

> **示例**
> ```pycon
>>> resolve_color(12)
(12, 12, 12)
>>> resolve_color((1, 2, 3))
(1, 2, 3)
>>> resolve_color(300)
Traceback (most recent call last):
...
ValueError: Color value 300 is out of range [0, 255]
```

参数

 * `color` (`Color`): 要解析的颜色。

返回

 * `RGB`: 解析后的 RGB 颜色。

引发

 * `ValueError`: 如果整数通道值超出 [0, 255] 范围。

### rgb_to_hsv

```python
def rgb_to_hsv(color: Color) -> HSV:
```

将 RGB 颜色转换为 HSV。

> **示例**
> ```pycon
>>> tuple(round(v, 3) for v in rgb_to_hsv((255, 0, 0)))
(0.0, 1.0, 1.0)
>>> tuple(round(v, 3) for v in rgb_to_hsv(128))
(0.0, 0.0, 0.502)
```

参数

 * `color` (`Color`): 要转换的颜色。

返回

 * `HSV`: 转换后的 HSV 颜色。

### str_to_rgb

```python
def str_to_rgb(string: str) -> RGB:
```

将字符串转换为其确定性 RGB 颜色。

> **示例**
> ```pycon
>>> str_to_rgb("car") == str_to_rgb("car")
True
>>> len(str_to_rgb("car"))
3
```

参数

 * `string` (`str`): 要转换的字符串。

返回

 * `RGB`: 确定性 RGB 颜色。

### visualize

```python
def visualize(image: np.ndarray, source_name: str, labels: Labels, classes: dict[str, dict[str, int]], blend_all: bool = False, categorical_encodings: dict[str, dict[str, int]] | None = None, *, skeletons: dict[str, tuple[list[str], list[tuple[int, int]]]] | None = None, draw_skeletons: bool = False, keypoint_label_mode: Literal['none', 'numbers', 'names', 'full'] = 'numbers') -> np.ndarray:
```

在图像上可视化标签。

参数

 * `image` (`np.ndarray`): 要可视化的图像。
 * `source_name` (`str`): 图像源的名称。
 * `labels` (`Labels`): 要可视化的标签。
 * `classes` (`dict[str, dict[str, int]]`): 从任务名称到类 ID 映射的映射。
 * `blend_all` (`bool`): 是否将所有标签混合到单个图像中。这会混合属于不同任务的标签。
 * `categorical_encodings` (`dict[str, dict[str, int]] | None`): 分类元数据任务的可选映射。键是完整的任务标识符，例如
   `"task_name/metadata/key"`，值将字符串标签映射到编码整数。
 * `skeletons` (`dict[str, tuple[list[str], list[tuple[int, int]]]] | None`): 按任务名称键入的可选关键点骨架元数据。
 * `draw_skeletons` (`bool`): 是否绘制关键点骨架边缘。
 * `keypoint_label_mode` (`Literal['none', 'numbers', 'names', 'full']`): 关键点标签模式。`"none"` 隐藏关键点标签，`"numbers"` 绘制数字索引，`"full"`
   在可用时绘制骨架关键点名称。

返回

 * `np.ndarray`: 可视化图像。

### wrap_text

```python
def wrap_text(text: str, max_width: int, font_scale: float, thickness: int = 1) -> list[str]:
```

将文本包装成适合给定宽度的行。
