-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_detection.cpp
209 lines (171 loc) · 7.34 KB
/
activity_detection.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
#include "activity_detection.h"
#include "img.h"
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <chrono>
static const float CLS_THRES = 0.3;
struct InferDeleter
{
template <typename T>
void operator()(T* obj) const
{
if (obj)
{
obj->destroy();
}
}
};
std::vector<std::string> string_split(std::string line, std::string sep) {
boost::tokenizer<boost::char_separator<char> > tokens(line, boost::char_separator<char>(sep.c_str()));
std::vector<std::string> ret;
for (auto iter = tokens.begin(); iter != tokens.end(); iter++) {
ret.push_back(*iter);
}
return ret;
}
ActivityDetector::ActivityDetector(std::string cfg_path, std::string wts_path, std::string name_path, int32_t batch_size, float ext_scale, nvinfer1::ILogger& logger) {
this->batch_size = batch_size;
this->ext_scale = ext_scale;
auto builder = nvinfer1::createInferBuilder(logger);
builder->setMaxBatchSize(MAX_BATCH_SIZE);
this->engine = this->init_engine(cfg_path, wts_path, builder);
this->ctx = this->engine->createExecutionContext();
std::cerr << "max_batch_size = " << this->engine->getMaxBatchSize() << std::endl;
assert(batch_size <= this->engine->getMaxBatchSize());
this->buffers = new UnifiedBufManager(std::shared_ptr<nvinfer1::ICudaEngine>(engine, InferDeleter()), this->batch_size);
std::string line;
std::ifstream ifs(name_path);
this->names.clear();
while (std::getline(ifs, line)) {
if (line.size() > 1) {
auto eles = string_split(line, "\t");
this->names.push_back(eles[0]);
this->thresholds.push_back(std::stof(eles[1]));
}
}
ifs.close();
std::cerr << "#classes = " << this->names.size() << std::endl;
}
ActivityDetector::ActivityDetector(std::string model_path, std::string name_path, int32_t batch_size, float ext_scale, nvinfer1::ILogger& logger) {
this->batch_size = batch_size;
this->ext_scale = ext_scale;
IRuntime* runtime = createInferRuntime(gLogger);
int64_t length;
std::ifstream model_file(model_path, std::ios::binary);
if (!model_file) {
std::cerr << "cannot open file: " << model_path << std::endl;
return;
}
model_file.read((char*)&length, sizeof(length));
std::cerr << "data length = " << length << std::endl;
char *buf = new char[length];
model_file.read(buf, length);
model_file.close();
this->engine = runtime->deserializeCudaEngine(buf, length, NULL);
delete buf;
this->ctx = this->engine->createExecutionContext();
std::cerr << "max_batch_size = " << this->engine->getMaxBatchSize() << std::endl;
assert(batch_size <= this->engine->getMaxBatchSize());
this->buffers = new UnifiedBufManager(std::shared_ptr<nvinfer1::ICudaEngine>(engine, InferDeleter()), this->batch_size);
std::string line;
std::ifstream ifs(name_path);
this->names.clear();
while (std::getline(ifs, line)) {
if (line.size() > 1) {
auto eles = string_split(line, "\t");
this->names.push_back(eles[0]);
this->thresholds.push_back(std::stof(eles[1]));
}
}
ifs.close();
std::cerr << "#classes = " << this->names.size() << std::endl;
}
nvinfer1::ICudaEngine* ActivityDetector::init_engine(std::string cfg_path, std::string weight_path, nvinfer1::IBuilder* builder) {
NetworkInfo info;
info.networkType = "yolov3";
info.configFilePath = cfg_path;
info.wtsFilePath = weight_path;
info.deviceType = "kGPU";
info.inputBlobName = input_blob_name;
Yolo yolo(info, builder);
return yolo.createEngine();
}
cv::Mat ActivityDetector::get_patch(cv::Mat img, NvDsInferParseObjectInfo box) {
float margin_scale = this->ext_scale / 2;
int32_t margin_x = (int)(box.width * margin_scale);
int32_t margin_y = (int)(box.height * margin_scale);
int32_t x = std::max((int32_t)box.left - margin_x, 0);
int32_t y = std::max((int32_t)box.top - margin_y, 0);
int32_t w = std::min((int32_t)box.width + 2 * margin_x, (int32_t)img.cols - x);
int32_t h = std::min((int32_t)box.height + 2 * margin_y, (int32_t)img.rows - y);
cv::Rect rect(x, y, w, h);
return img(rect);
}
std::vector<LabeledPeople> ActivityDetector::detect(cv::Mat img, std::vector<NvDsInferParseObjectInfo> boxes) {
std::vector<LabeledPeople> ret;
for (int32_t off = 0; off < (int32_t)boxes.size(); off += this->batch_size) {
auto beg_buf = std::chrono::system_clock::now();
float* p = (float*)this->buffers->getBuffer(std::string(input_blob_name));
int32_t cnt = 0;
int32_t stride = input_tensor_width * input_tensor_height * input_tensor_depth;
for (int32_t i = 0; i < batch_size && off + i < (int32_t)boxes.size();i ++) {
auto patch = get_patch(img, boxes[i]);
mat_8u3c_to_darknet_blob(patch, input_tensor_height, input_tensor_width, input_tensor_depth, p + i * stride);
cnt ++;
}
auto end_buf = std::chrono::system_clock::now();
auto beg_exe = std::chrono::system_clock::now();
bool status = this->ctx->execute(cnt, this->buffers->getDeviceBindings().data());
if (!status) {
std::cerr << "execution failed!" << std::endl;
return std::vector<LabeledPeople>();
}
auto end_exe = std::chrono::system_clock::now();
auto beg_post = std::chrono::system_clock::now();
float* res = (float*)this->buffers->getBuffer(std::string(output_blob_name));
assert(res != NULL);
for (int32_t k = 0; k < cnt; k++) {
std::vector<Activity> activities;
for (int32_t i = 0; i < (int32_t)this->names.size(); i++) {
if (res[i] * boxes[off + k].detectionConfidence> this->thresholds[i]) {
Activity act {
.activity=names[i],
.prob=res[i],
};
activities.push_back(act);
}
}
std::sort(activities.begin(), activities.end(), [](const Activity&a, const Activity&b) {
return a.prob > b.prob;
});
LabeledPeople people {
.loc = boxes[off + k],
.activities = activities,
};
ret.push_back(people);
res += names.size();
}
auto end_post = std::chrono::system_clock::now();
auto msecs = [](std::chrono::system_clock::time_point beg, std::chrono::system_clock::time_point end) -> int {
return std::chrono::duration_cast<std::chrono::milliseconds>(end - beg).count();
};
std::cerr << "[AD time cost@" << off << "] | buffer:" << msecs(beg_buf, end_buf) << "ms, exe:" << msecs(beg_exe, end_exe) << "ms, post:" << msecs(beg_post, end_post) << "ms" << std::endl;
}
return ret;
}
int32_t ActivityDetector::detect_capi(cv::Mat img, NvDsInferParseObjectInfo* boxes, int32_t num, const char* res[][NUM_ACTIVITIES]) {
std::vector<NvDsInferParseObjectInfo> vboxes(boxes, boxes+num);
auto lst = this->detect(img, vboxes);
int32_t off = 0;
for (auto lbl : lst) {
const char** p = res[off];
for (auto act : lbl.activities) {
*p = act.activity.c_str();
p++;
}
off++;
}
return 0;
}
ActivityDetector::~ActivityDetector() {
}