PaddleOCR/deploy/lite/ocr_db_crnn.cc

365 lines
11 KiB
C++
Raw Normal View History

2020-07-07 15:40:39 +08:00
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2020-07-02 10:31:34 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle_api.h" // NOLINT
2020-07-02 10:31:34 +08:00
#include <chrono>
2020-07-07 15:40:39 +08:00
#include "crnn_process.h"
#include "db_post_process.h"
2020-07-02 10:31:34 +08:00
using namespace paddle::lite_api; // NOLINT
2020-07-07 15:40:39 +08:00
using namespace std;
2020-07-02 10:31:34 +08:00
// 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,
2020-07-02 10:31:34 +08:00
const std::vector<float> mean,
const std::vector<float> scale) {
if (mean.size() != 3 || scale.size() != 3) {
std::cerr << "[ERROR] mean or scale size must equal to 3\n";
exit(1);
}
float32x4_t vmean0 = vdupq_n_f32(mean[0]);
float32x4_t vmean1 = vdupq_n_f32(mean[1]);
float32x4_t vmean2 = vdupq_n_f32(mean[2]);
float32x4_t vscale0 = vdupq_n_f32(scale[0]);
float32x4_t vscale1 = vdupq_n_f32(scale[1]);
float32x4_t vscale2 = vdupq_n_f32(scale[2]);
float *dout_c0 = dout;
float *dout_c1 = dout + size;
float *dout_c2 = dout + size * 2;
2020-07-02 10:31:34 +08:00
int i = 0;
for (; i < size - 3; i += 4) {
float32x4x3_t vin3 = vld3q_f32(din);
float32x4_t vsub0 = vsubq_f32(vin3.val[0], vmean0);
float32x4_t vsub1 = vsubq_f32(vin3.val[1], vmean1);
float32x4_t vsub2 = vsubq_f32(vin3.val[2], vmean2);
float32x4_t vs0 = vmulq_f32(vsub0, vscale0);
float32x4_t vs1 = vmulq_f32(vsub1, vscale1);
float32x4_t vs2 = vmulq_f32(vsub2, vscale2);
vst1q_f32(dout_c0, vs0);
vst1q_f32(dout_c1, vs1);
vst1q_f32(dout_c2, vs2);
din += 12;
dout_c0 += 4;
dout_c1 += 4;
dout_c2 += 4;
}
for (; i < size; i++) {
*(dout_c0++) = (*(din++) - mean[0]) * scale[0];
*(dout_c1++) = (*(din++) - mean[1]) * scale[1];
*(dout_c2++) = (*(din++) - mean[2]) * scale[2];
}
}
// resize image to a size multiple of 32 which is required by the network
cv::Mat DetResizeImg(const cv::Mat img, int max_size_len,
std::vector<float> &ratio_hw) {
2020-07-02 10:31:34 +08:00
int w = img.cols;
int h = img.rows;
float ratio = 1.f;
2020-07-07 15:40:39 +08:00
int max_wh = w >= h ? w : h;
if (max_wh > max_size_len) {
if (h > w) {
2020-07-02 10:31:34 +08:00
ratio = float(max_size_len) / float(h);
} else {
ratio = float(max_size_len) / float(w);
}
}
int resize_h = int(float(h) * ratio);
int resize_w = int(float(w) * ratio);
if (resize_h % 32 == 0)
resize_h = resize_h;
2020-07-07 15:40:39 +08:00
else if (resize_h / 32 < 1 + 1e-5)
2020-07-02 10:31:34 +08:00
resize_h = 32;
else
resize_h = (resize_h / 32 - 1) * 32;
if (resize_w % 32 == 0)
resize_w = resize_w;
2020-07-07 15:40:39 +08:00
else if (resize_w / 32 < 1 + 1e-5)
2020-07-02 10:31:34 +08:00
resize_w = 32;
else
2020-07-07 15:40:39 +08:00
resize_w = (resize_w / 32 - 1) * 32;
2020-07-02 10:31:34 +08:00
cv::Mat resize_img;
cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
2020-07-07 15:40:39 +08:00
ratio_hw.push_back(float(resize_h) / float(h));
ratio_hw.push_back(float(resize_w) / float(w));
2020-07-02 10:31:34 +08:00
return resize_img;
}
void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
2020-07-07 15:40:39 +08:00
std::shared_ptr<PaddlePredictor> predictor_crnn,
2020-07-10 20:02:49 +08:00
std::vector<std::string> &rec_text,
std::vector<float> &rec_text_score,
std::vector<std::string> charactor_dict) {
2020-07-02 10:31:34 +08:00
std::vector<float> mean = {0.5f, 0.5f, 0.5f};
std::vector<float> scale = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};
cv::Mat srcimg;
img.copyTo(srcimg);
cv::Mat crop_img;
cv::Mat resize_img;
int index = 0;
2020-07-07 15:40:39 +08:00
for (int i = boxes.size() - 1; i >= 0; i--) {
crop_img = GetRotateCropImage(srcimg, boxes[i]);
2020-07-02 10:31:34 +08:00
float wh_ratio = float(crop_img.cols) / float(crop_img.rows);
2020-07-10 20:02:49 +08:00
resize_img = CrnnResizeNormImg(crop_img, wh_ratio, false);
2020-07-02 10:31:34 +08:00
resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);
const float *dimg = reinterpret_cast<const float *>(resize_img.data);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
std::unique_ptr<Tensor> input_tensor0(
std::move(predictor_crnn->GetInput(0)));
2020-07-02 10:31:34 +08:00
input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols});
auto *data0 = input_tensor0->mutable_data<float>();
2020-07-02 10:31:34 +08:00
neon_mean_scale(dimg, data0, resize_img.rows * resize_img.cols, mean,
scale);
2020-07-02 10:31:34 +08:00
//// Run CRNN predictor
predictor_crnn->Run();
// Get output and run postprocess
std::unique_ptr<const Tensor> output_tensor0(
2020-07-07 15:40:39 +08:00
std::move(predictor_crnn->GetOutput(0)));
2020-07-08 17:50:54 +08:00
auto *rec_idx = output_tensor0->data<int64>();
2020-07-02 10:31:34 +08:00
auto rec_idx_lod = output_tensor0->lod();
auto shape_out = output_tensor0->shape();
std::vector<int> pred_idx;
2020-07-08 17:50:54 +08:00
for (int n = int(rec_idx_lod[0][0]); n < int(rec_idx_lod[0][1]); n += 1) {
2020-07-02 10:31:34 +08:00
pred_idx.push_back(int(rec_idx[n]));
}
if (pred_idx.size() < 1e-3)
continue;
2020-07-02 10:31:34 +08:00
index += 1;
2020-07-07 15:40:39 +08:00
std::string pred_txt = "";
2020-07-02 10:31:34 +08:00
for (int n = 0; n < pred_idx.size(); n++) {
2020-07-07 15:40:39 +08:00
pred_txt += charactor_dict[pred_idx[n]];
2020-07-02 10:31:34 +08:00
}
2020-07-07 15:40:39 +08:00
rec_text.push_back(pred_txt);
2020-07-02 10:31:34 +08:00
////get score
2020-07-07 15:40:39 +08:00
std::unique_ptr<const Tensor> output_tensor1(
std::move(predictor_crnn->GetOutput(1)));
auto *predict_batch = output_tensor1->data<float>();
2020-07-02 10:31:34 +08:00
auto predict_shape = output_tensor1->shape();
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++) {
2020-07-07 15:40:39 +08:00
argmax_idx = int(Argmax(&predict_batch[n * predict_shape[1]],
&predict_batch[(n + 1) * predict_shape[1]]));
max_value =
float(*std::max_element(&predict_batch[n * predict_shape[1]],
&predict_batch[(n + 1) * predict_shape[1]]));
2020-07-02 10:31:34 +08:00
if (blank - 1 - argmax_idx > 1e-5) {
score += max_value;
count += 1;
}
}
score /= count;
2020-07-07 15:40:39 +08:00
rec_text_score.push_back(score);
2020-07-02 10:31:34 +08:00
}
}
std::vector<std::vector<std::vector<int>>>
RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img,
std::map<std::string, double> Config) {
2020-07-02 10:31:34 +08:00
// Read img
2020-07-07 15:40:39 +08:00
int max_side_len = int(Config["max_side_len"]);
2020-07-02 10:31:34 +08:00
cv::Mat srcimg;
img.copyTo(srcimg);
2020-07-07 15:40:39 +08:00
std::vector<float> ratio_hw;
img = DetResizeImg(img, max_side_len, ratio_hw);
2020-07-02 10:31:34 +08:00
cv::Mat img_fp;
img.convertTo(img_fp, CV_32FC3, 1.0 / 255.f);
// Prepare input data from image
std::unique_ptr<Tensor> input_tensor0(std::move(predictor->GetInput(0)));
input_tensor0->Resize({1, 3, img_fp.rows, img_fp.cols});
auto *data0 = input_tensor0->mutable_data<float>();
2020-07-02 10:31:34 +08:00
std::vector<float> mean = {0.485f, 0.456f, 0.406f};
2020-07-07 15:40:39 +08:00
std::vector<float> scale = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
const float *dimg = reinterpret_cast<const float *>(img_fp.data);
2020-07-02 10:31:34 +08:00
neon_mean_scale(dimg, data0, img_fp.rows * img_fp.cols, mean, scale);
// Run predictor
predictor->Run();
// Get output and post process
2020-07-07 15:40:39 +08:00
std::unique_ptr<const Tensor> output_tensor(
std::move(predictor->GetOutput(0)));
auto *outptr = output_tensor->data<float>();
2020-07-02 10:31:34 +08:00
auto shape_out = output_tensor->shape();
// Save output
2020-07-10 20:02:49 +08:00
float pred[shape_out[2] * shape_out[3]];
unsigned char cbuf[shape_out[2] * shape_out[3]];
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
for (int i = 0; i < int(shape_out[2] * shape_out[3]); i++) {
2020-07-10 20:02:49 +08:00
pred[i] = float(outptr[i]);
cbuf[i] = (unsigned char)((outptr[i]) * 255);
2020-07-02 10:31:34 +08:00
}
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);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
const double threshold = double(Config["det_db_thresh"]) * 255;
2020-07-02 10:31:34 +08:00
const double maxvalue = 255;
cv::Mat bit_map;
cv::threshold(cbuf_map, bit_map, threshold, maxvalue, cv::THRESH_BINARY);
2020-07-07 15:40:39 +08:00
auto boxes = BoxesFromBitmap(pred_map, bit_map, Config);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
std::vector<std::vector<std::vector<int>>> filter_boxes =
FilterTagDetRes(boxes, ratio_hw[0], ratio_hw[1], srcimg);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
return filter_boxes;
}
std::shared_ptr<PaddlePredictor> loadModel(std::string model_file) {
MobileConfig config;
config.set_model_from_file(model_file);
std::shared_ptr<PaddlePredictor> predictor =
CreatePaddlePredictor<MobileConfig>(config);
return predictor;
}
cv::Mat Visualization(cv::Mat srcimg,
std::vector<std::vector<std::vector<int>>> boxes) {
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]));
2020-07-02 10:31:34 +08:00
}
}
cv::Mat img_vis;
srcimg.copyTo(img_vis);
2020-07-07 15:40:39 +08:00
for (int n = 0; n < boxes.size(); n++) {
const cv::Point *ppt[1] = {rook_points[n]};
2020-07-07 15:40:39 +08:00
int npt[] = {4};
cv::polylines(img_vis, ppt, npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
2020-07-02 10:31:34 +08:00
}
cv::imwrite("./vis.jpg", img_vis);
std::cout << "The detection visualized image saved in ./vis.jpg" << std::endl;
2020-07-07 15:40:39 +08:00
return img_vis;
}
std::vector<std::string> split(const std::string &str,
const std::string &delim) {
2020-07-07 15:40:39 +08:00
std::vector<std::string> res;
if ("" == str)
return res;
char *strs = new char[str.length() + 1];
2020-07-07 15:40:39 +08:00
std::strcpy(strs, str.c_str());
char *d = new char[delim.length() + 1];
2020-07-07 15:40:39 +08:00
std::strcpy(d, delim.c_str());
char *p = std::strtok(strs, d);
2020-07-07 15:40:39 +08:00
while (p) {
string s = p;
res.push_back(s);
p = std::strtok(NULL, d);
}
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
return res;
2020-07-02 10:31:34 +08:00
}
2020-07-07 15:40:39 +08:00
std::map<std::string, double> LoadConfigTxt(std::string config_path) {
auto config = ReadDict(config_path);
std::map<std::string, double> dict;
for (int i = 0; i < config.size(); i++) {
std::vector<std::string> res = split(config[i], " ");
dict[res[0]] = stod(res[1]);
}
return dict;
}
2020-07-02 10:31:34 +08:00
int main(int argc, char **argv) {
2020-07-07 15:40:39 +08:00
if (argc < 5) {
std::cerr << "[ERROR] usage: " << argv[0]
<< " det_model_file rec_model_file image_path\n";
2020-07-02 10:31:34 +08:00
exit(1);
}
std::string det_model_file = argv[1];
std::string rec_model_file = argv[2];
std::string img_path = argv[3];
2020-07-07 15:40:39 +08:00
std::string dict_path = argv[4];
//// load config from txt file
auto Config = LoadConfigTxt("./config.txt");
2020-07-02 10:31:34 +08:00
auto start = std::chrono::system_clock::now();
2020-07-07 15:40:39 +08:00
auto det_predictor = loadModel(det_model_file);
auto rec_predictor = loadModel(rec_model_file);
2020-07-10 20:02:49 +08:00
auto charactor_dict = ReadDict(dict_path);
2020-07-02 10:31:34 +08:00
cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);
2020-07-07 15:40:39 +08:00
auto boxes = RunDetModel(det_predictor, srcimg, Config);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
std::vector<std::string> rec_text;
std::vector<float> rec_text_score;
2020-07-10 20:02:49 +08:00
RunRecModel(boxes, srcimg, rec_predictor, rec_text, rec_text_score,
charactor_dict);
2020-07-02 10:31:34 +08:00
2020-07-07 15:40:39 +08:00
auto end = std::chrono::system_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start);
//// visualization
auto img_vis = Visualization(srcimg, boxes);
//// print recognized text
for (int i = 0; i < rec_text.size(); i++) {
std::cout << i << "\t" << rec_text[i] << "\t" << rec_text_score[i]
<< std::endl;
}
std::cout << "花费了"
<< double(duration.count()) *
std::chrono::microseconds::period::num /
std::chrono::microseconds::period::den
2020-07-02 10:31:34 +08:00
<< "" << std::endl;
return 0;
}