Skip to content

Commit

Permalink
Merge branch 'dev-0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
GFallasRR committed Jun 27, 2019
2 parents aca30ba + b22ac76 commit 4a11323
Show file tree
Hide file tree
Showing 31 changed files with 5,077 additions and 887 deletions.
479 changes: 479 additions & 0 deletions COPYING

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dnl please read gstreamer/docs/random/autotools before changing this file
dnl initialize autoconf
dnl releases only do -Wall, git and prerelease does -Werror too
dnl use a three digit version number for releases, and four for git/pre
AC_INIT([GStreamer Inference],[0.5.0.1],[https://github.com/RidgeRun/gst-inference/issues],[gst-inference])
AC_INIT([GStreamer Inference],[0.6.0.1],[https://github.com/RidgeRun/gst-inference/issues],[gst-inference])

AG_GST_INIT

Expand Down
10 changes: 8 additions & 2 deletions ext/r2inference/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
plugin_LTLIBRARIES = libgstinference.la

libgstinference_la_SOURCES = \
gstinceptionv1.c \
gstinceptionv2.c \
gstinceptionv3.c \
gstinceptionv4.c \
gstinference.c \
gsttinyyolov2.c \
gsttinyyolov3.c \
gstfacenetv1.c \
gstresnet50v1.c
gstresnet50v1.c \
gstmobilenetv2.c

libgstinference_la_CFLAGS = \
$(GST_CFLAGS) \
Expand Down Expand Up @@ -37,9 +40,12 @@ libgstinference_la_LIBTOOLFLAGS = \
$(GST_PLUGIN_LIBTOOLFLAGS)

noinst_HEADERS = \
gstinceptionv1.h \
gstinceptionv2.h \
gstinceptionv3.h \
gstinceptionv4.h \
gsttinyyolov2.h \
gsttinyyolov3.h \
gstfacenetv1.h \
gstresnet50v1.h
gstresnet50v1.h \
gstmobilenetv2.h
85 changes: 10 additions & 75 deletions ext/r2inference/gstfacenetv1.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
#include "gst/r2inference/gstinferencemeta.h"
#include <string.h>
#include <math.h>
#include "gst/r2inference/gstinferencepreprocess.h"
#include "gst/r2inference/gstinferencepostprocess.h"
#include "gst/r2inference/gstinferencedebug.h"

GST_DEBUG_CATEGORY_STATIC (gst_facenetv1_debug_category);
#define GST_CAT_DEFAULT gst_facenetv1_debug_category

#define MODEL_CHANNELS 3

/* prototypes */
static void gst_facenetv1_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
Expand Down Expand Up @@ -195,66 +200,8 @@ static gboolean
gst_facenetv1_preprocess (GstVideoInference * vi,
GstVideoFrame * inframe, GstVideoFrame * outframe)
{
gint i, j, pixel_stride, width, height, channels;
gfloat mean, std, variance, sum, normalized, R, G, B;

GST_LOG_OBJECT (vi, "Preprocess");
channels = GST_VIDEO_FRAME_N_COMPONENTS (inframe);
pixel_stride = GST_VIDEO_FRAME_COMP_STRIDE (inframe, 0) / channels;
width = GST_VIDEO_FRAME_WIDTH (inframe);
height = GST_VIDEO_FRAME_HEIGHT (inframe);

sum = 0;
normalized = 0;

for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
sum =
sum + (((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 0]);
sum =
sum + (((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 1]);
sum =
sum + (((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 2]);
}
}

mean = sum / (float) (width * height * channels);

for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
R = (gfloat) ((((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 0])) - mean;
G = (gfloat) ((((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 1])) - mean;
B = (gfloat) ((((guchar *) inframe->data[0])[(i * pixel_stride +
j) * channels + 2])) - mean;
normalized = normalized + pow (R, 2);
normalized = normalized + pow (G, 2);
normalized = normalized + pow (B, 2);
}
}

variance = normalized / (float) (width * height * channels);
std = sqrt (variance);

for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
((gfloat *) outframe->data[0])[(i * width + j) * channels + 0] =
(((guchar *) inframe->data[0])[(i * pixel_stride + j) * channels +
0] - mean) / std;
((gfloat *) outframe->data[0])[(i * width + j) * channels + 1] =
(((guchar *) inframe->data[0])[(i * pixel_stride + j) * channels +
1] - mean) / std;
((gfloat *) outframe->data[0])[(i * width + j) * channels + 2] =
(((guchar *) inframe->data[0])[(i * pixel_stride + j) * channels +
2] - mean) / std;
}
}

return TRUE;
return gst_normalize_face (inframe, outframe, MODEL_CHANNELS);
}

static gboolean
Expand All @@ -263,25 +210,13 @@ gst_facenetv1_postprocess (GstVideoInference * vi, const gpointer prediction,
gboolean * valid_prediction)
{
GstClassificationMeta *class_meta = (GstClassificationMeta *) meta_model;
GstDebugLevel level;
GstDebugLevel gst_debug_level = GST_LEVEL_LOG;
GST_LOG_OBJECT (vi, "Postprocess");

class_meta->num_labels = predsize / sizeof (gfloat);
class_meta->label_probs =
g_malloc (class_meta->num_labels * sizeof (gdouble));
for (gint i = 0; i < class_meta->num_labels; ++i) {
class_meta->label_probs[i] = (gdouble) ((gfloat *) prediction)[i];
}

/* Only display vector if debug level >= 6 */
level = gst_debug_category_get_threshold (gst_facenetv1_debug_category);
if (level >= GST_LEVEL_LOG) {
for (gint i = 0; i < class_meta->num_labels; ++i) {
gfloat current = ((gfloat *) prediction)[i];
GST_LOG_OBJECT (vi, "Output vector element %i : (%f)", i, current);
}
}
gst_fill_classification_meta (class_meta, prediction, predsize);

gst_inference_print_embedding (vi, gst_facenetv1_debug_category, class_meta,
prediction, gst_debug_level);
*valid_prediction = TRUE;
return TRUE;
}
Expand Down
178 changes: 178 additions & 0 deletions ext/r2inference/gstinceptionv1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* GStreamer
* Copyright (C) 2019 RidgeRun
*
* 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-gstinceptionv1
*
* The inceptionv1 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 ! inceptionv1 ! 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 "gstinceptionv1.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_inceptionv1_debug_category);
#define GST_CAT_DEFAULT gst_inceptionv1_debug_category

#define MEAN 128.0
#define STD 1/128.0
#define MODEL_CHANNELS 3

static gboolean gst_inceptionv1_preprocess (GstVideoInference * vi,
GstVideoFrame * inframe, GstVideoFrame * outframe);
static gboolean gst_inceptionv1_postprocess (GstVideoInference * vi,
const gpointer prediction, gsize predsize, GstMeta * meta_model,
GstVideoInfo * info_model, gboolean * valid_prediction);
static gboolean gst_inceptionv1_start (GstVideoInference * vi);
static gboolean gst_inceptionv1_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 _GstInceptionv1
{
GstVideoInference parent;
};

struct _GstInceptionv1Class
{
GstVideoInferenceClass parent;
};

/* class initialization */

G_DEFINE_TYPE_WITH_CODE (GstInceptionv1, gst_inceptionv1,
GST_TYPE_VIDEO_INFERENCE,
GST_DEBUG_CATEGORY_INIT (gst_inceptionv1_debug_category, "inceptionv1", 0,
"debug category for inceptionv1 element"));

static void
gst_inceptionv1_class_init (GstInceptionv1Class * 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),
"inceptionv1", "Filter",
"Infers incoming image frames using a pretrained GoogLeNet (Inception v1 or Inception v2) model",
"Carlos Rodriguez <[email protected]> \n\t\t\t"
" Jose Jimenez <[email protected]> \n\t\t\t"
" Michael Gruner <[email protected]> \n\t\t\t"
" Mauricio Montero <[email protected]>");

vi_class->start = GST_DEBUG_FUNCPTR (gst_inceptionv1_start);
vi_class->stop = GST_DEBUG_FUNCPTR (gst_inceptionv1_stop);
vi_class->preprocess = GST_DEBUG_FUNCPTR (gst_inceptionv1_preprocess);
vi_class->postprocess = GST_DEBUG_FUNCPTR (gst_inceptionv1_postprocess);
vi_class->inference_meta_info = gst_classification_meta_get_info ();
}

static void
gst_inceptionv1_init (GstInceptionv1 * inceptionv1)
{
}

static gboolean
gst_inceptionv1_preprocess (GstVideoInference * vi,
GstVideoFrame * inframe, GstVideoFrame * outframe)
{
GST_LOG_OBJECT (vi, "Preprocess");
return gst_normalize (inframe, outframe, MEAN, STD, MODEL_CHANNELS);
}

static gboolean
gst_inceptionv1_postprocess (GstVideoInference * vi, const gpointer prediction,
gsize predsize, GstMeta * meta_model, GstVideoInfo * info_model,
gboolean * valid_prediction)
{
GstClassificationMeta *class_meta = (GstClassificationMeta *) meta_model;
GstDebugLevel gst_debug_level = GST_LEVEL_LOG;
GST_LOG_OBJECT (vi, "Postprocess");

gst_fill_classification_meta (class_meta, prediction, predsize);

gst_inference_print_highest_probability (vi, gst_inceptionv1_debug_category,
class_meta, prediction, gst_debug_level);

*valid_prediction = TRUE;
return TRUE;
}

static gboolean
gst_inceptionv1_start (GstVideoInference * vi)
{
GST_INFO_OBJECT (vi, "Starting Inception v1");

return TRUE;
}

static gboolean
gst_inceptionv1_stop (GstVideoInference * vi)
{
GST_INFO_OBJECT (vi, "Stopping Inception v1");

return TRUE;
}
34 changes: 34 additions & 0 deletions ext/r2inference/gstinceptionv1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* GStreamer
* Copyright (C) 2018 RidgeRun
*
* 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.
*
*/

#ifndef _GST_INCEPTIONV1_H_
#define _GST_INCEPTIONV1_H_

#include <gst/r2inference/gstvideoinference.h>

G_BEGIN_DECLS

#define GST_TYPE_INCEPTIONV1 gst_inceptionv1_get_type ()
G_DECLARE_FINAL_TYPE (GstInceptionv1, gst_inceptionv1, GST, INCEPTIONV1, GstVideoInference)

G_END_DECLS

#endif
Loading

0 comments on commit 4a11323

Please sign in to comment.