GStreamer Video Analytics (GVA) Plugin
region_of_interest.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2018-2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  ******************************************************************************/
6 
13 #pragma once
14 
15 #include "tensor.h"
16 
17 #include <gst/gst.h>
18 #include <gst/video/gstvideometa.h>
19 
20 #include <cassert>
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 namespace GVA {
26 
30 template <typename T>
31 struct Rect {
32  T x, y, w, h;
33 };
34 
44  public:
49  Rect<uint32_t> rect() const {
50  return {_gst_meta->x, _gst_meta->y, _gst_meta->w, _gst_meta->h};
51  }
52 
58  Tensor det = detection();
59  return {det.get_double("x_min"), det.get_double("y_min"), det.get_double("x_max") - det.get_double("x_min"),
60  det.get_double("y_max") - det.get_double("y_min")};
61  }
62 
67  std::string label() const {
68  const char *str = g_quark_to_string(_gst_meta->roi_type);
69  return std::string(str ? str : "");
70  }
71 
76  double confidence() const {
77  return _detection ? _detection->confidence() : 0.0;
78  }
79 
84  int32_t object_id() const {
85  GstStructure *object_id_struct = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
86  if (!object_id_struct)
87  return 0;
88  int id = 0;
89  gst_structure_get_int(object_id_struct, "id", &id);
90  return id;
91  }
92 
97  std::vector<Tensor> tensors() const {
98  return this->_tensors;
99  }
100 
108  Tensor add_tensor(const std::string &name) {
109  GstStructure *tensor = gst_structure_new_empty(name.c_str());
110  gst_video_region_of_interest_meta_add_param(_gst_meta, tensor);
111  _tensors.emplace_back(tensor);
112  if (_tensors.back().is_detection())
113  _detection = &_tensors.back();
114 
115  return _tensors.back();
116  }
117 
128  if (!_detection) {
129  add_tensor("detection");
130  }
131  return *_detection;
132  }
133 
138  int label_id() const {
139  return _detection ? _detection->label_id() : 0;
140  }
141 
147  RegionOfInterest(GstVideoRegionOfInterestMeta *meta) : _gst_meta(meta), _detection(nullptr) {
148  if (not _gst_meta)
149  throw std::invalid_argument("GVA::RegionOfInterest: meta is nullptr");
150 
151  _tensors.reserve(g_list_length(meta->params));
152 
153  for (GList *l = meta->params; l; l = g_list_next(l)) {
154  GstStructure *s = (GstStructure *)l->data;
155  if (not gst_structure_has_name(s, "object_id")) {
156  _tensors.emplace_back(s);
157  if (_tensors.back().is_detection())
158  _detection = &_tensors.back();
159  }
160  }
161  }
162 
167  void set_label(std::string label) {
168  _gst_meta->roi_type = g_quark_from_string(label.c_str());
169  }
170 
175  void set_object_id(int32_t id) {
176  GstStructure *object_id = gst_video_region_of_interest_meta_get_param(_gst_meta, "object_id");
177  if (object_id) {
178  gst_structure_set(object_id, "id", G_TYPE_INT, id, NULL);
179  } else {
180  object_id = gst_structure_new("object_id", "id", G_TYPE_INT, id, NULL);
181  gst_video_region_of_interest_meta_add_param(_gst_meta, object_id);
182  }
183  }
184 
189  GstVideoRegionOfInterestMeta *_meta() const {
190  return _gst_meta;
191  }
192 
193  protected:
199  GstVideoRegionOfInterestMeta *_gst_meta;
204  std::vector<Tensor> _tensors;
209 };
210 
211 } // namespace GVA
GVA::Rect
Template structure for rectangle containing x, y, w, h fields.
Definition: region_of_interest.h:31
GVA::RegionOfInterest::_detection
Tensor * _detection
last added detection Tensor instance, defined as Tensor with name set to "detection"
Definition: region_of_interest.h:208
GVA::RegionOfInterest::label_id
int label_id() const
Get label_id from detection Tensor, last added to this RegionOfInterest.
Definition: region_of_interest.h:138
GVA::RegionOfInterest::detection
Tensor detection()
Returns detection Tensor, last added to this RegionOfInterest. As any other Tensor,...
Definition: region_of_interest.h:127
GVA::Tensor::get_double
double get_double(const std::string &field_name, double default_value=0) const
Get double contained in value stored at field_name.
Definition: tensor.h:226
GVA::Tensor::confidence
double confidence() const
Get confidence of detection or classification result extracted from the tensor.
Definition: tensor.h:155
GVA::RegionOfInterest::set_object_id
void set_object_id(int32_t id)
Set object ID.
Definition: region_of_interest.h:175
GVA::RegionOfInterest::object_id
int32_t object_id() const
Get unique id of the RegionOfInterest. The id typically created by gvatrack element.
Definition: region_of_interest.h:84
tensor.h
This file contains GVA::Tensor class which contains and describes neural network inference result.
GVA::RegionOfInterest::normalized_rect
Rect< double > normalized_rect()
Get bounding box of the RegionOfInterest as normalized coordinates in the range [0,...
Definition: region_of_interest.h:57
GVA::RegionOfInterest::add_tensor
Tensor add_tensor(const std::string &name)
Add new tensor (inference result) to this RegionOfInterest with name set. To add detection tensor,...
Definition: region_of_interest.h:108
GVA::Tensor::label_id
int label_id() const
Get label id.
Definition: tensor.h:324
GVA::RegionOfInterest::set_label
void set_label(std::string label)
Set RegionOfInterest label.
Definition: region_of_interest.h:167
GVA::RegionOfInterest::_meta
GstVideoRegionOfInterestMeta * _meta() const
Internal function, don't use or use with caution.
Definition: region_of_interest.h:189
GVA::RegionOfInterest::RegionOfInterest
RegionOfInterest(GstVideoRegionOfInterestMeta *meta)
Construct RegionOfInterest instance from GstVideoRegionOfInterestMeta. After this,...
Definition: region_of_interest.h:147
GVA::RegionOfInterest
This class represents region of interest - object describing detection result (bounding box) and cont...
Definition: region_of_interest.h:43
GVA::Tensor
This class represents tensor - map-like storage for inference result information, such as output blob...
Definition: tensor.h:38
GVA::RegionOfInterest::tensors
std::vector< Tensor > tensors() const
Get all Tensor instances added to this RegionOfInterest.
Definition: region_of_interest.h:97
GVA::RegionOfInterest::rect
Rect< uint32_t > rect() const
Get bounding box of the RegionOfInterest as pixel coordinates in original image.
Definition: region_of_interest.h:49
GVA::RegionOfInterest::_tensors
std::vector< Tensor > _tensors
vector of Tensor objects added to this RegionOfInterest (describing detection & inference results),...
Definition: region_of_interest.h:204
GVA::RegionOfInterest::confidence
double confidence() const
Get RegionOfInterest detection confidence (set by gvadetect)
Definition: region_of_interest.h:76
GVA::RegionOfInterest::_gst_meta
GstVideoRegionOfInterestMeta * _gst_meta
GstVideoRegionOfInterestMeta containing fields filled with detection result (produced by gvadetect el...
Definition: region_of_interest.h:199
GVA::RegionOfInterest::label
std::string label() const
Get RegionOfInterest label.
Definition: region_of_interest.h:67