forked from OAID/Tengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm_openpose.cpp
285 lines (248 loc) · 8.48 KB
/
tm_openpose.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
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
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "common.h"
#include "tengine_c_api.h"
#include "tengine_operations.h"
#define COCO
#define DEFAULT_REPEAT_COUNT 1
#define DEFAULT_THREAD_COUNT 1
#ifdef MPI
const int POSE_PAIRS[14][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {1, 5}, {5, 6}, {6, 7},
{1, 14}, {14, 8}, {8, 9}, {9, 10}, {14, 11}, {11, 12}, {12, 13}};
// std::string model_file = "models/openpose_mpi.tmfile";
int nPoints = 15;
#endif
#ifdef COCO
const int POSE_PAIRS[17][2] = {{1, 2}, {1, 5}, {2, 3}, {3, 4}, {5, 6}, {6, 7}, {1, 8}, {8, 9}, {9, 10},
{1, 11}, {11, 12}, {12, 13}, {1, 0}, {0, 14}, {14, 16}, {0, 15}, {15, 17}};
// std::string model_file = "models/openpose_coco.tmfile";
int nPoints = 18;
#endif
#ifdef BODY25
const int POSE_PAIRS[24][2] = {{1, 2}, {1, 5}, {2, 3}, {3, 4}, {5, 6}, {6, 7}, {1, 8}, {8, 9},
{9, 10}, {10, 11}, {11, 24}, {11, 22}, {22, 23}, {8, 12}, {12, 13}, {13, 14},
{14, 21}, {14, 19}, {19, 20}, {1, 0}, {0, 15}, {16, 18}, {0, 16}, {15, 17}};
// std::string model_file = "models/openpose_body25.tmfile"
int nPoints = 25;
#endif
void get_input_data_pose(cv::Mat img, float* input_data, int img_h, int img_w)
{
cv::resize(img, img, cv::Size(img_h, img_w));
img.convertTo(img, CV_32FC3);
float* img_data = ( float* )img.data;
int hw = img_h * img_w;
double scalefactor = 1.0 / 255;
float mean[3] = {0, 0, 0};
for (int h = 0; h < img_h; h++)
{
for (int w = 0; w < img_w; w++)
{
for (int c = 0; c < 3; c++)
{
input_data[c * hw + h * img_w + w] = scalefactor * (*img_data - mean[c]);
img_data++;
}
}
}
}
void post_process_pose(cv::Mat img, cv::Mat frameCopy, float threshold, float* outdata, int num, int H, int W)
{
std::vector<cv::Point> points(nPoints);
int frameWidth = img.rows;
int frameHeight = img.cols;
std::cout << "KeyPoints Coordinate:" << std::endl;
for (int n = 0; n < num; n++)
{
cv::Point maxloc;
int piexlNums = H * W;
double prob = -1;
for (int piexl = 0; piexl < piexlNums; ++piexl)
{
if (outdata[piexl] > prob)
{
prob = outdata[piexl];
maxloc.y = ( int )piexl / H;
maxloc.x = ( int )piexl % W;
}
}
cv::Point2f p(-1, -1);
if (prob > threshold)
{
p = maxloc;
p.y *= ( float )frameWidth / W;
p.x *= ( float )frameHeight / H;
cv::circle(frameCopy, cv::Point(( int )p.x, ( int )p.y), 8, cv::Scalar(0, 255, 255), -1);
cv::putText(frameCopy, cv::format("%d", n), cv::Point(( int )p.x, ( int )p.y), cv::FONT_HERSHEY_COMPLEX, 1,
cv::Scalar(0, 0, 255), 3);
}
points[n] = p;
std::cout << n << ":" << p << std::endl;
outdata += piexlNums;
}
int nPairs = sizeof(POSE_PAIRS) / sizeof(POSE_PAIRS[0]);
for (int n = 0; n < nPairs; n++)
{
cv::Point2f partA = points[POSE_PAIRS[n][0]];
cv::Point2f partB = points[POSE_PAIRS[n][1]];
if (partA.x <= 0 || partA.y <= 0 || partB.x <= 0 || partB.y <= 0)
continue;
cv::line(img, partA, partB, cv::Scalar(0, 255, 255), 8);
cv::circle(img, partA, 8, cv::Scalar(0, 0, 255), -1);
cv::circle(img, partB, 8, cv::Scalar(0, 0, 255), -1);
}
}
void show_usage()
{
fprintf(stderr, "[Usage]: [-h]\n [-m model_file] [-i image_file] [-r repeat_count] [-t thread_count]\n");
}
int main(int argc, char* argv[])
{
const char* model_file = nullptr;
const char* image_file = nullptr;
int repeat_count = DEFAULT_REPEAT_COUNT;
int num_thread = DEFAULT_THREAD_COUNT;
int img_h = 368;
int img_w = 368;
int res;
while ((res = getopt(argc, argv, "m:i:r:t:h:")) != -1)
{
switch (res)
{
case 'm':
model_file = optarg;
break;
case 'i':
image_file = optarg;
break;
case 'r':
repeat_count = atoi(optarg);
break;
case 't':
num_thread = atoi(optarg);
break;
case 'h':
show_usage();
return 0;
default:
break;
}
}
/* check files */
if (model_file == nullptr)
{
fprintf(stderr, "Error: Tengine model file not specified!\n");
show_usage();
return -1;
}
if (image_file == nullptr)
{
fprintf(stderr, "Error: Image file not specified!\n");
show_usage();
return -1;
}
if (!check_file_exist(model_file) || !check_file_exist(image_file))
return -1;
/* inital tengine */
init_tengine();
fprintf(stderr, "tengine-lite library version: %s\n", get_tengine_version());
/* create graph, load tengine model xxx.tmfile */
graph_t graph = create_graph(nullptr, "tengine", model_file);
if (graph == nullptr)
{
std::cout << "Create graph0 failed\n";
std::cout << "errno: " << get_tengine_errno() << "\n";
return -1;
}
/* set the input shape to initial the graph, and prerun graph to infer shape */
int channel = 3;
int img_size = img_h * img_w * channel;
int dims[] = {1, channel, img_h, img_w}; // nchw
float* input_data = ( float* )malloc(sizeof(float) * img_size);
tensor_t input_tensor = get_graph_input_tensor(graph, 0, 0);
if (input_tensor == nullptr)
{
fprintf(stderr, "Get input tensor failed\n");
return -1;
}
if (set_tensor_shape(input_tensor, dims, 4) < 0)
{
fprintf(stderr, "Set input tensor shape failed\n");
return -1;
};
if (prerun_graph(graph) < 0)
{
fprintf(stderr, "Prerun graph failed\n");
return -1;
}
/* prepare process input data, set the data mem to input tensor */
cv::Mat frame = cv::imread(image_file);
get_input_data_pose(frame, input_data, img_h, img_w);
// set_tensor_buffer(input_tensor, input_data, img_size*4);
if (set_tensor_buffer(input_tensor, input_data, img_size * 4) < 0)
{
fprintf(stderr, "Set input tensor buffer failed\n");
return -1;
}
/* run graph */
double min_time = __DBL_MAX__;
double max_time = -__DBL_MAX__;
double total_time = 0.;
for (int i = 0; i < 1; i++)
{
double start = get_current_time();
if (run_graph(graph, 1) < 0)
{
fprintf(stderr, "Run graph failed\n");
return -1;
}
double end = get_current_time();
double cur = end - start;
total_time += cur;
min_time = std::min(min_time, cur);
max_time = std::max(max_time, cur);
}
fprintf(stderr, "Repeat %d times, thread %d, avg time %.2f ms, max_time %.2f ms, min_time %.2f ms\n", 1, 1,
total_time, max_time, min_time);
fprintf(stderr, "--------------------------------------\n");
/* get output tensor */
tensor_t out_tensor = get_graph_output_tensor(graph, 0, 0);
int out_dim[4];
if (get_tensor_shape(out_tensor, out_dim, 4) <= 0)
{
std::cout << "get tensor shape failed, errno: " << get_tengine_errno() << "\n";
return 1;
}
float* outdata = ( float* )get_tensor_buffer(out_tensor);
int num = nPoints;
int H = out_dim[2];
int W = out_dim[3];
float show_threshold = 0.1;
cv::Mat frameCopy = frame.clone();
post_process_pose(frame, frameCopy, show_threshold, outdata, num, H, W);
cv::imwrite("Output-Keypionts.jpg", frameCopy);
cv::imwrite("Output-Skeleton.jpg", frame);
// Release memory for input tensor
release_graph_tensor(input_tensor);
// free output tensor memory
release_graph_tensor(out_tensor);
// Release memory for each node memory of tengine graph
if (postrun_graph(graph) != 0)
{
std::cout << "Postrun graph failed, errno: " << get_tengine_errno() << "\n";
return 1;
}
// free memory for input data
free(input_data);
// destory tengine graph
destroy_graph(graph);
// release all memory of tenging that allocate at beginning
release_tengine();
return 0;
}