提交 eb05033c 编写于 作者: S ShiningZhang

fix: the result of ocr_cpp_server is not complete

上级 072114e7
...@@ -191,42 +191,64 @@ int GeneralDetectionOp::inference() { ...@@ -191,42 +191,64 @@ int GeneralDetectionOp::inference() {
boxes = post_processor_.FilterTagDetRes(boxes, ratio_h, ratio_w, srcimg); boxes = post_processor_.FilterTagDetRes(boxes, ratio_h, ratio_w, srcimg);
for (int i = boxes.size() - 1; i >= 0; i--) { float max_wh_ratio = 0.0f;
crop_img = GetRotateCropImage(img, boxes[i]); std::vector<cv::Mat> crop_imgs;
std::vector<cv::Mat> resize_imgs;
float wh_ratio = float(crop_img.cols) / float(crop_img.rows); int max_resize_w = 0;
int max_resize_h = 0;
int box_num = boxes.size();
std::vector<std::vector<float>> output_rec;
for (int i = 0; i < box_num; ++i) {
cv::Mat line_img = GetRotateCropImage(img, boxes[i]);
float wh_ratio = float(line_img.cols) / float(line_img.rows);
max_wh_ratio = max_wh_ratio > wh_ratio ? max_wh_ratio : wh_ratio;
crop_imgs.push_back(line_img);
}
for (int i = 0; i < box_num; ++i) {
cv::Mat resize_img;
crop_img = crop_imgs[i];
this->resize_op_rec.Run( this->resize_op_rec.Run(
crop_img, resize_img_rec, wh_ratio, this->use_tensorrt_); crop_img, resize_img, max_wh_ratio, this->use_tensorrt_);
this->normalize_op_.Run( this->normalize_op_.Run(
&resize_img_rec, this->mean_rec, this->scale_rec, this->is_scale_); &resize_img, this->mean_rec, this->scale_rec, this->is_scale_);
std::vector<float> output_rec( max_resize_w = std::max(max_resize_w, resize_img.cols);
1 * 3 * resize_img_rec.rows * resize_img_rec.cols, 0.0f); max_resize_h = std::max(max_resize_h, resize_img.rows);
resize_imgs.push_back(resize_img);
this->permute_op_.Run(&resize_img_rec, output_rec.data()); }
int buf_size = 3 * max_resize_h * max_resize_w;
// Inference. output_rec = std::vector<std::vector<float>>(box_num,
output_shape = {1, 3, resize_img_rec.rows, resize_img_rec.cols}; std::vector<float>(buf_size, 0.0f));
out_num = std::accumulate( for (int i = 0; i < box_num; ++i) {
output_shape.begin(), output_shape.end(), 1, std::multiplies<int>()); resize_img_rec = resize_imgs[i];
databuf_size_out = out_num * sizeof(float);
databuf_data_out = MempoolWrapper::instance().malloc(databuf_size_out); this->permute_op_.Run(&resize_img_rec, output_rec[i].data());
if (!databuf_data_out) { }
LOG(ERROR) << "Malloc failed, size: " << databuf_size_out;
return -1; // Inference.
} output_shape = {box_num, 3, max_resize_h, max_resize_w};
memcpy(databuf_data_out, output_rec.data(), databuf_size_out); out_num = std::accumulate(
databuf_char_out = reinterpret_cast<char*>(databuf_data_out); output_shape.begin(), output_shape.end(), 1, std::multiplies<int>());
paddle::PaddleBuf paddleBuf(databuf_char_out, databuf_size_out); databuf_size_out = out_num * sizeof(float);
paddle::PaddleTensor tensor_out; databuf_data_out = MempoolWrapper::instance().malloc(databuf_size_out);
tensor_out.name = "image"; if (!databuf_data_out) {
tensor_out.dtype = paddle::PaddleDType::FLOAT32; LOG(ERROR) << "Malloc failed, size: " << databuf_size_out;
tensor_out.shape = {1, 3, resize_img_rec.rows, resize_img_rec.cols}; return -1;
tensor_out.data = paddleBuf; }
out->push_back(tensor_out); int offset = buf_size * sizeof(float);
for (int i = 0; i < box_num; ++i) {
memcpy(databuf_data_out + i * offset, output_rec[i].data(), offset);
} }
databuf_char_out = reinterpret_cast<char*>(databuf_data_out);
paddle::PaddleBuf paddleBuf(databuf_char_out, databuf_size_out);
paddle::PaddleTensor tensor_out;
tensor_out.name = "image";
tensor_out.dtype = paddle::PaddleDType::FLOAT32;
tensor_out.shape = output_shape;
tensor_out.data = paddleBuf;
out->push_back(tensor_out);
} }
out->erase(out->begin(), out->begin() + infer_outnum); out->erase(out->begin(), out->begin() + infer_outnum);
......
...@@ -63,7 +63,7 @@ class GeneralDetectionOp ...@@ -63,7 +63,7 @@ class GeneralDetectionOp
double det_db_thresh_ = 0.3; double det_db_thresh_ = 0.3;
double det_db_box_thresh_ = 0.5; double det_db_box_thresh_ = 0.5;
double det_db_unclip_ratio_ = 2.0; double det_db_unclip_ratio_ = 1.5;
std::vector<float> mean_det = {0.485f, 0.456f, 0.406f}; std::vector<float> mean_det = {0.485f, 0.456f, 0.406f};
std::vector<float> scale_det = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; std::vector<float> scale_det = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
......
...@@ -82,14 +82,14 @@ void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img, ...@@ -82,14 +82,14 @@ void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img,
else if (resize_h / 32 < 1 + 1e-5) else if (resize_h / 32 < 1 + 1e-5)
resize_h = 32; resize_h = 32;
else else
resize_h = (resize_h / 32) * 32; resize_h = (resize_h / 32 - 1) * 32;
if (resize_w % 32 == 0) if (resize_w % 32 == 0)
resize_w = resize_w; resize_w = resize_w;
else if (resize_w / 32 < 1 + 1e-5) else if (resize_w / 32 < 1 + 1e-5)
resize_w = 32; resize_w = 32;
else else
resize_w = (resize_w / 32) * 32; resize_w = (resize_w / 32 - 1) * 32;
if (!use_tensorrt) { if (!use_tensorrt) {
cv::resize(img, resize_img, cv::Size(resize_w, resize_h)); cv::resize(img, resize_img, cv::Size(resize_w, resize_h));
ratio_h = float(resize_h) / float(h); ratio_h = float(resize_h) / float(h);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册