提交 54cf0068 编写于 作者: L LDOUBLEV

update

上级 ff446b76
...@@ -12,14 +12,14 @@ ...@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "crnn_process.h" //NOLINT #include "crnn_process.h" //NOLINT
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <string> #include <string>
const std::vector<int> rec_image_shape{3, 32, 320}; const std::vector<int> rec_image_shape{3, 32, 320};
cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio) { cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio, bool is_norm) {
int imgC, imgH, imgW; int imgC, imgH, imgW;
imgC = rec_image_shape[0]; imgC = rec_image_shape[0];
imgW = rec_image_shape[2]; imgW = rec_image_shape[2];
...@@ -34,54 +34,31 @@ cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio) { ...@@ -34,54 +34,31 @@ cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio) {
else else
resize_w = int(ceilf(imgH * ratio)); resize_w = int(ceilf(imgH * ratio));
cv::Mat resize_img; cv::Mat resize_img;
cv::resize( cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f,
img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f, cv::INTER_CUBIC); cv::INTER_LINEAR);
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
for (int h = 0; h < resize_img.rows; h++) {
for (int w = 0; w < resize_img.cols; w++) {
resize_img.at<cv::Vec3f>(h, w)[0] =
(resize_img.at<cv::Vec3f>(h, w)[0] - 0.5) * 2;
resize_img.at<cv::Vec3f>(h, w)[1] =
(resize_img.at<cv::Vec3f>(h, w)[1] - 0.5) * 2;
resize_img.at<cv::Vec3f>(h, w)[2] =
(resize_img.at<cv::Vec3f>(h, w)[2] - 0.5) * 2;
}
}
cv::Mat dist;
cv::copyMakeBorder(resize_img,
dist,
0,
0,
0,
int(imgW - resize_w),
cv::BORDER_CONSTANT,
{0, 0, 0});
return dist;
}
cv::Mat CrnnResizeImg(cv::Mat img, float wh_ratio) {
int imgC, imgH, imgW;
imgC = rec_image_shape[0];
imgW = rec_image_shape[2];
imgH = rec_image_shape[1];
imgW = int(32 * wh_ratio); if (!is_norm) {
return resize_img;
} else {
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
for (int h = 0; h < resize_img.rows; h++) {
for (int w = 0; w < resize_img.cols; w++) {
resize_img.at<cv::Vec3f>(h, w)[0] =
(resize_img.at<cv::Vec3f>(h, w)[0] - 0.5) * 2;
resize_img.at<cv::Vec3f>(h, w)[1] =
(resize_img.at<cv::Vec3f>(h, w)[1] - 0.5) * 2;
resize_img.at<cv::Vec3f>(h, w)[2] =
(resize_img.at<cv::Vec3f>(h, w)[2] - 0.5) * 2;
}
}
float ratio = float(img.cols) / float(img.rows); cv::Mat dist;
int resize_w, resize_h; cv::copyMakeBorder(resize_img, dist, 0, 0, 0, int(imgW - resize_w),
if (ceilf(imgH * ratio) > imgW) cv::BORDER_CONSTANT, {0, 0, 0});
resize_w = imgW;
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);
return resize_img; return dist;
}
} }
std::vector<std::string> ReadDict(std::string path) { std::vector<std::string> ReadDict(std::string path) {
...@@ -140,9 +117,7 @@ cv::Mat GetRotateCropImage(cv::Mat srcimage, ...@@ -140,9 +117,7 @@ cv::Mat GetRotateCropImage(cv::Mat srcimage,
cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std); cv::Mat M = cv::getPerspectiveTransform(pointsf, pts_std);
cv::Mat dst_img; cv::Mat dst_img;
cv::warpPerspective(img_crop, cv::warpPerspective(img_crop, dst_img, M,
dst_img,
M,
cv::Size(img_crop_width, img_crop_height), cv::Size(img_crop_width, img_crop_height),
cv::BORDER_REPLICATE); cv::BORDER_REPLICATE);
......
...@@ -21,14 +21,12 @@ ...@@ -21,14 +21,12 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "math.h" //NOLINT #include "math.h" //NOLINT
#include "opencv2/core.hpp" #include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp" #include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio); cv::Mat CrnnResizeNormImg(cv::Mat img, float wh_ratio, bool is_norm);
cv::Mat CrnnResizeImg(cv::Mat img, float wh_ratio);
std::vector<std::string> ReadDict(std::string path); std::vector<std::string> ReadDict(std::string path);
......
...@@ -276,4 +276,4 @@ FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes, float ratio_h, ...@@ -276,4 +276,4 @@ FilterTagDetRes(std::vector<std::vector<std::vector<int>>> boxes, float ratio_h,
root_points.push_back(boxes[n]); root_points.push_back(boxes[n]);
} }
return root_points; return root_points;
} }
\ No newline at end of file
...@@ -107,8 +107,9 @@ cv::Mat DetResizeImg(const cv::Mat img, int max_size_len, ...@@ -107,8 +107,9 @@ cv::Mat DetResizeImg(const cv::Mat img, int max_size_len,
void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img, void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
std::shared_ptr<PaddlePredictor> predictor_crnn, std::shared_ptr<PaddlePredictor> predictor_crnn,
std::string dict_path, std::vector<std::string> &rec_text, std::vector<std::string> &rec_text,
std::vector<float> &rec_text_score) { std::vector<float> &rec_text_score,
std::vector<std::string> charactor_dict) {
std::vector<float> mean = {0.5f, 0.5f, 0.5f}; std::vector<float> mean = {0.5f, 0.5f, 0.5f};
std::vector<float> scale = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f}; std::vector<float> scale = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
...@@ -117,14 +118,12 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img, ...@@ -117,14 +118,12 @@ void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
cv::Mat crop_img; cv::Mat crop_img;
cv::Mat resize_img; cv::Mat resize_img;
auto charactor_dict = ReadDict(dict_path);
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 = float(crop_img.cols) / float(crop_img.rows);
resize_img = CrnnResizeImg(crop_img, wh_ratio); resize_img = CrnnResizeNormImg(crop_img, wh_ratio, false);
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f); resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
const float *dimg = reinterpret_cast<const float *>(resize_img.data); const float *dimg = reinterpret_cast<const float *>(resize_img.data);
...@@ -227,13 +226,12 @@ RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img, ...@@ -227,13 +226,12 @@ RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img,
auto shape_out = output_tensor->shape(); auto shape_out = output_tensor->shape();
// Save output // Save output
float pred[shape_out[2]][shape_out[3]]; float pred[shape_out[2] * shape_out[3]];
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[int(i / int(shape_out[3]))][int(i % shape_out[3])] = float(outptr[i]); pred[i] = float(outptr[i]);
cbuf[int(i / int(shape_out[3]))][int(i % shape_out[3])] = cbuf[i] = (unsigned char)((outptr[i]) * 255);
(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, (unsigned char *)cbuf);
...@@ -333,13 +331,15 @@ int main(int argc, char **argv) { ...@@ -333,13 +331,15 @@ int main(int argc, char **argv) {
auto det_predictor = loadModel(det_model_file); auto det_predictor = loadModel(det_model_file);
auto rec_predictor = loadModel(rec_model_file); auto rec_predictor = loadModel(rec_model_file);
auto charactor_dict = ReadDict(dict_path);
cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR); cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);
auto boxes = RunDetModel(det_predictor, srcimg, Config); auto boxes = RunDetModel(det_predictor, srcimg, Config);
std::vector<std::string> rec_text; std::vector<std::string> rec_text;
std::vector<float> rec_text_score; std::vector<float> rec_text_score;
RunRecModel(boxes, srcimg, rec_predictor, dict_path, rec_text, RunRecModel(boxes, srcimg, rec_predictor, rec_text, rec_text_score,
rec_text_score); charactor_dict);
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
auto duration = auto duration =
......
# PaddleOCR 模型部署 # PaddleOCR 端侧模型部署
PaddleOCR是集训练、预测、端侧部署于一体的实用OCR工具库。本教程将介绍在安卓移动端部署PaddleOCR超轻量中文检测、识别模型的主要流程 本教程将介绍在移动端部署PaddleOCR超轻量中文检测、识别模型的详细步骤
## 1. 准备环境 ## 1. 准备环境
...@@ -159,6 +159,7 @@ demo/cxx/ocr/ ...@@ -159,6 +159,7 @@ demo/cxx/ocr/
| |--11.jpg 待测试图像 | |--11.jpg 待测试图像
| |--ppocr_keys_v1.txt 字典文件 | |--ppocr_keys_v1.txt 字典文件
| |--libpaddle_light_api_shared.so C++预测库文件 | |--libpaddle_light_api_shared.so C++预测库文件
| |--config.txt DB-CRNN超参数配置
|-- config.txt DB-CRNN超参数配置 |-- config.txt DB-CRNN超参数配置
|-- crnn_process.cc 识别模型CRNN的预处理和后处理文件 |-- crnn_process.cc 识别模型CRNN的预处理和后处理文件
|-- crnn_process.h |-- crnn_process.h
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册