forked from omaralvarez/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectnet-console.cpp
133 lines (96 loc) · 3.54 KB
/
detectnet-console.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
/*
* http://github.com/dusty-nv/jetson-inference
*/
#include "detectNet.h"
#include "loadImage.h"
#include "cudaMappedMemory.h"
#include <sys/time.h>
uint64_t current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
return te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
}
// main entry point
int main( int argc, char** argv )
{
printf("detectnet-console\n args (%i): ", argc);
for( int i=0; i < argc; i++ )
printf("%i [%s] ", i, argv[i]);
printf("\n\n");
// retrieve filename argument
if( argc < 2 )
{
printf("detectnet-console: input image filename required\n");
return 0;
}
const char* imgFilename = argv[1];
// create detectNet
detectNet* net = detectNet::Create(argc, argv);
if( !net )
{
printf("detectnet-console: failed to initialize detectNet\n");
return 0;
}
net->EnableProfiler();
// alloc memory for bounding box & confidence value output arrays
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;
}
// load image from file on disk
float* imgCPU = NULL;
float* imgCUDA = NULL;
int imgWidth = 0;
int imgHeight = 0;
if( !loadImageRGBA(imgFilename, (float4**)&imgCPU, (float4**)&imgCUDA, &imgWidth, &imgHeight) )
{
printf("failed to load image '%s'\n", imgFilename);
return 0;
}
// classify image
int numBoundingBoxes = maxBoxes;
printf("detectnet-console: beginning processing network (%zu)\n", current_timestamp());
const bool result = net->Detect(imgCUDA, imgWidth, imgHeight, bbCPU, &numBoundingBoxes, confCPU);
printf("detectnet-console: finished processing network (%zu)\n", current_timestamp());
if( !result )
printf("detectnet-console: failed to classify '%s'\n", imgFilename);
else if( argc > 2 ) // if the user supplied an output filename
{
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(imgCUDA, imgCUDA, imgWidth, imgHeight, bbCUDA + (lastStart * 4), (n - lastStart) + 1, lastClass) )
printf("detectnet-console: failed to draw boxes\n");
lastClass = nc;
lastStart = n;
}
}
CUDA(cudaThreadSynchronize());
// save image to disk
printf("detectnet-console: writing %ix%i image to '%s'\n", imgWidth, imgHeight, argv[2]);
if( !saveImageRGBA(argv[2], (float4*)imgCPU, imgWidth, imgHeight, 255.0f) )
printf("detectnet-console: failed saving %ix%i image to '%s'\n", imgWidth, imgHeight, argv[2]);
else
printf("detectnet-console: successfully wrote %ix%i image to '%s'\n", imgWidth, imgHeight, argv[2]);
}
//printf("detectnet-console: '%s' -> %2.5f%% class #%i (%s)\n", imgFilename, confidence * 100.0f, img_class, "pedestrian");
printf("\nshutting down...\n");
CUDA(cudaFreeHost(imgCPU));
delete net;
return 0;
}