forked from dusty-nv/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 8
/
imagenet-camera.cpp
474 lines (396 loc) · 12.5 KB
/
imagenet-camera.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* http://github.com/ross-abaco/jetson-inference
*
*/
#include <string>
#if V4L_CAMERA
#include "v4l2Camera.h"
#else
#include "rtpStream.h"
#include "gstCamera.h"
#include "gvStream.h"
#endif
#include "config.h"
#include "debug.h"
#if SDL_DISPLAY
#include "sdlDisplay.h"
#include "glTexture.h"
#define glDisplay sdlDisplay
#else
#include "glDisplay.h"
#include "glTexture.h"
#endif
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>
#include "cudaNormalize.h"
#include "cudaYUV.h"
#include "imageNet.h"
using namespace std;
bool signal_recieved = false;
void sig_handler(int signo)
{
if( signo == SIGINT )
{
debug_print("received SIGINT\n");
signal_recieved = true;
}
}
bool display_confidence = false;
void key_handler(char key)
{
switch (key)
{
case ' ' :
display_confidence ? display_confidence=false : display_confidence=true;
break;
default:
printf("unregistered keypress (%c), ignoring\n", key);
}
}
void* mRGBA = 0;
void convertColour(camera *camera, void* imgCUDA, void** imgRGBA)
{
#if VIDEO_SRC==VIDEO_GST_RTP_SRC
if ( !camera->ConvertYUVtoRGBA(imgCUDA, imgRGBA) )
printf("imagenet-camera: failed to convert from YUV to RGBAf\n");
#endif
#if VIDEO_SRC==VIDEO_RTP_STREAM_SOURCE
if ( !camera->ConvertYUVtoRGBf(imgCUDA, imgRGBA ) )
printf("imagenet-camera: failed to convert from YUV to RGBAf\n");
#endif
#if VIDEO_SRC==VIDEO_GST_V4L_SRC
if ( !camera->ConvertRGBtoRGBA(imgCUDA, imgRGBA) )
printf("imagenet-camera: failed to convert from RGB to RGBAf\n");
#endif
#if VIDEO_SRC==VIDEO_GV_STREAM_SOURCE
#if VIDEO_GV_PIXEL_FORMAT==VIDEO_GV_RGB8
if ( !camera->ConvertRGBtoRGBA(imgCUDA, imgRGBA) )
printf("imagenet-camera: failed to convert from RGB to RGBAf\n");
#elif VIDEO_GV_PIXEL_FORMAT==VIDEO_GV_YUV422
if ( !camera->ConvertYUVtoRGBf(imgCUDA, imgRGBA) )
printf("imagenet-camera: failed to convert from YUV to RGBAf\n");
#elif VIDEO_GV_PIXEL_FORMAT==VIDEO_GV_BAYER_GR8
if ( !camera->ConvertBAYER_GR8toRGBA(imgCUDA, imgRGBA) )
printf("imagenet-camera: failed to convert from BAYER_GR8 to RGBAf\n");
#else
printf("imagenet-camera: unrecognised pixel format %s\n", VIDEO_GV_PIXEL_FORMAT);
#endif
#endif
#if VIDEO_SRC==VIDEO_NV
if ( !camera->ConvertNV12toRGBA(imgCUDA, ConvertBAYER_GR8toRGBAimgRGBA) )
printf("imagenet-camera: failed to convert from NV12 to RGBAf\n");
#endif
}
int main( int argc, char** argv )
{
char tmp[200][200];
float tmpConf[200];
unsigned int frame = 0;
int itemCount = 0;
char str[256];
#if ABACO
int logo;
printf("Abaco Systems (abaco.com) Inferance Demonstration\n");
#else
printf("Jetson Inferance Demonstration\n");
#endif
printf("\tAuthor : ross.newman@abaco.com (dustinf@nvidia.com)\n");
printf("\tVideo Src : %s\n", VIDEO_SRC_NAME);
printf("\tBytes Per Pixel : %u",VIDEO_BYTES_PER_PIXEL);
#if VIDEO_SRC==VIDEO_GV_STREAM_SOURCE
printf(" (%s)", VIDEO_GV_PIXEL_FORMAT_NAME);
#endif
printf("\n");
printf("\tHidth : %u\n", HEIGHT);
printf("\tHeight : %u\n", WIDTH);
printf("\tFramerate : %f\n\n", (float)VIDEO_DEFAULT_FRAMERATE);
SDL_Color white = {255, 255, 255, 0}; // WWhite
SDL_Color orange = {247, 107, 34, 0}; // Abaco orange
SDL_Color black = {40, 40, 40, 0}; // Black
debug_print("imagenet-camera\n args (%i): ", argc);
// Some help.
printf("Keyboard commands:\n\tF Fullscreen\n\tQ Quit\n\tSPACE Toggle overlay\n");
/*
* parse network type from CLI arguments
*/
imageNet::NetworkType networkType = imageNet::GOOGLENET;
if( argc > 1 && strcmp(argv[1], "alexnet") == 0 )
networkType = imageNet::ALEXNET;
if( signal(SIGINT, sig_handler) == SIG_ERR )
debug_print("\ncan't catch SIGINT\n");
/*
* create the camera device
*/
#if VIDEO_SRC==VIDEO_GST_RTP_SRC || VIDEO_SRC==VIDEO_GST_V4L_SRC
int width = WIDTH;
int height = HEIGHT;
std::ostringstream pipeline;
#endif
#if VIDEO_SRC==VIDEO_GST_RTP_SRC
pipeline << "udpsrc address=239.192.1.44 port=5004 caps=\"application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)YCbCr-4:2:2, depth=(string)8, width=(string)" << width << ", height=(string)" << height << ", payload=(int)96\" ! ";
pipeline << "rtpvrawdepay ! queue ! ";
pipeline << "appsink name=mysink";
#endif
#if VIDEO_SRC==VIDEO_GST_V4L_SRC
pipeline << "v4l2src device=" << VIDEO_GST_V4L_SRC_DEVICE << " ! ";
pipeline << "video/x-raw, width=(int)" << width << ", height=(int)" << height << ", framerate=" << VIDEO_GST_V4L_SRC_FRAMERATE << "/1, ";
pipeline << "format=RGB ! ";
pipeline << "appsink name=mysink";
#endif
#if VIDEO_SRC==VIDEO_GST_RTP_SRC || VIDEO_SRC==VIDEO_GST_V4L_SRC
static std::string pip = pipeline.str();
std::cout << pip << "\n";
gstCamera* camera = gstCamera::Create(pip, HEIGHT, WIDTH);
#elif VIDEO_SRC==VIDEO_RTP_STREAM_SOURCE
rtpStream* camera = new rtpStream(HEIGHT, WIDTH);
camera->rtpStreamIn((char*)IP_UNICAST, IP_PORT_IN);
#elif VIDEO_SRC==VIDEO_GV_STREAM_SOURCE
gvStream* camera = new gvStream(HEIGHT, WIDTH);
#elif VIDEO_SRC==VIDEO_NV // USB Nvida TX1 CSI Webcam
gstCamera* camera = gstCamera::Create();
#endif
if( !camera )
{
debug_print("\nimagenet-camera: failed to initialize video device\n");
return 0;
}
printf("\nimagenet-camera: successfully initialized video device (%s%s)\n", VIDEO_SRC_NAME, VIDEO_GV_PIXEL_FORMAT_NAME);
/*
* create imageNet
*/
imageNet* net = imageNet::Create(networkType);
if( !net )
{
printf("imagenet-console: failed to initialize imageNet\n");
return 0;
}
/*
* create openGL window
*/
glDisplay* display = glDisplay::Create(camera->GetWidth(), camera->GetHeight());
glTexture* texture = NULL;
#if GST_RTP_SINK
rtpStream rtpStreaming(HEIGHT, WIDTH, (char*)"127.0.0.1", 5004);
rtpStreaming.Open();
#endif
/*
* register keyboard callback
*/
display->RegisterKeyCallback(key_handler);
if( !display ) {
printf("\nimagenet-camera: failed to create openGL display\n");
}
else
{
texture = glTexture::Create(camera->GetWidth(), camera->GetHeight(), GL_RGBA32F_ARB/*GL_RGBA8*/);
texture->Render(display->mRenderer);
if( !texture )
printf("imagenet-camera: failed to create openGL texture\n");
}
#if ABACO
/*
* load logo
*/
logo = texture->ImageLoad((char*)"abaco.bmp");
#endif
/*
* start streaming
*/
if( !camera->Open() )
{
printf("\nimagenet-camera: failed to open camera for streaming\n");
return 0;
}
debug_print("\nimagenet-camera: camera open for streaming\n");
/*
* processing loop
*/
float confidence = 0.0f;
while( ( !signal_recieved) && (!display->Quit() ) )
{
void* dummy = NULL;
void* imgCPU = NULL;
void* imgCUDA = NULL;
void* imgRGBA = NULL; // buffer holding converted RGBA video
/*
* Get the latest frame
*/
if( !camera->Capture(&imgCPU, &imgCUDA, 1000) )
printf("\nimagenet-camera: failed to capture frame\n");
/*
* Convert capture colorspace to the required RGBA
*/
convertColour(camera, imgCUDA, &imgRGBA);
/*
* Classify image
*/
const int img_class = net->Classify((float*)imgRGBA, camera->GetWidth(), camera->GetHeight(), &confidence);
if( img_class >= 0 )
{
debug_print("imagenet-camera: %2.5f%% class #%i (%s)\n", confidence * 100.0f, img_class, net->GetClassDesc(img_class));
sprintf(str, "%05.2f%% %s", confidence * 100.0f, net->GetClassDesc(img_class));
if( display != NULL )
{
char banner[256];
sprintf(banner, "TensorRT build %x | %s | %s | %04.1f FPS | %s", NV_GIE_VERSION, net->GetNetworkName(), net->HasFP16() ? "FP16" : "FP32", display->GetFPS(), VIDEO_SRC_NAME);
display->SetTitle(banner);
}
}
else
{
printf("imagenet-camera: classify failure, aborting\n");
return 0;
}
// update display
if( display != NULL )
{
int baroffset=0;
display->UserEvents();
display->BeginRender();
#if ABACO
texture->Image(320, 0, logo);
texture->Box(0, camera->GetHeight()-40, camera->GetWidth(), camera->GetHeight(), 0xF16B22FF);
#endif
if (camera->GetHeight() < 720)
{
texture->Box(0, 0, camera->GetWidth(), 35, 0xF16B22FF);
baroffset = 62;
}
else
{
texture->Box(0, 0, camera->GetWidth(), 52, 0xF16B22FF);
baroffset = 103;
}
// Update confidence/s less frequently
if (frame++ % 50 == 0)
{
// Bubble sort the list
{
bool swap = true;
while (swap)
{
swap = false;
for (int ii=0;ii<net->mItems.count-1;ii++)
{
if (net->mItems.index[ii].confidence < net->mItems.index[ii+1].confidence)
{
float tmpf;
unsigned int tmpn;
tmpf = net->mItems.index[ii].confidence;
net->mItems.index[ii].confidence = net->mItems.index[ii+1].confidence;
net->mItems.index[ii+1].confidence = tmpf;
tmpn = net->mItems.index[ii].number;
net->mItems.index[ii].number = net->mItems.index[ii+1].number;
net->mItems.index[ii+1].number = tmpn;
swap=true;
}
}
}
}
for (itemCount=0;itemCount<net->mItems.count;itemCount++)
{
sprintf((char*)&tmp[itemCount][0], "%05.2f%% %s", net->mItems.index[itemCount].confidence * 100.0f, net->GetClassDesc(net->mItems.index[itemCount].number));
tmpConf[itemCount] = net->mItems.index[itemCount].confidence;
}
}
// limit displayed items to 15
if (itemCount > 15 )
itemCount = 15;
// Render the confidence bar
if (display_confidence)
{
for (int ii=0;ii<itemCount;ii++)
{
int t;
long c = 0;
// First bar is green rest are red
if (ii == 0)
c = 0x2BB24CFF;
else
c = 0xD34536FF;
t = tmpConf[ii] * 100.0f;
texture->Box(45, baroffset+(ii*22), 45 + ((300 / 100) * t), baroffset+20+(ii*22), c);
}
}
// Render the video
if( texture != NULL )
{
// rescale image pixel intensities for display
CUDA(cudaNormalizeRGBA((float4*)imgRGBA, make_float2(0.0f, 255.0f),
(float4*)imgRGBA, make_float2(0.0f, 1.0f),
camera->GetWidth(), camera->GetHeight()));
// map from CUDA to openGL using GL interop
void* tex_map = texture->MapCUDA();
if( tex_map != NULL )
{
cudaMemcpy(tex_map, imgRGBA, texture->GetSize(), cudaMemcpyDeviceToDevice);
texture->Unmap();
}
// draw the texture
texture->Render(0,0);
}
if (display_confidence)
{
// Render the text
for (int ii=0;ii<itemCount;ii++)
{
texture->RenderText((char*)&tmp[ii][0], white, 50, baroffset-3+(ii*22), 18);
}
}
if (camera->GetHeight() < 720)
{
texture->RenderText(str, white, 15, 5, 18);
}
else
{
texture->RenderText(str, white, 15, 5, 28);
}
#if ABACO
if (camera->GetWidth() < 1024)
{
texture->RenderText((char*)"abaco.com", white, 15, camera->GetHeight()-32, 18);
}
else
{
texture->RenderText((char*)"WE INNOVATE. WE DELIVER. ", black, camera->GetWidth()-381, camera->GetHeight()-33, 18);
texture->RenderText((char*)"YOU SUCCEED.", white, camera->GetWidth()-140, camera->GetHeight()-33, 18);
texture->RenderText((char*)"abaco.com", white, 15, camera->GetHeight()-40, 28);
}
#endif
#if GST_RTP_SINK
rtpStreaming.Transmit((char*)imgCPU);
#endif
display->EndRender();
}
}
debug_print("\nimagenet-camera: un-initializing video device\n");
if( net != NULL )
{
delete net;
net = NULL;
}
/*
* shutdown the camera device
*/
if( camera != NULL )
{
delete camera;
camera = NULL;
}
if( display != NULL )
{
delete display;
display = NULL;
}
#if GST_RTP_SINK
rtpStreaming.Close();
#endif
debug_print("imagenet-camera: video device has been un-initialized.\n");
debug_print("imagenet-camera: this concludes the test of the video device.\n");\
printf("Done./n");
return 0;
}