• Jetson Orin NX
• Deepstream 6.3
• JetPack 5.1.2
Hello!
I am creating a custom plugin (written in python) to get familiar with writing to the deepstream metadata and then use the nvdsosd
plugin to draw boxes/text/masks.
So far, text and boxes work fine, but I cannot get the instance masks working. Here’s a snippet of my code
class GstCustomDraw(GstBase.BaseTransform):
# omitted code
def load_mask(self):
""" Load and preprocess the black & white mask image """
if not self.mask_path:
print("No mask path provided.")
return
try:
print(f"Loading mask from: {self.mask_path}")
img = cv2.imread(self.mask_path, cv2.IMREAD_GRAYSCALE)
if img is None:
print("Failed to load mask image!")
return
_, self.mask = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
self.mask = np.uint8(self.mask)
print("Mask successfully loaded!")
except Exception as e:
print(f"Error loading mask: {e}")
def add_object_meta(self, batch_meta, frame_meta):
""" Add a square object to the metadata """
obj_meta = pyds.nvds_acquire_obj_meta_from_pool(batch_meta)
# Set object metadata
obj_meta.confidence = 0.85 # Hardcoded confidence
obj_meta.class_id = 1 # Assign a class ID
obj_meta.obj_label = "Custom Object"
# Set rect params
rect_params = obj_meta.rect_params
rect_params.left = frame_meta.source_frame_width // 4
rect_params.top = frame_meta.source_frame_height // 4
rect_params.width = self.square_size
rect_params.height = self.square_size
rect_params.border_width = 3
rect_params.has_bg_color = 1
rect_params.bg_color.set(*self.square_color) # Use configured color
rect_params.border_color.set(0.0, 1.0, 0.0, 1.0) # Green border
# Assign mask data
mask_params = obj_meta.mask_params
mask_params.threshold = 128
mask_params.width = self.mask.shape[1]
mask_params.height = self.mask.shape[0]
mask_params.size = self.mask.size
mask_params.data[:] = self.mask.flatten()
# set text params
text_params = obj_meta.text_params
text_params.display_text = "Custom Object"
text_params.x_offset = int(rect_params.left)
text_params.y_offset = int(rect_params.top)
text_params.font_params.font_name = "Serif"
text_params.font_params.font_size = 15
text_params.font_params.font_color.set(0.0, 0.0, 0.0, 1.0) # Black
text_params.set_bg_clr = 1
text_params.text_bg_clr.set(1.0, 0.0, 0.0, 1.0) # Red background
pyds.nvds_add_obj_meta_to_frame(frame_meta, obj_meta, None)
# omitted code
I have found examples on how to read from obj_meta.mask_params
, but nothing on how to write to the NvDsMeta.
Am I doing something wrong?
Here is the gstreamer pipeline I am using to test
gst-launch-1.0 videotestsrc pattern=ball animation-mode=1 ! \
"video/x-raw, width=1280, height=960, format=RGBA" ! \
nvvideoconvert ! "video/x-raw(memory:NVMM), format=RGBA" ! \
nvstreammux0.sink_0 nvstreammux name=nvstreammux0 batch-size=1 width=1280 height=960 live-source=TRUE ! \
queue ! customdraw square-size=200 mask-path=mask.png ! \
queue ! nvdsosd display-clock=1 display-bbox=1 display-mask=1 ! \
queue ! nvvideoconvert ! <some_sink>
This is a screenshot of what I am getting (the display_meta
code is ommited from the snipped above).
Thank you!