Blame view

src/vpt_det_demo/main.cpp 4.01 KB
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
  #ifdef _MSC_VER
  #include "../vpt_det/vpt.h"
  #else
  #include "vpt.h"
  #endif
  
  #include <highgui.h>
  #include <opencv2/opencv.hpp>
  #include <fstream>
  #include <iostream>
  #include "sy_common.h"
  #include "utools.h"
  #include <vector>
  
  #ifdef _WIN32
  #include<windows.h>
  #else
  #include <sys/time.h>
  #endif
  
  
  int main()
  {
  
  #ifdef _MSC_VER
  	LARGE_INTEGER nFreq, nBeginTime, nEndTime;
  	QueryPerformanceFrequency(&nFreq);
  #else
  	struct timeval t1, t2;
  #endif
  	double time_val = 0;
  	std::vector<double> time_Array;
  
  	CvFont font;
  	cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0, 0, 1, 3);
  	int fontFace = CV_FONT_HERSHEY_COMPLEX;
  	double fontScale = 1;
  	int thickness = 2;
  	static std::string type[9] = { "person", "bike", "motor", "tricycle", "car", "bigbus", "lorry", "tractor", "midibus" };
  
  	void* handle;
  	
  	vpt_param param;
  	param.mode = DEVICE_GPU;
  	param.gpuid = 0;
  	param.threshold = 0.6;
   param.engine = ENGINE_TENSORRT;
  	param.preprocess_param = 
  		"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"
  		;
  	param.serialize_file = "FPN_VPT";
  	param.max_batch = 20;
  	vpt_init(&handle, param);
  
  
  	const int batchsize = 10;
  
  
  	/*³õʼ»¯½á¹û½á¹¹Ìå*/
  	vpt_result *result = new vpt_result[batchsize];
  	for (int b = 0; b < batchsize; b++)
  	{
  		result[b].obj_count_ = 0;
  		result[b].obj_results_ = new vpt_obj_result[MAX_DET_COUNT];
  	}
  
  	//CTOOLS_Img *images = new CTOOLS_Img[batchsize];
  	//cv::Mat img;
  	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/%d.jpg", b);// b);
  #else
  		sprintf(strpath, "../../data/%d.jpg", b);
  
  #endif
  
  		//cv::Mat img/*[b]*/;
  		//memset(&img, 0, sizeof(img));
  		img[b] = cv::imread(strpath);
  		images[b].set_data(img[b].cols, img[b].rows, img[b].channels(), img[b].data);
  	}
  
  
  	//vpt_batch(handle, images, batchsize, &result);
  
  	int count = 1000;
  	while (count--)
  	{
  
  #ifdef _MSC_VER
  		QueryPerformanceCounter(&nBeginTime);
  #else
  		gettimeofday(&t1, NULL);
  #endif
  
  		vpt_batch(handle, images, batchsize, &result);
  
  #ifdef _MSC_VER
  		QueryPerformanceCounter(&nEndTime);
  		time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart;
  		printf("vpt_batch cost: %.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("vpt_batch cost= %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("batch size: %d, vpt_batch avg cost: %.2f\n", batchsize, total / (time_Array.size() - 1));
  
  	for (int b = 0; b < batchsize; b++)
  	{
  		std::cout << "b: " << b << ", count: " << result[b].obj_count_ << std::endl;
  		for (int c = 0; c < result[b].obj_count_; c++)
  		{
  			char str_i[100];
  			sprintf(str_i, "%s:%.2f", type[result[b].obj_results_[c].obj_index].c_str(), result[b].obj_results_[c].obj_score);
  
  			rectangle(img[b],
  				cvPoint(result[b].obj_results_[c].obj_rect.left_ - 5, result[b].obj_results_[c].obj_rect.top_ - 15),
  				cvPoint(result[b].obj_results_[c].obj_rect.left_ + result[b].obj_results_[c].obj_rect.width_ + 5,
  					result[b].obj_results_[c].obj_rect.top_ + result[b].obj_results_[c].obj_rect.height_ + 10),
  				cv::Scalar(127, 64, 120), 3, 1);
  			cv::putText(img[b], str_i,
  				cv::Point(result[b].obj_results_[c].obj_rect.left_, result[b].obj_results_[c].obj_rect.top_),
  				fontFace, fontScale,
  				cv::Scalar(58, 158, 199), thickness, 8);
  
  		}
  
  #ifdef _MSC_VER
  		//cv::imshow("RES", image);
  		//cv::waitKey(0);
  
  		cv::Mat showImg;
  		cv::resize(img[b], showImg, cv::Size(640, 480));
  		cv::imshow("image", showImg);
  		cv::waitKey(0);
  #else
  		sprintf(strpath, "vpt_result/%d.jpg", b);
  		cv::imwrite(strpath, img[b]);
  #endif
  
  	}
  	vpt_release(&handle);
  
  	return 0;
  
  
  
  }