deepstream_gie_yaml.cpp 8.63 KB
Newer Older
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
/*
 * SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
 *
 * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
 * property and proprietary rights in and to this material, related
 * documentation and any modifications thereto. Any use, reproduction,
 * disclosure or distribution of this material and related documentation
 * without an express license agreement from NVIDIA CORPORATION or
 * its affiliates is strictly prohibited.
 */

#include "deepstream_common.h"
#include "deepstream_config_file_parser.h"
#include "deepstream_config_yaml.h"
#include <string>
#include <cstring>
#include <iostream>

using std::cout;
using std::endl;

gboolean
parse_gie_yaml (NvDsGieConfig *config, std::string group_str, gchar *cfg_file_path)
{
  gboolean ret = FALSE;
  YAML::Node configyml = YAML::LoadFile(cfg_file_path);
  char *group = (char*) malloc(sizeof(char) * 1024);
  std::strncpy (group, group_str.c_str(), 1023);

  if (configyml[group_str]["enable"]) {
    gboolean val = configyml[group_str]["enable"].as<gboolean>();
    if(val == FALSE)
      return TRUE;
  }

  config->bbox_border_color_table = g_hash_table_new (NULL, NULL);
  config->bbox_bg_color_table = g_hash_table_new (NULL, NULL);
  config->bbox_border_color = (NvOSD_ColorParams) {1, 0, 0, 1};
  std::string border_str = "bbox-border-color";
  std::string bg_str = "bbox-bg-color";

 for(YAML::const_iterator itr = configyml[group_str].begin();
     itr != configyml[group_str].end(); ++itr)
  {
    std::string paramKey = itr->first.as<std::string>();

    if (paramKey == "enable") {
      config->enable = itr->second.as<gboolean>();
    } else if (paramKey == "input-tensor-meta") {
      config->input_tensor_meta = itr->second.as<gboolean>();
    } else if (paramKey == "operate-on-class-ids") {
      std::string str = itr->second.as<std::string>();
      std::vector<std::string> vec = split_string (str);
      int length = vec.size();
      int *arr = (int *) malloc(length * sizeof(int));
      for (unsigned int i = 0; i < vec.size(); i++) {
        arr[i] = std::stoi(vec[i]);
      }
      config->list_operate_on_class_ids = arr;
      config->num_operate_on_class_ids = length;
    } else if (paramKey == "batch-size") {
      config->batch_size = itr->second.as<guint>();
      config->is_batch_size_set = TRUE;
    } else if (paramKey == "model-engine-file") {
      std::string temp = itr->second.as<std::string>();
      char* str = (char*) malloc(sizeof(char) * 1024);
      std::strncpy (str, temp.c_str(), 1023);
      config->model_engine_file_path = (char*) malloc(sizeof(char) * 1024);
      if (!get_absolute_file_path_yaml (cfg_file_path, str,
            config->model_engine_file_path)) {
        g_printerr ("Error: Could not parse model-engine-file in %s.\n", group);
        g_free (str);
        goto done;
      }
      g_free (str);
    } else if (paramKey == "plugin-type") {
      config->plugin_type = (NvDsGiePluginType) itr->second.as<guint>();
    } else if (paramKey == "audio-transform") {
      std::string temp = itr->second.as<std::string>();
      config->audio_transform = (char*) malloc(sizeof(char) * 1024);
      std::strncpy (config->audio_transform, temp.c_str(), 1023);
    } else if (paramKey == "audio-framesize") {
      config->frame_size = itr->second.as<guint>();
      config->is_frame_size_set = TRUE;
    } else if (paramKey == "audio-hopsize") {
      config->hop_size = itr->second.as<guint>();
      config->is_hop_size_set = TRUE;
    } else if (paramKey == "audio-input-rate") {
      config->input_audio_rate = itr->second.as<guint>();
      config->is_hop_size_set = TRUE;
    } else if (paramKey == "labelfile-path") {
      std::string temp = itr->second.as<std::string>();
      char* str = (char*) malloc(sizeof(char) * 1024);
      std::strncpy (str, temp.c_str(), 1023);
      config->label_file_path = (char*) malloc(sizeof(char) * 1024);
      if (!get_absolute_file_path_yaml (cfg_file_path, str,
            config->label_file_path)) {
        g_printerr ("Error: Could not parse labelfile-path in %s.\n", group);
        g_free (str);
        goto done;
      }
      g_free (str);
    } else if (paramKey == "config-file") {
      std::string temp = itr->second.as<std::string>();
      char* str = (char*) malloc(sizeof(char) * 1024);
      std::strncpy (str, temp.c_str(), 1023);
      config->config_file_path = (char*) malloc(sizeof(char) * 1024);
      if (!get_absolute_file_path_yaml (cfg_file_path, str,
            config->config_file_path)) {
        g_printerr ("Error: Could not parse config-file in %s.\n", group);
        g_free (str);
        goto done;
      }
      g_free (str);
    } else if (paramKey == "interval") {
      config->interval = itr->second.as<guint>();
      config->is_interval_set = TRUE;
    } else if (paramKey == "gie-unique-id") {
      config->unique_id = itr->second.as<guint>();
      config->is_unique_id_set = TRUE;
    } else if (paramKey == "operate-on-gie-id") {
      config->operate_on_gie_id = itr->second.as<gint>();
      config->is_operate_on_gie_id_set = TRUE;
    } else if (paramKey.compare(0, border_str.size(), border_str) == 0) {
      NvOSD_ColorParams *clr_params;
      std::string str = itr->second.as<std::string>();
      std::vector<std::string> vec = split_string (str);
      if (vec.size() != 4) {
        NVGSTDS_ERR_MSG_V
            ("Number of Color params should be exactly 4 "
            "floats {r, g, b, a} between 0 and 1");
        goto done;
      }

      gint64 class_index = -1;
      if(paramKey != border_str) {
        class_index = std::stoi(paramKey.substr(border_str.size()));
      }

      gdouble list[4];
      for(unsigned int i = 0; i < 4; i++) {
        list[i] = std::stod(vec[i]);
      }

      if (class_index == -1) {
        clr_params = &config->bbox_border_color;
      } else {
        clr_params = (NvOSD_ColorParams*) g_malloc (sizeof (NvOSD_ColorParams));
        g_hash_table_insert (config->bbox_border_color_table,
            class_index + (gchar *) NULL, clr_params);
      }

      clr_params->red = list[0];
      clr_params->green = list[1];
      clr_params->blue = list[2];
      clr_params->alpha = list[3];
    } else if (paramKey.compare(0, bg_str.size(), bg_str) == 0) {
      NvOSD_ColorParams *clr_params;
      std::string str = itr->second.as<std::string>();
      std::vector<std::string> vec = split_string (str);
      if (vec.size() != 4) {
        NVGSTDS_ERR_MSG_V
            ("Number of Color params should be exactly 4 "
            "floats {r, g, b, a} between 0 and 1");
        goto done;
      }

      gint64 class_index = -1;
      if(paramKey != bg_str) {
        class_index = std::stoi(paramKey.substr(bg_str.size()));
      }

      gdouble list[4];
      for(unsigned int i = 0; i < 4; i++) {
        list[i] = std::stod(vec[i]);
      }

      if (class_index == -1) {
        clr_params = &config->bbox_bg_color;
        config->have_bg_color = TRUE;
      } else {
        clr_params = (NvOSD_ColorParams*) g_malloc (sizeof (NvOSD_ColorParams));
        g_hash_table_insert (config->bbox_bg_color_table, class_index + (gchar *) NULL,
            clr_params);
      }

      clr_params->red = list[0];
      clr_params->green = list[1];
      clr_params->blue = list[2];
      clr_params->alpha = list[3];
    } else if (paramKey == "infer-raw-output-dir") {
      std::string temp = itr->second.as<std::string>();
      char* str = (char*) malloc(sizeof(char) * 1024);
      std::strncpy (str, temp.c_str(), 1023);
      config->raw_output_directory = (char*) malloc(sizeof(char) * 1024);
      if (!get_absolute_file_path_yaml (cfg_file_path, str,
            config->raw_output_directory)) {
        g_printerr ("Error: Could not parse infer-raw-output-dir in %s.\n", group);
        g_free (str);
        goto done;
      }
      g_free (str);
    } else if (paramKey == "gpu-id") {
      config->gpu_id = itr->second.as<guint>();
      config->is_gpu_id_set = TRUE;
    } else if (paramKey == "nvbuf-memory-type") {
      config->nvbuf_memory_type = itr->second.as<guint>();
    } else {
      cout << "[WARNING] Unknown param found in gie: " << paramKey << endl;
    }
  }

  if (config->enable && config->label_file_path && !parse_labels_file (config)) {
    cout << "Failed while parsing label file " << config->label_file_path << endl;
    goto done;
  }
  if (!config->config_file_path) {
    cout << "Config file not provided for group " << group_str << endl;
    goto done;
  }

  ret = TRUE;

  if (group) {
    g_free (group);
    group = NULL;
  }
done:
  if (!ret) {
    cout <<  __func__ << " failed" << endl;
  }
  if (group) {
    g_free (group);
    group = NULL;
  }
  return ret;
}