From 7e008591162a39e7d49b07d34a805b297962a881 Mon Sep 17 00:00:00 2001 From: zhiboniu Date: Thu, 30 Jun 2022 11:10:36 +0000 Subject: [PATCH] update ema filter; refix keypoint transform to orgimage --- deploy/lite/include/keypoint_postprocess.h | 3 ++- deploy/lite/src/keypoint_postprocess.cc | 25 +++++++++++++++++----- deploy/python/det_keypoint_unite_infer.py | 12 ++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/deploy/lite/include/keypoint_postprocess.h b/deploy/lite/include/keypoint_postprocess.h index 0d1e747f3..4e0e54c26 100644 --- a/deploy/lite/include/keypoint_postprocess.h +++ b/deploy/lite/include/keypoint_postprocess.h @@ -33,7 +33,8 @@ void transform_preds(std::vector& coords, std::vector& scale, std::vector& output_size, std::vector& dim, - std::vector& target_coords); + std::vector& target_coords, + bool affine); void box_to_center_scale(std::vector& box, int width, int height, diff --git a/deploy/lite/src/keypoint_postprocess.cc b/deploy/lite/src/keypoint_postprocess.cc index 5f28d2adc..6c75ece87 100644 --- a/deploy/lite/src/keypoint_postprocess.cc +++ b/deploy/lite/src/keypoint_postprocess.cc @@ -74,11 +74,26 @@ void transform_preds(std::vector& coords, std::vector& scale, std::vector& output_size, std::vector& dim, - std::vector& target_coords) { - cv::Mat trans(2, 3, CV_64FC1); - get_affine_transform(center, scale, 0, output_size, trans, 1); - for (int p = 0; p < dim[1]; ++p) { - affine_tranform(coords[p * 2], coords[p * 2 + 1], trans, target_coords, p); + std::vector& target_coords, + bool affine=false) { + if (affine) { + cv::Mat trans(2, 3, CV_64FC1); + get_affine_transform(center, scale, 0, output_size, trans, 1); + for (int p = 0; p < dim[1]; ++p) { + affine_tranform( + coords[p * 2], coords[p * 2 + 1], trans, target_coords, p); + } + } else { + float heat_w = static_cast(output_size[0]); + float heat_h = static_cast(output_size[1]); + float x_scale = scale[0] / heat_w; + float y_scale = scale[1] / heat_h; + float offset_x = center[0] - scale[0] / 2.; + float offset_y = center[1] - scale[1] / 2.; + for (int i = 0; i < dim[1]; i++) { + target_coords[i * 3 + 1] = x_scale * coords[i * 2] + offset_x; + target_coords[i * 3 + 2] = y_scale * coords[i * 2 + 1] + offset_y; + } } } diff --git a/deploy/python/det_keypoint_unite_infer.py b/deploy/python/det_keypoint_unite_infer.py index f0b80752e..df22cde8d 100644 --- a/deploy/python/det_keypoint_unite_infer.py +++ b/deploy/python/det_keypoint_unite_infer.py @@ -165,7 +165,7 @@ def topdown_unite_predict_video(detector, frame2, results, topdown_keypoint_detector, keypoint_batch_size, FLAGS.run_benchmark) - if FLAGS.smooth and len(keypoint_res['keypoint'][0])==1: + if FLAGS.smooth and len(keypoint_res['keypoint'][0]) == 1: current_keypoints = np.array(keypoint_res['keypoint'][0][0]) smooth_keypoints = keypoint_smoothing.smooth_process( current_keypoints) @@ -215,7 +215,7 @@ class KeypointSmoothing(object): fc_d=0.1, fc_min=0.1, beta=0.1, - thres_mult=0.2): + thres_mult=0.3): super(KeypointSmoothing, self).__init__() self.image_width = width self.image_height = height @@ -234,7 +234,7 @@ class KeypointSmoothing(object): if self.filter_type == 'OneEuro': self.smooth_func = self.one_euro_filter elif self.filter_type == 'EMA': - self.smooth_func = self.exponential_smoothing + self.smooth_func = self.ema_filter else: raise ValueError('filter type must be one_euro or ema') @@ -261,6 +261,7 @@ class KeypointSmoothing(object): else: result = self.smooth_func(current_keypoint, self.x_prev_hat[index], index) + return result def one_euro_filter(self, x_cur, x_pre, index): @@ -276,6 +277,11 @@ class KeypointSmoothing(object): self.x_prev_hat[index] = x_cur_hat return x_cur_hat + def ema_filter(self, x_cur, x_pre, index): + x_cur_hat = self.exponential_smoothing(x_cur, x_pre) + self.x_prev_hat[index] = x_cur_hat + return x_cur_hat + def smoothing_factor(self, te, fc): r = 2 * math.pi * fc * te return r / (r + 1) -- GitLab