NvDsInferSegmentationMeta

class pyds.NvDsInferSegmentationMeta

保存一个帧/一个对象的分割模型输出信息。“nvinfer” 插件为分割模型添加此元数据。此元数据作为 NvDsUserMeta 添加到相应帧的 frame_user_meta_list 或相应对象的 object_user_meta_list,并将 meta_type 设置为 NVDSINFER_SEGMENTATION_META。使用 get_segmentation_masks() 获取掩码数据。

变量:
  • classesint, 分割输出中类别的数量。

  • widthint, 分割输出类别图的宽度。

  • heightint, 分割输出类别图的高度。

  • class_map – 2D 像素类别图的数组。像素 (x,y) 的输出将位于索引 (y * width + x) 处。

  • class_probabilities_map – 包含概率的原始数组。类别 c 和像素 (x,y) 的概率将位于索引 (c * width * height + y * width + x) 处。

  • priv_data – 用于元数据生成器内部内存管理的私有数据。

用法示例

def map_mask_as_display_bgr(mask):
    """ Assigning multiple colors as image output using the information
        contained in mask. (BGR is opencv standard.)
    """
    # getting a list of available classes
    m_list = list(set(mask.flatten()))

    shp = mask.shape
    bgr = np.zeros((shp[0], shp[1], 3))
    for idx in m_list:
        bgr[mask == idx] = COLORS[idx]
    return bgr

...

l_user = frame_meta.frame_user_meta_list #Get glist containing NvDsUserMeta objects from given NvDsFrameMeta
while l_user is not None:
    try:
        # Note that l_user.data needs a cast to pyds.NvDsUserMeta
        # The casting is done by pyds.NvDsUserMeta.cast()
        # The casting also keeps ownership of the underlying memory
        # in the C code, so the Python garbage collector will leave
        # it alone.
        seg_user_meta = pyds.NvDsUserMeta.cast(l_user.data)
    except StopIteration:
        break
    if seg_user_meta and seg_user_meta.base_meta.meta_type == \
        pyds.NVDSINFER_SEGMENTATION_META:
        try:
            # Note that seg_user_meta.user_meta_data needs a cast to
            # pyds.NvDsInferSegmentationMeta
            # The casting is done by pyds.NvDsInferSegmentationMeta.cast()
            # The casting also keeps ownership of the underlying memory
            # in the C code, so the Python garbage collector will leave
            # it alone.
            segmeta = pyds.NvDsInferSegmentationMeta.cast(seg_user_meta.user_meta_data)
        except StopIteration:
            break
        # Retrieve mask data in the numpy format from segmeta
        # Note that pyds.get_segmentation_masks() expects object of
        # type NvDsInferSegmentationMeta
        masks = pyds.get_segmentation_masks(segmeta)
        masks = np.array(masks, copy=True, order='C')
        # map the obtained masks to colors of 2 classes.
        frame_image = map_mask_as_display_bgr(masks)
        cv2.imwrite(folder_name + "/" + str(frame_number) + ".jpg", frame_image)
    try:
        l_user = l_user.next
    except StopIteration:
        break
cast(self: capsule) pyds.NvDsInferSegmentationMeta

将给定对象/数据转换为 NvDsInferSegmentationMeta, 调用 pyds.NvDsInferSegmentationMeta.cast(data)