forked from terryky/tflite_gles_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
352 lines (296 loc) · 9.1 KB
/
main.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
345
346
347
348
349
350
351
/* ------------------------------------------------ *
* The MIT License (MIT)
* Copyright (c) 2020 terryky1220@gmail.com
* ------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <float.h>
#include <GLES2/gl2.h>
#include "util_egl.h"
#include "util_debugstr.h"
#include "util_pmeter.h"
#include "util_texture.h"
#include "util_render2d.h"
#include "trt_dbface.h"
#include "util_camera_capture.h"
#include "util_video_decode.h"
#include "render_imgui.h"
#define UNUSED(x) (void)(x)
/* resize image to DNN network input size and convert to fp32. */
void
feed_dbface_image(texture_2d_t *srctex, int win_w, int win_h)
{
int x, y, w, h;
float *buf_fp32 = (float *)get_dbface_input_buf (&w, &h);
unsigned char *buf_ui8 = NULL;
static unsigned char *pui8 = NULL;
if (pui8 == NULL)
pui8 = (unsigned char *)malloc(w * h * 4);
buf_ui8 = pui8;
draw_2d_texture_ex (srctex, 0, win_h - h, w, h, 1);
glPixelStorei (GL_PACK_ALIGNMENT, 4);
glReadPixels (0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf_ui8);
/* convert UI8 [0, 255] ==> FP32 [-1, 1] */
float mean = 128.0f;
float std = 128.0f;
for (y = 0; y < h; y ++)
{
for (x = 0; x < w; x ++)
{
int r = *buf_ui8 ++;
int g = *buf_ui8 ++;
int b = *buf_ui8 ++;
buf_ui8 ++; /* skip alpha */
*buf_fp32 ++ = (float)(r - mean) / std;
*buf_fp32 ++ = (float)(g - mean) / std;
*buf_fp32 ++ = (float)(b - mean) / std;
}
}
return;
}
static void
render_detect_region (int ofstx, int ofsty, int texw, int texh,
dbface_result_t *detection, imgui_data_t *imgui_data)
{
float col_white[] = {1.0f, 1.0f, 1.0f, 1.0f};
float *col_frame = imgui_data->frame_color;
for (int i = 0; i < detection->num; i ++)
{
face_t *face = &(detection->faces[i]);
float x1 = face->topleft.x * texw + ofstx;
float y1 = face->topleft.y * texh + ofsty;
float x2 = face->btmright.x * texw + ofstx;
float y2 = face->btmright.y * texh + ofsty;
float score = face->score;
/* rectangle region */
draw_2d_rect (x1, y1, x2-x1, y2-y1, col_frame, 2.0f);
/* detect score */
char buf[512];
sprintf (buf, "%d", (int)(score * 100));
draw_dbgstr_ex (buf, x1, y1-22, 1.0f, col_white, col_frame);
/* key points */
for (int j = 0; j < kFaceKeyNum; j ++)
{
float x = face->keys[j].x * texw + ofstx;
float y = face->keys[j].y * texh + ofsty;
int r = 4;
draw_2d_fillrect (x - (r/2), y - (r/2), r, r, col_frame);
}
}
}
/* Adjust the texture size to fit the window size
*
* Portrait
* Landscape +------+
* +-+------+-+ +------+
* | | | | | |
* | | | | | |
* +-+------+-+ +------+
* +------+
*/
static void
adjust_texture (int win_w, int win_h, int texw, int texh,
int *dx, int *dy, int *dw, int *dh)
{
float win_aspect = (float)win_w / (float)win_h;
float tex_aspect = (float)texw / (float)texh;
float scale;
float scaled_w, scaled_h;
float offset_x, offset_y;
if (win_aspect > tex_aspect)
{
scale = (float)win_h / (float)texh;
scaled_w = scale * texw;
scaled_h = scale * texh;
offset_x = (win_w - scaled_w) * 0.5f;
offset_y = 0;
}
else
{
scale = (float)win_w / (float)texw;
scaled_w = scale * texw;
scaled_h = scale * texh;
offset_x = 0;
offset_y = (win_h - scaled_h) * 0.5f;
}
*dx = (int)offset_x;
*dy = (int)offset_y;
*dw = (int)scaled_w;
*dh = (int)scaled_h;
}
#if defined (USE_IMGUI)
void
mousemove_cb (int x, int y)
{
imgui_mousemove (x, y);
}
void
button_cb (int button, int state, int x, int y)
{
imgui_mousebutton (button, state, x, y);
}
void
keyboard_cb (int key, int state, int x, int y)
{
}
#endif
void
setup_imgui (int win_w, int win_h, imgui_data_t *imgui_data)
{
#if defined (USE_IMGUI)
egl_set_motion_func (mousemove_cb);
egl_set_button_func (button_cb);
egl_set_key_func (keyboard_cb);
init_imgui (win_w, win_h);
#endif
imgui_data->frame_color[0] = 0.0f;
imgui_data->frame_color[1] = 0.5f;
imgui_data->frame_color[2] = 1.0f;
imgui_data->frame_color[3] = 1.0f;
}
/*--------------------------------------------------------------------------- *
* M A I N F U N C T I O N
*--------------------------------------------------------------------------- */
int
main(int argc, char *argv[])
{
char input_name_default[] = "./assets/pexels-matheus-bertelli-2467506.jpg";
char *input_name = NULL;
int count;
int win_w = 900;
int win_h = 900;
int texw, texh, draw_x, draw_y, draw_w, draw_h;
texture_2d_t captex = {0};
double ttime[10] = {0}, interval, invoke_ms;
int enable_camera = 1;
imgui_data_t imgui_data = {0};
UNUSED (argc);
UNUSED (*argv);
#if defined (USE_INPUT_VIDEO_DECODE)
int enable_video = 0;
#endif
{
int c;
const char *optstring = "v:x";
while ((c = getopt (argc, argv, optstring)) != -1)
{
switch (c)
{
#if defined (USE_INPUT_VIDEO_DECODE)
case 'v':
enable_video = 1;
input_name = optarg;
break;
#endif
case 'x':
enable_camera = 0;
break;
}
}
while (optind < argc)
{
input_name = argv[optind];
optind++;
}
}
if (input_name == NULL)
input_name = input_name_default;
egl_init_with_platform_window_surface (2, 0, 0, 0, win_w, win_h);
init_2d_renderer (win_w, win_h);
init_pmeter (win_w, win_h, 500);
init_dbgstr (win_w, win_h);
init_trt_dbface (&imgui_data.dbface_config);
setup_imgui (win_w, win_h, &imgui_data);
#if defined (USE_GL_DELEGATE) || defined (USE_GPU_DELEGATEV2)
/* we need to recover framebuffer because GPU Delegate changes the FBO binding */
glBindFramebuffer (GL_FRAMEBUFFER, 0);
glViewport (0, 0, win_w, win_h);
#endif
#if defined (USE_INPUT_VIDEO_DECODE)
/* initialize FFmpeg video decode */
if (enable_video && init_video_decode () == 0)
{
create_video_texture (&captex, input_name);
texw = captex.width;
texh = captex.height;
enable_camera = 0;
}
else
#endif
#if defined (USE_INPUT_CAMERA_CAPTURE)
/* initialize V4L2 capture function */
if (enable_camera && init_capture (CAPTURE_SQUARED_CROP) == 0)
{
create_capture_texture (&captex);
texw = captex.width;
texh = captex.height;
}
else
#endif
{
int texid;
load_jpg_texture (input_name, &texid, &texw, &texh);
captex.texid = texid;
captex.width = texw;
captex.height = texh;
captex.format = pixfmt_fourcc ('R', 'G', 'B', 'A');
enable_camera = 0;
}
adjust_texture (win_w, win_h, texw, texh, &draw_x, &draw_y, &draw_w, &draw_h);
glClearColor (0.f, 0.f, 0.f, 1.0f);
/* --------------------------------------- *
* Render Loop
* --------------------------------------- */
for (count = 0; ; count ++)
{
dbface_result_t face_ret = {0};
char strbuf[512];
PMETER_RESET_LAP ();
PMETER_SET_LAP ();
ttime[1] = pmeter_get_time_ms ();
interval = (count > 0) ? ttime[1] - ttime[0] : 0;
ttime[0] = ttime[1];
glClear (GL_COLOR_BUFFER_BIT);
#if defined (USE_INPUT_VIDEO_DECODE)
/* initialize FFmpeg video decode */
if (enable_video)
{
update_video_texture (&captex);
}
#endif
#if defined (USE_INPUT_CAMERA_CAPTURE)
if (enable_camera)
{
update_capture_texture (&captex);
}
#endif
/* --------------------------------------- *
* Face detection
* --------------------------------------- */
feed_dbface_image (&captex, win_w, win_h);
ttime[2] = pmeter_get_time_ms ();
invoke_dbface (&face_ret, &imgui_data.dbface_config);
ttime[3] = pmeter_get_time_ms ();
invoke_ms = ttime[3] - ttime[2];
/* --------------------------------------- *
* render scene
* --------------------------------------- */
glClear (GL_COLOR_BUFFER_BIT);
/* visualize the face detection results. */
draw_2d_texture_ex (&captex, draw_x, draw_y, draw_w, draw_h, 0);
render_detect_region (draw_x, draw_y, draw_w, draw_h, &face_ret, &imgui_data);
/* --------------------------------------- *
* post process
* --------------------------------------- */
draw_pmeter (0, 40);
sprintf (strbuf, "Interval:%5.1f [ms]\nTFLite :%5.1f [ms]", interval, invoke_ms);
draw_dbgstr (strbuf, 10, 10);
#if defined (USE_IMGUI)
invoke_imgui (&imgui_data);
#endif
egl_swap();
}
return 0;
}