From 757d01601f0c79279b9f8ae69d37b9d76f0c01aa Mon Sep 17 00:00:00 2001 From: WenmuZhou Date: Tue, 11 May 2021 11:16:07 +0800 Subject: [PATCH] add det_use_polygon_score --- deploy/lite/config.txt | 1 + deploy/lite/db_post_process.cc | 52 +++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/deploy/lite/config.txt b/deploy/lite/config.txt index 23f0171d..4c68105d 100644 --- a/deploy/lite/config.txt +++ b/deploy/lite/config.txt @@ -3,4 +3,5 @@ det_db_thresh 0.3 det_db_box_thresh 0.5 det_db_unclip_ratio 1.6 det_db_use_dilate 0 +det_use_polygon_score 1 use_direction_classify 1 \ No newline at end of file diff --git a/deploy/lite/db_post_process.cc b/deploy/lite/db_post_process.cc index 0c1c8b92..77919fb1 100644 --- a/deploy/lite/db_post_process.cc +++ b/deploy/lite/db_post_process.cc @@ -190,6 +190,51 @@ float BoxScoreFast(std::vector> box_array, cv::Mat pred) { return score; } +float PolygonScoreAcc(std::vector contour, cv::Mat pred) { + int width = pred.cols; + int height = pred.rows; + std::vector box_x; + std::vector box_y; + for (int i = 0; i < contour.size(); ++i) { + box_x.push_back(contour[i].x); + box_y.push_back(contour[i].y); + } + + int xmin = + clamp(int(std::floor(*(std::min_element(box_x.begin(), box_x.end())))), 0, + width - 1); + int xmax = + clamp(int(std::ceil(*(std::max_element(box_x.begin(), box_x.end())))), 0, + width - 1); + int ymin = + clamp(int(std::floor(*(std::min_element(box_y.begin(), box_y.end())))), 0, + height - 1); + int ymax = + clamp(int(std::ceil(*(std::max_element(box_y.begin(), box_y.end())))), 0, + height - 1); + + cv::Mat mask; + mask = cv::Mat::zeros(ymax - ymin + 1, xmax - xmin + 1, CV_8UC1); + + cv::Point *rook_point = new cv::Point[contour.size()]; + + for (int i = 0; i < contour.size(); ++i) { + rook_point[i] = cv::Point(int(box_x[i]) - xmin, int(box_y[i]) - ymin); + } + const cv::Point *ppt[1] = {rook_point}; + int npt[] = {int(contour.size())}; + + cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(1)); + + cv::Mat croppedImg; + pred(cv::Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)) + .copyTo(croppedImg); + float score = cv::mean(croppedImg, mask)[0]; + + delete[] rook_point; + return score; +} + std::vector>> BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap, std::map Config) { @@ -197,6 +242,7 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap, const int max_candidates = 1000; const float box_thresh = static_cast(Config["det_db_box_thresh"]); const float unclip_ratio = static_cast(Config["det_db_unclip_ratio"]); + const int det_use_polygon_score = int(Config["det_use_polygon_score"]); int width = bitmap.cols; int height = bitmap.rows; @@ -228,7 +274,11 @@ BoxesFromBitmap(const cv::Mat pred, const cv::Mat bitmap, } float score; - score = BoxScoreFast(array, pred); + if (det_use_polygon_score) { + score = PolygonScoreAcc(contours[i], pred); + } else { + score = BoxScoreFast(array, pred); + } // end box_score_fast if (score < box_thresh) continue; -- GitLab