forked from dusty-nv/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdetectnet-camera.cpp
330 lines (269 loc) · 8.63 KB
/
detectnet-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
/*
* http://github.com/ross-nv/jetson-inference
*/
#include "config.h"
#if V4L_CAMERA
#include "v4l2Camera.h"
#else
#include "gstCamera.h"
#include "rtpStream.h"
#include "gvStream.h"
#endif
#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 "cudaMappedMemory.h"
#include "cudaNormalize.h"
#include "detectNet.h"
bool signal_recieved = false;
void sig_handler(int signo)
{
if( signo == SIGINT )
{
printf("received SIGINT\n");
signal_recieved = true;
}
}
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_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");
#endif
#endif
}
int main( int argc, char** argv )
{
#if ABACO
int logo;
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
#endif
#if ABACO
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%s\n",VIDEO_BYTES_PER_PIXEL, VIDEO_GV_PIXEL_FORMAT_NAME);
printf("\tHidth : %u\n", HEIGHT);
printf("\tHeight : %u\n", WIDTH);
printf("\tFramerate : %f\n\n", VIDEO_DEFAULT_FRAMERATE);
/*
* parse network type from CLI arguments
*/
detectNet::NetworkType networkType = detectNet::PEDNET_MULTI;
networkType = DETECTNET_TYPE;
if( signal(SIGINT, sig_handler) == SIG_ERR )
printf("\ncan't catch SIGINT\n");
/*
* create the camera device
*/
#if (VIDEO_SRC==VIDEO_GST_V4L_SRC) || (VIDEO_SRC==GST_RTP_SRC)
int width = WIDTH;
int height = HEIGHT;
std::ostringstream pipeline;
#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 << "queue ! rtpvrawdepay ! queue ! ";
pipeline << "appsink name=mysink";
#else
pipeline << "v4l2src device=/dev/video0 ! ";
pipeline << "video/x-raw, width=(int)" << width << ", height=(int)" << height << ", ";
pipeline << "format=RGB ! ";
pipeline << "appsink name=mysink";
#endif
static std::string pip = pipeline.str();
gstCamera* camera = gstCamera::Create(pip, height, width);
#elif VIDEO_SRC==VIDEO_GV_STREAM_SOURCE
gvStream* camera = new gvStream(HEIGHT, WIDTH);
#else
gstCamera* camera = gstCamera::Create();
#endif
if( !camera )
{
printf("\ndetectnet-camera: failed to initialize video device\n");
return 0;
}
printf("\ndetectnet-camera: successfully initialized video device\n");
/*
* create detectNet
*/
detectNet* net = detectNet::Create(networkType);
if( !net )
{
printf("detectnet-camera: failed to initialize imageNet\n");
return 0;
}
/*
* allocate memory for output bounding boxes and class confidence
*/
const uint32_t maxBoxes = net->GetMaxBoundingBoxes(); printf("maximum bounding boxes: %u\n", maxBoxes);
const uint32_t classes = net->GetNumClasses();
float* bbCPU = NULL;
float* bbCUDA = NULL;
float* confCPU = NULL;
float* confCUDA = NULL;
if( !cudaAllocMapped((void**)&bbCPU, (void**)&bbCUDA, maxBoxes * sizeof(float4)) ||
!cudaAllocMapped((void**)&confCPU, (void**)&confCUDA, maxBoxes * classes * sizeof(float)) )
{
printf("detectnet-console: failed to alloc output memory\n");
return 0;
}
/*
* create openGL window
*/
glDisplay* display = glDisplay::Create(camera->GetWidth(), camera->GetHeight());
glTexture* texture = NULL;
if( !display ) {
printf("\ndetectnet-camera: failed to create openGL display\n");
}
else
{
texture = glTexture::Create(camera->GetWidth(), camera->GetHeight(), GL_RGBA32F_ARB/*GL_RGBA8*/);
if( !texture )
printf("detectnet-camera: failed to create openGL texture\n");
}
#if ABACO
/*
* load logo
*/
logo = texture->ImageLoad((char*)"abaco.bmp");
#endif
/*
* start streaming
*/
if( !camera->Open() )
{
printf("\ndetectnet-camera: failed to open camera for streaming\n");
return 0;
}
printf("\ndetectnet-camera: camera open for streaming\n");
/*
* processing loop
*/
float confidence = 0.0f;
while( !display->Quit() && !signal_recieved )
{
void* imgCPU = NULL;
void* imgCUDA = NULL;
void* imgRGBA = NULL;
// get the latest frame
if( !camera->Capture(&imgCPU, &imgCUDA, 1000) )
printf("\ndetectnet-camera: failed to capture frame\n");
/*
* Convert capture colorspace to the required RGBA
*/
convertColour(camera, imgCUDA, &imgRGBA);
// classify image with detectNet
int numBoundingBoxes = maxBoxes;
if( net->Detect((float*)imgRGBA, camera->GetWidth(), camera->GetHeight(), bbCPU, &numBoundingBoxes, confCPU))
{
printf("%i bounding boxes detected\n", numBoundingBoxes);
int lastClass = 0;
int lastStart = 0;
for( int n=0; n < numBoundingBoxes; n++ )
{
const int nc = confCPU[n*2+1];
float* bb = bbCPU + (n * 4);
printf("bounding box %i (%f, %f) (%f, %f) w=%f h=%f\n", n, bb[0], bb[1], bb[2], bb[3], bb[2] - bb[0], bb[3] - bb[1]);
if( nc != lastClass || n == (numBoundingBoxes - 1) )
{
if( !net->DrawBoxes((float*)imgRGBA, (float*)imgRGBA, camera->GetWidth(), camera->GetHeight(),
bbCUDA + (lastStart * 4), (n - lastStart) + 1, lastClass) )
printf("detectnet-console: failed to draw boxes\n");
lastClass = nc;
lastStart = n;
}
}
if( display != NULL )
{
char str[256];
sprintf(str, "TensorRT build %x | %s | %04.1f FPS", NV_GIE_VERSION, net->HasFP16() ? "FP16" : "FP32", display->GetFPS());
//sprintf(str, "GIE build %x | %s | %04.1f FPS | %05.2f%% %s", NV_GIE_VERSION, net->GetNetworkName(), display->GetFPS(), confidence * 100.0f, net->GetClassDesc(img_class));
display->SetTitle(str);
}
}
// update display
if( display != NULL )
{
display->UserEvents();
display->BeginRender();
#if ABACO
texture->Image(320, 0, logo);
texture->Box(0, camera->GetHeight()-40, camera->GetWidth(), camera->GetHeight(), 0xF16B22FF);
#endif
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 ABACO
if (!(camera->GetWidth() < 1024))
{
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
display->EndRender();
}
}
printf("\ndetectnet-camera: un-initializing video device\n");
/*
* shutdown the camera device
*/
if( camera != NULL )
{
delete camera;
camera = NULL;
}
if( display != NULL )
{
delete display;
display = NULL;
}
printf("detectnet-camera: video device has been un-initialized.\n");
printf("detectnet-camera: this concludes the test of the video device.\n");
return 0;
}