diff --git a/deploy/cpp_infer/include/preprocess_op.h b/deploy/cpp_infer/include/preprocess_op.h index bda7d123..a19bac95 100644 --- a/deploy/cpp_infer/include/preprocess_op.h +++ b/deploy/cpp_infer/include/preprocess_op.h @@ -62,4 +62,4 @@ public: const std::vector &rec_image_shape = {3, 48, 192}); }; -} // namespace PaddleOCR \ No newline at end of file +} // namespace PaddleOCR diff --git a/deploy/cpp_infer/src/preprocess_op.cpp b/deploy/cpp_infer/src/preprocess_op.cpp index 1d3e31a1..e2f87e5a 100644 --- a/deploy/cpp_infer/src/preprocess_op.cpp +++ b/deploy/cpp_infer/src/preprocess_op.cpp @@ -81,7 +81,7 @@ void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img, else if (resize_h / 32 < 1 + 1e-5) resize_h = 32; else - resize_h = (resize_h / 32 - 1) * 32; + resize_h = resize_h / 32 * 32; if (resize_w % 32 == 0) resize_w = resize_w; @@ -96,13 +96,17 @@ void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img, ratio_w = float(resize_w) / float(w); } -void CrnnResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img, float wh_ratio, +void CrnnResizeImg::Run(const cv::Mat &img, cv::Mat &pad_resize_img, + float max_wh_ratio, const std::vector &rec_image_shape) { int imgC, imgH, imgW; imgC = rec_image_shape[0]; imgH = rec_image_shape[1]; imgW = rec_image_shape[2]; + float wh_ratio = 1.0 * imgW / imgH; + wh_ratio = std::max(max_wh_ratio, wh_ratio); + imgW = int(32 * wh_ratio); float ratio = float(img.cols) / float(img.rows); @@ -112,8 +116,12 @@ void CrnnResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img, float wh_ratio, else resize_w = int(ceilf(imgH * ratio)); + cv::Mat resize_img; cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f, cv::INTER_LINEAR); + cv::copyMakeBorder(resize_img, pad_resize_img, 0, 0, 0, + int(imgW - resize_img.cols), cv::BORDER_CONSTANT, + {127, 127, 127}); } void ClsResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img,