draw_bounding_boxes¶
- torchvision.utils.draw_bounding_boxes(image: Tensor, boxes: Tensor, labels: Optional[list[str]] = None, colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill: Optional[bool] = False, width: int = 1, font: Optional[str] = None, font_size: Optional[int] = None, label_colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, label_background_colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill_labels: bool = False) Tensor[原始碼]¶
在給定的 RGB 影像上繪製邊界框。影像值應為 uint8(範圍 [0, 255])或 float(範圍 [0, 1])。如果 fill 為 True,則最終張量應儲存為 PNG 影像。
- 引數:
image (Tensor) – 形狀為 (C, H, W) 且 dtype 為 uint8 或 float 的張量。
boxes (Tensor) – 形狀為 (N, 4) 或 (N, 8) 的張量,包含邊界框。對於 (N, 4),格式為 (xmin, ymin, xmax, ymax),邊界框是相對於影像的絕對座標。換句話說:0 <= xmin < xmax < W 且 0 <= ymin < ymax < H。對於 (N, 8),格式為 (x1, y1, x2, y2, x3, y3, x4, y4),邊界框是相對於底層物件的絕對座標,因此無需驗證後兩個不等式。
labels (List[str]) – 包含邊界框標籤的列表。
colors (顏色或顏色列表, 可選) – 包含邊界框顏色的列表,或所有邊界框的單一顏色。顏色可以表示為 PIL 字串,例如“red”或“#FF00FF”,或表示為 RGB 元組,例如
(240, 10, 157)。預設情況下,會為邊界框生成隨機顏色。fill (bool) – 如果為 True,則用指定的顏色填充邊界框。
width (int) – 邊界框的寬度。
font (str) – 包含 TrueType 字型的檔名。如果在此檔名中找不到檔案,載入器也可能在其他目錄中搜索,例如 Windows 上的 fonts/ 目錄,或 macOS 上的 /Library/Fonts/、/System/Library/Fonts/ 和 ~/Library/Fonts/。
font_size (int) – 所需的字型大小(以磅為單位)。
label_colors (顏色或顏色列表, 可選) – 標籤文字的顏色。有關詳細資訊,請參閱 colors 引數的說明。預設情況下,使用與邊界框相同的顏色,如果
fill_labels為 True,則為黑色。label_background_colors (顏色或顏色列表, 可選) – 標籤文字框填充的顏色。預設情況下,使用與邊界框相同的顏色。當
fill_labels為 False 時忽略。fill_labels (bool) – 如果為 True,則用指定的顏色(來自
label_background_colors引數,或來自colors引數,如果未指定)填充標籤背景。預設值:False。
- 返回:
繪製了邊界框的影像張量,dtype 為 uint8。
- 返回型別:
img (Tensor[C, H, W])
使用
draw_bounding_boxes的示例