Skip to content

Commit c73df17

Browse files
committed
box2cs scale bugfix
1 parent 75cdae3 commit c73df17

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

Sources/KeypointDecoder/KeypointDecoder.mm

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
const size_t keypointsNumber = 17;
1212
const size_t modelWidth = 192;
1313
const size_t modelHeight = 256;
14+
const float aspect_ratio = modelWidth * 1.0 / modelHeight;
15+
const float pixel_std = 200.0;
1416

1517
@implementation KeypointDecoder
1618

@@ -72,12 +74,7 @@ -(void) run: (UIImage* ) uiImage
7274
std::vector<float> & center,
7375
std::vector<float> & scale)
7476
{
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);
8178

8279
std::vector<int> output_size = {modelWidth, modelHeight};
8380
cv::Mat trans;
@@ -288,6 +285,40 @@ - (UIImage*) renderHumanPose: (UIImage*) uiImage
288285
return preview;
289286
}
290287

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+
291322
struct HumanPose {
292323
std::vector<cv::Point2f> keypoints;
293324
std::vector<float> scores;

Sources/KeypointDecoderCPP/include/keypoint_postprocess.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,3 @@ void get_final_preds(std::vector<float>& heatmap,
5555
std::vector<float>& preds,
5656
int batchid,
5757
bool DARK = true);
58-
void box2cs(const std::vector<float> & box,
59-
std::vector<float> & center,
60-
std::vector<float> & scale,
61-
std::vector<float> image_size);

Sources/KeypointDecoderCPP/src/keypoint_postprocess.cc

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,3 @@ void get_final_preds(std::vector<float>& heatmap,
231231
std::vector<int> img_size{heatmap_width, heatmap_height};
232232
transform_preds(coords, center, scale, img_size, dim, preds);
233233
}
234-
235-
void box2cs(const std::vector<float> & box,
236-
std::vector<float> & center,
237-
std::vector<float> & scale,
238-
std::vector<float> image_size){
239-
float input_h = image_size[0];
240-
float input_w = image_size[1];
241-
float aspect_ratio = input_h / input_w;
242-
243-
float x = box[0];
244-
float y = box[1];
245-
float w = box[2];
246-
float h = box[3];
247-
float cx = x + w * 0.5;
248-
float cy = y + h * 0.5;
249-
center = {cx, cy};
250-
251-
if(w > aspect_ratio * h)
252-
h = w * 1.0 / aspect_ratio;
253-
else if(w < aspect_ratio * h)
254-
w = h * aspect_ratio;
255-
float cw = w / 200.0 * 1.25;
256-
float ch = h / 200.0 * 1.25;
257-
scale = {cw, ch};
258-
}

0 commit comments

Comments
 (0)