Merge pull request #316 from LDOUBLEV/fixocr

add static_cast
This commit is contained in:
dyning 2020-07-13 22:21:23 +08:00 committed by GitHub
commit 533219a721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 64 deletions

View File

@ -27,12 +27,12 @@ cv::Mat CrnnResizeImg(cv::Mat img, float wh_ratio) {
imgW = int(32 * wh_ratio); imgW = int(32 * wh_ratio);
float ratio = float(img.cols) / float(img.rows); float ratio = static_cast<float>(img.cols) / static_cast<float>(img.rows);
int resize_w, resize_h; int resize_w, resize_h;
if (ceilf(imgH * ratio) > imgW) if (ceilf(imgH * ratio) > imgW)
resize_w = imgW; resize_w = imgW;
else else
resize_w = int(ceilf(imgH * ratio)); resize_w = static_cast<int>(ceilf(imgH * ratio));
cv::Mat resize_img; cv::Mat resize_img;
cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f, cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f,
cv::INTER_LINEAR); cv::INTER_LINEAR);
@ -76,10 +76,12 @@ cv::Mat GetRotateCropImage(cv::Mat srcimage,
points[i][1] -= top; points[i][1] -= top;
} }
int img_crop_width = int(sqrt(pow(points[0][0] - points[1][0], 2) + int img_crop_width =
pow(points[0][1] - points[1][1], 2))); static_cast<int>(sqrt(pow(points[0][0] - points[1][0], 2) +
int img_crop_height = int(sqrt(pow(points[0][0] - points[3][0], 2) + pow(points[0][1] - points[1][1], 2)));
pow(points[0][1] - points[3][1], 2))); int img_crop_height =
static_cast<int>(sqrt(pow(points[0][0] - points[3][0], 2) +
pow(points[0][1] - points[3][1], 2)));
cv::Point2f pts_std[4]; cv::Point2f pts_std[4];
pts_std[0] = cv::Point2f(0., 0.); 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::Size(img_crop_width, img_crop_height),
cv::BORDER_REPLICATE); cv::BORDER_REPLICATE);
if (float(dst_img.rows) >= float(dst_img.cols) * 1.5) { const float ratio = 1.5;
if (static_cast<float>(dst_img.rows) >=
static_cast<float>(dst_img.cols) * ratio) {
cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth()); cv::Mat srcCopy = cv::Mat(dst_img.rows, dst_img.cols, dst_img.depth());
cv::transpose(dst_img, srcCopy); cv::transpose(dst_img, srcCopy);
cv::flip(srcCopy, srcCopy, 0); cv::flip(srcCopy, srcCopy, 0);

View File

@ -42,10 +42,14 @@ cv::RotatedRect Unclip(std::vector<std::vector<float>> box,
ClipperLib::ClipperOffset offset; ClipperLib::ClipperOffset offset;
ClipperLib::Path p; ClipperLib::Path p;
p << ClipperLib::IntPoint(int(box[0][0]), int(box[0][1])) p << ClipperLib::IntPoint(static_cast<int>(box[0][0]),
<< ClipperLib::IntPoint(int(box[1][0]), int(box[1][1])) static_cast<int>(box[0][1]))
<< ClipperLib::IntPoint(int(box[2][0]), int(box[2][1])) << ClipperLib::IntPoint(static_cast<int>(box[1][0]),
<< ClipperLib::IntPoint(int(box[3][0]), int(box[3][1])); static_cast<int>(box[1][1]))
<< ClipperLib::IntPoint(static_cast<int>(box[2][0]),
static_cast<int>(box[2][1]))
<< ClipperLib::IntPoint(static_cast<int>(box[3][0]),
static_cast<int>(box[3][1]));
offset.AddPath(p, ClipperLib::jtRound, ClipperLib::etClosedPolygon); offset.AddPath(p, ClipperLib::jtRound, ClipperLib::etClosedPolygon);
ClipperLib::Paths soln; ClipperLib::Paths soln;
@ -149,23 +153,31 @@ float BoxScoreFast(std::vector<std::vector<float>> box_array, cv::Mat pred) {
float box_x[4] = {array[0][0], array[1][0], array[2][0], array[3][0]}; 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]}; 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, int xmin = clamp(
width - 1); static_cast<int>(std::floorf(*(std::min_element(box_x, box_x + 4)))), 0,
int xmax = clamp(int(std::ceilf(*(std::max_element(box_x, box_x + 4)))), 0, width - 1);
width - 1); int xmax =
int ymin = clamp(int(std::floorf(*(std::min_element(box_y, box_y + 4)))), 0, clamp(static_cast<int>(std::ceilf(*(std::max_element(box_x, box_x + 4)))),
height - 1); 0, width - 1);
int ymax = clamp(int(std::ceilf(*(std::max_element(box_y, box_y + 4)))), 0, int ymin = clamp(
height - 1); static_cast<int>(std::floorf(*(std::min_element(box_y, box_y + 4)))), 0,
height - 1);
int ymax =
clamp(static_cast<int>(std::ceilf(*(std::max_element(box_y, box_y + 4)))),
0, height - 1);
cv::Mat mask; cv::Mat mask;
mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1); mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1);
cv::Point root_point[4]; cv::Point root_point[4];
root_point[0] = cv::Point(int(array[0][0]) - xmin, int(array[0][1]) - ymin); root_point[0] = cv::Point(static_cast<int>(array[0][0]) - xmin,
root_point[1] = cv::Point(int(array[1][0]) - xmin, int(array[1][1]) - ymin); static_cast<int>(array[0][1]) - ymin);
root_point[2] = cv::Point(int(array[2][0]) - xmin, int(array[2][1]) - ymin); root_point[1] = cv::Point(static_cast<int>(array[1][0]) - xmin,
root_point[3] = cv::Point(int(array[3][0]) - xmin, int(array[3][1]) - ymin); static_cast<int>(array[1][1]) - ymin);
root_point[2] = cv::Point(static_cast<int>(array[2][0]) - xmin,
static_cast<int>(array[2][1]) - ymin);
root_point[3] = cv::Point(static_cast<int>(array[3][0]) - xmin,
static_cast<int>(array[3][1]) - ymin);
const cv::Point *ppt[1] = {root_point}; const cv::Point *ppt[1] = {root_point};
int npt[] = {4}; int npt[] = {4};
cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1)); cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1));
@ -183,8 +195,8 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap,
std::map<std::string, double> Config) { std::map<std::string, double> Config) {
const int min_size = 3; const int min_size = 3;
const int max_candidates = 1000; const int max_candidates = 1000;
const float box_thresh = float(Config["det_db_box_thresh"]); const float box_thresh = static_cast<float>(Config["det_db_box_thresh"]);
const float unclip_ratio = float(Config["det_db_unclip_ratio"]); const float unclip_ratio = static_cast<float>(Config["det_db_unclip_ratio"]);
int width = bitmap.cols; int width = bitmap.cols;
int height = bitmap.rows; int height = bitmap.rows;
@ -233,12 +245,13 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap,
std::vector<std::vector<int>> intcliparray; std::vector<std::vector<int>> intcliparray;
for (int num_pt = 0; num_pt < 4; num_pt++) { for (int num_pt = 0; num_pt < 4; num_pt++) {
std::vector<int> a{int(clamp(roundf(cliparray[num_pt][0] / float(width) * std::vector<int> a{
float(dest_width)), static_cast<int>(clamp(
float(0), float(dest_width))), roundf(cliparray[num_pt][0] / float(width) * float(dest_width)),
int(clamp(roundf(cliparray[num_pt][1] / float(height) * float(0), float(dest_width))),
float(dest_height)), static_cast<int>(clamp(
float(0), float(dest_height)))}; roundf(cliparray[num_pt][1] / float(height) * float(dest_height)),
float(0), float(dest_height)))};
intcliparray.push_back(a); intcliparray.push_back(a);
} }
boxes.push_back(intcliparray); boxes.push_back(intcliparray);
@ -254,23 +267,27 @@ FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes, float ratio_h,
int oriimg_w = srcimg.cols; int oriimg_w = srcimg.cols;
std::vector<std::vector<std::vector<int>>> root_points; std::vector<std::vector<std::vector<int>>> root_points;
for (int n = 0; n < boxes.size(); n++) { for (int n = 0; n < static_cast<int>(boxes.size()); n++) {
boxes[n] = OrderPointsClockwise(boxes[n]); boxes[n] = OrderPointsClockwise(boxes[n]);
for (int m = 0; m < boxes[0].size(); m++) { for (int m = 0; m < static_cast<int>(boxes[0].size()); m++) {
boxes[n][m][0] /= ratio_w; boxes[n][m][0] /= ratio_w;
boxes[n][m][1] /= ratio_h; 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][0] =
boxes[n][m][1] = int(std::min(std::max(boxes[n][m][1], 0), oriimg_h - 1)); static_cast<int>(std::min(std::max(boxes[n][m][0], 0), oriimg_w - 1));
boxes[n][m][1] =
static_cast<int>(std::min(std::max(boxes[n][m][1], 0), oriimg_h - 1));
} }
} }
for (int n = 0; n < boxes.size(); n++) { for (int n = 0; n < boxes.size(); n++) {
int rect_width, rect_height; int rect_width, rect_height;
rect_width = int(sqrt(pow(boxes[n][0][0] - boxes[n][1][0], 2) + rect_width =
pow(boxes[n][0][1] - boxes[n][1][1], 2))); static_cast<int>(sqrt(pow(boxes[n][0][0] - boxes[n][1][0], 2) +
rect_height = int(sqrt(pow(boxes[n][0][0] - boxes[n][3][0], 2) + pow(boxes[n][0][1] - boxes[n][1][1], 2)));
pow(boxes[n][0][1] - boxes[n][3][1], 2))); rect_height =
static_cast<int>(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) if (rect_width <= 10 || rect_height <= 10)
continue; continue;
root_points.push_back(boxes[n]); root_points.push_back(boxes[n]);

View File

@ -22,9 +22,9 @@ using namespace paddle::lite_api; // NOLINT
using namespace std; using namespace std;
// fill tensor with mean and scale and trans layout: nhwc -> nchw, neon speed up // 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, void NeonMeanScale(const float *din, float *dout, int size,
const std::vector<float> mean, const std::vector<float> mean,
const std::vector<float> scale) { const std::vector<float> scale) {
if (mean.size() != 3 || scale.size() != 3) { if (mean.size() != 3 || scale.size() != 3) {
std::cerr << "[ERROR] mean or scale size must equal to 3\n"; std::cerr << "[ERROR] mean or scale size must equal to 3\n";
exit(1); exit(1);
@ -75,14 +75,14 @@ cv::Mat DetResizeImg(const cv::Mat img, int max_size_len,
int max_wh = w >= h ? w : h; int max_wh = w >= h ? w : h;
if (max_wh > max_size_len) { if (max_wh > max_size_len) {
if (h > w) { if (h > w) {
ratio = float(max_size_len) / float(h); ratio = static_cast<float>(max_size_len) / static_cast<float>(h);
} else { } else {
ratio = float(max_size_len) / float(w); ratio = static_cast<float>(max_size_len) / static_cast<float>(w);
} }
} }
int resize_h = int(float(h) * ratio); int resize_h = static_cast<int>(float(h) * ratio);
int resize_w = int(float(w) * ratio); int resize_w = static_cast<int>(float(w) * ratio);
if (resize_h % 32 == 0) if (resize_h % 32 == 0)
resize_h = resize_h; resize_h = resize_h;
else if (resize_h / 32 < 1 + 1e-5) 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::Mat resize_img;
cv::resize(img, resize_img, cv::Size(resize_w, resize_h)); cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
ratio_hw.push_back(float(resize_h) / float(h)); ratio_hw.push_back(static_cast<float>(resize_h) / static_cast<float>(h));
ratio_hw.push_back(float(resize_w) / float(w)); ratio_hw.push_back(static_cast<float>(resize_w) / static_cast<float>(w));
return resize_img; return resize_img;
} }
@ -121,7 +121,8 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
int index = 0; int index = 0;
for (int i = boxes.size() - 1; i >= 0; i--) { for (int i = boxes.size() - 1; i >= 0; i--) {
crop_img = GetRotateCropImage(srcimg, boxes[i]); crop_img = GetRotateCropImage(srcimg, boxes[i]);
float wh_ratio = float(crop_img.cols) / float(crop_img.rows); float wh_ratio =
static_cast<float>(crop_img.cols) / static_cast<float>(crop_img.rows);
resize_img = CrnnResizeImg(crop_img, wh_ratio); resize_img = CrnnResizeImg(crop_img, wh_ratio);
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f); resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
@ -133,8 +134,7 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols}); input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols});
auto *data0 = input_tensor0->mutable_data<float>(); auto *data0 = input_tensor0->mutable_data<float>();
neon_mean_scale(dimg, data0, resize_img.rows * resize_img.cols, mean, NeonMeanScale(dimg, data0, resize_img.rows * resize_img.cols, mean, scale);
scale);
//// Run CRNN predictor //// Run CRNN predictor
predictor_crnn->Run(); predictor_crnn->Run();
@ -147,8 +147,9 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
auto shape_out = output_tensor0->shape(); auto shape_out = output_tensor0->shape();
std::vector<int> pred_idx; std::vector<int> pred_idx;
for (int n = int(rec_idx_lod[0][0]); n < int(rec_idx_lod[0][1]); n += 1) { for (int n = static_cast<int>(rec_idx_lod[0][0]);
pred_idx.push_back(int(rec_idx[n])); n < static_cast<int>(rec_idx_lod[0][1]); n += 1) {
pred_idx.push_back(static_cast<int>(rec_idx[n]));
} }
if (pred_idx.size() < 1e-3) if (pred_idx.size() < 1e-3)
@ -169,16 +170,15 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
auto predict_lod = output_tensor1->lod(); auto predict_lod = output_tensor1->lod();
int argmax_idx;
int blank = predict_shape[1]; int blank = predict_shape[1];
float score = 0.f; float score = 0.f;
int count = 0; int count = 0;
float max_value = 0.0f;
for (int n = predict_lod[0][0]; n < predict_lod[0][1] - 1; n++) { for (int n = predict_lod[0][0]; n < predict_lod[0][1] - 1; n++) {
argmax_idx = int(Argmax(&predict_batch[n * predict_shape[1]], int argmax_idx =
&predict_batch[(n + 1) * predict_shape[1]])); static_cast<int>(Argmax(&predict_batch[n * predict_shape[1]],
max_value = &predict_batch[(n + 1) * predict_shape[1]]));
float max_value =
float(*std::max_element(&predict_batch[n * predict_shape[1]], float(*std::max_element(&predict_batch[n * predict_shape[1]],
&predict_batch[(n + 1) * predict_shape[1]])); &predict_batch[(n + 1) * predict_shape[1]]));
@ -214,7 +214,7 @@ RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img,
std::vector<float> mean = {0.485f, 0.456f, 0.406f}; std::vector<float> mean = {0.485f, 0.456f, 0.406f};
std::vector<float> scale = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; std::vector<float> scale = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
const float *dimg = reinterpret_cast<const float *>(img_fp.data); const float *dimg = reinterpret_cast<const float *>(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 // Run predictor
predictor->Run(); predictor->Run();
@ -230,12 +230,14 @@ RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img,
unsigned char cbuf[shape_out[2] * shape_out[3]]; unsigned char cbuf[shape_out[2] * shape_out[3]];
for (int i = 0; i < int(shape_out[2] * shape_out[3]); i++) { for (int i = 0; i < int(shape_out[2] * shape_out[3]); i++) {
pred[i] = float(outptr[i]); pred[i] = static_cast<float>(outptr[i]);
cbuf[i] = (unsigned char)((outptr[i]) * 255); cbuf[i] = static_cast<unsigned char>((outptr[i]) * 255);
} }
cv::Mat cbuf_map(shape_out[2], shape_out[3], CV_8UC1, (unsigned char *)cbuf); cv::Mat cbuf_map(shape_out[2], shape_out[3], CV_8UC1,
cv::Mat pred_map(shape_out[2], shape_out[3], CV_32F, (float *)pred); reinterpret_cast<unsigned char *> cbuf);
cv::Mat pred_map(shape_out[2], shape_out[3], CV_32F,
reinterpret_cast<float *> pred);
const double threshold = double(Config["det_db_thresh"]) * 255; const double threshold = double(Config["det_db_thresh"]) * 255;
const double maxvalue = 255; const double maxvalue = 255;
@ -264,7 +266,8 @@ cv::Mat Visualization(cv::Mat srcimg,
cv::Point rook_points[boxes.size()][4]; cv::Point rook_points[boxes.size()][4];
for (int n = 0; n < boxes.size(); n++) { for (int n = 0; n < boxes.size(); n++) {
for (int m = 0; m < boxes[0].size(); m++) { 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<int>(boxes[n][m][0]),
static_cast<int>(boxes[n][m][1]));
} }
} }
cv::Mat img_vis; cv::Mat img_vis;