forked from dusty-nv/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagenet-camera.cpp
222 lines (164 loc) · 4.81 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
/*
* http://github.com/dusty-nv/jetson-inference
*/
#include "gstCamera.h"
#include "glDisplay.h"
#include "glTexture.h"
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "cudaNormalize.h"
#include "cudaFont.h"
#include "imageNet.h"
#define DEFAULT_CAMERA -1 // -1 for onboard camera, or change to index of /dev/video V4L2 camera (>=0)
bool signal_recieved = false;
void sig_handler(int signo)
{
if( signo == SIGINT )
{
printf("received SIGINT\n");
signal_recieved = true;
}
}
int main( int argc, char** argv )
{
printf("imagenet-camera\n args (%i): ", argc);
for( int i=0; i < argc; i++ )
printf("%i [%s] ", i, argv[i]);
printf("\n\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 )
printf("\ncan't catch SIGINT\n");
/*
* create the camera device
*/
gstCamera* camera = gstCamera::Create(DEFAULT_CAMERA);
if( !camera )
{
printf("\nimagenet-camera: failed to initialize video device\n");
return 0;
}
printf("\nimagenet-camera: successfully initialized video device\n");
printf(" width: %u\n", camera->GetWidth());
printf(" height: %u\n", camera->GetHeight());
printf(" depth: %u (bpp)\n\n", camera->GetPixelDepth());
/*
* 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();
glTexture* texture = NULL;
if( !display ) {
printf("\nimagenet-camera: failed to create openGL display\n");
}
else
{
texture = glTexture::Create(camera->GetWidth(), camera->GetHeight(), GL_RGBA32F_ARB/*GL_RGBA8*/);
if( !texture )
printf("imagenet-camera: failed to create openGL texture\n");
}
/*
* create font
*/
cudaFont* font = cudaFont::Create();
/*
* start streaming
*/
if( !camera->Open() )
{
printf("\nimagenet-camera: failed to open camera for streaming\n");
return 0;
}
printf("\nimagenet-camera: camera open for streaming\n");
/*
* processing loop
*/
float confidence = 0.0f;
while( !signal_recieved )
{
void* imgCPU = NULL;
void* imgCUDA = NULL;
// get the latest frame
if( !camera->Capture(&imgCPU, &imgCUDA, 1000) )
printf("\nimagenet-camera: failed to capture frame\n");
//else
// printf("imagenet-camera: recieved new frame CPU=0x%p GPU=0x%p\n", imgCPU, imgCUDA);
// convert from YUV to RGBA
void* imgRGBA = NULL;
if( !camera->ConvertRGBA(imgCUDA, &imgRGBA) )
printf("imagenet-camera: failed to convert from NV12 to RGBA\n");
// classify image
const int img_class = net->Classify((float*)imgRGBA, camera->GetWidth(), camera->GetHeight(), &confidence);
if( img_class >= 0 )
{
printf("imagenet-camera: %2.5f%% class #%i (%s)\n", confidence * 100.0f, img_class, net->GetClassDesc(img_class));
if( font != NULL )
{
char str[256];
sprintf(str, "%05.2f%% %s", confidence * 100.0f, net->GetClassDesc(img_class));
font->RenderOverlay((float4*)imgRGBA, (float4*)imgRGBA, camera->GetWidth(), camera->GetHeight(),
str, 0, 0, make_float4(255.0f, 255.0f, 255.0f, 255.0f));
}
if( display != NULL )
{
char str[256];
sprintf(str, "TensorRT build %x | %s | %s | %04.1f FPS", NV_GIE_VERSION, net->GetNetworkName(), net->HasFP16() ? "FP16" : "FP32", display->GetFPS());
//sprintf(str, "TensorRT 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( 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(100,100);
}
display->EndRender();
}
}
printf("\nimagenet-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("imagenet-camera: video device has been un-initialized.\n");
printf("imagenet-camera: this concludes the test of the video device.\n");
return 0;
}