NvDsInferSegmentationMeta

class pyds.NvDsInferSegmentationMeta

Holds the segmentation model output information for one frame / one object. The “nvinfer” plugins adds this meta for segmentation models. This meta data is added as NvDsUserMeta to the frame_user_meta_list of the corresponding frame_meta or object_user_meta_list of the corresponding object with the meta_type set to NVDSINFER_SEGMENTATION_META. Get mask data using get_segmentation_masks().

Variables:
  • classesint, Number of classes in the segmentation output.

  • widthint, Width of the segmentation output class map.

  • heightint, Height of the segmentation output class map.

  • class_map – Array for 2D pixel class map. The output for pixel (x,y) will be at index (y * width + x).

  • class_probabilities_map – The raw array containing the probabilities. The probability for class c and pixel (x,y) will be at index (c * width * height + y * width + x).

  • priv_data – Private data used for the meta producer’s internal memory management.

Example usage:

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

cast given object/data to NvDsInferSegmentationMeta, call pyds.NvDsInferSegmentationMeta.cast(data)