GStreamer Video Analytics (GVA) Plugin
|
Go to the documentation of this file.
28 #include <gst/gstbuffer.h>
29 #include <gst/video/gstvideometa.h>
30 #include <gst/video/video.h>
32 #include <opencv2/opencv.hpp>
54 std::unique_ptr<GstVideoInfo, std::function<void(GstVideoInfo *)>>
info;
66 throw std::invalid_argument(
"GVA::VideoFrame: buffer or info nullptr");
76 if (not
buffer or not caps) {
77 throw std::invalid_argument(
"GVA::VideoFrame: buffer or caps nullptr");
79 info = std::unique_ptr<GstVideoInfo, std::function<void(GstVideoInfo *)>>(gst_video_info_new(),
81 if (!gst_video_info_from_caps(
info.get(), caps)) {
82 throw std::runtime_error(
"GVA::VideoFrame: gst_video_info_from_caps failed");
94 throw std::invalid_argument(
"GVA::VideoFrame: buffer is nullptr");
98 throw std::logic_error(
"GVA::VideoFrame: video_meta() is nullptr");
100 info = std::unique_ptr<GstVideoInfo, std::function<void(GstVideoInfo *)>>(gst_video_info_new(),
101 gst_video_info_free);
103 throw std::logic_error(
"GVA::VideoFrame: gst_video_info_new() failed");
105 info->width = meta->width;
106 info->height = meta->height;
109 if (meta->stride == NULL ||
sizeof(
info->stride) <
sizeof(meta->stride)) {
110 memset(
info->stride, 0,
sizeof(
info->stride));
111 throw std::logic_error(
"GVA::VideoFrame: stride copy failed");
114 memcpy(
info->stride, meta->stride,
sizeof(meta->stride));
122 return gst_buffer_get_video_meta(
buffer);
138 return get_regions();
145 const std::vector<RegionOfInterest>
regions()
const {
146 return get_regions();
154 return get_tensors();
162 return get_tensors();
170 std::vector<std::string> json_messages;
171 GstGVAJSONMeta *meta = NULL;
172 gpointer state = NULL;
173 GType meta_api_type = g_type_from_name(GVA_JSON_META_API_NAME);
174 while ((meta = (GstGVAJSONMeta *)gst_buffer_iterate_meta_filtered(
buffer, &state, meta_api_type))) {
175 json_messages.emplace_back(meta->message);
177 return json_messages;
193 double confidence = 0.0,
bool normalized =
false) {
195 if (
info->width == 0 or
info->height == 0) {
196 throw std::logic_error(
"Failed to normalize coordinates width/height equal to 0");
204 clip_normalized_rect(x, y, w, h);
207 double _x = x *
info->width + 0.5;
208 double _y = y *
info->height + 0.5;
209 double _w = w *
info->width + 0.5;
210 double _h = h *
info->height + 0.5;
212 if (!gst_buffer_is_writable(
buffer))
213 throw std::runtime_error(
"Buffer is not writable.");
215 GstVideoRegionOfInterestMeta *meta = gst_buffer_add_video_region_of_interest_meta(
216 buffer, label.c_str(), double_to_uint(_x), double_to_uint(_y), double_to_uint(_w), double_to_uint(_h));
219 GstStructure *detection =
220 gst_structure_new(
"detection",
"x_min", G_TYPE_DOUBLE, x,
"x_max", G_TYPE_DOUBLE, x + w,
"y_min",
221 G_TYPE_DOUBLE, y,
"y_max", G_TYPE_DOUBLE, y + h, NULL);
223 gst_structure_set(detection,
"confidence", G_TYPE_DOUBLE, confidence, NULL);
225 gst_video_region_of_interest_meta_add_param(meta, detection);
235 const GstMetaInfo *meta_info = gst_meta_get_info(GVA_TENSOR_META_IMPL_NAME);
237 if (!gst_buffer_is_writable(
buffer))
238 throw std::runtime_error(
"Buffer is not writable.");
250 const GstMetaInfo *meta_info = gst_meta_get_info(GVA_JSON_META_IMPL_NAME);
252 if (!gst_buffer_is_writable(
buffer))
253 throw std::runtime_error(
"Buffer is not writable.");
255 GstGVAJSONMeta *json_meta = (GstGVAJSONMeta *)gst_buffer_add_meta(
buffer, meta_info, NULL);
256 json_meta->message = g_strdup(message.c_str());
264 if (!gst_buffer_is_writable(
buffer))
265 throw std::runtime_error(
"Buffer is not writable.");
267 if (!gst_buffer_remove_meta(
buffer, (GstMeta *)roi.
_meta())) {
268 throw std::out_of_range(
"GVA::VideoFrame: RegionOfInterest doesn't belong to this frame");
278 gpointer state = NULL;
279 while ((meta = GST_GVA_TENSOR_META_ITERATE(
buffer, &state))) {
281 if (!gst_buffer_is_writable(
buffer))
282 throw std::runtime_error(
"Buffer is not writable.");
284 if (gst_buffer_remove_meta(
buffer, (GstMeta *)meta))
288 throw std::out_of_range(
"GVA::VideoFrame: Tensor doesn't belong to this frame");
292 void clip_normalized_rect(
double &x,
double &y,
double &w,
double &h) {
293 if (!((x >= 0) && (y >= 0) && (w >= 0) && (h >= 0) && (x + w <= 1) && (y + h <= 1))) {
294 GST_DEBUG(
"ROI coordinates x=[%.5f, %.5f], y=[%.5f, %.5f] are out of range [0,1] and will be clipped", x,
297 x = (x < 0) ? 0 : (x > 1) ? 1 : x;
298 y = (y < 0) ? 0 : (y > 1) ? 1 : y;
299 w = (w < 0) ? 0 : (w > 1 - x) ? 1 - x : w;
300 h = (h < 0) ? 0 : (h > 1 - y) ? 1 - y : h;
304 unsigned int double_to_uint(
double val) {
305 unsigned int max = std::numeric_limits<unsigned int>::max();
306 unsigned int min = std::numeric_limits<unsigned int>::min();
307 return (val < min) ? min : ((val > max) ? max :
static_cast<unsigned int>(val));
310 std::vector<RegionOfInterest> get_regions()
const {
311 std::vector<RegionOfInterest>
regions;
312 GstMeta *meta = NULL;
313 gpointer state = NULL;
315 while ((meta = gst_buffer_iterate_meta_filtered(
buffer, &state, GST_VIDEO_REGION_OF_INTEREST_META_API_TYPE)))
316 regions.emplace_back((GstVideoRegionOfInterestMeta *)meta);
320 std::vector<Tensor> get_tensors()
const {
323 gpointer state = NULL;
324 GType meta_api_type = g_type_from_name(
"GstGVATensorMetaAPI");
GstBuffer * buffer
GstBuffer with inference results metadata attached (Gstreamer pipeline's GstBuffer,...
Definition: video_frame.h:49
VideoFrame(GstBuffer *buffer, GstVideoInfo *info)
Construct VideoFrame instance from GstBuffer and GstVideoInfo. This is preferred way of creating Vide...
Definition: video_frame.h:63
std::vector< RegionOfInterest > regions()
Get RegionOfInterest objects attached to VideoFrame.
Definition: video_frame.h:137
void remove_region(const RegionOfInterest &roi)
Remove RegionOfInterest.
Definition: video_frame.h:263
RegionOfInterest add_region(double x, double y, double w, double h, std::string label=std::string(), double confidence=0.0, bool normalized=false)
Attach RegionOfInterest to this VideoFrame. This function takes ownership of region_tensor,...
Definition: video_frame.h:192
void add_message(const std::string &message)
Attach message to this VideoFrame.
Definition: video_frame.h:249
GstVideoMeta * video_meta()
Get video metadata of buffer.
Definition: video_frame.h:121
const std::vector< Tensor > tensors() const
Get Tensor objects attached to VideoFrame.
Definition: video_frame.h:161
This file contains GVA::RegionOfInterest class to control region of interest for particular GVA::Vide...
GstVideoInfo * video_info()
Get GstVideoInfo of this VideoFrame. This is preferrable way of getting any image information.
Definition: video_frame.h:129
std::vector< Tensor > tensors()
Get Tensor objects attached to VideoFrame.
Definition: video_frame.h:153
std::unique_ptr< GstVideoInfo, std::function< void(GstVideoInfo *)> > info
GstVideoInfo containing actual video information for this VideoFrame.
Definition: video_frame.h:54
const std::vector< RegionOfInterest > regions() const
Get RegionOfInterest objects attached to VideoFrame.
Definition: video_frame.h:145
VideoFrame(GstBuffer *buffer)
Construct VideoFrame instance from GstBuffer. Video information will be obtained from buffer....
Definition: video_frame.h:92
GstVideoRegionOfInterestMeta * _meta() const
Internal function, don't use or use with caution.
Definition: region_of_interest.h:189
std::vector< std::string > messages()
Get messages attached to this VideoFrame.
Definition: video_frame.h:169
GstStructure * _structure
ptr to GstStructure that contains all tensor (inference results) data & info.
Definition: tensor.h:358
This class represents region of interest - object describing detection result (bounding box) and cont...
Definition: region_of_interest.h:43
Tensor add_tensor()
Attach empty Tensor to this VideoFrame.
Definition: video_frame.h:234
This class represents tensor - map-like storage for inference result information, such as output blob...
Definition: tensor.h:38
VideoFrame(GstBuffer *buffer, const GstCaps *caps)
Construct VideoFrame instance from GstBuffer and GstCaps.
Definition: video_frame.h:75
This class represents video frame - object for working with RegionOfInterest and Tensor objects which...
Definition: video_frame.h:43
void remove_tensor(const Tensor &tensor)
Remove Tensor.
Definition: video_frame.h:276