0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
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
|
#include <iostream>
#include <sstream>
#include <fstream>
#include "vpt.h"
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include "opencv2/imgproc/types_c.h"
#include "time.h"
#include "sys/time.h"
#include "sy_errorinfo.h"
#include "utils.h"
#include "dvpp_process.h"
#include <chrono>
#include <dirent.h>
using namespace std;
using namespace cv;
double msecond() {
struct timeval tv;
gettimeofday(&tv, 0);
return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
}
void getAllNm(std::string pthNm, std::vector<std::string>& fileList)
{
DIR *dir;
struct dirent *ptr;
dir = opendir(pthNm.c_str()); ///open the dir
int filenum = 0;
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
// char* to string
std::string curNm = ptr->d_name;
if(curNm != "." && curNm != "..")
{
filenum++;
fileList.push_back(curNm);
//printf("file %d name: %s\n", filenum, curNm.c_str());
}
}
closedir(dir);
}
int main() {
cout << vpt_get_version() << endl;
vpt_param param;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
53
54
|
// param.modelNames = "vpt0715_310p.om";
param.modelNames = "vpt230323_310p.om";
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
55
56
57
58
59
60
61
|
param.threshold = 0.4;
param.devId = 0;
param.isTrk = false;
void* handle = nullptr;
cout << "init start " << endl;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
62
63
|
ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED);
ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED);
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
64
|
aclrtContext ctx;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
65
|
ACL_CALL(aclrtCreateContext(&ctx, param.devId), ACL_SUCCESS, SY_FAILED);
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
66
|
aclrtStream stream = nullptr;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
67
|
ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
68
69
70
71
72
73
74
75
76
77
78
|
DvppProcess* dvpp = new DvppProcess();
dvpp->InitResource(stream);
int ret = vpt_init(&handle, param);
if (ret == 0) {
cout << "init success " << endl;
ifstream infile("list.txt");
string file;
if(1) {
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
79
|
// while (infile >> file) {
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
// string filename = "700W/" + file;
// string filename = "imgs/" + file;
// const char* img_file_path="examples/";
const char* img_file_path="imgs/";
std::vector<std::string> fileList;
getAllNm(img_file_path,fileList);
if (fileList.empty()) throw std::logic_error("No suitable images were found");
double t3 = 0;
for (auto & file : fileList) {
string filename = img_file_path + file;
cout << "img path: " << filename << endl;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
94
|
const int batchsize = 5;
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
sy_img imgs[batchsize];
//debug====================================================================
double pt1,pt2,st1,st2;
st1 = msecond();
ImageData src[batchsize], dvpp_data[batchsize];
for (int b = 0; b < batchsize; b++) {
Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息
pt1 = msecond();
ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码
pt2 = msecond();
printf("debug decode time: %.2f\n", pt2 - pt1);
imgs[b].w_ = dvpp_data[b].width;//dvpp_data[b].alignWidth;
imgs[b].h_ = dvpp_data[b].height;//dvpp_data[b].alignHeight;
imgs[b].data_ = dvpp_data[b].data.get();
}
st2 = msecond();
printf("debug dvpp process time: %.2f\n", (st2 - st1)/batchsize);
// printf("debug:%d\n",__LINE__);
//debug end================================================================
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
114
115
116
117
|
vpt_result * results = new vpt_result[batchsize];
for (int b = 0; b < batchsize; b++) {
results[b].obj_results_ = new vpt_obj_result[MAX_DET_COUNT];
}
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
118
119
|
double t1,t2;
t1 = msecond();
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
120
|
int ret = vpt_batch(handle, imgs, batchsize, results);
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
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
|
t2 = msecond();
printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize);
t3 = t3 + t2-t1;
//debug==========================================================
// vpt_result* results_b10;
// t1 = msecond();
// ret = vpt_batch10(handle, imgs, batchsize, &results_b10);
// t2 = msecond();
// printf("debug mean batch process time: %.2f\n", (t2 - t1)/batchsize);
//debug end======================================================
// draw results
Mat cvImg = imread(filename.c_str());
for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++) {
printf("debug det num:%d\n",results[batchIdx].obj_count_);
for (int i = 0; i < results[batchIdx].obj_count_; i++) {
// record===============================================================================================================
float xmin = results[batchIdx].obj_results_[i].obj_rect.left_;
float ymin = results[batchIdx].obj_results_[i].obj_rect.top_;
float xmax = results[batchIdx].obj_results_[i].obj_rect.width_ + results[batchIdx].obj_results_[i].obj_rect.left_;
float ymax = results[batchIdx].obj_results_[i].obj_rect.height_ + results[batchIdx].obj_results_[i].obj_rect.top_;
float txc = (xmin + xmax) / 2.0; // x center
float tyc = (ymin + ymax) / 2.0; // y center
float tw = results[batchIdx].obj_results_[i].obj_rect.width_;
float th = results[batchIdx].obj_results_[i].obj_rect.height_;
int label = results[batchIdx].obj_results_[i].obj_index;
float score = results[batchIdx].obj_results_[i].obj_score;
//printf("=====:%d %8.12f %8.12f %8.12f %8.12f %8.12f\n",label, xmin/cvImg.cols,ymin/cvImg.rows,xmax/cvImg.cols,ymax/cvImg.rows, score);
//printf("%%%%%:%d %8.12f %8.12f %8.12f %8.12f %8.12f\n",label, txc/cvImg.cols,tyc/cvImg.rows,tw/cvImg.cols,th/cvImg.rows, score);
// record end===========================================================================================================
Point lt(results[batchIdx].obj_results_[i].obj_rect.left_, results[batchIdx].obj_results_[i].obj_rect.top_);
Point rb((results[batchIdx].obj_results_[i].obj_rect.left_ +
results[batchIdx].obj_results_[i].obj_rect.width_), (results[batchIdx].obj_results_[i].obj_rect.top_ +
results[batchIdx].obj_results_[i].obj_rect.height_));
rectangle(cvImg, lt, rb, cv::Scalar(0, 0, 255), 4);
// cout << results[batchIdx].obj_results_[i].obj_rect.left_ << " "<< results[batchIdx].obj_results_[i].obj_rect.top_ <<" " << results[batchIdx].obj_results_[i].obj_rect.width_ << " " << results[batchIdx].obj_results_[i].obj_rect.height_ << endl;
char buffer[50];
int fontface = cv::FONT_HERSHEY_SIMPLEX;
double scale = 0.8;
int thickness = 2;
int baseline = 0;
snprintf(buffer, sizeof(buffer), "%d:%.2f", results[batchIdx].obj_results_[i].obj_index,results[batchIdx].obj_results_[i].obj_score);
cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness, &baseline);
cv::putText(cvImg, buffer, lt - cv::Point(0, baseline), fontface,
scale, cv::Scalar(0, 0, 255), thickness, 4);
}
string jpgSaveName = "result/" + file;
cv::imwrite(jpgSaveName, cvImg);
}
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
176
177
178
179
180
|
for (int i = 0; i < batchsize; i++) {
delete [] results[i].obj_results_;
}
delete [] results;
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
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
|
// for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++){
// printf("debug det_b10 num:%d\n",results_b10[batchIdx].obj_count_);
// for (int i = 0; i < results_b10[batchIdx].obj_count_; i++) {
// Point lt(results_b10[batchIdx].obj_results_[i].obj_rect.left_, results_b10[batchIdx].obj_results_[i].obj_rect.top_);
// Point rb((results_b10[batchIdx].obj_results_[i].obj_rect.left_ +
// results_b10[batchIdx].obj_results_[i].obj_rect.width_), (results_b10[batchIdx].obj_results_[i].obj_rect.top_ +
// results_b10[batchIdx].obj_results_[i].obj_rect.height_));
// rectangle(cvImg, lt, rb, cv::Scalar(0, 0, 255), 4);
// // cout << results[batchIdx].obj_results_[i].obj_rect.left_ << " "<< results[batchIdx].obj_results_[i].obj_rect.top_ <<" " << results[batchIdx].obj_results_[i].obj_rect.width_ << " " << results[batchIdx].obj_results_[i].obj_rect.height_ << endl;
// char buffer[50];
// int fontface = cv::FONT_HERSHEY_SIMPLEX;
// double scale = 0.8;
// int thickness = 2;
// int baseline = 0;
// snprintf(buffer, sizeof(buffer), "%d:%.2f", results_b10[batchIdx].obj_results_[i].obj_index,results_b10[batchIdx].obj_results_[i].obj_score);
// cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness, &baseline);
// cv::putText(cvImg, buffer, lt - cv::Point(0, baseline), fontface,
// scale, cv::Scalar(0, 0, 255), thickness, 4);
// }
// string jpgSaveName = "result/" + file;
// cv::imwrite(jpgSaveName, cvImg);
// }
}
printf("mean time cost:%f\n",t3/fileList.size());
}
if(0) {
const int batchsize = 10;
sy_img imgs[batchsize];
ImageData src[batchsize], dvpp_data[batchsize];
int b = 0;
const char* img_file_path="imgs/";
std::vector<std::string> fileList;
getAllNm(img_file_path,fileList);
if (fileList.empty()) throw std::logic_error("No suitable images were found");
for (auto & file : fileList) {
string filename = img_file_path + file;
cout << "img path: " << filename << endl;
//debug====================================================================
if(b < batchsize) {
Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息
ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码
imgs[b].w_ = dvpp_data[b].width;
imgs[b].h_ = dvpp_data[b].height;
imgs[b].data_ = dvpp_data[b].data.get();
b ++;
}
if(b == batchsize) {
b = 0;
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
237
238
239
240
|
vpt_result * results = new vpt_result[batchsize];
for (int b = 0; b < batchsize; b++) {
results[b].obj_results_ = new vpt_obj_result[MAX_DET_COUNT];
}
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
241
242
|
double t1,t2;
t1 = msecond();
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
243
|
int ret = vpt_batch(handle, imgs, batchsize, results);
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
t2 = msecond();
printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize);
//debug==========================================================
// vpt_result* results_b10;
// t1 = msecond();
// ret = vpt_batch10(handle, imgs, batchsize, &results_b10);
// t2 = msecond();
// printf("debug mean batch process time: %.2f\n", (t2 - t1)/batchsize);
//debug end======================================================
//draw results
for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++){
printf("debug det num:%d\n",results[batchIdx].obj_count_);
// printf("debug det_b10 num:%d\n",results_b10[batchIdx].obj_count_);
}
|
9b4d4adf
Hu Chunming
添加定时抓拍;
|
260
261
262
263
264
|
for (int i = 0; i < batchsize; i++) {
delete [] results[i].obj_results_;
}
delete [] results;
|
0b4cd5d5
Hu Chunming
完成轨迹定时抓拍
|
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// debug================================
// for (int b = 0; b < batchsize; b++) {
// delete [] src[b].data.get();
// delete [] dvpp_data[b].data.get();
// // delete[] imgs[b].data_ ;
// imgs[b].data_ = NULL;
// }
// debug end============================
}
} }
}
vpt_release(&handle);
aclrtDestroyStream(stream);
aclrtDestroyContext(ctx);
aclrtResetDevice(param.devId);
aclFinalize();
return 0;
}
|