forked from nnstreamer/nnstreamer-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnstreamer_example_decoder_image_labelling.c
344 lines (281 loc) · 8.1 KB
/
nnstreamer_example_decoder_image_labelling.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* @file nnstreamer_example_decoder_image_labelling.c
* @date 4 Oct 2018
* @brief Tensor stream example with tensor decoder
* @see https://github.com/nnsuite/nnstreamer
* @author Jinhyuck Park <[email protected]>
* @bug No known bugs.
*
* NNStreamer example for image classification with only gstreamer plug-in's include decoder.
*
* Pipeline :
* v4l2src -- tee ------------------------------------------------------- textoverlay -- videoconvert -- ximagesink
* | |
* --- videoscale -- tensor_converter -- tensor_filter -- tensor_decoder
*
*
* 'tensor_filter' for image classification.
* Get model by
* $ cd $NNST_ROOT/bin
* $ bash get-model.sh image-classification-tflite
*
* 'tensor decoder' updates classification result links to text_sink of textoverlay.
*
* Run example :
* Before running this example, GST_PLUGIN_PATH should be updated for nnstreamer plug-in.
* $ export GST_PLUGIN_PATH=$GST_PLUGIN_PATH:<nnstreamer plugin path>
* $ ./nnstreamer_example_decoder_image_labelling
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include <gst/gst.h>
/**
* @brief Macro for debug mode.
*/
#ifndef DBG
#define DBG FALSE
#endif
/**
* @brief Macro for debug message.
*/
#define _print_log(...) if (DBG) g_message (__VA_ARGS__)
/**
* @brief Macro to check error case.
*/
#define _check_cond_err(cond) \
do { \
if (!(cond)) { \
_print_log ("app failed! [line : %d]", __LINE__); \
goto error; \
} \
} while (0)
/**
* @brief Data structure for tflite model info.
*/
typedef struct
{
gchar *model_path; /**< tflite model file path */
} tflite_info_s;
/**
* @brief Data structure for app.
*/
typedef struct
{
GMainLoop *loop; /**< main event loop */
GstElement *pipeline; /**< gst pipeline for data stream */
GstBus *bus; /**< gst bus for data pipeline */
guint received; /**< received buffer count */
tflite_info_s tflite_info; /**< tflite model info */
} AppData;
/**
* @brief Data for pipeline and result.
*/
static AppData g_app;
/**
* @brief Free data in tflite info structure.
*/
static void
_tflite_free_info (tflite_info_s * tflite_info)
{
g_return_if_fail (tflite_info != NULL);
if (tflite_info->model_path) {
g_free (tflite_info->model_path);
tflite_info->model_path = NULL;
}
}
/**
* @brief Check tflite model.
*
* This example uses 'Mobilenet_1.0_224_quant' for image classification.
*/
static gboolean
_tflite_init_info (tflite_info_s * tflite_info, const gchar * path)
{
const gchar tflite_model[] = "mobilenet_v1_1.0_224_quant.tflite";
g_return_val_if_fail (tflite_info != NULL, FALSE);
tflite_info->model_path = NULL;
/* check model file exists */
tflite_info->model_path = g_strdup_printf ("%s/%s", path, tflite_model);
if (access (tflite_info->model_path, F_OK) != 0) {
g_critical ("cannot find tflite model [%s]", tflite_info->model_path);
return FALSE;
}
return TRUE;
}
/**
* @brief Free resources in app data.
*/
static void
_free_app_data (void)
{
if (g_app.loop) {
g_main_loop_unref (g_app.loop);
g_app.loop = NULL;
}
if (g_app.bus) {
gst_bus_remove_signal_watch (g_app.bus);
gst_object_unref (g_app.bus);
g_app.bus = NULL;
}
if (g_app.pipeline) {
gst_object_unref (g_app.pipeline);
g_app.pipeline = NULL;
}
_tflite_free_info (&g_app.tflite_info);
}
/**
* @brief Function to print error message.
*/
static void
_parse_err_message (GstMessage * message)
{
gchar *debug;
GError *error;
g_return_if_fail (message != NULL);
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (message, &error, &debug);
break;
case GST_MESSAGE_WARNING:
gst_message_parse_warning (message, &error, &debug);
break;
default:
return;
}
gst_object_default_error (GST_MESSAGE_SRC (message), error, debug);
g_error_free (error);
g_free (debug);
}
/**
* @brief Function to print qos message.
*/
static void
_parse_qos_message (GstMessage * message)
{
GstFormat format;
guint64 processed;
guint64 dropped;
gst_message_parse_qos_stats (message, &format, &processed, &dropped);
_print_log ("format[%d] processed[%" G_GUINT64_FORMAT "] dropped[%"
G_GUINT64_FORMAT "]", format, processed, dropped);
}
/**
* @brief Callback for message.
*/
static void
_message_cb (GstBus * bus, GstMessage * message, gpointer user_data)
{
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_EOS:
_print_log ("received eos message");
g_main_loop_quit (g_app.loop);
break;
case GST_MESSAGE_ERROR:
_print_log ("received error message");
_parse_err_message (message);
g_main_loop_quit (g_app.loop);
break;
case GST_MESSAGE_WARNING:
_print_log ("received warning message");
_parse_err_message (message);
break;
case GST_MESSAGE_STREAM_START:
_print_log ("received start message");
break;
case GST_MESSAGE_QOS:
_parse_qos_message (message);
break;
default:
break;
}
}
/**
* @brief Set window title.
* @param name GstXImageSink element name
* @param title window title
*/
static void
_set_window_title (const gchar * name, const gchar * title)
{
GstTagList *tags;
GstPad *sink_pad;
GstElement *element;
element = gst_bin_get_by_name (GST_BIN (g_app.pipeline), name);
g_return_if_fail (element != NULL);
sink_pad = gst_element_get_static_pad (element, "sink");
if (sink_pad) {
tags = gst_tag_list_new (GST_TAG_TITLE, title, NULL);
gst_pad_send_event (sink_pad, gst_event_new_tag (tags));
gst_object_unref (sink_pad);
}
gst_object_unref (element);
}
/**
* @brief Main function.
*/
int
main (int argc, char **argv)
{
const gchar tflite_model_path[] = "./tflite_model_img";
const gchar tflite_label[] = "./tflite_model_img/labels.txt";
/* 224x224 for tflite model */
const guint width = 224;
const guint height = 224;
gchar *str_pipeline;
gulong handle_id;
GstElement *element;
_print_log ("start app..");
/* init app variable */
g_app.received = 0;
_check_cond_err (_tflite_init_info (&g_app.tflite_info, tflite_model_path));
/* init gstreamer */
gst_init (&argc, &argv);
/* main loop */
g_app.loop = g_main_loop_new (NULL, FALSE);
_check_cond_err (g_app.loop != NULL);
/* init pipeline */
str_pipeline =
g_strdup_printf
("textoverlay name=overlay font-desc=Sans,24 ! videoconvert ! ximagesink name=img_test "
"v4l2src name=cam_src ! videoconvert ! videoscale ! video/x-raw,width=640,height=480,format=RGB ! tee name=t_raw "
"t_raw. ! queue ! overlay.video_sink "
"t_raw. ! queue ! videoscale ! video/x-raw,width=%d,height=%d ! tensor_converter !"
"tensor_filter framework=tensorflow-lite model=%s ! "
"tensor_decoder mode=image_labeling option1=%s ! overlay.text_sink",
width, height, g_app.tflite_info.model_path, tflite_label);
_print_log ("%s\n", str_pipeline);
g_app.pipeline = gst_parse_launch (str_pipeline, NULL);
g_free (str_pipeline);
_check_cond_err (g_app.pipeline != NULL);
/* bus and message callback */
g_app.bus = gst_element_get_bus (g_app.pipeline);
_check_cond_err (g_app.bus != NULL);
gst_bus_add_signal_watch (g_app.bus);
handle_id = g_signal_connect (g_app.bus, "message",
(GCallback) _message_cb, NULL);
_check_cond_err (handle_id > 0);
/* start pipeline */
gst_element_set_state (g_app.pipeline, GST_STATE_PLAYING);
/* set window title */
_set_window_title ("img_test", "NNStreamer Example");
/* run main loop */
g_main_loop_run (g_app.loop);
/* cam source element */
element = gst_bin_get_by_name (GST_BIN (g_app.pipeline), "cam_src");
gst_element_set_state (element, GST_STATE_READY);
gst_element_set_state (g_app.pipeline, GST_STATE_READY);
g_usleep (200 * 1000);
gst_element_set_state (element, GST_STATE_NULL);
gst_element_set_state (g_app.pipeline, GST_STATE_NULL);
g_usleep (200 * 1000);
gst_object_unref (element);
error:
_print_log ("close app..");
_free_app_data ();
return 0;
}