GStreamer Video Analytics (GVA) Plugin
audio_event.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  ******************************************************************************/
6 
13 #pragma once
14 
15 #include "tensor.h"
16 
17 #include "gva_audio_event_meta.h"
18 
19 #include <gst/audio/gstaudiometa.h>
20 #include <gst/gst.h>
21 
22 #include <cassert>
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26 
27 namespace GVA {
28 
32 template <typename T>
33 struct Segment {
34  T start, end;
35 };
36 
45 class AudioEvent {
46  public:
52  return {_gst_meta->start_timestamp, _gst_meta->end_timestamp};
53  }
54 
59  std::string label() const {
60  const char *str = g_quark_to_string(_gst_meta->event_type);
61  return std::string(str ? str : "");
62  }
63 
68  double confidence() const {
69  return _detection ? _detection->confidence() : 0.0;
70  }
71 
76  std::vector<Tensor> tensors() const {
77  return this->_tensors;
78  }
79 
87  Tensor add_tensor(const std::string &name) {
88  if (name.empty())
89  throw std::invalid_argument("GVA::AudioEvent: name is empty");
90  GstStructure *tensor = gst_structure_new_empty(name.c_str());
91  gst_gva_audio_event_meta_add_param(_gst_meta, tensor);
92  _tensors.emplace_back(tensor);
93  if (_tensors.back().is_detection())
94  _detection = &_tensors.back();
95 
96  return _tensors.back();
97  }
98 
109  if (!_detection) {
110  add_tensor("detection");
111  }
112  return *_detection;
113  }
114 
119  int label_id() const {
120  return _detection ? _detection->label_id() : 0;
121  }
122 
129  if (not _gst_meta)
130  throw std::invalid_argument("GVA::AudioEvent: meta is nullptr");
131 
132  _tensors.reserve(g_list_length(meta->params));
133 
134  for (GList *l = meta->params; l; l = g_list_next(l)) {
135  GstStructure *s = (GstStructure *)l->data;
136  if (not gst_structure_has_name(s, "object_id")) {
137  _tensors.emplace_back(s);
138  if (_tensors.back().is_detection())
139  _detection = &_tensors.back();
140  }
141  }
142  }
143 
148  void set_label(std::string label) {
149  _gst_meta->event_type = g_quark_from_string(label.c_str());
150  }
151 
157  return _gst_meta;
158  }
159 
160  protected:
170  std::vector<Tensor> _tensors;
175 };
176 
177 } // namespace GVA
GVA::AudioEvent::set_label
void set_label(std::string label)
Set AudioEvent label.
Definition: audio_event.h:148
GVA::AudioEvent::AudioEvent
AudioEvent(GstGVAAudioEventMeta *meta)
Construct AudioEvent instance from GstGVAAudioEventMeta. After this, AudioEvent will obtain all tenso...
Definition: audio_event.h:128
GVA::AudioEvent::_meta
GstGVAAudioEventMeta * _meta() const
Internal function, don't use or use with caution.
Definition: audio_event.h:156
GstGVAAudioEventMeta
Definition: gva_audio_event_meta.h:29
GVA::Tensor::confidence
double confidence() const
Get confidence of detection or classification result extracted from the tensor.
Definition: tensor.h:155
GVA::AudioEvent::label_id
int label_id() const
Get label_id from detection Tensor, last added to this AudioEvent.
Definition: audio_event.h:119
GVA::AudioEvent::_tensors
std::vector< Tensor > _tensors
vector of Tensor objects added to this AudioEvent (describing detection & inference results),...
Definition: audio_event.h:170
GVA::AudioEvent::confidence
double confidence() const
Get AudioEvent detection confidence (set by gvaaudiodetect)
Definition: audio_event.h:68
tensor.h
This file contains GVA::Tensor class which contains and describes neural network inference result.
GVA::AudioEvent::label
std::string label() const
Get AudioEvent label.
Definition: audio_event.h:59
GVA::AudioEvent::detection
Tensor detection()
Returns detection Tensor, last added to this AudioEvent. As any other Tensor, returned detection Tens...
Definition: audio_event.h:108
GVA::Tensor::label_id
int label_id() const
Get label id.
Definition: tensor.h:324
GVA::AudioEvent::_detection
Tensor * _detection
last added detection Tensor instance, defined as Tensor with name set to "detection"
Definition: audio_event.h:174
GVA::AudioEvent::segment
Segment< gulong > segment() const
Get Segment of AudioEvent as start and end timestamps, timestamps are presentation time.
Definition: audio_event.h:51
GVA::AudioEvent::_gst_meta
GstGVAAudioEventMeta * _gst_meta
GstGVAAudioEventMeta containing fields filled with detection result (produced by gvaaudiodetect eleme...
Definition: audio_event.h:165
GVA::AudioEvent::add_tensor
Tensor add_tensor(const std::string &name)
Add new tensor (inference result) to this AudioEvent with name set. To add detection tensor,...
Definition: audio_event.h:87
GVA::Tensor
This class represents tensor - map-like storage for inference result information, such as output blob...
Definition: tensor.h:38
GVA::AudioEvent
This class represents audio event - object describing audio event detection result (segment) and cont...
Definition: audio_event.h:45
GVA::Segment
Template structure for audio segment containing start, end fields.
Definition: audio_event.h:33
GVA::AudioEvent::tensors
std::vector< Tensor > tensors() const
Get all Tensor instances added to this AudioEvent.
Definition: audio_event.h:76