[TensorDec] add init functions of bounding boxes mode
authorjinhyuck-park <jinhyuck83.park@samsung.com>
Thu, 1 Nov 2018 04:39:05 +0000 (13:39 +0900)
committerMyungJoo Ham <myungjoo.ham@gmail.com>
Mon, 5 Nov 2018 01:10:59 +0000 (10:10 +0900)
add initial functions of tensor decoder bounding boxes

Signed-off-by: jinhyuck-park <jinhyuck83.park@samsung.com>
gst/tensor_decoder/tensordec.c
gst/tensor_decoder/tensordec.h

index 80a73b9..066181e 100644 (file)
@@ -150,6 +150,25 @@ gst_tensordec_image_labeling_init (Mode_image_labeling * mode_image_label)
 }
 
 /**
+ * @brief initialize data in tensor decoder image labeling info structure.
+ */
+static void
+gst_tensordec_bounding_boxes_init (Mode_boundig_boxes * mode_boundig_boxes)
+{
+  mode_boundig_boxes->label_path = NULL;
+  mode_boundig_boxes->labels = NULL;
+  mode_boundig_boxes->total_labels = 0;
+  mode_boundig_boxes->box_prior_path = NULL;
+  int i, j;
+
+  for (i = 0; i < BOX_SIZE; i++) {
+    for (j = 0; j < DETECTION_MAX; j++) {
+      mode_boundig_boxes->box_priors[i][j] = 0;
+    }
+  }
+}
+
+/**
  * @brief set label info data for Tensor decoder.
  */
 static gboolean
@@ -157,7 +176,7 @@ gst_set_mode_image_label_info (GstTensorDec * self)
 {
   FILE *fp;
 
-  if ((fp = fopen (self->tensordec_image_label.label_path, "r")) != NULL) {
+  if ((fp = fopen (self->image_labeling.label_path, "r")) != NULL) {
     char *line = NULL;
     size_t len = 0;
     ssize_t read;
@@ -165,8 +184,8 @@ gst_set_mode_image_label_info (GstTensorDec * self)
 
     while ((read = getline (&line, &len, fp)) != -1) {
       label = g_strdup ((gchar *) line);
-      self->tensordec_image_label.labels =
-          g_list_append (self->tensordec_image_label.labels, label);
+      self->image_labeling.labels =
+          g_list_append (self->image_labeling.labels, label);
     }
 
     if (line) {
@@ -179,9 +198,103 @@ gst_set_mode_image_label_info (GstTensorDec * self)
     return FALSE;
   }
 
-  self->tensordec_image_label.total_labels =
-      g_list_length (self->tensordec_image_label.labels);
+  self->image_labeling.total_labels =
+      g_list_length (self->image_labeling.labels);
+  err_print ("finished to load labels");
+  return TRUE;
+}
+
+/**
+ * @brief Read strings from file.
+ */
+static gboolean
+read_lines (const gchar * file_name, GList ** lines)
+{
+/** 
+*Not imployed yet TBD
+*/
+
+  return TRUE;
+}
+
+/**
+ * @brief Load box priors.
+ */
+static gboolean
+gst_tensordec_load_box_priors (GstTensorDec * self)
+{
+  GList *box_priors = NULL;
+  gchar *box_row;
+  int row;
+
+  g_return_val_if_fail (self != NULL, FALSE);
+  g_return_val_if_fail (read_lines (self->bounding_boxes.box_prior_path,
+          &box_priors), FALSE);
+
+  for (row = 0; row < BOX_SIZE; row++) {
+    int column = 0;
+    int i = 0, j = 0;
+    char buff[11];
+
+    memset (buff, 0, 11);
+    box_row = (gchar *) g_list_nth_data (box_priors, row);
+
+    while ((box_row[i] != '\n') && (box_row[i] != '\0')) {
+      if (box_row[i] != ' ') {
+        buff[j] = box_row[i];
+        j++;
+      } else {
+        if (j != 0) {
+          self->bounding_boxes.box_priors[row][column++] = atof (buff);
+          memset (buff, 0, 11);
+        }
+        j = 0;
+      }
+      i++;
+    }
+
+    self->bounding_boxes.box_priors[row][column++] = atof (buff);
+  }
+
+  g_list_free (box_priors);
+  return TRUE;
+}
+
+/**
+ * @brief set bounding boxes info data for Tensor decoder.
+ */
+static gboolean
+gst_set_mode_boundig_boxes_info (GstTensorDec * self)
+{
+  FILE *fp;
+
+  if ((fp = fopen (self->bounding_boxes.label_path, "r")) != NULL) {
+    char *line = NULL;
+    size_t len = 0;
+    ssize_t read;
+    gchar *label;
+
+    while ((read = getline (&line, &len, fp)) != -1) {
+      label = g_strdup ((gchar *) line);
+      self->bounding_boxes.labels =
+          g_list_append (self->bounding_boxes.labels, label);
+    }
+
+    if (line) {
+      free (line);
+    }
+
+    fclose (fp);
+  } else {
+    err_print ("cannot find label file of boundig boxes in tensor decoder");
+    return FALSE;
+  }
+
+  self->bounding_boxes.total_labels =
+      g_list_length (self->bounding_boxes.labels);
   err_print ("finished to load labels");
+
+  gst_tensordec_load_box_priors (self);
   return TRUE;
 }
 
@@ -474,7 +587,26 @@ gst_tensordec_init (GstTensorDec * self)
   self->output_type = OUTPUT_UNKNOWN;
   self->mode = DIRECT_VIDEO;
   gst_tensor_config_init (&self->tensor_config);
-  gst_tensordec_image_labeling_init (&self->tensordec_image_label);
+}
+
+/**
+ * @brief initialize the new element
+ * instantiate pads and add them to element
+ * set pad calback functions
+ * initialize instance structure
+ */
+static void
+gst_tensordec_mode_init (GstTensorDec * self)
+{
+  if (self->mode == IMAGE_LABELING) {
+
+    gst_tensordec_image_labeling_init (&self->image_labeling);
+
+  } else if (self->mode == BOUNDING_BOXES) {
+
+    gst_tensordec_bounding_boxes_init (&self->bounding_boxes);
+
+  }
 }
 
 /**
@@ -502,8 +634,13 @@ gst_tensordec_set_property (GObject * object, guint prop_id,
       break;
     case PROP_MODE_OPTION1:
       if (self->mode == IMAGE_LABELING) {
-        self->tensordec_image_label.label_path = g_value_dup_string (value);
+        gst_tensordec_mode_init (self);
+        self->image_labeling.label_path = g_value_dup_string (value);
         gst_set_mode_image_label_info (self);
+      } else if (self->mode == BOUNDING_BOXES) {
+        gst_tensordec_mode_init (self);
+        self->bounding_boxes.label_path = g_value_dup_string (value);
+        gst_set_mode_boundig_boxes_info (self);
       }
       break;
     default:
@@ -512,6 +649,7 @@ gst_tensordec_set_property (GObject * object, guint prop_id,
   }
 }
 
+
 /**
  * @brief Get property (GObject vmethod)
  */
@@ -530,7 +668,7 @@ gst_tensordec_get_property (GObject * object, guint prop_id,
       g_value_set_string (value, mode_names[self->mode]);
       break;
     case PROP_MODE_OPTION1:
-      g_value_set_string (value, self->tensordec_image_label.label_path);
+      g_value_set_string (value, self->image_labeling.label_path);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -679,7 +817,7 @@ gst_tensordec_update_top_label_index (GstTensorDec * self,
   guint8 max_score = 0;
 
   g_return_val_if_fail (scores != NULL, -1);
-  g_return_val_if_fail (len == self->tensordec_image_label.total_labels, -1);
+  g_return_val_if_fail (len == self->image_labeling.total_labels, -1);
 
   for (i = 0; i < len; i++) {
     if (scores[i] > 0 && scores[i] > max_score) {
@@ -701,13 +839,12 @@ gst_get_image_label (GstTensorDec * self, gint label)
   guint length;
   gint check_label = label;
   g_return_val_if_fail (self != NULL, NULL);
-  g_return_val_if_fail (self->tensordec_image_label.labels != NULL, NULL);
+  g_return_val_if_fail (self->image_labeling.labels != NULL, NULL);
 
-  length = g_list_length (self->tensordec_image_label.labels);
+  length = g_list_length (self->image_labeling.labels);
   g_return_val_if_fail (check_label >= 0 && check_label < length, NULL);
 
-  return (gchar *) g_list_nth_data
-      (self->tensordec_image_label.labels, check_label);
+  return (gchar *) g_list_nth_data (self->image_labeling.labels, check_label);
 }
 
 
@@ -905,6 +1042,7 @@ gst_tensordec_transform_caps (GstBaseTransform * trans,
    * If direction is sink, check src. Depending on sink's format, we could choose video or audio.
    * Currently video/x-raw and audio/x-raw supported.
    */
+
   if (direction == GST_PAD_SINK) {
     /** caps from media */
     GstStructure *s = gst_caps_get_structure (caps, 0);
index 55a7e8c..aec9827 100644 (file)
@@ -45,6 +45,8 @@ G_BEGIN_DECLS
 #define GST_IS_TENSORDEC_CLASS(klass) \
   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TENSORDEC))
 #define GST_TENSORDEC_CAST(obj)  ((GstTensorDec *)(obj))
+#define BOX_SIZE        4
+#define DETECTION_MAX   1917
 typedef struct _GstTensorDec GstTensorDec;
 typedef struct _GstTensorDecClass GstTensorDecClass;
 
@@ -59,6 +61,18 @@ typedef struct
 } Mode_image_labeling;
 
 /**
+ * @brief Data structure for boundig box info.
+ */
+typedef struct
+{
+  gchar *label_path; /**< label file path */
+  GList *labels; /**< list of loaded labels */
+  gchar *box_prior_path; /**< label file path */
+  gfloat box_priors[BOX_SIZE][DETECTION_MAX];
+  guint total_labels; /**< count of labels */
+} Mode_boundig_boxes;
+
+/**
  * @brief Internal data structure for tensordec instances.
  */
 struct _GstTensorDec
@@ -75,7 +89,8 @@ struct _GstTensorDec
   /** For Tensor */
   gboolean configured; /**< TRUE if already successfully configured tensor metadata */
   GstTensorConfig tensor_config; /**< configured tensor info */
-  Mode_image_labeling tensordec_image_label;/** tensor decoder image labeling mode info */
+  Mode_image_labeling image_labeling;/** tensor decoder image labeling mode info */
+  Mode_boundig_boxes bounding_boxes;/** tensor decoder image labeling mode info */
 };
 
 /**