diff --git a/src/nv_infer_server_manager.cpp b/src/nv_infer_server_manager.cpp index 9ebafbb..5fd831e 100644 --- a/src/nv_infer_server_manager.cpp +++ b/src/nv_infer_server_manager.cpp @@ -472,8 +472,14 @@ void NvInferServerManager::update_frame_with_face_body_meta( text_params_imprecise_face.display_text = g_strdup(imprecise_face_str[0]); // g_strdup(pgie_class_str[0]); /* Display text above the left top corner of the object. */ - text_params_imprecise_face.x_offset = rect_params_imprecise_face.left; - text_params_imprecise_face.y_offset = rect_params_imprecise_face.top; + text_params_imprecise_face.x_offset = + (rect_params_imprecise_face.left - 15 < 0) + ? 15 + : rect_params_imprecise_face.left - 15; + text_params_imprecise_face.y_offset = + (rect_params_imprecise_face.top - 15 < 0) + ? 15 + : rect_params_imprecise_face.top - 15; /* Set black background for the text. */ text_params_imprecise_face.set_bg_clr = 1; text_params_imprecise_face.text_bg_clr = NvOSD_ColorParams{0, 0, 0, 1}; @@ -535,9 +541,9 @@ void NvInferServerManager::update_frame_with_face_body_meta( // face_obj->object_id); /* Display text above the left top corner of the object. */ text_params_body.x_offset = - (rect_params_body.left - 30 < 0) ? 0 : rect_params_body.left - 30; + (rect_params_body.left - 30 < 0) ? 10 : rect_params_body.left - 30; text_params_body.y_offset = - (rect_params_body.top - 30 < 0) ? 0 : rect_params_body.top - 30; + (rect_params_body.top - 30 < 0) ? 10 : rect_params_body.top - 30; /* Set black background for the text. */ text_params_body.set_bg_clr = 1; diff --git a/src/nv_tracker_manager.cpp b/src/nv_tracker_manager.cpp index acda8ca..77842a7 100644 --- a/src/nv_tracker_manager.cpp +++ b/src/nv_tracker_manager.cpp @@ -321,6 +321,8 @@ GstPadProbeReturn NvTrackerManager::tracker_src_pad_buffer_probe( face_rect_params->width = x2 - x1; face_rect_params->height = y2 - y1; + clamp_rect_params(frame_meta, face_rect_params); + NvDsObjectMeta *face_obj = nvds_acquire_obj_meta_from_pool(batch_meta); face_obj->unique_component_id = @@ -372,8 +374,13 @@ GstPadProbeReturn NvTrackerManager::tracker_src_pad_buffer_probe( // obj_meta->object_id, final_face_obj->object_id); /* Display text above the left top corner of the * object.*/ - text_params.x_offset = rect_params.left; - text_params.y_offset = rect_params.top + 30; + text_params.x_offset = (rect_params.left - 15 < 0) + ? 15 + : rect_params.left - 15; + text_params.y_offset = (rect_params.top - 15 < 0) + ? 15 + : rect_params.top - 15; + /* Set black background for the text. */ text_params.set_bg_clr = 1; text_params.text_bg_clr = NvOSD_ColorParams{0, 0, 0, 1}; @@ -543,4 +550,49 @@ GstPadProbeReturn NvTrackerManager::tracker_src_pad_buffer_probe( frame_number++; return GST_PAD_PROBE_OK; -} \ No newline at end of file +} + +void NvTrackerManager::clamp_rect_params(NvDsFrameMeta *frame_meta, + NvOSD_RectParams *rect_params) { + guint frame_width = frame_meta->source_frame_width; + guint frame_height = frame_meta->source_frame_height; + + // read values (DeepStream stores rect params as floats) + float left = rect_params->left; + float top = rect_params->top; + float width = rect_params->width; + float height = rect_params->height; + float right = left + width; + float bottom = top + height; + + // CHECK for invalid numbers (NaN/inf) or out-of-bounds + bool invalid = false; + if (!std::isfinite(left) || !std::isfinite(top) || !std::isfinite(width) || + !std::isfinite(height)) { + invalid = true; + } else if (width <= 0.0f || height <= 0.0f) { + invalid = true; + } + + // clamp coordinates into frame (clip) + float clamped_left = + std::max(0.0f, std::min(left, (float)frame_width - 1.0f)); + float clamped_top = + std::max(0.0f, std::min(top, (float)frame_height - 1.0f)); + float clamped_right = abs(std::min(right, (float)frame_width - 1.0f)); + float clamped_bottom = abs(std::min(bottom, (float)frame_height - 1.0f)); + + float clamped_w = clamped_right - clamped_left; + float clamped_h = clamped_bottom - clamped_top; + if (clamped_w <= 0.0f || clamped_h <= 0.0f) { + invalid = true; + } + (void)invalid; + + rect_params->left = clamped_left; + rect_params->top = clamped_top; + rect_params->width = clamped_w; + rect_params->height = clamped_h; + return; +} + diff --git a/src/nv_tracker_manager.hpp b/src/nv_tracker_manager.hpp index f99cd82..d40926c 100644 --- a/src/nv_tracker_manager.hpp +++ b/src/nv_tracker_manager.hpp @@ -57,4 +57,5 @@ class NvTrackerManager { static std::optional< std::tuple, float>> face_box_extract(float *); + static void clamp_rect_params(NvDsFrameMeta *, NvOSD_RectParams *); }; \ No newline at end of file