pyds.NvDsObjectMeta unexpected string assignment behavior

**• Hardware Platform (Jetson / GPU): T1200
**• DeepStream Version: 7.0
**• TensorRT Version: 10.3.0
**• Issue Type( questions, new requirements, bugs): bugs

Hi,
I’m using an object detector and want to change some of its results in post-process. For example, consolidate several classes under the same category. To do that, I’m updating the meta data of the detection (working in python), and I’ve encountered some unusual behavior. I’m not sure if this is by design or unexpected:

  1. Is assigning to a pyds.NvDsObjectMeta supported?
  2. If not, what is the appropriate way to modify a detector’s result before feeding it downstream (for example, into a tracker)?

Sample (and unexpected behavior):

# Python
# In this example I try to overwrite the "truck" class with a "car" 
obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)  # The metadata for object-detection 
print(obj_meta.obj_label)
>> "truck"

obj_meta.obj_label = "car"
print(obj_meta.obj_label)
>> "carck"

obj_meta.obj_label = "car\0"
print(obj_meta.obj_label)
>> "car"

It seems like there’s some leftover for a regular assignment and the string terminates where the original string terminated. Instead of “car”, I get “carck”. Only when explicitly adding \0 explicitly does it terminate correctly.

Is this an expected behavior?

Thanks

I think it’s a bug, which is related to the native code. Please refer to the below patch. This patch is based on DS-7.1, please port to DS-7.0 based on this

Then recompile and install pyds

diff --git a/bindings/BINDINGSGUIDE.md b/bindings/BINDINGSGUIDE.md
index cd8abaa..a120eed 100644
--- a/bindings/BINDINGSGUIDE.md
+++ b/bindings/BINDINGSGUIDE.md
@@ -270,13 +270,15 @@ Note that a _return value policy_ must be set. In this case, we want to prevent
 
 The [```include/bind/bind_string_property_definitions.h```](include/bind/bind_string_property_definitions.h) header file includes this macro definition:
 ```c++
-#define STRING_CHAR_ARRAY(TYPE, FIELD)                             \
+#define STRING_CHAR_ARRAY(TYPE, FIELD, FIELD_SIZE)                 \
         [](const TYPE &self)->std::string {                        \
             return std::string(self.FIELD);                        \
         },                                                         \
         [](TYPE &self, std::string str) {                          \
             int strSize = str.size();                              \
-            str.copy(self.FIELD, strSize);                         \
+            memset(self.FIELD, 0, FIELD_SIZE);                     \
+            int len = FIELD_SIZE - 1 > strSize ? strSize : FIELD_SIZE - 1; \
+            str.copy(self.FIELD, len);                             \
         },                                                         \
         py::return_value_policy::reference
 
diff --git a/bindings/include/bind/bind_string_property_definitions.h b/bindings/include/bind/bind_string_property_definitions.h
index 1c608c1..72a405e 100644
--- a/bindings/include/bind/bind_string_property_definitions.h
+++ b/bindings/include/bind/bind_string_property_definitions.h
@@ -44,13 +44,15 @@ namespace py = pybind11;
         },                                                         \
         py::return_value_policy::reference
 
-#define STRING_CHAR_ARRAY(TYPE, FIELD)                             \
+#define STRING_CHAR_ARRAY(TYPE, FIELD, FIELD_SIZE)                 \
         [](const TYPE &self)->std::string {                        \
             return std::string(self.FIELD);                        \
         },                                                         \
         [](TYPE &self, std::string str) {                          \
             int strSize = str.size();                              \
-            str.copy(self.FIELD, strSize);                         \
+            memset(self.FIELD, 0, FIELD_SIZE);                     \
+            int len = FIELD_SIZE - 1 > strSize ? strSize : FIELD_SIZE - 1; \
+            str.copy(self.FIELD, len);                             \
         },                                                         \
         py::return_value_policy::reference
 
diff --git a/bindings/src/bindnvdsmeta.cpp b/bindings/src/bindnvdsmeta.cpp
index a42a6c6..247032b 100644
--- a/bindings/src/bindnvdsmeta.cpp
+++ b/bindings/src/bindnvdsmeta.cpp
@@ -325,7 +325,7 @@ namespace pydeepstream {
                      pydsdoc::nvmeta::ObjectMetaDoc::cast)
 
                 .def_property("obj_label",
-                              STRING_CHAR_ARRAY(NvDsObjectMeta, obj_label))
+                              STRING_CHAR_ARRAY(NvDsObjectMeta, obj_label, MAX_LABEL_SIZE))
                 .def_readwrite("classifier_meta_list",
                                &NvDsObjectMeta::classifier_meta_list)
                 .def_readwrite("obj_user_meta_list",
@@ -390,7 +390,7 @@ namespace pydeepstream {
                 .def_readwrite("base_meta", &NvDsLabelInfo::base_meta)
                 .def_readwrite("num_classes", &NvDsLabelInfo::num_classes)
                 .def_property("result_label",
-                              STRING_CHAR_ARRAY(NvDsLabelInfo, result_label))
+                              STRING_CHAR_ARRAY(NvDsLabelInfo, result_label, MAX_LABEL_SIZE))
 
                 .def("cast",
                      [](void *data) {
diff --git a/bindings/src/bindtrackermeta.cpp b/bindings/src/bindtrackermeta.cpp
index f817aa0..3c0f990 100644
--- a/bindings/src/bindtrackermeta.cpp
+++ b/bindings/src/bindtrackermeta.cpp
@@ -48,7 +48,7 @@ namespace pydeepstream {
                 .def_readwrite("uniqueId", &NvDsTargetMiscDataObject::uniqueId)
                 .def_readwrite("classId", &NvDsTargetMiscDataObject::classId)
                 .def_property("objLabel",
-                              STRING_CHAR_ARRAY(NvDsTargetMiscDataObject, objLabel))
+                              STRING_CHAR_ARRAY(NvDsTargetMiscDataObject, objLabel, MAX_LABEL_SIZE))
 
                 .def("cast",
                      [](void *data) {
diff --git a/bindings/src/utils.cpp b/bindings/src/utils.cpp
index 9f97794..987b9b7 100644
--- a/bindings/src/utils.cpp
+++ b/bindings/src/utils.cpp
@@ -86,7 +86,7 @@ namespace pydeepstream {
                 .def_readwrite("scaleImg", &NvDsObjEncUsrArgs::scaleImg)
                 .def_readwrite("scaledWidth", &NvDsObjEncUsrArgs::scaledWidth)
                 .def_readwrite("scaledHeight", &NvDsObjEncUsrArgs::scaledHeight)
-                .def_property("fileNameImg", STRING_CHAR_ARRAY(NvDsObjEncUsrArgs, fileNameImg))
+                .def_property("fileNameImg", STRING_CHAR_ARRAY(NvDsObjEncUsrArgs, fileNameImg, FILE_NAME_SIZE))
                 .def_readwrite("objNum", &NvDsObjEncUsrArgs::objNum)
                 .def_readwrite("quality", &NvDsObjEncUsrArgs::quality)
                 .def_readwrite("isFrame", &NvDsObjEncUsrArgs::isFrame)

Additional suggestion:modifying label.txt should be a better choice rather than modifying obj_meta.obj_label.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.