Skip to content

Commit 559b9fe

Browse files
committed
Replace raw "keypoints"/"classification_result" strings with named constants
Define TENSOR_TYPE_KEYPOINTS and TENSOR_TYPE_CLASSIFICATION constants in tensor.h (C++) and tensor.py (Python), then replace all raw string literals across the codebase with references to these constants. Additionally, simplify the tensor type filter in jsonconverter.cpp: change the confusing `!has_field("type") || type() == "keypoints"` condition to a direct `type() != TENSOR_TYPE_CLASSIFICATION` check, which clearly expresses the intent of excluding classification tensors from the raw tensors section.
1 parent d975a97 commit 559b9fe

19 files changed

Lines changed: 72 additions & 51 deletions

File tree

include/dlstreamer/gst/videoanalytics/region_of_interest.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ class RegionOfInterest {
231231
for (GList *l = meta->params; l; l = g_list_next(l)) {
232232
GstStructure *s = GST_STRUCTURE(l->data);
233233
const char *type = gst_structure_get_string(s, "type");
234-
if (not gst_structure_has_name(s, "object_id") && (type == nullptr || strcmp(type, "keypoints") != 0) &&
235-
(type == nullptr || strcmp(type, "classification_result") != 0)) {
234+
if (not gst_structure_has_name(s, "object_id") &&
235+
(type == nullptr || strcmp(type, TENSOR_TYPE_KEYPOINTS) != 0) &&
236+
(type == nullptr || strcmp(type, TENSOR_TYPE_CLASSIFICATION) != 0)) {
236237
_tensors.emplace_back(s);
237238
if (_tensors.back().is_detection())
238239
_detection = &_tensors.back();

include/dlstreamer/gst/videoanalytics/tensor.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
namespace GVA {
2929

30+
/// Tensor type string for keypoints data (e.g. human pose estimation results)
31+
constexpr const char *TENSOR_TYPE_KEYPOINTS = "keypoints";
32+
/// Tensor type string for classification results
33+
constexpr const char *TENSOR_TYPE_CLASSIFICATION = "classification_result";
34+
3035
/**
3136
* @brief This class represents tensor - map-like storage for inference result information, such as output blob
3237
* description (output layer dims, layout, rank, precision, etc.), inference result in a raw and interpreted forms.
@@ -610,7 +615,7 @@ class Tensor {
610615
*/
611616
bool convert_to_meta(GstAnalyticsMtd *mtd, GstAnalyticsODMtd *od_mtd, GstAnalyticsRelationMeta *meta) const {
612617

613-
if (type() == "keypoints") {
618+
if (type() == TENSOR_TYPE_KEYPOINTS) {
614619
GstAnalyticsGroupMtd *group_mtd = reinterpret_cast<GstAnalyticsGroupMtd *>(mtd);
615620
const std::vector<guint> dimensions = dims();
616621
const std::vector<float> raw_positions = data<float>();
@@ -657,7 +662,7 @@ class Tensor {
657662
throw std::runtime_error("Failed to create keypoints group meta");
658663

659664
return true;
660-
} else if (type() == "classification_result") {
665+
} else if (type() == TENSOR_TYPE_CLASSIFICATION) {
661666
GstAnalyticsClsMtd *cls_mtd = mtd;
662667
gfloat confidence = this->confidence();
663668
GQuark label = g_quark_from_string(this->label().c_str());
@@ -789,11 +794,11 @@ class Tensor {
789794
}
790795

791796
// create keypoint tensor
792-
GstStructure *gst_structure = gst_structure_new_empty("keypoints");
797+
GstStructure *gst_structure = gst_structure_new_empty(TENSOR_TYPE_KEYPOINTS);
793798
Tensor tensor(gst_structure);
794799

795800
tensor.set_precision(Precision::FP32);
796-
tensor.set_type("keypoints");
801+
tensor.set_type(TENSOR_TYPE_KEYPOINTS);
797802
tensor.set_format(format_str);
798803

799804
tensor.set_dims({static_cast<guint>(keypoint_count), static_cast<guint>(keypoint_dimension)});
@@ -813,7 +818,7 @@ class Tensor {
813818
gsize class_count = gst_analytics_cls_mtd_get_length(cls_mtd);
814819

815820
GstStructure *tensor = gst_structure_new_empty("classification");
816-
gst_structure_set(tensor, "type", G_TYPE_STRING, "classification_result", NULL);
821+
gst_structure_set(tensor, "type", G_TYPE_STRING, TENSOR_TYPE_CLASSIFICATION, NULL);
817822

818823
gfloat result_confidence = 0;
819824
std::string result_label;

python/gstgva/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# ==============================================================================
2-
# Copyright (C) 2018-2020 Intel Corporation
2+
# Copyright (C) 2018-2026 Intel Corporation
33
#
44
# SPDX-License-Identifier: MIT
55
# ==============================================================================
66

77
from .region_of_interest import RegionOfInterest
88
from .video_frame import VideoFrame
9-
from .tensor import Tensor
9+
from .tensor import Tensor, TENSOR_TYPE_KEYPOINTS, TENSOR_TYPE_CLASSIFICATION

python/gstgva/region_of_interest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from gi.repository import GstVideo, GLib, GObject, Gst, GstAnalytics, DLStreamerMeta
2525
# pylint: enable=no-name-in-module,unused-import
2626

27-
from .tensor import Tensor
27+
from .tensor import Tensor, TENSOR_TYPE_KEYPOINTS, TENSOR_TYPE_CLASSIFICATION
2828
from .util import VideoRegionOfInterestMeta, GstStructureHandle
2929
from .util import libgst, libgstvideo
3030
from . import meta_registry # noqa: F401 — registers DLStreamerMeta types with GstAnalytics
@@ -57,8 +57,8 @@ def __init__(
5757
tensor = Tensor(tensor_structure)
5858
if (
5959
tensor.name() != "object_id"
60-
and tensor.type() != "keypoints"
61-
and tensor.type() != "classification_result"
60+
and tensor.type() != TENSOR_TYPE_KEYPOINTS
61+
and tensor.type() != TENSOR_TYPE_CLASSIFICATION
6262
):
6363
self._tensors.append(tensor)
6464
if tensor.is_detection():

python/gstgva/tensor.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
)
2929
from .util import GVATensorMeta
3030

31+
## Tensor type string for keypoints data (e.g. human pose estimation results)
32+
TENSOR_TYPE_KEYPOINTS = "keypoints"
33+
## Tensor type string for classification results
34+
TENSOR_TYPE_CLASSIFICATION = "classification_result"
35+
3136

3237
## @brief This class represents tensor - map-like storage for inference result information, such as output blob
3338
# description (output layer dims, layout, rank, precision, etc.), inference result in a raw and interpreted forms.
@@ -495,7 +500,7 @@ def convert_to_meta(
495500
# @param od_meta parent object-detection metadata (required for keypoints)
496501
# @return GstAnalyticsMtd on success, None if tensor type is not supported
497502
mtd = None
498-
if self.type() == "keypoints":
503+
if self.type() == TENSOR_TYPE_KEYPOINTS:
499504
dimensions = self.dims()
500505
raw_data = self.data()
501506
confidence_val = self.confidence()
@@ -551,7 +556,7 @@ def convert_to_meta(
551556

552557
mtd = group_mtd
553558

554-
elif self.type() == "classification_result":
559+
elif self.type() == TENSOR_TYPE_CLASSIFICATION:
555560
confidence_level = (
556561
self.confidence() if self.confidence() is not None else 0.0
557562
)
@@ -661,11 +666,11 @@ def convert_to_tensor(mtd: GstAnalytics.Mtd) -> ctypes.c_void_p | None:
661666
point_connections.append(i)
662667

663668
# create keypoint tensor
664-
structure = libgst.gst_structure_new_empty("keypoints".encode("utf-8"))
669+
structure = libgst.gst_structure_new_empty(TENSOR_TYPE_KEYPOINTS.encode("utf-8"))
665670
tensor = Tensor(structure)
666671

667672
tensor.set_precision(Tensor.PRECISION.FP32)
668-
tensor.set_type("keypoints")
673+
tensor.set_type(TENSOR_TYPE_KEYPOINTS)
669674
tensor.set_format(semantic_tag)
670675

671676
tensor.set_dims([keypoint_count, keypoint_dimension])
@@ -704,7 +709,7 @@ def convert_to_tensor(mtd: GstAnalytics.Mtd) -> ctypes.c_void_p | None:
704709
result_confidence = confidence
705710

706711
tensor.set_name("classification")
707-
tensor["type"] = "classification_result"
712+
tensor["type"] = TENSOR_TYPE_CLASSIFICATION
708713
tensor.set_label(result_label)
709714
tensor["confidence"] = result_confidence
710715

src/monolithic/gst/elements/gvametaconvert/jsonconverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ json convert_frame_tensors(GstGvaMetaConvert *converter, GstBuffer *buffer) {
289289
const std::vector<GVA::Tensor> tensors = video_frame.tensors();
290290
json array = json::array();
291291
for (auto &tensor : video_frame.tensors()) {
292-
if (!tensor.has_field("type") || tensor.type() == "keypoints") {
292+
if (tensor.type() != GVA::TENSOR_TYPE_CLASSIFICATION) {
293293
array.push_back(convert_tensor(tensor));
294294
}
295295
}
@@ -606,7 +606,7 @@ json convert_lidar_inference_meta(GstGvaMetaConvert *converter, GstBuffer *buffe
606606
objects.push_back(detection);
607607
}
608608

609-
if (converter->add_tensor_data && (!tensor.has_field("type") || tensor.type() == "keypoints") &&
609+
if (converter->add_tensor_data && tensor.type() != GVA::TENSOR_TYPE_CLASSIFICATION &&
610610
tensor.has_field("data_buffer")) {
611611
tensors.push_back(convert_tensor(tensor));
612612
}

src/monolithic/gst/elements/gvawatermark/gstgvawatermarkimpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ void Impl::preparePrimsForTensor(const GVA::Tensor &tensor, GVA::Rect<double> re
10561056
*/
10571057
void Impl::preparePrimsForKeypoints(const GVA::Tensor &tensor, GVA::Rect<double> rectangle,
10581058
std::vector<render::Prim> &prims) const {
1059-
if (tensor.type() != "keypoints")
1059+
if (tensor.type() != GVA::TENSOR_TYPE_KEYPOINTS)
10601060
return;
10611061

10621062
const auto keypoints_data = tensor.data<float>();

src/monolithic/gst/inference_elements/common/post_processor/converters/to_roi/blob_to_roi_converter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "post_processor/blob_to_meta_converter.h"
1010
#include "post_processor/post_proc_common.h"
1111

12+
#include <dlstreamer/gst/videoanalytics/tensor.h>
1213
#include <gst/gst.h>
1314

1415
#include <map>
@@ -100,7 +101,7 @@ class BlobToROIConverter : public BlobToMetaConverter {
100101
for (auto &structure : this->tensors) {
101102
GVA::Tensor tensor(structure);
102103

103-
if (tensor.type() != "keypoints") {
104+
if (tensor.type() != GVA::TENSOR_TYPE_KEYPOINTS) {
104105
continue;
105106
};
106107

src/monolithic/gst/inference_elements/common/post_processor/converters/to_roi/centerface.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "safe_arithmetic.hpp"
1313

1414
#include <dlstreamer/gst/metadata/gstanalyticskeypointdescriptor.h>
15+
#include <dlstreamer/gst/videoanalytics/tensor.h>
1516
#include <gst/gst.h>
1617
#include <opencv2/opencv.hpp>
1718

@@ -94,8 +95,8 @@ const float *CenterfaceConverter::parseOutputBlob(const OutputBlobs &output_blob
9495
void CenterfaceConverter::addLandmarksTensor(DetectedObject &detected_object, const float *landmarks,
9596
int num_of_landmarks) const {
9697
GstStructure *tensor = gst_structure_copy(getModelProcOutputInfo().get());
97-
gst_structure_set_name(tensor, "keypoints");
98-
gst_structure_set(tensor, "type", G_TYPE_STRING, "keypoints", NULL);
98+
gst_structure_set_name(tensor, GVA::TENSOR_TYPE_KEYPOINTS);
99+
gst_structure_set(tensor, "type", G_TYPE_STRING, GVA::TENSOR_TYPE_KEYPOINTS, NULL);
99100
gst_structure_set(tensor, "precision", G_TYPE_INT, GVA_PRECISION_FP32, NULL);
100101
gst_structure_set(tensor, "format", G_TYPE_STRING, GST_ANALYTICS_KEYPOINT_FACE_CENTERFACE_5, NULL);
101102

src/monolithic/gst/inference_elements/common/post_processor/converters/to_roi/yolo_v26.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "inference_backend/logger.h"
1111

1212
#include <dlstreamer/gst/metadata/gstanalyticskeypointdescriptor.h>
13+
#include <dlstreamer/gst/videoanalytics/tensor.h>
1314
#include <gst/gst.h>
1415
#include <opencv2/opencv.hpp>
1516
#include <string>
@@ -121,8 +122,8 @@ void YOLOv26PoseConverter::parseOutputBlob(const float *data, const std::vector<
121122
GstStructure *gst_structure = gst_structure_copy(getModelProcOutputInfo().get());
122123
GVA::Tensor tensor(gst_structure);
123124

124-
tensor.set_name("keypoints");
125-
tensor.set_type("keypoints");
125+
tensor.set_name(GVA::TENSOR_TYPE_KEYPOINTS);
126+
tensor.set_type(GVA::TENSOR_TYPE_KEYPOINTS);
126127
tensor.set_format(coco17_descriptor->semantic_tag);
127128

128129
// set tensor data (positions)

0 commit comments

Comments
 (0)