test.cpp.bk
15.2 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include "../HCP/HCP.h"
//#include <cv.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
//#include <cuda_runtime.h>
#include "sy_common.h"
#ifdef _WIN32
#include<windows.h>
#include<io.h>
#else
#include <sys/time.h>
#endif
using namespace std;
//using namespace cv;
string up[12] = { "T恤", "马甲/吊带/背心", "衬衫", "西服", "毛衣", "皮衣/夹克", "羽绒服", "大衣/风衣", "外套", "连衣裙", "无上衣", "其他"};
string up_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
string down[6] = { "长裤", "短裤", "长裙", "短裙", "连衣裙", "其他"};
string down_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
string bao[5] = { "无包", "单肩包", "双肩包", "其他", "钱包" };
string bag_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
string head[6] = { "长发", "短发", "光头", "帽子", "头盔", "其他" };
string clothing_text[5] = { "纯色", "碎花", "条纹", "格子", "其他" };
string sex[3] = { "男", "女", "不明"};
string figure[3] = { "胖", "瘦", "中" };
string nationality[5] = { "汉族", "维族", "黑人", "白人", "其他" };
string age[6] = { "幼儿", "儿童", "青年", "中年", "老年", "不明" };
string eye[4] = { "正常眼睛", "眼镜", "墨镜", "其他" };
string mouth[3] = { "正常嘴", "戴口罩", "其他" };
string weibo[3] = { "无围巾", "普通围巾", "包头围巾" };
string carColor[13] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他", "银" };
string orient[3] = { "正面", "背面", "侧面" };
string drivenum[4] = { "0人", "1人", "2人", "更多人" };
string dasan[2] = { "无", "有"};
string take[2] = { "无", "物品"};
const int FIR_INDEX_SIZE = 20;
const int SEC_INDEX_SIZE[FIR_INDEX_SIZE] = {12,12,6,12,5,12,6,5,3,3,5,6,4,3,3,13,3,4,2,2 };
#ifdef _WIN32
int getFiles(string path, vector<string>& nameList, vector<string>& pathList) {
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
int fileCount = 0;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
int count = getFiles(p.assign(path).append("\\").append(fileinfo.name), nameList, pathList);
fileCount += count;
//cout << fileinfo.name << " " << count << endl;
}
}
else {
fileCount++;
pathList.push_back(p.assign(path).append("\\").append(fileinfo.name));
nameList.push_back(fileinfo.name);
//cout << p.assign(path).append("\\").append(fileinfo.name) << endl;
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
return fileCount;
}
#endif
//****************** 返回所有小类别的结果 ********************//
void HCPAllResult()
{
printf("HCP Version: %s\n", HumanCarParsing_GetVersion());
#ifdef _WIN32
LARGE_INTEGER begin, end, freq;
QueryPerformanceFrequency(&freq);
#else
struct timeval tv1, tv2;
#endif
double totalTime = 0.0;
/*初始化*/
void *tools;
HCP_Param param;
param.mode = DEVICE_GPU;
param.gpuid = 0;
if (InitHCP(tools, param) != 0)
cout << "Init HCP Failed!" << endl;
int count =11;
// while (count--)
{
for (int index = 0; index < 10; index++)
// for (int index = 0; index < 1; index++)
{
char imgpath[260];
cv::Mat img;
#ifdef _MSC_VER
sprintf(imgpath, "../../data/HumanCarImageData/%d.jpg", index);// b);
#else
sprintf(imgpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", index);
#endif
img = cv::imread(imgpath);
/*初始化结果结构体*/
ctools_result *result;
/*人骑车结构化检测*/
#ifdef _WIN32
QueryPerformanceCounter(&begin);
#else
gettimeofday(&tv1, NULL);
#endif
HumanCarParsingProcess(tools, img.data, img.cols, img.rows, img.channels(), &result, true);
#ifdef _WIN32
QueryPerformanceCounter(&end);
printf("process time: %.2f\n", (double)(end.QuadPart - begin.QuadPart) * 1000 / (double)freq.QuadPart);
#else
gettimeofday(&tv2, NULL);
long dt1 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000;
long dt2 = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
printf("process time: %ld ms\n", dt2 - dt1);
#endif
/*输出保存结构*/
//cout << endl << imgpath << " " << endl;
cout << imgpath << " " << endl;
ctools_result &cur_result = result[0];
int big_class_count = cur_result.obj_count_;
if (big_class_count != FIR_INDEX_SIZE)
{
printf("big_class_count: %d, wrong result.\n", big_class_count);
break;
}
for (size_t i = 0; i < FIR_INDEX_SIZE; i++)
{
//各类别统计结果
int resIndex = 0;
float resMaxprob = -100;
ctools_obj_result &index_score = cur_result.obj_results_[i];
for (int j = 0; j < index_score.data_count_; j++)
{
if (index_score.data_[j] > resMaxprob)
{
resMaxprob = index_score.data_[j];
resIndex = j;
}
}
//输出结果
switch (i)
{
case 0:
cout << up[resIndex] << " ";
break;
case 1:
cout << up_color[resIndex] << " ";
break;
case 2:
cout << down[resIndex] << " ";
break;
case 3:
cout << down_color[resIndex] << " ";
break;
case 4:
cout << bao[resIndex] << " ";
break;
case 5:
cout << bag_color[resIndex] << " ";
break;
case 6:
cout << head[resIndex] << " ";
break;
case 7:
cout << clothing_text[resIndex] << " ";
break;
case 8:
cout << sex[resIndex] << " ";
break;
case 9:
cout << figure[resIndex] << " ";
break;
case 10:
cout << nationality[resIndex] << " ";
break;
case 11:
cout << age[resIndex] << " ";
break;
case 12:
cout << eye[resIndex] << " ";
break;
case 13:
cout << mouth[resIndex] << " ";
break;
case 14:
cout << weibo[resIndex] << " ";
break;
case 15:
cout << carColor[resIndex] << " ";
break;
case 16:
cout << orient[resIndex] << " ";
break;
case 17:
cout << drivenum[resIndex] << " ";
break;
case 18:
cout << dasan[resIndex] << " ";
break;
case 19:
cout << take[resIndex] << " ";
break;
default:
break;
}
}
cout << endl;
}
}
// cout << "AVG TIME: " << totalTime / 100 << endl;
/*释放资源*/
ReleaseHCP(tools);
}
//****************** 仅返回大类别的检测结果 ********************//
void HCPOneResult()
{
printf("HCP Version: %s\n", HumanCarParsing_GetVersion());
/*初始化*/
void *tools;
HCP_Param param;
param.mode = DEVICE_GPU;
param.gpuid = 0;
if (InitHCP(tools, param) != 0)
cout << "Init HCP Failed!" << endl;
for (int index = 0; index < 10; index++)
{
char imgpath[260];
cv::Mat img;
#ifdef _MSC_VER
sprintf(imgpath, "../../data/HumanCarImageData/%d.jpg", index);// b);
#else
sprintf(imgpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", index);
#endif
img = cv::imread(imgpath);
/*初始化结果结构体*/
ctools_result *result;
/*人骑车结构化检测*/
HumanCarParsingProcess(tools, img.data, img.cols, img.rows, img.channels(), &result, false);
/*输出保存结构*/
cout << endl << imgpath << " " << endl;
ctools_result &cur_result = result[0];
int big_class_count = cur_result.obj_count_;
ctools_obj_result &index_score = cur_result.obj_results_[big_class_count];
if (index_score.data_count_ != FIR_INDEX_SIZE * 2)
{
printf("count: %d, wrong result.\n",index_score.data_count_);
break;
}
for (size_t i = 0; i < FIR_INDEX_SIZE; i++)
{
//各类别统计结果
//int resIndex = result[i][0].index; //仅返回大类别种类时,直接用第一个元素的值即可
int resIndex = index_score.data_[i * 2];
//输出结果
switch (i)
{
case 0:
cout << up[resIndex] << " ";
break;
case 1:
cout << up_color[resIndex] << " ";
break;
case 2:
cout << down[resIndex] << " ";
break;
case 3:
cout << down_color[resIndex] << " ";
break;
case 4:
cout << bao[resIndex] << " ";
break;
case 5:
cout << bag_color[resIndex] << " ";
break;
case 6:
cout << head[resIndex] << " ";
break;
case 7:
cout << clothing_text[resIndex] << " ";
break;
case 8:
cout << sex[resIndex] << " ";
break;
case 9:
cout << figure[resIndex] << " ";
break;
case 10:
cout << nationality[resIndex] << " ";
break;
case 11:
cout << age[resIndex] << " ";
break;
case 12:
cout << eye[resIndex] << " ";
break;
case 13:
cout << mouth[resIndex] << " ";
break;
case 14:
cout << weibo[resIndex] << " ";
break;
case 15:
cout << carColor[resIndex] << " ";
break;
case 16:
cout << orient[resIndex] << " ";
break;
case 17:
cout << drivenum[resIndex] << " ";
break;
case 18:
cout << dasan[resIndex] << " ";
break;
case 19:
cout << take[resIndex] << " ";
break;
default:
break;
}
}
}
/*释放资源*/
ReleaseHCP(tools);
}
void HCPBatch()
{
//ofstream f("hcp_result.txt");//log
//vector<string> nameList;
//vector<string> pathList;
//int piccount = getFiles("E:/开发/视频结构化/视频结构化新框架修改/二次属性测试图片/二次属性测试图片/picHCP/", nameList, pathList);
//f << "total vehicle pic count = " << piccount << endl;
//const int batchsize = 10;
printf("HCP Version: %s\n", HumanCarParsing_GetVersion());
const int batchsize = 20;
void *tools;
HCP_Param param;
param.mode = DEVICE_GPU;
param.gpuid = 0;
if (InitHCP(tools, param) != 0)
cout << "Init HCP Failed!" << endl;
/*初始化结果结构体*/
ctools_result *result;
sy_img images[batchsize];
cv::Mat img[batchsize];
char strpath[1024];
memset(strpath, 0, sizeof(strpath));
for (int b = 0; b < batchsize; b++)
{
#ifdef _MSC_VER
sprintf(strpath, "../../data/HumanCarImageData/%d.jpg", b);
sprintf(strpath, "../../data/HCPTEST/%d.jpg", b);
#else
sprintf(strpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", b);
#endif
img[b] = cv::imread(strpath);
//img[b] = cv::imread(pathList[b]);
images[b].SetData(img[b].cols, img[b].rows, img[b].channels(), img[b].data);
}
#ifdef _MSC_VER
LARGE_INTEGER nFreq, nBeginTime, nEndTime;
QueryPerformanceFrequency(&nFreq);
#else
struct timeval t1, t2;
#endif
double time_val = 0;
vector<double> time_Array;
int count = 10000;
//while (count--)
{
#ifdef _MSC_VER
QueryPerformanceCounter(&nBeginTime);
#else
gettimeofday(&t1, NULL);
#endif
HumanCarParsingProcessBatch(tools, images, batchsize, &result);
#ifdef _MSC_VER
QueryPerformanceCounter(&nEndTime);
time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart;
printf("HumanCarParsingProcessBatch: %.2f ms \n", time_val);
#else
gettimeofday(&t2, NULL);
time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0;
printf("HumanCarParsingProcessBatch = %lf ms\n", time_val);
#endif
time_Array.push_back(time_val);
}
double total = 0;
for (int n = 1; n < time_Array.size(); n++)
{
total += time_Array[n];
}
printf("HumanCarParsingProcessBatch avg cost: %.2f\n", total / (time_Array.size() - 1));
for (int b = 0; b < batchsize; b++)
{
cout << "../../data/HumanCarImageData/"<< b << ".jpg " << endl;
ctools_result &cur_result = result[b];
int big_class_count = cur_result.obj_count_;
ctools_obj_result &index_score = cur_result.obj_results_[big_class_count];
//for (int i = 0; i < cur_result.obj_count_ + 1; i++)
// {
// ctools_obj_result &index_score_1 = cur_result.obj_results_[i];
//
// for (int j = 0; j < index_score_1.data_count_; j++)
// printf("%.2f ", index_score_1.data_[j]);
// printf("\n");
// }
if (index_score.data_count_ != FIR_INDEX_SIZE * 2)
{
printf("wrong result.\n");
break;
}
for (int i = 0; i < FIR_INDEX_SIZE; i++)
{
//int resIndex = result[j][i].index;
int resIndex = index_score.data_[i * 2];
//输出结果
switch (i)
{
case 0:
cout << up[resIndex] << " ";
break;
case 1:
cout << up_color[resIndex] << " ";
break;
case 2:
cout << down[resIndex] << " ";
break;
case 3:
cout << down_color[resIndex] << " ";
break;
case 4:
cout << bao[resIndex] << " ";
break;
case 5:
cout << bag_color[resIndex] << " ";
break;
case 6:
cout << head[resIndex] << " ";
break;
case 7:
cout << clothing_text[resIndex] << " ";
break;
case 8:
cout << sex[resIndex] << " ";
break;
case 9:
cout << figure[resIndex] << " ";
break;
case 10:
cout << nationality[resIndex] << " ";
break;
case 11:
cout << age[resIndex] << " ";
break;
case 12:
cout << eye[resIndex] << " ";
break;
case 13:
cout << mouth[resIndex] << " ";
break;
case 14:
cout << weibo[resIndex] << " ";
break;
case 15:
cout << carColor[resIndex] << " ";
break;
case 16:
cout << orient[resIndex] << " ";
break;
case 17:
cout << drivenum[resIndex] << " ";
break;
case 18:
cout << dasan[resIndex] << " ";
break;
case 19:
cout << take[resIndex] << " ";
break;
default:
break;
}
}
cout << endl;
//f << b << " - " << nameList[b] << endl;
//for (int i = 0; i < FIR_INDEX_SIZE; i++)
//{
// //int resIndex = result[j][i].index;
// int resIndex = index_score.data_[i * 2];
// //输出结果
// switch (i)
// {
// case 0:
// f << up[resIndex] << "_";
// break;
// case 1:
// f << up_color[resIndex] << "_";
// break;
// case 2:
// f<< down[resIndex] << "_";
// break;
// case 3:
// f<< down_color[resIndex] << "_";
// break;
// case 4:
// f<< bao[resIndex] << "_";
// break;
// case 5:
// f << bag_color[resIndex] << "_";
// break;
// case 6:
// f << head[resIndex] << "_";
// break;
// case 7:
// f << clothing_text[resIndex] << "_";
// break;
// case 8:
// f << sex[resIndex] << "_";
// break;
// case 9:
// f << figure[resIndex] << "_";
// break;
// case 10:
// f << nationality[resIndex] << "_";
// break;
// case 11:
// f << age[resIndex] << "_";
// break;
// case 12:
// f << eye[resIndex] << "_";
// break;
// case 13:
// f << mouth[resIndex] << "_";
// break;
// case 14:
// f << weibo[resIndex] << "_";
// break;
// case 15:
// f << carColor[resIndex] << "_";
// break;
// case 16:
// f << orient[resIndex] << "_";
// break;
// case 17:
// f << drivenum[resIndex] << "_";
// break;
// case 18:
// f << dasan[resIndex] << "_";
// break;
// case 19:
// f << take[resIndex] << "_";
// break;
// default:
// break;
// }
//}
//f << endl;
//f << endl;
}
ReleaseHCP(tools);
}
int main()
{
HCPBatch();
//int count = 100;
//while (count--)
// HCPAllResult(); //返回所有小类别的检测结果
// HCPOneResult(); //仅返回大类别的检测结果
#ifdef _MSC_VER
system("pause");
#endif
return 0;
}