|
| 1 | +/* |
| 2 | + * GStreamer |
| 3 | + * Copyright (C) 2019 RidgeRun |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Library General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Library General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Library General Public |
| 16 | + * License along with this library; if not, write to the |
| 17 | + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | + * Boston, MA 02111-1307, USA. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * SECTION:element-gstinceptionv1 |
| 24 | + * |
| 25 | + * The inceptionv1 element allows the user to infer/execute a pretrained model |
| 26 | + * based on the GoogLeNet (Inception v1 or Inception v2) architectures on |
| 27 | + * incoming image frames. |
| 28 | + * |
| 29 | + * <refsect2> |
| 30 | + * <title>Example launch line</title> |
| 31 | + * |[ |
| 32 | + * gst-launch-1.0 -v videotestsrc ! inceptionv1 ! xvimagesink |
| 33 | + * ]| |
| 34 | + * Process video frames from the camera using a GoogLeNet (Inception v1 or |
| 35 | + * Inception v2) model. |
| 36 | + * </refsect2> |
| 37 | + */ |
| 38 | + |
| 39 | +#ifdef HAVE_CONFIG_H |
| 40 | +#include "config.h" |
| 41 | +#endif |
| 42 | + |
| 43 | +#include "gstinceptionv1.h" |
| 44 | +#include "gst/r2inference/gstinferencemeta.h" |
| 45 | +#include <string.h> |
| 46 | +#include "gst/r2inference/gstinferencepreprocess.h" |
| 47 | +#include "gst/r2inference/gstinferencepostprocess.h" |
| 48 | +#include "gst/r2inference/gstinferencedebug.h" |
| 49 | + |
| 50 | +GST_DEBUG_CATEGORY_STATIC (gst_inceptionv1_debug_category); |
| 51 | +#define GST_CAT_DEFAULT gst_inceptionv1_debug_category |
| 52 | + |
| 53 | +#define MEAN 128.0 |
| 54 | +#define STD 1/128.0 |
| 55 | +#define MODEL_CHANNELS 3 |
| 56 | + |
| 57 | +static gboolean gst_inceptionv1_preprocess (GstVideoInference * vi, |
| 58 | + GstVideoFrame * inframe, GstVideoFrame * outframe); |
| 59 | +static gboolean gst_inceptionv1_postprocess (GstVideoInference * vi, |
| 60 | + const gpointer prediction, gsize predsize, GstMeta * meta_model, |
| 61 | + GstVideoInfo * info_model, gboolean * valid_prediction); |
| 62 | +static gboolean gst_inceptionv1_start (GstVideoInference * vi); |
| 63 | +static gboolean gst_inceptionv1_stop (GstVideoInference * vi); |
| 64 | + |
| 65 | +enum |
| 66 | +{ |
| 67 | + PROP_0 |
| 68 | +}; |
| 69 | + |
| 70 | +/* pad templates */ |
| 71 | +#define CAPS \ |
| 72 | + "video/x-raw, " \ |
| 73 | + "width=224, " \ |
| 74 | + "height=224, " \ |
| 75 | + "format={RGB, RGBx, RGBA, BGR, BGRx, BGRA, xRGB, ARGB, xBGR, ABGR}" |
| 76 | + |
| 77 | +static GstStaticPadTemplate sink_model_factory = |
| 78 | +GST_STATIC_PAD_TEMPLATE ("sink_model", |
| 79 | + GST_PAD_SINK, |
| 80 | + GST_PAD_REQUEST, |
| 81 | + GST_STATIC_CAPS (CAPS) |
| 82 | + ); |
| 83 | + |
| 84 | +static GstStaticPadTemplate src_model_factory = |
| 85 | +GST_STATIC_PAD_TEMPLATE ("src_model", |
| 86 | + GST_PAD_SRC, |
| 87 | + GST_PAD_REQUEST, |
| 88 | + GST_STATIC_CAPS (CAPS) |
| 89 | + ); |
| 90 | + |
| 91 | +struct _GstInceptionv1 |
| 92 | +{ |
| 93 | + GstVideoInference parent; |
| 94 | +}; |
| 95 | + |
| 96 | +struct _GstInceptionv1Class |
| 97 | +{ |
| 98 | + GstVideoInferenceClass parent; |
| 99 | +}; |
| 100 | + |
| 101 | +/* class initialization */ |
| 102 | + |
| 103 | +G_DEFINE_TYPE_WITH_CODE (GstInceptionv1, gst_inceptionv1, |
| 104 | + GST_TYPE_VIDEO_INFERENCE, |
| 105 | + GST_DEBUG_CATEGORY_INIT (gst_inceptionv1_debug_category, "inceptionv1", 0, |
| 106 | + "debug category for inceptionv1 element")); |
| 107 | + |
| 108 | +static void |
| 109 | +gst_inceptionv1_class_init (GstInceptionv1Class * klass) |
| 110 | +{ |
| 111 | + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 112 | + GstVideoInferenceClass *vi_class = GST_VIDEO_INFERENCE_CLASS (klass); |
| 113 | + |
| 114 | + gst_element_class_add_static_pad_template (element_class, |
| 115 | + &sink_model_factory); |
| 116 | + gst_element_class_add_static_pad_template (element_class, &src_model_factory); |
| 117 | + |
| 118 | + gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass), |
| 119 | + "inceptionv1", "Filter", |
| 120 | + "Infers incoming image frames using a pretrained GoogLeNet (Inception v1 or Inception v2) model", |
| 121 | + "Carlos Rodriguez <[email protected]> \n\t\t\t" |
| 122 | + " Jose Jimenez <[email protected]> \n\t\t\t" |
| 123 | + " Michael Gruner <[email protected]> \n\t\t\t" |
| 124 | + " Mauricio Montero <[email protected]>"); |
| 125 | + |
| 126 | + vi_class->start = GST_DEBUG_FUNCPTR (gst_inceptionv1_start); |
| 127 | + vi_class->stop = GST_DEBUG_FUNCPTR (gst_inceptionv1_stop); |
| 128 | + vi_class->preprocess = GST_DEBUG_FUNCPTR (gst_inceptionv1_preprocess); |
| 129 | + vi_class->postprocess = GST_DEBUG_FUNCPTR (gst_inceptionv1_postprocess); |
| 130 | + vi_class->inference_meta_info = gst_classification_meta_get_info (); |
| 131 | +} |
| 132 | + |
| 133 | +static void |
| 134 | +gst_inceptionv1_init (GstInceptionv1 * inceptionv1) |
| 135 | +{ |
| 136 | +} |
| 137 | + |
| 138 | +static gboolean |
| 139 | +gst_inceptionv1_preprocess (GstVideoInference * vi, |
| 140 | + GstVideoFrame * inframe, GstVideoFrame * outframe) |
| 141 | +{ |
| 142 | + GST_LOG_OBJECT (vi, "Preprocess"); |
| 143 | + return gst_normalize (inframe, outframe, MEAN, STD, MODEL_CHANNELS); |
| 144 | +} |
| 145 | + |
| 146 | +static gboolean |
| 147 | +gst_inceptionv1_postprocess (GstVideoInference * vi, const gpointer prediction, |
| 148 | + gsize predsize, GstMeta * meta_model, GstVideoInfo * info_model, |
| 149 | + gboolean * valid_prediction) |
| 150 | +{ |
| 151 | + GstClassificationMeta *class_meta = (GstClassificationMeta *) meta_model; |
| 152 | + GstDebugLevel gst_debug_level = GST_LEVEL_LOG; |
| 153 | + GST_LOG_OBJECT (vi, "Postprocess"); |
| 154 | + |
| 155 | + gst_fill_classification_meta (class_meta, prediction, predsize); |
| 156 | + |
| 157 | + gst_inference_print_highest_probability (vi, gst_inceptionv1_debug_category, |
| 158 | + class_meta, prediction, gst_debug_level); |
| 159 | + |
| 160 | + *valid_prediction = TRUE; |
| 161 | + return TRUE; |
| 162 | +} |
| 163 | + |
| 164 | +static gboolean |
| 165 | +gst_inceptionv1_start (GstVideoInference * vi) |
| 166 | +{ |
| 167 | + GST_INFO_OBJECT (vi, "Starting Inception v1"); |
| 168 | + |
| 169 | + return TRUE; |
| 170 | +} |
| 171 | + |
| 172 | +static gboolean |
| 173 | +gst_inceptionv1_stop (GstVideoInference * vi) |
| 174 | +{ |
| 175 | + GST_INFO_OBJECT (vi, "Stopping Inception v1"); |
| 176 | + |
| 177 | + return TRUE; |
| 178 | +} |
0 commit comments