|
11 | 11 | const size_t keypointsNumber = 17; |
12 | 12 | const size_t modelWidth = 192; |
13 | 13 | const size_t modelHeight = 256; |
| 14 | +const float aspect_ratio = modelWidth * 1.0 / modelHeight; |
| 15 | +const float pixel_std = 200.0; |
14 | 16 |
|
15 | 17 | @implementation KeypointDecoder |
16 | 18 |
|
@@ -72,12 +74,7 @@ -(void) run: (UIImage* ) uiImage |
72 | 74 | std::vector<float> & center, |
73 | 75 | std::vector<float> & scale) |
74 | 76 | { |
75 | | - float imageWidth = image.cols; |
76 | | - float imageHeight = image.rows; |
77 | | - |
78 | | - std::vector<float> _box(box); |
79 | | - std::vector<float> image_size {imageHeight, imageWidth}; |
80 | | - box2cs(_box, center, scale, image_size); |
| 77 | + box2cs(box, center, scale); |
81 | 78 |
|
82 | 79 | std::vector<int> output_size = {modelWidth, modelHeight}; |
83 | 80 | cv::Mat trans; |
@@ -288,6 +285,40 @@ - (UIImage*) renderHumanPose: (UIImage*) uiImage |
288 | 285 | return preview; |
289 | 286 | } |
290 | 287 |
|
| 288 | +std::vector<float> xywh2cs(float x, float y, float w, float h) { |
| 289 | + std::vector<float> center(2, 0); |
| 290 | + center[0] = x + w * 0.5; |
| 291 | + center[1] = y + h * 0.5; |
| 292 | + |
| 293 | + if (w > aspect_ratio * h) { |
| 294 | + h = w * 1.0 / aspect_ratio; |
| 295 | + } else if (w < aspect_ratio * h) { |
| 296 | + w = h * aspect_ratio; |
| 297 | + } |
| 298 | + std::vector<float> scale = {static_cast<float>(w * 1.0 / pixel_std), static_cast<float>(h * 1.0 / pixel_std)}; |
| 299 | + if (center[0] != -1) { |
| 300 | + std::transform(scale.begin(), scale.end(), scale.begin(), |
| 301 | + std::bind(std::multiplies<float>(), std::placeholders::_1, 1.25)); |
| 302 | + } |
| 303 | + return {center[0], center[1], scale[0], scale[1]}; |
| 304 | +} |
| 305 | + |
| 306 | +void box2cs(const std::vector<float> & box, |
| 307 | + std::vector<float> & center, |
| 308 | + std::vector<float> & scale){ |
| 309 | + |
| 310 | + float x, y, w, h; |
| 311 | + x = box[0]; |
| 312 | + y = box[1]; |
| 313 | + w = box[2]; |
| 314 | + h = box[3]; |
| 315 | + const std::vector<float> & bbox = xywh2cs(x, y, w, h); |
| 316 | + |
| 317 | + center = { bbox[0], bbox[1] }; |
| 318 | + scale = { bbox[2], bbox[3] }; |
| 319 | + |
| 320 | +} |
| 321 | + |
291 | 322 | struct HumanPose { |
292 | 323 | std::vector<cv::Point2f> keypoints; |
293 | 324 | std::vector<float> scores; |
|
0 commit comments