-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgstinceptionv2.c
263 lines (215 loc) · 7.57 KB
/
gstinceptionv2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* GStreamer
* Copyright (C) 2018-2020 RidgeRun <support@ridgerun.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* SECTION:element-gstinceptionv2
*
* The inceptionv2 element allows the user to infer/execute a pretrained model
* based on the GoogLeNet (Inception v1 or Inception v2) architectures on
* incoming image frames.
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch-1.0 -v videotestsrc ! inceptionv2 ! xvimagesink
* ]|
* Process video frames from the camera using a GoogLeNet (Inception v1 or
* Inception v2) model.
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstinceptionv2.h"
#include "gst/r2inference/gstinferencemeta.h"
#include <string.h>
#include "gst/r2inference/gstinferencepreprocess.h"
#include "gst/r2inference/gstinferencepostprocess.h"
#include "gst/r2inference/gstinferencedebug.h"
GST_DEBUG_CATEGORY_STATIC (gst_inceptionv2_debug_category);
#define GST_CAT_DEFAULT gst_inceptionv2_debug_category
#define MEAN 128.0
#define STD 1/128.0
#define MODEL_CHANNELS 3
/* prototypes */
static void gst_inceptionv2_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_inceptionv2_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_inceptionv2_dispose (GObject * object);
static void gst_inceptionv2_finalize (GObject * object);
static gboolean gst_inceptionv2_preprocess (GstVideoInference * vi,
GstVideoFrame * inframe, GstVideoFrame * outframe);
static gboolean gst_inceptionv2_postprocess (GstVideoInference * vi,
const gpointer prediction, gsize predsize, GstMeta * meta_model,
GstVideoInfo * info_model, gboolean * valid_prediction,
gchar ** labels_list, gint num_labels);
static gboolean gst_inceptionv2_start (GstVideoInference * vi);
static gboolean gst_inceptionv2_stop (GstVideoInference * vi);
enum
{
PROP_0
};
/* pad templates */
#define CAPS \
"video/x-raw, " \
"width=224, " \
"height=224, " \
"format={RGB, RGBx, RGBA, BGR, BGRx, BGRA, xRGB, ARGB, xBGR, ABGR}"
static GstStaticPadTemplate sink_model_factory =
GST_STATIC_PAD_TEMPLATE ("sink_model",
GST_PAD_SINK,
GST_PAD_REQUEST,
GST_STATIC_CAPS (CAPS)
);
static GstStaticPadTemplate src_model_factory =
GST_STATIC_PAD_TEMPLATE ("src_model",
GST_PAD_SRC,
GST_PAD_REQUEST,
GST_STATIC_CAPS (CAPS)
);
struct _GstInceptionv2
{
GstVideoInference parent;
};
struct _GstInceptionv2Class
{
GstVideoInferenceClass parent;
};
/* class initialization */
G_DEFINE_TYPE_WITH_CODE (GstInceptionv2, gst_inceptionv2,
GST_TYPE_VIDEO_INFERENCE,
GST_DEBUG_CATEGORY_INIT (gst_inceptionv2_debug_category, "inceptionv2", 0,
"debug category for inceptionv2 element"));
static void
gst_inceptionv2_class_init (GstInceptionv2Class * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstVideoInferenceClass *vi_class = GST_VIDEO_INFERENCE_CLASS (klass);
gst_element_class_add_static_pad_template (element_class,
&sink_model_factory);
gst_element_class_add_static_pad_template (element_class, &src_model_factory);
gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
"inceptionv2", "Filter",
"Infers incoming image frames using a pretrained GoogLeNet (Inception v1 or Inception v2) model",
"Carlos Rodriguez <carlos.rodriguez@ridgerun.com> \n\t\t\t"
" Jose Jimenez <jose.jimenez@ridgerun.com> \n\t\t\t"
" Michael Gruner <michael.gruner@ridgerun.com>");
gobject_class->set_property = gst_inceptionv2_set_property;
gobject_class->get_property = gst_inceptionv2_get_property;
gobject_class->dispose = gst_inceptionv2_dispose;
gobject_class->finalize = gst_inceptionv2_finalize;
vi_class->start = GST_DEBUG_FUNCPTR (gst_inceptionv2_start);
vi_class->stop = GST_DEBUG_FUNCPTR (gst_inceptionv2_stop);
vi_class->preprocess = GST_DEBUG_FUNCPTR (gst_inceptionv2_preprocess);
vi_class->postprocess = GST_DEBUG_FUNCPTR (gst_inceptionv2_postprocess);
}
static void
gst_inceptionv2_init (GstInceptionv2 * inceptionv2)
{
}
void
gst_inceptionv2_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstInceptionv2 *inceptionv2 = GST_INCEPTIONV2 (object);
GST_DEBUG_OBJECT (inceptionv2, "set_property");
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_inceptionv2_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstInceptionv2 *inceptionv2 = GST_INCEPTIONV2 (object);
GST_DEBUG_OBJECT (inceptionv2, "get_property");
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_inceptionv2_dispose (GObject * object)
{
GstInceptionv2 *inceptionv2 = GST_INCEPTIONV2 (object);
GST_DEBUG_OBJECT (inceptionv2, "dispose");
/* clean up as possible. may be called multiple times */
G_OBJECT_CLASS (gst_inceptionv2_parent_class)->dispose (object);
}
void
gst_inceptionv2_finalize (GObject * object)
{
GstInceptionv2 *inceptionv2 = GST_INCEPTIONV2 (object);
GST_DEBUG_OBJECT (inceptionv2, "finalize");
/* clean up object here */
G_OBJECT_CLASS (gst_inceptionv2_parent_class)->finalize (object);
}
static gboolean
gst_inceptionv2_preprocess (GstVideoInference * vi,
GstVideoFrame * inframe, GstVideoFrame * outframe)
{
GST_LOG_OBJECT (vi, "Preprocess");
return gst_normalize (inframe, outframe, MEAN, STD, MODEL_CHANNELS);
}
static gboolean
gst_inceptionv2_postprocess (GstVideoInference * vi, const gpointer prediction,
gsize predsize, GstMeta * meta_model, GstVideoInfo * info_model,
gboolean * valid_prediction, gchar ** labels_list, gint num_labels)
{
GstInferenceMeta *imeta = NULL;
GstInferenceClassification *c = NULL;
GstInferencePrediction *root = NULL;
gboolean ret = TRUE;
g_return_val_if_fail (vi != NULL, FALSE);
g_return_val_if_fail (meta_model != NULL, FALSE);
g_return_val_if_fail (info_model != NULL, FALSE);
GST_LOG_OBJECT (vi, "Postprocess Meta");
imeta = (GstInferenceMeta *) meta_model;
root = imeta->prediction;
if (!root) {
GST_ERROR_OBJECT (vi, "Prediction is not part of the Inference Meta");
ret = FALSE;
goto out;
}
c = gst_create_class_from_prediction (vi, prediction, predsize, labels_list,
num_labels);
gst_inference_prediction_append_classification (root, c);
gst_inference_print_predictions (vi, gst_inceptionv2_debug_category, imeta);
*valid_prediction = TRUE;
out:
return ret;
}
static gboolean
gst_inceptionv2_start (GstVideoInference * vi)
{
GST_INFO_OBJECT (vi, "Starting Inception v2");
return TRUE;
}
static gboolean
gst_inceptionv2_stop (GstVideoInference * vi)
{
GST_INFO_OBJECT (vi, "Stopping Inception v2");
return TRUE;
}