From 8f028d38cc24b43eec94066045fcb7fd468f41be Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Mon, 13 Jul 2020 21:02:37 +0800 Subject: [PATCH] add static_cast --- deploy/lite/crnn_process.cc | 18 +++++--- deploy/lite/db_post_process.cc | 81 ++++++++++++++++++++-------------- deploy/lite/ocr_db_crnn.cc | 53 +++++++++++----------- 3 files changed, 88 insertions(+), 64 deletions(-) diff --git a/deploy/lite/crnn_process.cc b/deploy/lite/crnn_process.cc index 9f3df37d27..7528f36fe6 100644 --- a/deploy/lite/crnn_process.cc +++ b/deploy/lite/crnn_process.cc @@ -27,12 +27,12 @@ cv::Mat CrnnResizeImg(cv::Mat img, float wh_ratio) { imgW = int(32 * wh_ratio); - float ratio = float(img.cols) / float(img.rows); + float ratio = static_cast(img.cols) / static_cast(img.rows); int resize_w, resize_h; if (ceilf(imgH * ratio) > imgW) resize_w = imgW; else - resize_w = int(ceilf(imgH * ratio)); + resize_w = static_cast(ceilf(imgH * ratio)); cv::Mat resize_img; cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f, cv::INTER_LINEAR); @@ -76,10 +76,12 @@ cv::Mat GetRotateCropImage(cv::Mat srcimage, points[i][1] -= top; } - int img_crop_width = int(sqrt(pow(points[0][0] - points[1][0], 2) + - pow(points[0][1] - points[1][1], 2))); - int img_crop_height = int(sqrt(pow(points[0][0] - points[3][0], 2) + - pow(points[0][1] - points[3][1], 2))); + int img_crop_width = + static_cast(sqrt(pow(points[0][0] - points[1][0], 2) + + pow(points[0][1] - points[1][1], 2))); + int img_crop_height = + static_cast(sqrt(pow(points[0][0] - points[3][0], 2) + + pow(points[0][1] - points[3][1], 2))); cv::Point2f pts_std[4]; pts_std[0] = cv::Point2f(0., 0.); @@ -100,7 +102,9 @@ cv::Mat GetRotateCropImage(cv::Mat srcimage, cv::Size(img_crop_width, img_crop_height), cv::BORDER_REPLICATE); - if (float(dst_img.rows) >= float(dst_img.cols) * 1.5) { + const float ratio = 1.5; + if (static_cast(dst_img.rows) >= + static_cast(dst_img.cols) * ratio) { cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth()); cv::transpose(dst_img, srcCopy); cv::flip(srcCopy, srcCopy, 0); diff --git a/deploy/lite/db_post_process.cc b/deploy/lite/db_post_process.cc index a6cffe7b8e..ff9caa2f11 100644 --- a/deploy/lite/db_post_process.cc +++ b/deploy/lite/db_post_process.cc @@ -42,10 +42,14 @@ cv::RotatedRect Unclip(std::vector> box, ClipperLib::ClipperOffset offset; ClipperLib::Path p; - p << ClipperLib::IntPoint(int(box[0][0]), int(box[0][1])) - << ClipperLib::IntPoint(int(box[1][0]), int(box[1][1])) - << ClipperLib::IntPoint(int(box[2][0]), int(box[2][1])) - << ClipperLib::IntPoint(int(box[3][0]), int(box[3][1])); + p << ClipperLib::IntPoint(static_cast(box[0][0]), + static_cast(box[0][1])) + << ClipperLib::IntPoint(static_cast(box[1][0]), + static_cast(box[1][1])) + << ClipperLib::IntPoint(static_cast(box[2][0]), + static_cast(box[2][1])) + << ClipperLib::IntPoint(static_cast(box[3][0]), + static_cast(box[3][1])); offset.AddPath(p, ClipperLib::jtRound, ClipperLib::etClosedPolygon); ClipperLib::Paths soln; @@ -149,23 +153,31 @@ float BoxScoreFast(std::vector> box_array, cv::Mat pred) { float box_x[4] = {array[0][0], array[1][0], array[2][0], array[3][0]}; float box_y[4] = {array[0][1], array[1][1], array[2][1], array[3][1]}; - int xmin = clamp(int(std::floorf(*(std::min_element(box_x, box_x + 4)))), 0, - width - 1); - int xmax = clamp(int(std::ceilf(*(std::max_element(box_x, box_x + 4)))), 0, - width - 1); - int ymin = clamp(int(std::floorf(*(std::min_element(box_y, box_y + 4)))), 0, - height - 1); - int ymax = clamp(int(std::ceilf(*(std::max_element(box_y, box_y + 4)))), 0, - height - 1); + int xmin = clamp( + static_cast(std::floorf(*(std::min_element(box_x, box_x + 4)))), 0, + width - 1); + int xmax = + clamp(static_cast(std::ceilf(*(std::max_element(box_x, box_x + 4)))), + 0, width - 1); + int ymin = clamp( + static_cast(std::floorf(*(std::min_element(box_y, box_y + 4)))), 0, + height - 1); + int ymax = + clamp(static_cast(std::ceilf(*(std::max_element(box_y, box_y + 4)))), + 0, height - 1); cv::Mat mask; mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1); cv::Point root_point[4]; - root_point[0] = cv::Point(int(array[0][0]) - xmin, int(array[0][1]) - ymin); - root_point[1] = cv::Point(int(array[1][0]) - xmin, int(array[1][1]) - ymin); - root_point[2] = cv::Point(int(array[2][0]) - xmin, int(array[2][1]) - ymin); - root_point[3] = cv::Point(int(array[3][0]) - xmin, int(array[3][1]) - ymin); + root_point[0] = cv::Point(static_cast(array[0][0]) - xmin, + static_cast(array[0][1]) - ymin); + root_point[1] = cv::Point(static_cast(array[1][0]) - xmin, + static_cast(array[1][1]) - ymin); + root_point[2] = cv::Point(static_cast(array[2][0]) - xmin, + static_cast(array[2][1]) - ymin); + root_point[3] = cv::Point(static_cast(array[3][0]) - xmin, + static_cast(array[3][1]) - ymin); const cv::Point *ppt[1] = {root_point}; int npt[] = {4}; cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1)); @@ -183,8 +195,8 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap, std::map Config) { const int min_size = 3; const int max_candidates = 1000; - const float box_thresh = float(Config["det_db_box_thresh"]); - const float unclip_ratio = float(Config["det_db_unclip_ratio"]); + const float box_thresh = static_cast(Config["det_db_box_thresh"]); + const float unclip_ratio = static_cast(Config["det_db_unclip_ratio"]); int width = bitmap.cols; int height = bitmap.rows; @@ -233,12 +245,13 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap, std::vector> intcliparray; for (int num_pt = 0; num_pt < 4; num_pt++) { - std::vector a{int(clamp(roundf(cliparray[num_pt][0] / float(width) * - float(dest_width)), - float(0), float(dest_width))), - int(clamp(roundf(cliparray[num_pt][1] / float(height) * - float(dest_height)), - float(0), float(dest_height)))}; + std::vector a{ + static_cast(clamp( + roundf(cliparray[num_pt][0] / float(width) * float(dest_width)), + float(0), float(dest_width))), + static_cast(clamp( + roundf(cliparray[num_pt][1] / float(height) * float(dest_height)), + float(0), float(dest_height)))}; intcliparray.push_back(a); } boxes.push_back(intcliparray); @@ -254,23 +267,27 @@ FilterTagDetRes(std::vector>> boxes, float ratio_h, int oriimg_w = srcimg.cols; std::vector>> root_points; - for (int n = 0; n < boxes.size(); n++) { + for (int n = 0; n < static_cast(boxes.size()); n++) { boxes[n] = OrderPointsClockwise(boxes[n]); - for (int m = 0; m < boxes[0].size(); m++) { + for (int m = 0; m < static_cast(boxes[0].size()); m++) { boxes[n][m][0] /= ratio_w; boxes[n][m][1] /= ratio_h; - boxes[n][m][0] = int(std::min(std::max(boxes[n][m][0], 0), oriimg_w - 1)); - boxes[n][m][1] = int(std::min(std::max(boxes[n][m][1], 0), oriimg_h - 1)); + boxes[n][m][0] = + static_cast(std::min(std::max(boxes[n][m][0], 0), oriimg_w - 1)); + boxes[n][m][1] = + static_cast(std::min(std::max(boxes[n][m][1], 0), oriimg_h - 1)); } } for (int n = 0; n < boxes.size(); n++) { int rect_width, rect_height; - rect_width = int(sqrt(pow(boxes[n][0][0] - boxes[n][1][0], 2) + - pow(boxes[n][0][1] - boxes[n][1][1], 2))); - rect_height = int(sqrt(pow(boxes[n][0][0] - boxes[n][3][0], 2) + - pow(boxes[n][0][1] - boxes[n][3][1], 2))); + rect_width = + static_cast(sqrt(pow(boxes[n][0][0] - boxes[n][1][0], 2) + + pow(boxes[n][0][1] - boxes[n][1][1], 2))); + rect_height = + static_cast(sqrt(pow(boxes[n][0][0] - boxes[n][3][0], 2) + + pow(boxes[n][0][1] - boxes[n][3][1], 2))); if (rect_width <= 10 || rect_height <= 10) continue; root_points.push_back(boxes[n]); diff --git a/deploy/lite/ocr_db_crnn.cc b/deploy/lite/ocr_db_crnn.cc index d251df3fb4..547afbf0a5 100644 --- a/deploy/lite/ocr_db_crnn.cc +++ b/deploy/lite/ocr_db_crnn.cc @@ -22,9 +22,9 @@ using namespace paddle::lite_api; // NOLINT using namespace std; // fill tensor with mean and scale and trans layout: nhwc -> nchw, neon speed up -void neon_mean_scale(const float *din, float *dout, int size, - const std::vector mean, - const std::vector scale) { +void NeonMeanScale(const float *din, float *dout, int size, + const std::vector mean, + const std::vector scale) { if (mean.size() != 3 || scale.size() != 3) { std::cerr << "[ERROR] mean or scale size must equal to 3\n"; exit(1); @@ -75,14 +75,14 @@ cv::Mat DetResizeImg(const cv::Mat img, int max_size_len, int max_wh = w >= h ? w : h; if (max_wh > max_size_len) { if (h > w) { - ratio = float(max_size_len) / float(h); + ratio = static_cast(max_size_len) / static_cast(h); } else { - ratio = float(max_size_len) / float(w); + ratio = static_cast(max_size_len) / static_cast(w); } } - int resize_h = int(float(h) * ratio); - int resize_w = int(float(w) * ratio); + int resize_h = static_cast(float(h) * ratio); + int resize_w = static_cast(float(w) * ratio); if (resize_h % 32 == 0) resize_h = resize_h; else if (resize_h / 32 < 1 + 1e-5) @@ -100,8 +100,8 @@ cv::Mat DetResizeImg(const cv::Mat img, int max_size_len, cv::Mat resize_img; cv::resize(img, resize_img, cv::Size(resize_w, resize_h)); - ratio_hw.push_back(float(resize_h) / float(h)); - ratio_hw.push_back(float(resize_w) / float(w)); + ratio_hw.push_back(static_cast(resize_h) / static_cast(h)); + ratio_hw.push_back(static_cast(resize_w) / static_cast(w)); return resize_img; } @@ -121,7 +121,8 @@ void RunRecModel(std::vector>> boxes, cv::Mat img, int index = 0; for (int i = boxes.size() - 1; i >= 0; i--) { crop_img = GetRotateCropImage(srcimg, boxes[i]); - float wh_ratio = float(crop_img.cols) / float(crop_img.rows); + float wh_ratio = + static_cast(crop_img.cols) / static_cast(crop_img.rows); resize_img = CrnnResizeImg(crop_img, wh_ratio); resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f); @@ -133,8 +134,7 @@ void RunRecModel(std::vector>> boxes, cv::Mat img, input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols}); auto *data0 = input_tensor0->mutable_data(); - neon_mean_scale(dimg, data0, resize_img.rows * resize_img.cols, mean, - scale); + NeonMeanScale(dimg, data0, resize_img.rows * resize_img.cols, mean, scale); //// Run CRNN predictor predictor_crnn->Run(); @@ -147,8 +147,9 @@ void RunRecModel(std::vector>> boxes, cv::Mat img, auto shape_out = output_tensor0->shape(); std::vector pred_idx; - for (int n = int(rec_idx_lod[0][0]); n < int(rec_idx_lod[0][1]); n += 1) { - pred_idx.push_back(int(rec_idx[n])); + for (int n = static_cast(rec_idx_lod[0][0]); + n < static_cast(rec_idx_lod[0][1]); n += 1) { + pred_idx.push_back(static_cast(rec_idx[n])); } if (pred_idx.size() < 1e-3) @@ -169,16 +170,15 @@ void RunRecModel(std::vector>> boxes, cv::Mat img, auto predict_lod = output_tensor1->lod(); - int argmax_idx; int blank = predict_shape[1]; float score = 0.f; int count = 0; - float max_value = 0.0f; for (int n = predict_lod[0][0]; n < predict_lod[0][1] - 1; n++) { - argmax_idx = int(Argmax(&predict_batch[n * predict_shape[1]], - &predict_batch[(n + 1) * predict_shape[1]])); - max_value = + int argmax_idx = + static_cast(Argmax(&predict_batch[n * predict_shape[1]], + &predict_batch[(n + 1) * predict_shape[1]])); + float max_value = float(*std::max_element(&predict_batch[n * predict_shape[1]], &predict_batch[(n + 1) * predict_shape[1]])); @@ -214,7 +214,7 @@ RunDetModel(std::shared_ptr predictor, cv::Mat img, std::vector mean = {0.485f, 0.456f, 0.406f}; std::vector scale = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; const float *dimg = reinterpret_cast(img_fp.data); - neon_mean_scale(dimg, data0, img_fp.rows * img_fp.cols, mean, scale); + NeonMeanScale(dimg, data0, img_fp.rows * img_fp.cols, mean, scale); // Run predictor predictor->Run(); @@ -230,12 +230,14 @@ RunDetModel(std::shared_ptr predictor, cv::Mat img, unsigned char cbuf[shape_out[2] * shape_out[3]]; for (int i = 0; i < int(shape_out[2] * shape_out[3]); i++) { - pred[i] = float(outptr[i]); - cbuf[i] = (unsigned char)((outptr[i]) * 255); + pred[i] = static_cast(outptr[i]); + cbuf[i] = static_cast((outptr[i]) * 255); } - cv::Mat cbuf_map(shape_out[2], shape_out[3], CV_8UC1, (unsigned char *)cbuf); - cv::Mat pred_map(shape_out[2], shape_out[3], CV_32F, (float *)pred); + cv::Mat cbuf_map(shape_out[2], shape_out[3], CV_8UC1, + reinterpret_cast cbuf); + cv::Mat pred_map(shape_out[2], shape_out[3], CV_32F, + reinterpret_cast pred); const double threshold = double(Config["det_db_thresh"]) * 255; const double maxvalue = 255; @@ -264,7 +266,8 @@ cv::Mat Visualization(cv::Mat srcimg, cv::Point rook_points[boxes.size()][4]; for (int n = 0; n < boxes.size(); n++) { for (int m = 0; m < boxes[0].size(); m++) { - rook_points[n][m] = cv::Point(int(boxes[n][m][0]), int(boxes[n][m][1])); + rook_points[n][m] = cv::Point(static_cast(boxes[n][m][0]), + static_cast(boxes[n][m][1])); } } cv::Mat img_vis;