此页面由 AI 自动翻译。查看英文原版
最后更新

本页目录

  • 函数
  • adjust_bboxes_to_image_border
  • bbox_iou
  • box_prompt
  • build_mask_coeffs
  • compute_fastsam_mask
  • decode_fastsam_output
  • format_results
  • merge_masks
  • point_prompt
  • process_masks
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包
最后更新
Python API: depthai_nodes.node.parsers.utils.fastsam

函数

adjust_bboxes_to_image_border

Python
1def adjust_bboxes_to_image_border(boxes: np.ndarray, image_shape: tuple[int, int], threshold: int = 20) -> np.ndarray:
来源:https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L6 (Ultralytics) 如果边界框在某个阈值内,则将其调整为贴合图像边界。参数
  • boxes (np.ndarray): 边界框
  • image_shape (tuple[int, int]): 图像形状
  • threshold (int): 像素阈值
返回
  • np.ndarray: 调整后的边界框

bbox_iou

Python
1def bbox_iou(box1: np.ndarray, boxes: np.ndarray, iou_thres: float = 0.9, image_shape: tuple[int, int] = (640, 640), raw_output: bool = False) -> np.ndarray:
来源:https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/utils.py#L30 (Ultralytics - 重写为 numpy) 计算边界框与一组其他边界框的交并比。参数
  • box1 (np.ndarray): 表示单个边界框的形状为 (4, ) 的数组。
  • boxes (np.ndarray): 表示多个边界框的形状为 (n, 4) 的数组。
  • iou_thres (float): IoU 阈值
  • image_shape (tuple[int, int]): 图像形状(高,宽)
  • raw_output (bool): 如果为 True,则返回原始 IoU 值而不是索引
返回
  • np.ndarray: IoU > 阈值的框的索引,如果 raw_output 为 True,则为原始 IoU 值

box_prompt

Python
1def box_prompt(masks: np.ndarray, bbox: tuple[int, int, int, int], orig_shape: tuple[int, int]) -> np.ndarray:
修改边界框属性并计算掩码与边界框之间的 IoU。来源:https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L284 已修改为使用 numpy 而不是 torch。参数
  • masks (np.ndarray): FastSAM 模型的生成掩码
  • bbox (tuple[int, int, int, int]): 提示边界框坐标
  • orig_shape (tuple[int, int]): 图像的原始形状
返回
  • np.ndarray: 修改后的掩码

build_mask_coeffs

Python
1def build_mask_coeffs(parsed_results: np.ndarray, masks_outputs_values: list[np.ndarray], protos_len: int) -> np.ndarray:
收集所有检测的掩码系数,按头部分组。参数
  • parsed_results (np.ndarray): FastSAM 解码输出
  • masks_outputs_values (list[np.ndarray]): 模型掩码输出
  • protos_len (int): 原型数量
返回
  • np.ndarray

compute_fastsam_mask

Python
1def compute_fastsam_mask(outputs_values: list[np.ndarray], masks_outputs_values: list[np.ndarray], protos_output: np.ndarray, protos_len: int, *, conf_threshold: float, n_classes: int, iou_threshold: float, mask_conf: float, prompt: str, points: tuple[int, int] | None, point_label: int | None, bbox: tuple[int, int, int, int] | None) -> tuple[np.ndarray, int]:
解码 FastSAM 输出并应用配置的掩码提示。参数
  • outputs_values (list[np.ndarray]): 按输出头排序的检测张量。
  • masks_outputs_values (list[np.ndarray]): 按检测头匹配排序的掩码系数张量。
  • protos_output (np.ndarray): 批处理原型张量,形状为 (1, channels, height, width)。
  • protos_len (int): 每个掩码系数向量使用的原型通道数。
  • conf_threshold (float): 用于过滤候选的最小检测置信度。
  • n_classes (int): 检测张量中编码的对象类别数。
  • iou_threshold (float): 用于非极大值抑制的交并比阈值。
  • mask_conf (float): 用于二值化掩码 logits 的概率阈值。
  • prompt (str): 掩码选择模式:"everything"、"bbox" 或 "point"。
  • points (tuple[int, int] | None): 用于点选择的图像像素坐标中的提示点。
  • point_label (int | None): 用于包含或排除匹配掩码的点提示标签。
  • bbox (tuple[int, int, int, int] | None): 图像像素坐标中的边界框提示。
返回
  • tuple[np.ndarray, int]: 合并的实例掩码和选定掩码数量的对。未选定掩码会生成值为 -1 且计数为 0 的背景掩码。

decode_fastsam_output

Python
1def decode_fastsam_output(outputs: list[np.ndarray], strides: list[int], anchors: list[np.ndarray | None], img_shape: tuple[int, int], conf_thres: float = 0.5, iou_thres: float = 0.45, num_classes: int = 1) -> np.ndarray:
解码 FastSAM 模型的输出。参数
  • outputs (list[np.ndarray]): FastSAM 输出列表
  • strides (list[int]): 步幅列表
  • anchors (list[np.ndarray | None]): 锚点列表
  • img_shape (tuple[int, int]): 图像形状
  • conf_thres (float): 置信度阈值
  • iou_thres (float): IoU 阈值
  • num_classes (int): 类别数量
返回
  • np.ndarray: NMS 输出

format_results

Python
1def format_results(bboxes: np.ndarray, masks: np.ndarray, filter: int = 0) -> list[dict[str, Any]]:
将检测结果格式化为注释列表,每个注释包含 ID、分割、边界框、分数和面积。来源:https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L59参数
  • bboxes (np.ndarray): 检测到的对象的边界框
  • masks (np.ndarray): 检测到的对象的掩码
  • filter (int): 过滤值
返回
  • list[dict[str, Any]]: 格式化的注释

merge_masks

Python
1def merge_masks(masks: np.ndarray) -> np.ndarray:
将掩码合并为 2D 数组,其中每个对象由唯一的标签表示。参数
  • masks (np.ndarray): 3D 掩码数组
返回
  • np.ndarray: 2D 掩码数组

point_prompt

Python
1def point_prompt(bboxes: np.ndarray, masks: np.ndarray, points: list[tuple[int, int]], pointlabel: list[int], orig_shape: tuple[int, int]) -> np.ndarray:
根据用户输入调整检测到的掩码上的点,并返回修改后的结果。来源:https://github.com/ultralytics/ultralytics/blob/8094df3c474fe83d874fd83da19f704205e1eef3/ultralytics/models/fastsam/prompt.py#L317 已修改为使用 numpy 而不是 torch。参数
  • bboxes (np.ndarray): 检测到的对象的边界框
  • masks (np.ndarray): 检测到的对象的掩码
  • points (list[tuple[int, int]]): 要调整的点
  • pointlabel (list[int]): 点标签
  • orig_shape (tuple[int, int]): 图像的原始形状
返回
  • np.ndarray: 修改后的掩码

process_masks

Python
1def process_masks(parsed_results: np.ndarray, mask_coeffs: np.ndarray, protos: np.ndarray, orig_shape: tuple[int, int], mask_conf: float) -> np.ndarray:
将输出处理为所有检测的全尺寸掩码。参数
  • parsed_results (np.ndarray): FastSAM 解码输出
  • mask_coeffs (np.ndarray): 掩码系数
  • protos (np.ndarray): 模型输出中的原型
  • orig_shape (tuple[int, int]): 模型的输入形状
  • mask_conf (float): 掩码置信度
返回
  • np.ndarray