2434ed4a
Zou XiKun
trt 20210304
|
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
|
#include "vpt.h"
#include "sy_errorinfo.h"
#include "sy_common.h"
#include "utools.h"
#include <time.h>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <cuda.h>
//model caffe2
#include "ga_vpt_predict_net_caffe2.h"
#include "ga_vpt_init_net_caffe2.h"
//model trt
#include "ga_vpt_predict_net.h"
#include "ga_vpt_init_net.h"
#include "vpt_fpn_plugin_factory.h"
#include "ga_trt_fpn_vpt_calibrator.h"
#include "authority.h"//授权文件
#include<boost/thread.hpp>
//视频结构化
/*
#ifdef _MSC_VER
#define productSN "96BA6C1E806049BBB69CD273B08624B9" //视频结构化系列_图片接口_tensorrt_Windows
#else
#define productSN "8A0BFFECDED64D13BEFBE8C016F30905" //视频结构化系列_图片接口_tensorrt_Linux
#endif
*/
//统一视频结构化图片接口产品id -- update 1112
//#ifdef _MSC_VER
//#define productSN "96BA6C1E806049BBB69CD273B08624B9" //windows 车型系列产品序列??图片接口
//#else
//#define productSN "8A0BFFECDED64D13BEFBE8C016F30905" //linux 车型系列产品序列??-图片接口
//#endif
#ifdef _MSC_VER
#define productSN "5A72239196F54B678D311E7FC11849DC" //windows 视频结构化-图片接口
#else
#define productSN "7CF8B4797F9E441686145BED07F662DE" //linux 视频结构化--图片接口
#endif
//#define THRESHOLD 0.6
//#define AUTHORIZATION
typedef struct objDetector {
void* detect_handle;
float threshold;
VPT_FPNPluginFactory tensorrt_plugin_factory;
int licence_status = -1;//授权相关参数
int thrd_status = -1; //授权相关参数
boost::thread thrd; //授权相关参数
objDetector()
{
detect_handle = NULL;
threshold = 0.6;
}
}objDetector;
int vpt_init(void **handle, vpt_param param)
{
int ret = SUCCESS;
#ifdef AUTHORIZATION
#ifdef _WIN32
if (SUCCESS == (ret = sy_licence(productSN)))
#elif __linux__
char* wtime = new char[15];
memset(wtime, 0, 15);
if (SUCCESS == (ret = sy_licence(productSN, &wtime)))
#endif
#else
ret = sy_time_check(2021, 4);
if (ret == SUCCESS)
#endif
{
cuInit(0);
int device_count = 0;
cuDeviceGetCount(&device_count);
if (param.gpuid >= device_count)
{
printf("\nGPU_ID PARAM WRONG!\n");
return GPUID_PARAM_ERROR;
}
objDetector * tools = new objDetector;
ctools_init_params vpt_param;
vpt_param.thres_ = param.threshold;
vpt_param.log_level_ = 0;
vpt_param.device_type_ = param.mode;
vpt_param.device_id_ = param.gpuid;
vpt_param.engine_type_ = param.engine;
vpt_param.model_type_ = MODEL_FPN;
#ifdef _MSC_VER
vpt_param.weight_file_ = NULL;
vpt_param.net_file_ = NULL;
#else
vpt_param.weight_file_ = NULL;
vpt_param.net_file_ = NULL;
#endif
vpt_param.data_process_str_ = param.preprocess_param;
vpt_param.need_im_info_ = 1; // true
if (vpt_param.engine_type_ == ENGINE_MCAFFE2)
{
//caffe2模型预处理参数
vpt_param.data_process_str_ =
"CopyData_CPU2GPU_U8;"
"TypeConvert_U8_F32;"
"ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32,"
"submean_b,103.94,submean_g,116.78,submean_r,123.68,"
"variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;"
"NHWC2NCHW_F32"
;
vpt_param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2;
vpt_param.weight_array_len_ = ga_vpt_init_net_len_caffe2;
vpt_param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2;
vpt_param.net_array_len_ = ga_vpt_predict_net_len_caffe2;
}
else if (vpt_param.engine_type_ == ENGINE_TENSORRT)
{
vpt_param.weight_array_ = (uint8_t*)ga_vpt_init_net;
vpt_param.weight_array_len_ = ga_vpt_init_net_len;
vpt_param.net_array_ = (uint8_t*)ga_vpt_predict_net;
vpt_param.net_array_len_ = ga_vpt_predict_net_len;
vpt_param.trt_serialize_file_ = param.serialize_file;// NULL;// "FPN_VPT_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//trt版本预处理参数
param.preprocess_param =
"CopyData_CPU2GPU_U8;"
"TypeConvert_U8_F32;"
"ResizeMaxPad_F32_F32,test_size,720,test_max_size,1280,max_height,736,max_width,1280,"
"submean_b,103.94,submean_g,116.78,submean_r,123.68,"
"variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;"
"NHWC2NCHW_F32"
;
memset(vpt_param.tensorrt_param_str_, 0, sizeof(vpt_param.tensorrt_param_str_));
int batch_size = param.max_batch;
std::string g_data_mode = "FP32";
bool g_is_create_calibrator = false;
sprintf(vpt_param.tensorrt_param_str_, "max_batchsize %d,"
"data_mode %s,"
"is_create_calibrator %d,"
"input_names data im_info,"
"output_names cls_prob bbox_pred_final rois_count_each_img",
batch_size, g_data_mode.c_str(), g_is_create_calibrator);
vpt_param.tensorrt_calibrator_file_ = NULL;// "trt_fpn_vpt_calibrator";
vpt_param.tensorrt_calibrator_array_len_ = ga_trt_fpn_vpt_calibrator_len;// "trt_fpn_vpt_calibrator";
vpt_param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_fpn_vpt_calibrator;// "trt_fpn_vpt_calibrator";
vpt_param.tensorrt_plugin_factory_ptr_ = &(tools->tensorrt_plugin_factory);
}
tools->threshold = param.threshold;
int flag = ctools_init(&tools->detect_handle, &vpt_param);
if (SUCCESS != flag)
{
printf("VPT_Init(ERROR): Init failed\n");
*handle = NULL;
}
else
*handle = (void*)tools;
return flag;
}
else
{
return AUTHOR_ERROR;
}
#ifdef AUTHORIZATION
#ifdef __linux__
if (wtime)
{
delete[] wtime;
wtime = NULL;
}
#endif
#endif
}
int vpt_process(void * handle, sy_img img, vpt_result *result)
{
if (NULL == handle)
{
printf("vpt_det(error): handle == null.\n");
return HANDLE_NULL_ERROR;
}
if (img.data_ == NULL || img.w_ == 0 || img.h_ == 0)
{
printf("vpt_det(error): imgdata == null or width == 0 or height == 0.\n");
return PARAMS_NULL_ERROR;
}
return vpt_batch(handle, &img, 1, &result);
}
int vpt_batch(void * handle, sy_img *batch_img, int batchsize, vpt_result **result)
{
if (NULL == handle)
{
printf("vpt_det(error): handle == null.\n");
return HANDLE_NULL_ERROR;
}
if (batchsize == 0)
{
printf("vpt_det(error): batch_size == 0.\n");
return PARAMS_NULL_ERROR;
}
for (int b = 0; b < batchsize; b++)
{
if (batch_img[b].data_ == NULL || batch_img[b].w_ == 0 || batch_img[b].h_ == 0)
{
printf("vpt_det(error): imgdata == null or width == 0 or height == 0.\n");
return PARAMS_NULL_ERROR;
}
}
objDetector * m_handle = (objDetector *)handle;
ctools_result *detresult;
int res_status = ctools_process(m_handle->detect_handle, batch_img, batchsize, &detresult);
for (int b = 0; b < batchsize; b++)
{
ctools_result &cur_result = detresult[b];
//(*result)[b].obj_count_ = cur_result.obj_count_ > MAX_DET_COUNT ? MAX_DET_COUNT : cur_result.obj_count_;
int res_count = 0;
for (int c = 0; c < cur_result.obj_count_ && c < MAX_DET_COUNT; c++)
{
float score = cur_result.obj_results_[c].data_[1];
if (score >= m_handle->threshold) //更改为left top right bottom score index
{
(*result)[b].obj_results_[res_count].obj_id = c;
(*result)[b].obj_results_[res_count].obj_rect.left_ = cur_result.obj_results_[c].data_[2];
(*result)[b].obj_results_[res_count].obj_rect.top_ = cur_result.obj_results_[c].data_[3];
(*result)[b].obj_results_[res_count].obj_rect.width_ = cur_result.obj_results_[c].data_[4] - cur_result.obj_results_[c].data_[2];
(*result)[b].obj_results_[res_count].obj_rect.height_ = cur_result.obj_results_[c].data_[5] - cur_result.obj_results_[c].data_[3];
(*result)[b].obj_results_[res_count].obj_index = cur_result.obj_results_[c].data_[0] - 1;
(*result)[b].obj_results_[res_count].obj_score = cur_result.obj_results_[c].data_[1];
res_count++;
}
}
(*result)[b].obj_count_ = res_count;
}
return SUCCESS;
}
void vpt_release(void **handle)
{
if (*handle)
{
objDetector * m_handle = (objDetector *)*handle;
ctools_release(&m_handle->detect_handle);
m_handle = NULL;
}
}
const char * vpt_get_version()
{
return "vpt_det_v0.0.0.190327";
}
|